Skip to content

Commit

Permalink
Trim down deployment triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
psav committed Dec 9, 2021
1 parent addd7e1 commit b2d07c3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion controllers/cloud.redhat.com/clowdapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (r *ClowdAppReconciler) SetupWithManager(mgr ctrl.Manager) error {
handler.EnqueueRequestsFromMapFunc(r.appsToEnqueueUponEnvUpdate),
builder.WithPredicates(getEnvironmentPredicate(r.Log, "app")),
).
Owns(&apps.Deployment{}, builder.WithPredicates(getAlwaysPredicate(r.Log, "app"))).
Owns(&apps.Deployment{}, builder.WithPredicates(getDeploymentPredicate(r.Log, "app"))).
Owns(&core.Service{}, builder.WithPredicates(getGenerationOnlyPredicate(r.Log, "app"))).
Owns(&core.ConfigMap{}, builder.WithPredicates(getGenerationOnlyPredicate(r.Log, "app"))).
WithOptions(controller.Options{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (r *ClowdEnvironmentReconciler) SetupWithManager(mgr ctrl.Manager) error {

ctrlr := ctrl.NewControllerManagedBy(mgr).For(&crd.ClowdEnvironment{})

ctrlr.Owns(&apps.Deployment{}, builder.WithPredicates(getAlwaysPredicate(r.Log, "app")))
ctrlr.Owns(&apps.Deployment{}, builder.WithPredicates(getDeploymentPredicate(r.Log, "app")))
ctrlr.Owns(&core.Service{}, builder.WithPredicates(getGenerationOnlyPredicate(r.Log, "app")))
ctrlr.Watches(
&source.Kind{Type: &crd.ClowdApp{}},
Expand Down
42 changes: 42 additions & 0 deletions controllers/cloud.redhat.com/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/RedHatInsights/go-difflib/difflib"
strimzi "github.com/RedHatInsights/strimzi-client-go/apis/kafka.strimzi.io/v1beta2"
"github.com/go-logr/logr"
apps "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
Expand Down Expand Up @@ -39,6 +40,21 @@ func defaultPredicateLog(logr logr.Logger, ctrlName string) predicate.Funcs {
}
}

func deploymentUpdateFunc(e event.UpdateEvent) bool {
objOld := e.ObjectOld.(*apps.Deployment)
objNew := e.ObjectNew.(*apps.Deployment)
if objNew.GetGeneration() != objOld.GetGeneration() {
return true
}
if (objOld.Status.AvailableReplicas != objNew.Status.AvailableReplicas) && (objNew.Status.AvailableReplicas == objNew.Status.Replicas) {
return true
}
if (objOld.Status.AvailableReplicas == objOld.Status.Replicas) && (objNew.Status.AvailableReplicas != objOld.Status.ReadyReplicas) {
return true
}
return false
}

func kafkaUpdateFunc(e event.UpdateEvent) bool {
objOld := e.ObjectOld.(*strimzi.Kafka)
objNew := e.ObjectNew.(*strimzi.Kafka)
Expand Down Expand Up @@ -80,6 +96,32 @@ func generationOnlyPredicateWithLog(logr logr.Logger, ctrlName string) predicate
return predicates
}

// These functions are returned for deployments
// These functions always return on an update
func getDeploymentPredicate(logr logr.Logger, ctrlName string) predicate.Predicate {
if clowderconfig.LoadedConfig.DebugOptions.Logging.DebugLogging {
return deploymentPredicateWithLog(logr, ctrlName)
}
return predicate.Funcs{
UpdateFunc: deploymentUpdateFunc,
}
}

func deploymentPredicateWithLog(logr logr.Logger, ctrlName string) predicate.Predicate {
predicates := defaultPredicateLog(logr, ctrlName)
predicates.UpdateFunc = func(e event.UpdateEvent) bool {
gvk, _ := utils.GetKindFromObj(Scheme, e.ObjectNew)
displayUpdateDiff(e, logr, ctrlName, gvk)
result := deploymentUpdateFunc(e)
if result {
logr.Info("Reconciliation trigger", "ctrl", ctrlName, "type", "update", "resType", gvk.Kind, "name", e.ObjectNew.GetName(), "namespace", e.ObjectNew.GetNamespace())
return true
}
return false
}
return predicates
}

// These functions always return on an update
func getAlwaysPredicate(logr logr.Logger, ctrlName string) predicate.Predicate {
if clowderconfig.LoadedConfig.DebugOptions.Logging.DebugLogging {
Expand Down

0 comments on commit b2d07c3

Please sign in to comment.