mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-14 22:33:34 +00:00
Job: Add ControllerRef on all created Pods.
This commit is contained in:
parent
33d036a564
commit
bdfe18f638
@ -46,6 +46,9 @@ import (
|
|||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// controllerKind contains the schema.GroupVersionKind for this controller type.
|
||||||
|
var controllerKind = batch.SchemeGroupVersion.WithKind("Job")
|
||||||
|
|
||||||
type JobController struct {
|
type JobController struct {
|
||||||
kubeClient clientset.Interface
|
kubeClient clientset.Interface
|
||||||
podControl controller.PodControlInterface
|
podControl controller.PodControlInterface
|
||||||
@ -507,7 +510,7 @@ func (jm *JobController) manageJob(activePods []*v1.Pod, succeeded int32, job *b
|
|||||||
for i := int32(0); i < diff; i++ {
|
for i := int32(0); i < diff; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
defer wait.Done()
|
defer wait.Done()
|
||||||
if err := jm.podControl.CreatePods(job.Namespace, &job.Spec.Template, job); err != nil {
|
if err := jm.podControl.CreatePodsWithControllerRef(job.Namespace, &job.Spec.Template, job, newControllerRef(job)); err != nil {
|
||||||
defer utilruntime.HandleError(err)
|
defer utilruntime.HandleError(err)
|
||||||
// Decrement the expected number of creates because the informer won't observe this pod
|
// Decrement the expected number of creates because the informer won't observe this pod
|
||||||
jm.expectations.CreationObserved(jobKey)
|
jm.expectations.CreationObserved(jobKey)
|
||||||
|
@ -104,6 +104,7 @@ func newPodList(count int32, status v1.PodPhase, job *batch.Job) []v1.Pod {
|
|||||||
Name: fmt.Sprintf("pod-%v", rand.String(10)),
|
Name: fmt.Sprintf("pod-%v", rand.String(10)),
|
||||||
Labels: job.Spec.Selector.MatchLabels,
|
Labels: job.Spec.Selector.MatchLabels,
|
||||||
Namespace: job.Namespace,
|
Namespace: job.Namespace,
|
||||||
|
OwnerReferences: []metav1.OwnerReference{*newControllerRef(job)},
|
||||||
},
|
},
|
||||||
Status: v1.PodStatus{Phase: status},
|
Status: v1.PodStatus{Phase: status},
|
||||||
}
|
}
|
||||||
@ -274,6 +275,22 @@ func TestControllerSyncJob(t *testing.T) {
|
|||||||
if int32(len(fakePodControl.DeletePodName)) != tc.expectedDeletions {
|
if int32(len(fakePodControl.DeletePodName)) != tc.expectedDeletions {
|
||||||
t.Errorf("%s: unexpected number of deletes. Expected %d, saw %d\n", name, tc.expectedDeletions, len(fakePodControl.DeletePodName))
|
t.Errorf("%s: unexpected number of deletes. Expected %d, saw %d\n", name, tc.expectedDeletions, len(fakePodControl.DeletePodName))
|
||||||
}
|
}
|
||||||
|
// Each create should have an accompanying ControllerRef.
|
||||||
|
if len(fakePodControl.ControllerRefs) != int(tc.expectedCreations) {
|
||||||
|
t.Errorf("%s: unexpected number of ControllerRefs. Expected %d, saw %d\n", name, tc.expectedCreations, len(fakePodControl.ControllerRefs))
|
||||||
|
}
|
||||||
|
// Make sure the ControllerRefs are correct.
|
||||||
|
for _, controllerRef := range fakePodControl.ControllerRefs {
|
||||||
|
if got, want := controllerRef.APIVersion, "batch/v1"; got != want {
|
||||||
|
t.Errorf("controllerRef.APIVersion = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
if got, want := controllerRef.Kind, "Job"; got != want {
|
||||||
|
t.Errorf("controllerRef.Kind = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
if controllerRef.Controller == nil || *controllerRef.Controller != true {
|
||||||
|
t.Errorf("controllerRef.Controller is not set to true")
|
||||||
|
}
|
||||||
|
}
|
||||||
// validate status
|
// validate status
|
||||||
if actual.Status.Active != tc.expectedActive {
|
if actual.Status.Active != tc.expectedActive {
|
||||||
t.Errorf("%s: unexpected number of active pods. Expected %d, saw %d\n", name, tc.expectedActive, actual.Status.Active)
|
t.Errorf("%s: unexpected number of active pods. Expected %d, saw %d\n", name, tc.expectedActive, actual.Status.Active)
|
||||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package job
|
package job
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/kubernetes/pkg/api/v1"
|
"k8s.io/kubernetes/pkg/api/v1"
|
||||||
batch "k8s.io/kubernetes/pkg/apis/batch/v1"
|
batch "k8s.io/kubernetes/pkg/apis/batch/v1"
|
||||||
)
|
)
|
||||||
@ -29,3 +30,14 @@ func IsJobFinished(j *batch.Job) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newControllerRef(j *batch.Job) *metav1.OwnerReference {
|
||||||
|
isController := true
|
||||||
|
return &metav1.OwnerReference{
|
||||||
|
APIVersion: controllerKind.GroupVersion().String(),
|
||||||
|
Kind: controllerKind.Kind,
|
||||||
|
Name: j.Name,
|
||||||
|
UID: j.UID,
|
||||||
|
Controller: &isController,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user