From 68af4c3f67085d044d6781609031c1ac69e9794a Mon Sep 17 00:00:00 2001 From: adrianc Date: Tue, 27 Aug 2024 16:53:16 +0300 Subject: [PATCH] reduce the use of HasAnnotation() in some places its not needed to check for existance as we checked for both existance and empty string, both covered by direclty accessing annotation map with given key. Signed-off-by: adrianc --- internal/controller/nodemaintenance_controller.go | 5 ++--- internal/podcompletion/podcompletion.go | 5 ++--- internal/podcompletion/podcompletion_test.go | 1 - 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/internal/controller/nodemaintenance_controller.go b/internal/controller/nodemaintenance_controller.go index c55da0a..ffb68ae 100644 --- a/internal/controller/nodemaintenance_controller.go +++ b/internal/controller/nodemaintenance_controller.go @@ -535,9 +535,8 @@ func (r *NodeMaintenanceReconciler) handleReadyState(ctx context.Context, reqLog return res, err } - // set ready-time annotation - if !metav1.HasAnnotation(nm.ObjectMeta, ReadyTimeAnnotation) || - nm.Annotations[ReadyTimeAnnotation] == "" { + // set ready-time annotation if not present on not set + if nm.Annotations[ReadyTimeAnnotation] == "" { metav1.SetMetaDataAnnotation(&nm.ObjectMeta, ReadyTimeAnnotation, time.Now().UTC().Format(time.RFC3339)) err := r.Update(ctx, nm) if err != nil { diff --git a/internal/podcompletion/podcompletion.go b/internal/podcompletion/podcompletion.go index 3f29160..7843565 100644 --- a/internal/podcompletion/podcompletion.go +++ b/internal/podcompletion/podcompletion.go @@ -63,9 +63,8 @@ func (p *podCompletionHandler) HandlePodCompletion(ctx context.Context, reqLog l var err error var startTime time.Time - if !metav1.HasAnnotation(nm.ObjectMeta, WaitForPodCompletionStartAnnot) || - nm.Annotations[WaitForPodCompletionStartAnnot] == "" { - // set waitForPodCompletion time annotation + if nm.Annotations[WaitForPodCompletionStartAnnot] == "" { + // set waitForPodCompletion time annotation if not set or empty startTime = time.Now().UTC() metav1.SetMetaDataAnnotation(&nm.ObjectMeta, WaitForPodCompletionStartAnnot, startTime.Format(time.RFC3339)) err = p.k8sclient.Update(ctx, nm) diff --git a/internal/podcompletion/podcompletion_test.go b/internal/podcompletion/podcompletion_test.go index 60a64fd..364402f 100644 --- a/internal/podcompletion/podcompletion_test.go +++ b/internal/podcompletion/podcompletion_test.go @@ -77,7 +77,6 @@ var _ = Describe("podcompletion tests", func() { Expect(fakeClient.Get(testCtx, client.ObjectKeyFromObject(nm), nm)).ToNot(HaveOccurred()) Expect(nm.Status.WaitForCompletion).To(Equal([]string{"default/test-pod"})) - Expect(metav1.HasAnnotation(nm.ObjectMeta, podcompletion.WaitForPodCompletionStartAnnot)).To(BeTrue()) t1, e := time.Parse(time.RFC3339, nm.Annotations[podcompletion.WaitForPodCompletionStartAnnot]) Expect(e).ToNot(HaveOccurred())