Skip to content

Commit

Permalink
reduce the use of HasAnnotation()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
adrianchiris committed Aug 27, 2024
1 parent f9fb4e5 commit 68af4c3
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
5 changes: 2 additions & 3 deletions internal/controller/nodemaintenance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions internal/podcompletion/podcompletion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion internal/podcompletion/podcompletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 68af4c3

Please sign in to comment.