From cd7f9c2ca160d6d79cc385a0aa3dd61b91829e3b Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Fri, 11 Oct 2024 14:03:59 +0200 Subject: [PATCH] Consoleplugin mutator (#935) * consoleplugin mutator Signed-off-by: Eguzki Astiz Lezaun * generic mutator Signed-off-by: Eguzki Astiz Lezaun --------- Signed-off-by: Eguzki Astiz Lezaun --- controllers/consoleplugin_reconciler.go | 3 +- pkg/library/reconcilers/base_mutator.go | 33 +++++++++++++++++++ .../consoleplugin/consoleplugin_mutator.go | 22 +++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 pkg/library/reconcilers/base_mutator.go create mode 100644 pkg/openshift/consoleplugin/consoleplugin_mutator.go diff --git a/controllers/consoleplugin_reconciler.go b/controllers/consoleplugin_reconciler.go index 10b12a900..a9cf15d7a 100644 --- a/controllers/consoleplugin_reconciler.go +++ b/controllers/consoleplugin_reconciler.go @@ -117,7 +117,8 @@ func (r *ConsolePluginReconciler) Run(eventCtx context.Context, _ []controller.R if !topologyExists { utils.TagObjectToDelete(consolePlugin) } - err = r.ReconcileResource(ctx, &consolev1.ConsolePlugin{}, consolePlugin, reconcilers.CreateOnlyMutator) + consolePluginMutator := reconcilers.Mutator[*consolev1.ConsolePlugin](consoleplugin.ServiceMutator) + err = r.ReconcileResource(ctx, &consolev1.ConsolePlugin{}, consolePlugin, consolePluginMutator) if err != nil { logger.Error(err, "reconciling consoleplugin") return err diff --git a/pkg/library/reconcilers/base_mutator.go b/pkg/library/reconcilers/base_mutator.go new file mode 100644 index 000000000..e389113af --- /dev/null +++ b/pkg/library/reconcilers/base_mutator.go @@ -0,0 +1,33 @@ +package reconcilers + +import ( + "fmt" + + "sigs.k8s.io/controller-runtime/pkg/client" +) + +// TypedMutateFn is a function which mutates the existing T into it's desired state. +type TypedMutateFn[T client.Object] func(desired, existing T) bool + +func Mutator[T client.Object](opts ...TypedMutateFn[T]) MutateFn { + return func(existingObj, desiredObj client.Object) (bool, error) { + existing, ok := existingObj.(T) + if !ok { + return false, fmt.Errorf("existing %T is not %T", existingObj, *new(T)) + } + desired, ok := desiredObj.(T) + if !ok { + return false, fmt.Errorf("desired %T is not %T", desiredObj, *new(T)) + } + + update := false + + // Loop through each option + for _, opt := range opts { + tmpUpdate := opt(desired, existing) + update = update || tmpUpdate + } + + return update, nil + } +} diff --git a/pkg/openshift/consoleplugin/consoleplugin_mutator.go b/pkg/openshift/consoleplugin/consoleplugin_mutator.go new file mode 100644 index 000000000..5e45b332e --- /dev/null +++ b/pkg/openshift/consoleplugin/consoleplugin_mutator.go @@ -0,0 +1,22 @@ +package consoleplugin + +import ( + "reflect" + + consolev1 "github.com/openshift/api/console/v1" +) + +func ServiceMutator(desired, existing *consolev1.ConsolePlugin) bool { + if desired.Spec.Backend.Service == nil { + panic("coded ConsolePlugin does not specify service") + } + + update := false + + if !reflect.DeepEqual(existing.Spec.Backend.Service, desired.Spec.Backend.Service) { + existing.Spec.Backend.Service = desired.Spec.Backend.Service + update = true + } + + return update +}