Skip to content

Commit

Permalink
Consoleplugin mutator (#935)
Browse files Browse the repository at this point in the history
* consoleplugin mutator

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* generic mutator

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

---------

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki authored Oct 11, 2024
1 parent c0438c3 commit cd7f9c2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion controllers/consoleplugin_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions pkg/library/reconcilers/base_mutator.go
Original file line number Diff line number Diff line change
@@ -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
}
}
22 changes: 22 additions & 0 deletions pkg/openshift/consoleplugin/consoleplugin_mutator.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit cd7f9c2

Please sign in to comment.