Skip to content

Commit

Permalink
Implement DrainAnnotationPredicate to watch only for a needed annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
e0ne committed Jul 5, 2023
1 parent ca325f1 commit 7179a85
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
5 changes: 2 additions & 3 deletions controllers/drain_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

Expand Down Expand Up @@ -92,7 +91,7 @@ func (dr *DrainReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}
drainingNodes++
} else {
reqLogger.Info("Too many nodes to be draining at the moment. Skipping node %s", node.Name)
reqLogger.Info("Too many nodes to be draining at the moment. Skipping node %s", "node", node.Name)
return reconcile.Result{}, nil
}
}
Expand Down Expand Up @@ -121,7 +120,7 @@ func (dr *DrainReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

// Watch for spec and annotation changes
nodePredicates := builder.WithPredicates(predicate.AnnotationChangedPredicate{})
nodePredicates := builder.WithPredicates(DrainAnnotationPredicate{})

return ctrl.NewControllerManagedBy(mgr).
For(&sriovnetworkv1.SriovOperatorConfig{}).
Expand Down
47 changes: 47 additions & 0 deletions controllers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ package controllers

import (
"bytes"
"context"
"encoding/json"
"os"
"strings"

"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
)

Expand All @@ -40,6 +45,48 @@ const (

var namespace = os.Getenv("NAMESPACE")

type DrainAnnotationPredicate struct {
predicate.Funcs
}

func (DrainAnnotationPredicate) Create(e event.CreateEvent) bool {
logger := log.FromContext(context.TODO())
if e.Object == nil {
logger.Error(nil, "Create event has no object for create", "event", e)
return false
}

if _, hasAnno := e.Object.GetAnnotations()[constants.NodeDrainAnnotation]; hasAnno {
logger.Error(nil, "Create event: node has no drain annotation", "event", e)
return true
}
return false
}

func (DrainAnnotationPredicate) Update(e event.UpdateEvent) bool {
logger := log.FromContext(context.TODO())
if e.ObjectOld == nil {
logger.Error(nil, "Update event has no old object to update", "event", e)
return false
}
if e.ObjectNew == nil {
logger.Error(nil, "Update event has no new object for update", "event", e)
return false
}

oldAnno, hasOldAnno := e.ObjectOld.GetAnnotations()[constants.NodeDrainAnnotation]
newAnno, hasNewAnno := e.ObjectNew.GetAnnotations()[constants.NodeDrainAnnotation]

if !hasOldAnno || !hasNewAnno {
logger.Error(nil, "Update event: can not compare annotations", "old", hasOldAnno)
logger.Error(nil, "Update event: can not compare annotations", "new", hasNewAnno)
logger.Error(nil, "Update event: can not compare annotations", "event", e)
return false
}

return oldAnno == newAnno
}

func GetImagePullSecrets() []string {
imagePullSecrets := os.Getenv("IMAGE_PULL_SECRETS")
if imagePullSecrets != "" {
Expand Down

0 comments on commit 7179a85

Please sign in to comment.