Skip to content

Commit

Permalink
Bit of code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Apr 5, 2024
1 parent e7ee32d commit e227226
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
77 changes: 42 additions & 35 deletions api/v2/types_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ func (cs Conditions) filterOutCondition(t ConditionType) Conditions {
return newConditions
}

// IsAnnotationTrue returns true if the given object has an annotation with a given
// key and the value of this annotation is a true boolean.
func IsAnnotationTrue(o client.Object, key string) bool {
enabled, err := strconv.ParseBool(o.GetAnnotations()[key])
return err == nil && enabled
}

// IsAnnotationFalse returns true if the given object has an annotation with a given
// key and the value of this annotation is a false boolean.
func IsAnnotationFalse(o client.Object, key string) bool {
enabled, err := strconv.ParseBool(o.GetAnnotations()[key])
return err == nil && !enabled
}

// RemoveAnnotation removes an annotation by a given key from an object if present by updating it with the given client.
// It returns true when the annotation was present and removed and an error if the update process went wrong.
func RemoveAnnotation(ctx context.Context, c client.Client, o client.Object, key string) (bool, error) {
Expand All @@ -141,21 +155,6 @@ func RemoveAnnotation(ctx context.Context, c client.Client, o client.Object, key
return true, nil
}

// AnnotationRemovedPredicate returns a predicate when the given annotation key was removed.
func AnnotationRemovedPredicate(annotation string) predicate.Funcs {
return predicate.Funcs{
CreateFunc: func(ce event.CreateEvent) bool {
return false
},
UpdateFunc: func(update event.UpdateEvent) bool {
return annotationWasRemoved(update, annotation)
},
DeleteFunc: func(de event.DeleteEvent) bool {
return false
},
}
}

func annotationWasRemoved(update event.UpdateEvent, annotation string) bool {
if cmp.Equal(update.ObjectOld.GetAnnotations(), update.ObjectNew.GetAnnotations()) {
return false
Expand All @@ -177,21 +176,6 @@ func annotationWasRemoved(update event.UpdateEvent, annotation string) bool {
return o && !n
}

// AnnotationAddedPredicate returns a predicate when the given annotation key was added.
func AnnotationAddedPredicate(annotation string) predicate.Funcs {
return predicate.Funcs{
CreateFunc: func(ce event.CreateEvent) bool {
return false
},
UpdateFunc: func(update event.UpdateEvent) bool {
return annotationWasAdded(update, annotation)
},
DeleteFunc: func(de event.DeleteEvent) bool {
return false
},
}
}

func annotationWasAdded(update event.UpdateEvent, annotation string) bool {
if cmp.Equal(update.ObjectOld.GetAnnotations(), update.ObjectNew.GetAnnotations()) {
return false
Expand All @@ -213,9 +197,32 @@ func annotationWasAdded(update event.UpdateEvent, annotation string) bool {
return !o && n
}

// IsAnnotationTrue returns true if the given object has an annotation with a given
// key and the value of this annotation is a true boolean.
func IsAnnotationTrue(o client.Object, key string) bool {
enabled, err := strconv.ParseBool(o.GetAnnotations()[key])
return err == nil && enabled
// AnnotationAddedPredicate returns a predicate when the given annotation key was added.
func AnnotationAddedPredicate(annotation string) predicate.Funcs {
return predicate.Funcs{
CreateFunc: func(ce event.CreateEvent) bool {
return false
},
UpdateFunc: func(update event.UpdateEvent) bool {
return annotationWasAdded(update, annotation)
},
DeleteFunc: func(de event.DeleteEvent) bool {
return false
},
}
}

// AnnotationRemovedPredicate returns a predicate when the given annotation key was removed.
func AnnotationRemovedPredicate(annotation string) predicate.Funcs {
return predicate.Funcs{
CreateFunc: func(ce event.CreateEvent) bool {
return false
},
UpdateFunc: func(update event.UpdateEvent) bool {
return annotationWasRemoved(update, annotation)
},
DeleteFunc: func(de event.DeleteEvent) bool {
return false
},
}
}
19 changes: 9 additions & 10 deletions controllers/generic_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,17 @@ func (g GenericController[O]) Reconcile(ctx context.Context, req ctrl.Request) (
}
}

wasPresent, err := v2.RemoveAnnotation(ctx, g.c, o, v2.ReconcileAnnotation)
if err != nil {
return ctrl.Result{}, fmt.Errorf("unable to remove reconcile annotation: %w", err)
}
if wasPresent, err := v2.RemoveAnnotation(ctx, g.c, o, v2.ReconcileAnnotation); wasPresent {
if err != nil {
return ctrl.Result{}, fmt.Errorf("unable to remove reconcile annotation: %w", err)
}

if wasPresent {
// the update of the annotation removal triggers the next reconciliation
log.Info("removed reconcile annotation from resource")
return ctrl.Result{}, nil
}

var (
statusErr error
)
var statusErr error

if g.hasStatus {
defer func() {
Expand All @@ -169,6 +166,8 @@ func (g GenericController[O]) Reconcile(ctx context.Context, req ctrl.Request) (
}

if v2.IsAnnotationTrue(o, v2.MaintenanceAnnotation) {
log.Info("reconciling in maintenance mode")

rctx.InMaintenance = true

defer func() {
Expand All @@ -180,12 +179,12 @@ func (g GenericController[O]) Reconcile(ctx context.Context, req ctrl.Request) (
return
}

log.Info("removed maintenance annotation")
log.Info("cleaned up maintenance annotation")
}()
}

log.Info("reconciling resource")
err = g.reconciler.Reconcile(rctx)
err := g.reconciler.Reconcile(rctx)
if err != nil {
var requeueErr *requeueError

Expand Down

0 comments on commit e227226

Please sign in to comment.