Files
Joshua Hoblitt 9f1ed201db core: typed watch handlers and predicates
All existing controller runtime watches are converted to use "typed"
handlers and predicates instead of operating on `client.Object`.  The
intent is to be bug for bug equivalent with the existing logic while
replacing run time type assertions and switch statements with compile
time type constraints and type casts. In several cases, functions using
assertions were split up such that each function only handles a single
Kind at a time. It is hoped that this will improve readability and
maintainability while facilitating future refactoring such as migrating
some watches to using IndexFields.

Of particular note is that the massive switch statement in
`WatchControllerPredicate()`  from
`pkg/operator/ceph/controller/predicate.go` has been replaced with
generics, reflection, and splitting the obc logic into its own predicate
function. There are still many helper functions operating on
`client.Object`. These were not updated unless required by the compiler
in order to limit the size of this change. The type safety of these
funcs should be improved as followup work.

 It is strongly suggested that going forward, handlers and predicates
 only handle a single Kind (generic or not) and that switches / type
 assertions are heavily discouraged or forbidden. This PR removed all
 but a single switch statement in a predicate, which should be addressed
 in future work.

Signed-off-by: Joshua Hoblitt <josh@hoblitt.com>
2025-04-04 09:40:56 -07:00

73 lines
2.4 KiB
Go

/*
Copyright 2021 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package operator to manage Kubernetes storage.
package operator
import (
"github.com/rook/rook/pkg/operator/ceph/controller"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)
// predicateController is the predicate function to trigger reconcile on operator configuration cm change
func operatorSettingConfigMapPredicate[T *corev1.ConfigMap]() predicate.TypedFuncs[T] {
return predicate.TypedFuncs[T]{
CreateFunc: func(e event.TypedCreateEvent[T]) bool {
obj := (*corev1.ConfigMap)(e.Object)
return obj.GetName() == controller.OperatorSettingConfigMapName
},
UpdateFunc: func(e event.TypedUpdateEvent[T]) bool {
objOld := (*corev1.ConfigMap)(e.ObjectOld)
objNew := (*corev1.ConfigMap)(e.ObjectNew)
if objOld.GetName() == controller.OperatorSettingConfigMapName && objNew.GetName() == controller.OperatorSettingConfigMapName {
if objOld.Data["ROOK_CURRENT_NAMESPACE_ONLY"] != objNew.Data["ROOK_CURRENT_NAMESPACE_ONLY"] {
logger.Debug("ROOK_CURRENT_NAMESPACE_ONLY config updated, reloading the manager")
controller.ReloadManager()
// No need to ask for reconciliation since the context is going to be terminated when
// the signal is caught and the reconcile will run when the controller starts.
return false
}
// We still want to reconcile the operator manager if the configmap is updated
return true
}
return false
},
DeleteFunc: func(e event.TypedDeleteEvent[T]) bool {
obj := (*corev1.ConfigMap)(e.Object)
if obj.GetName() == controller.OperatorSettingConfigMapName {
logger.Debug("operator configmap deleted, not reconciling")
return false
}
return false
},
GenericFunc: func(e event.TypedGenericEvent[T]) bool {
return false
},
}
}