Kubebuilder: Status not set correctly on Cron Job tests example

Created on 3 Nov 2020  路  5Comments  路  Source: kubernetes-sigs/kubebuilder


I am trying to test a resource by creating a sub resource with a set status, as done here. Yet when I query the job as such"

createdJob := &batchv1.Job{}
ns := types.NamespacedName{Name: JobName, Namespace: CronjobNamespace}
err := k8sClient.Get(ctx, ns, createdJob)
fmt.Printf("%+v\n", createdJob)

I get no status field:

&Job{ObjectMeta:{      0 0001-01-01 00:00:00 +0000 UTC <nil> <nil> map[] map[] [] []  []},Spec:JobSpec{Parallelism:nil,Completions:nil,ActiveDeadlineSeconds:nil,Selector:nil,ManualSelector:nil,Template:{{      0 0001-01-01 00:00:00 +0000 UTC <nil> <nil> map[] map[] [] []  []} {[] [] [] []  <nil> <nil>  map[]   <nil>  false false false <nil> nil []   nil  [] []  <nil> nil [] <nil> <nil> <nil> map[] []}},BackoffLimit:nil,TTLSecondsAfterFinished:nil,},Status:JobStatus{Conditions:[]JobCondition{},StartTime:<nil>,CompletionTime:<nil>,Active:0,Succeeded:0,Failed:0,},}


I expected the Status field to be populated with Active: 2. Can I not retrieve the status using k8sClient.Get()? Or do I need to add a sleep between the creation and fetch?

What versions of software are you using? Specifically, the following are often useful:

  • go version 1.15.2
  • kubebuilder version (kubebuilder version) and scaffolding version (check your PROJECT file)
  • controller-runtime version up to date with master
  • controller-tools version up to date with master
  • Kubernetes & kubectl versions 1.19 client

/kind bug

kinbug

All 5 comments

Hi @EnriqueL8,

Are you using the project in the docs https://github.com/kubernetes-sigs/kubebuilder/blob/master/docs/book/src/cronjob-tutorial/testdata and it is not working as expected or it is about your specific test? If it is about your specific test, are you using the client to update the object after the change? I'd like to recommend you get the object, perform the change and then use the client to update it. Then, you can fetch the object again in order to check if the value is as expected.

If the above info is not enough to help you with, could you please share a snippet of the code used to perform all operations and or a link for we check your project? It might help us see what is missing.

Hi @camilamacedo86,

Thanks for the quick answer!

Yes, I am using https://github.com/kubernetes-sigs/kubebuilder/blob/master/docs/book/src/cronjob-tutorial/testdata/project/testdata/project/controllers/cronjob_controller_test.go. I have pushed my changes to a branch: https://github.com/EnriqueL8/kubebuilder/tree/test_status if you want to take a look.

I tried to update the object as you suggested, but also didn't get the status information correctly. See:

&Job{ObjectMeta:{test-job  default /apis/batch/v1/namespaces/default/jobs/test-job 98c97f39-cded-414f-8ad4-e1875d67013d 53 0 2020-11-03 15:23:08 +0000 GMT <nil> <nil> map[controller-uid:98c97f39-cded-414f-8ad4-e1875d67013d job-name:test-job] map[] [{batch.tutorial.kubebuilder.io/v1 CronJob test-cronjob 7570817c-370f-46ff-bc3c-1c0697ddcb63 0xc00053b0c9 0xc00053b0ca}] []  []},Spec:JobSpec{Parallelism:*1,Completions:*1,ActiveDeadlineSeconds:nil,Selector:&v1.LabelSelector{MatchLabels:map[string]string{controller-uid: 98c97f39-cded-414f-8ad4-e1875d67013d,},MatchExpressions:[]LabelSelectorRequirement{},},ManualSelector:nil,Template:{{      0 0001-01-01 00:00:00 +0000 UTC <nil> <nil> map[controller-uid:98c97f39-cded-414f-8ad4-e1875d67013d job-name:test-job] map[] [] []  []} {[] [] [{test-container test-image [] []  [] [] [] {map[] map[]} [] [] nil nil nil nil /dev/termination-log File Always nil false false false}] [] OnFailure 0xc00053b118 <nil> ClusterFirst map[]   <nil>  false false false <nil> PodSecurityContext{SELinuxOptions:nil,RunAsUser:nil,RunAsNonRoot:nil,SupplementalGroups:[],FSGroup:nil,RunAsGroup:nil,Sysctls:[]Sysctl{},WindowsOptions:nil,FSGroupChangePolicy:nil,} []   nil default-scheduler [] []  <nil> nil [] <nil> <nil> <nil> map[] []}},BackoffLimit:*6,TTLSecondsAfterFinished:nil,},Status:JobStatus{Conditions:[]JobCondition{},StartTime:<nil>,CompletionTime:<nil>,Active:0,Succeeded:0,Failed:0,},}

I am trying the same thing in my personal tests, so if I can figure out how this works here then it should also work there.

Update: I checked the controller code and it never looks for the status.active field. It just says the job is active if not in the state complete|failed|true. So the tests will still pass, and never look at the status field set. Function used in controller to populated activeJobs array of batchv1.Jobs:

       isJobFinished := func(job *kbatch.Job) (bool, kbatch.JobConditionType) {
        for _, c := range job.Status.Conditions {
            if (c.Type == kbatch.JobComplete || c.Type == kbatch.JobFailed) && c.Status == corev1.ConditionTrue {
                return true, c.Type
            }
        }

        return false, ""
    }

Is this an antipattern to set the status of resources in order to test behaviour of controllers? For example, let's say I have a controller that creates a Pod and it is reconciling until the Pod is Ready. Once it is ready, the controller will populate the status of the resource it reconciled against to PodReady: true

it was discussed in the slack; https://kubernetes.slack.com/archives/CAR30FCJZ/p1604503804366000
As agreed we will close this one. However, please feel free to raise a new issue with questions if you need.

Solution!

Use: k8sClient.Status().Update(&pod)

Was this page helpful?
0 / 5 - 0 ratings