Skip to content

Commit f5db101

Browse files
committed
Add status conditions for image prefetch progress and failure in controller
Signed-off-by: zeroalphat <taichi-takemura@cybozu.co.jp>
1 parent 99ebc49 commit f5db101

File tree

2 files changed

+85
-29
lines changed

2 files changed

+85
-29
lines changed

internal/controller/imageprefetch_controller.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,18 @@ func (r *ImagePrefetchReconciler) updateStatus(ctx context.Context, imgPrefetch
330330
logger := log.FromContext(ctx)
331331
imgPrefetch.Status.ObservedGeneration = imgPrefetch.Generation
332332
imgPrefetch.Status.SelectedNodes = selectedNodes
333+
meta.SetStatusCondition(&imgPrefetch.Status.Conditions, metav1.Condition{
334+
Type: ofenv1.ConditionReady,
335+
Status: metav1.ConditionFalse,
336+
Reason: "ImagePrefetchProgressing",
337+
Message: "Waiting for all nodes to pull the image",
338+
})
339+
meta.SetStatusCondition(&imgPrefetch.Status.Conditions, metav1.Condition{
340+
Type: ofenv1.ConditionImagePullFailed,
341+
Status: metav1.ConditionFalse,
342+
Reason: "ImagePrefetchFailed",
343+
Message: "Waiting for all nodes to pull the image",
344+
})
333345
result := ctrl.Result{RequeueAfter: 10 * time.Second}
334346

335347
nodeImageSets := &ofenv1.NodeImageSetList{}

internal/controller/imageprefetch_controller_test.go

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -227,43 +227,87 @@ var _ = Describe("ImagePrefetch Controller", func() {
227227
g.Expect(err).NotTo(HaveOccurred())
228228

229229
conditionImagePrefetchReady := meta.FindStatusCondition(imagePrefetch.Status.Conditions, ofenv1.ConditionReady)
230-
g.Expect(conditionImagePrefetchReady).To(BeNil())
230+
g.Expect(conditionImagePrefetchReady).NotTo(BeNil())
231+
g.Expect(conditionImagePrefetchReady.Status).To(Equal(metav1.ConditionFalse))
231232
conditionImagePrefetchProcessing := meta.FindStatusCondition(imagePrefetch.Status.Conditions, ofenv1.ConditionProgressing)
232233
g.Expect(conditionImagePrefetchProcessing).NotTo(BeNil())
233234
g.Expect(conditionImagePrefetchProcessing.Status).To(Equal(metav1.ConditionTrue))
234235
conditionImagePrefetchFailed := meta.FindStatusCondition(imagePrefetch.Status.Conditions, ofenv1.ConditionImagePullFailed)
235-
g.Expect(conditionImagePrefetchFailed).To(BeNil())
236+
g.Expect(conditionImagePrefetchFailed).NotTo(BeNil())
237+
g.Expect(conditionImagePrefetchFailed.Status).To(Equal(metav1.ConditionFalse))
236238
}).Should(Succeed())
237239

238-
By("updating nodeImageSet's status to image available")
239-
nodeImageSets := &ofenv1.NodeImageSetList{}
240-
err := k8sClient.List(ctx, nodeImageSets, &client.ListOptions{
241-
LabelSelector: labels.SelectorFromSet(map[string]string{
242-
constants.OwnerImagePrefetchNamespace: testName,
243-
}),
244-
})
245-
Expect(err).NotTo(HaveOccurred())
240+
By("updating nodeImageSet's status to image pull failed")
241+
failedCondition := metav1.Condition{
242+
Type: ofenv1.ConditionImageDownloadFailed,
243+
Reason: "test",
244+
Status: metav1.ConditionTrue,
245+
LastTransitionTime: metav1.Now(),
246+
}
247+
Eventually(func(g Gomega) {
248+
nodeImageSets := &ofenv1.NodeImageSetList{}
249+
err := k8sClient.List(ctx, nodeImageSets, &client.ListOptions{
250+
LabelSelector: labels.SelectorFromSet(map[string]string{
251+
constants.OwnerImagePrefetchNamespace: testName,
252+
}),
253+
})
254+
g.Expect(err).NotTo(HaveOccurred())
246255

247-
now := metav1.Now()
248-
for _, nodeImageSet := range nodeImageSets.Items {
249-
nodeImageSet.Status.Conditions = []metav1.Condition{
250-
{
251-
Type: ofenv1.ConditionImageAvailable,
252-
Reason: "test",
253-
Status: metav1.ConditionTrue,
254-
LastTransitionTime: now,
255-
},
256-
{
257-
Type: ofenv1.ConditionImageDownloadComplete,
258-
Reason: "test",
259-
Status: metav1.ConditionTrue,
260-
LastTransitionTime: now,
261-
},
256+
for _, nodeImageSet := range nodeImageSets.Items {
257+
nodeImageSet.Status.Conditions = []metav1.Condition{failedCondition}
258+
err = k8sClient.Status().Update(ctx, &nodeImageSet)
259+
g.Expect(err).NotTo(HaveOccurred())
262260
}
261+
}).Should(Succeed())
262+
263+
By("checking imagePrefetch status to be failed")
264+
Eventually(func(g Gomega) {
265+
imagePrefetch := &ofenv1.ImagePrefetch{}
266+
err := k8sClient.Get(ctx, client.ObjectKey{Name: testName, Namespace: testName}, imagePrefetch)
267+
g.Expect(err).NotTo(HaveOccurred())
263268

264-
err = k8sClient.Status().Update(ctx, &nodeImageSet)
265-
Expect(err).NotTo(HaveOccurred())
269+
conditionImagePrefetchReady := meta.FindStatusCondition(imagePrefetch.Status.Conditions, ofenv1.ConditionReady)
270+
g.Expect(conditionImagePrefetchReady).NotTo(BeNil())
271+
g.Expect(conditionImagePrefetchReady.Status).To(Equal(metav1.ConditionFalse))
272+
conditionImagePrefetchProcessing := meta.FindStatusCondition(imagePrefetch.Status.Conditions, ofenv1.ConditionProgressing)
273+
g.Expect(conditionImagePrefetchProcessing).NotTo(BeNil())
274+
g.Expect(conditionImagePrefetchProcessing.Status).To(Equal(metav1.ConditionTrue))
275+
conditionImagePrefetchFailed := meta.FindStatusCondition(imagePrefetch.Status.Conditions, ofenv1.ConditionImagePullFailed)
276+
g.Expect(conditionImagePrefetchFailed).NotTo(BeNil())
277+
g.Expect(conditionImagePrefetchFailed.Status).To(Equal(metav1.ConditionTrue))
278+
}).Should(Succeed())
279+
280+
By("updating nodeImageSet's status to image available")
281+
failedCondition.Status = metav1.ConditionFalse
282+
imageAvailableCondition := metav1.Condition{
283+
Type: ofenv1.ConditionImageAvailable,
284+
Reason: "test",
285+
Status: metav1.ConditionTrue,
286+
LastTransitionTime: metav1.Now(),
266287
}
288+
imageDownloadCompleteCondition := metav1.Condition{
289+
Type: ofenv1.ConditionImageDownloadComplete,
290+
Reason: "test",
291+
Status: metav1.ConditionTrue,
292+
LastTransitionTime: metav1.Now(),
293+
}
294+
295+
Eventually(func(g Gomega) {
296+
nodeImageSets := &ofenv1.NodeImageSetList{}
297+
err := k8sClient.List(ctx, nodeImageSets, &client.ListOptions{
298+
LabelSelector: labels.SelectorFromSet(map[string]string{
299+
constants.OwnerImagePrefetchNamespace: testName,
300+
}),
301+
})
302+
g.Expect(err).NotTo(HaveOccurred())
303+
304+
for _, nodeImageSet := range nodeImageSets.Items {
305+
nodeImageSet.Status.Conditions = []metav1.Condition{
306+
failedCondition, imageAvailableCondition, imageDownloadCompleteCondition}
307+
err = k8sClient.Status().Update(ctx, &nodeImageSet)
308+
Expect(err).NotTo(HaveOccurred())
309+
}
310+
}).Should(Succeed())
267311

268312
By("checking imagePrefetch status to be ready")
269313
Eventually(func(g Gomega) {
@@ -279,8 +323,8 @@ var _ = Describe("ImagePrefetch Controller", func() {
279323
g.Expect(conditionImagePrefetchProcessing).NotTo(BeNil())
280324
g.Expect(conditionImagePrefetchProcessing.Status).To(Equal(metav1.ConditionTrue))
281325
conditionImagePrefetchFailed := meta.FindStatusCondition(imagePrefetch.Status.Conditions, ofenv1.ConditionImagePullFailed)
282-
g.Expect(conditionImagePrefetchFailed).To(BeNil())
283-
326+
g.Expect(conditionImagePrefetchFailed).NotTo(BeNil())
327+
g.Expect(conditionImagePrefetchFailed.Status).To(Equal(metav1.ConditionFalse))
284328
}).Should(Succeed())
285329
})
286330

0 commit comments

Comments
 (0)