@@ -56,6 +56,7 @@ import (
56
56
_ "k8s.io/kubernetes/pkg/apis/core/install"
57
57
"k8s.io/kubernetes/pkg/controller"
58
58
"k8s.io/kubernetes/pkg/controller/job/metrics"
59
+ "k8s.io/kubernetes/pkg/controller/job/util"
59
60
"k8s.io/kubernetes/pkg/controller/testutil"
60
61
"k8s.io/kubernetes/pkg/features"
61
62
"k8s.io/utils/clock"
@@ -2879,6 +2880,110 @@ func TestSingleJobFailedCondition(t *testing.T) {
2879
2880
2880
2881
}
2881
2882
2883
+ func TestJobControllerMissingJobSucceedEvent (t * testing.T ) {
2884
+ t .Cleanup (setDurationDuringTest (& syncJobBatchPeriod , fastSyncJobBatchPeriod ))
2885
+ logger , ctx := ktesting .NewTestContext (t )
2886
+ job1 := newJob (1 , 1 , 6 , batch .NonIndexedCompletion )
2887
+ job1 .Name = "job1"
2888
+ clientSet := fake .NewSimpleClientset (job1 )
2889
+ fakeClock := clocktesting .NewFakeClock (time .Now ())
2890
+ jm , informer := newControllerFromClientWithClock (ctx , t , clientSet , controller .NoResyncPeriodFunc , fakeClock )
2891
+ jm .podControl = & controller.RealPodControl {
2892
+ KubeClient : clientSet ,
2893
+ Recorder : testutil .NewFakeRecorder (),
2894
+ }
2895
+ jm .podStoreSynced = alwaysReady
2896
+ jm .jobStoreSynced = alwaysReady
2897
+
2898
+ err := informer .Batch ().V1 ().Jobs ().Informer ().GetIndexer ().Add (job1 )
2899
+ if err != nil {
2900
+ t .Fatalf ("Unexpected error when adding job to indexer %v" , err )
2901
+ }
2902
+ // 1st reconcile should create a new pod
2903
+ err = jm .syncJob (ctx , testutil .GetKey (job1 , t ))
2904
+ if err != nil {
2905
+ t .Fatalf ("Unexpected error when syncing jobs %v" , err )
2906
+ }
2907
+
2908
+ podIndexer := informer .Core ().V1 ().Pods ().Informer ().GetIndexer ()
2909
+ podList , err := clientSet .Tracker ().List (
2910
+ schema.GroupVersionResource {Version : "v1" , Resource : "pods" },
2911
+ schema.GroupVersionKind {Version : "v1" , Kind : "Pod" },
2912
+ "default" )
2913
+ if err != nil {
2914
+ t .Fatalf ("Unexpected error when fetching pods %v" , err )
2915
+ }
2916
+ // manually adding the just-created pod from fake clientset memory to informer cache because informer is not started.
2917
+ // we are updating the pod status to succeeded which should update the job status to succeeded and remove the finalizer of the pod.
2918
+ justCreatedPod := podList .(* v1.PodList ).Items [0 ]
2919
+ fmt .Printf ("pod is %v\n " , podList .(* v1.PodList ).Items [0 ])
2920
+ justCreatedPod .Status .Phase = v1 .PodSucceeded
2921
+ err = podIndexer .Add (& justCreatedPod )
2922
+ if err != nil {
2923
+ t .Fatalf ("Unexpected error when adding pod to indexer %v" , err )
2924
+ }
2925
+ jm .addPod (logger , & justCreatedPod )
2926
+ err = jm .syncJob (ctx , testutil .GetKey (job1 , t ))
2927
+ if err != nil {
2928
+ t .Fatalf ("Unexpected error when syncing jobs %v" , err )
2929
+ }
2930
+
2931
+ jobList , err := clientSet .Tracker ().List (
2932
+ schema.GroupVersionResource {Group : "batch" , Version : "v1" , Resource : "jobs" },
2933
+ schema.GroupVersionKind {Group : "batch" , Version : "v1" , Kind : "Job" },
2934
+ "default" )
2935
+ if err != nil {
2936
+ t .Fatalf ("Unexpected error when trying to get job from the store: %v" , err )
2937
+ }
2938
+ updatedJob := jobList .(* batch.JobList ).Items [0 ]
2939
+ if ! util .IsJobSucceeded (& updatedJob ) {
2940
+ t .Fatalf ("job status is not succeeded: %v" , updatedJob )
2941
+ }
2942
+
2943
+ // add the updated pod from the fake clientset memory to informer cache because informer is not started.
2944
+ podList , err = clientSet .Tracker ().List (
2945
+ schema.GroupVersionResource {Version : "v1" , Resource : "pods" },
2946
+ schema.GroupVersionKind {Version : "v1" , Kind : "Pod" },
2947
+ "default" )
2948
+ if err != nil {
2949
+ t .Fatalf ("Unexpected error when fetching pods %v" , err )
2950
+ }
2951
+ fmt .Printf ("pod is %v\n " , podList .(* v1.PodList ).Items [0 ])
2952
+ updatedPod := podList .(* v1.PodList ).Items [0 ]
2953
+ updatedPod .Status .Phase = v1 .PodSucceeded
2954
+ err = podIndexer .Add (& updatedPod )
2955
+ if err != nil {
2956
+ t .Fatalf ("Unexpected error when adding pod to indexer %v" , err )
2957
+ }
2958
+
2959
+ // removing the just created pod from fake clientset memory inorder for the sync job to succeed if creating a new pod because of bug
2960
+ // but the pod will remain inside informer cache
2961
+ err = clientSet .Tracker ().Delete (
2962
+ schema.GroupVersionResource {Version : "v1" , Resource : "pods" },
2963
+ "default" , "" )
2964
+ if err != nil {
2965
+ t .Fatalf ("Unexpected error when deleting pod to indexer %v" , err )
2966
+ }
2967
+
2968
+ err = jm .syncJob (ctx , testutil .GetKey (job1 , t ))
2969
+ if err != nil {
2970
+ t .Fatalf ("Unexpected error when syncing jobs %v" , err )
2971
+ }
2972
+ time .Sleep (time .Second )
2973
+
2974
+ podList , err = clientSet .Tracker ().List (
2975
+ schema.GroupVersionResource {Version : "v1" , Resource : "pods" },
2976
+ schema.GroupVersionKind {Version : "v1" , Kind : "Pod" },
2977
+ "default" )
2978
+ if err != nil {
2979
+ t .Fatalf ("Unexpected error when syncing jobs %v" , err )
2980
+ }
2981
+ // no pod should be created
2982
+ if len (podList .(* v1.PodList ).Items ) != 0 {
2983
+ t .Errorf ("expect no pods to be created but %v pods are created" , len (podList .(* v1.PodList ).Items ))
2984
+ }
2985
+ }
2986
+
2882
2987
func TestSyncJobComplete (t * testing.T ) {
2883
2988
_ , ctx := ktesting .NewTestContext (t )
2884
2989
clientset := clientset .NewForConfigOrDie (& restclient.Config {Host : "" , ContentConfig : restclient.ContentConfig {GroupVersion : & schema.GroupVersion {Group : "" , Version : "v1" }}})
0 commit comments