-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |