Skip to content

Commit 66d6b4f

Browse files
authored
📖 Update test examples not to set Status on create (#4016)
Update test examples not to set Status on create
1 parent 1e749ef commit 66d6b4f

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,6 @@ var _ = Describe("CronJob controller", func() {
163163
},
164164
},
165165
},
166-
Status: batchv1.JobStatus{
167-
Active: 2,
168-
},
169166
}
170167

171168
// Note that your CronJob’s GroupVersionKind is required to set up this owner reference.
@@ -175,6 +172,13 @@ var _ = Describe("CronJob controller", func() {
175172
controllerRef := metav1.NewControllerRef(createdCronjob, gvk)
176173
testJob.SetOwnerReferences([]metav1.OwnerReference{*controllerRef})
177174
Expect(k8sClient.Create(ctx, testJob)).Should(Succeed())
175+
// Note that you can not manage the status values while creating the resource.
176+
// The status field is managed separately to reflect the current state of the resource.
177+
// Therefore, it should be updated using a PATCH or PUT operation after the resource has been created.
178+
// Additionally, it is recommended to use StatusConditions to manage the status. For further information see:
179+
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
180+
testJob.Status.Active = 2
181+
Expect(k8sClient.Status().Update(ctx, testJob)).Should(Succeed())
178182
/*
179183
Adding this Job to our test CronJob should trigger our controller’s reconciler logic.
180184
After that, we can write a test that evaluates whether our controller eventually updates our CronJob’s Status field as expected!

hack/docs/internal/cronjob-tutorial/writing_tests_controller.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ var _ = Describe("CronJob controller", func() {
182182
},
183183
},
184184
},
185-
Status: batchv1.JobStatus{
186-
Active: 2,
187-
},
188185
}
189186
190187
// Note that your CronJob’s GroupVersionKind is required to set up this owner reference.
@@ -194,6 +191,13 @@ var _ = Describe("CronJob controller", func() {
194191
controllerRef := metav1.NewControllerRef(createdCronjob, gvk)
195192
testJob.SetOwnerReferences([]metav1.OwnerReference{*controllerRef})
196193
Expect(k8sClient.Create(ctx, testJob)).Should(Succeed())
194+
// Note that you can not manage the status values while creating the resource.
195+
// The status field is managed separately to reflect the current state of the resource.
196+
// Therefore, it should be updated using a PATCH or PUT operation after the resource has been created.
197+
// Additionally, it is recommended to use StatusConditions to manage the status. For further information see:
198+
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
199+
testJob.Status.Active = 2
200+
Expect(k8sClient.Status().Update(ctx, testJob)).Should(Succeed())
197201
/*
198202
Adding this Job to our test CronJob should trigger our controller’s reconciler logic.
199203
After that, we can write a test that evaluates whether our controller eventually updates our CronJob’s Status field as expected!

0 commit comments

Comments
 (0)