From ae12e6be5124007655adc863e0218fbfa726fe21 Mon Sep 17 00:00:00 2001 From: Brendan Shephard Date: Wed, 7 Feb 2024 11:35:04 +1000 Subject: [PATCH] [watches] Fix watches declarations for CR update This change fixes the Watches declarations to comply with the new controller-runtime standards. Signed-off-by: Brendan Shephard --- controllers/heat_controller.go | 9 ++++----- controllers/heatapi_controller.go | 30 ++++++++++++---------------- controllers/heatcfnapi_controller.go | 30 ++++++++++++---------------- controllers/heatengine_controller.go | 19 ++++++++---------- 4 files changed, 38 insertions(+), 50 deletions(-) diff --git a/controllers/heat_controller.go b/controllers/heat_controller.go index f554c638..ef89e707 100644 --- a/controllers/heat_controller.go +++ b/controllers/heat_controller.go @@ -38,7 +38,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/source" heat "github.com/openstack-k8s-operators/heat-operator/pkg/heat" heatapi "github.com/openstack-k8s-operators/heat-operator/pkg/heatapi" @@ -245,7 +244,7 @@ func (r *HeatReconciler) SetupWithManager(mgr ctrl.Manager) error { return err } - memcachedFn := func(o client.Object) []reconcile.Request { + memcachedFn := func(ctx context.Context, o client.Object) []reconcile.Request { result := []reconcile.Request{} // get all Heat CRs @@ -287,17 +286,17 @@ func (r *HeatReconciler) SetupWithManager(mgr ctrl.Manager) error { Owns(&corev1.ServiceAccount{}). Owns(&rbacv1.Role{}). Owns(&rbacv1.RoleBinding{}). - Watches(&source.Kind{Type: &memcachedv1.Memcached{}}, + Watches(&memcachedv1.Memcached{}, handler.EnqueueRequestsFromMapFunc(memcachedFn)). Watches( - &source.Kind{Type: &corev1.Secret{}}, + &corev1.Secret{}, handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc), builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). Complete(r) } -func (r *HeatReconciler) findObjectsForSrc(src client.Object) []reconcile.Request { +func (r *HeatReconciler) findObjectsForSrc(ctx context.Context, src client.Object) []reconcile.Request { requests := []reconcile.Request{} l := log.FromContext(context.Background()).WithName("Controllers").WithName("Heat") diff --git a/controllers/heatapi_controller.go b/controllers/heatapi_controller.go index 6b14b421..7f463797 100644 --- a/controllers/heatapi_controller.go +++ b/controllers/heatapi_controller.go @@ -37,7 +37,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/source" "github.com/go-logr/logr" heatv1beta1 "github.com/openstack-k8s-operators/heat-operator/api/v1beta1" @@ -66,15 +65,13 @@ type HeatAPIReconciler struct { Kclient kubernetes.Interface } -var ( - keystoneServices = []map[string]string{ - { - "type": heat.ServiceType, - "name": heat.ServiceName, - "desc": "Heat API service", - }, - } -) +var keystoneServices = []map[string]string{ + { + "type": heat.ServiceType, + "name": heat.ServiceName, + "desc": "Heat API service", + }, +} // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatapis,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatapis/status,verbs=get;update;patch @@ -182,7 +179,6 @@ func (r *HeatAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re // SetupWithManager sets up the controller with the Manager. func (r *HeatAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { - // index passwordSecretField if err := mgr.GetFieldIndexer().IndexField(context.Background(), &heatv1beta1.HeatAPI{}, passwordSecretField, func(rawObj client.Object) []string { // Extract the secret name from the spec, if one is provided @@ -243,7 +239,7 @@ func (r *HeatAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { return err } - configMapFn := func(o client.Object) []reconcile.Request { + configMapFn := func(ctx context.Context, o client.Object) []reconcile.Request { result := []reconcile.Request{} apis := &heatv1beta1.HeatAPIList{} @@ -283,17 +279,17 @@ func (r *HeatAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { Owns(&appsv1.Deployment{}). Owns(&corev1.Service{}). // watch the config CMs we don't own - Watches(&source.Kind{Type: &corev1.ConfigMap{}}, + Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(configMapFn)). Watches( - &source.Kind{Type: &corev1.Secret{}}, + &corev1.Secret{}, handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc), builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). Complete(r) } -func (r *HeatAPIReconciler) findObjectsForSrc(src client.Object) []reconcile.Request { +func (r *HeatAPIReconciler) findObjectsForSrc(ctx context.Context, src client.Object) []reconcile.Request { requests := []reconcile.Request{} l := log.FromContext(context.Background()).WithName("Controllers").WithName("HeatAPI") @@ -607,8 +603,8 @@ func (r *HeatAPIReconciler) reconcileNormal(ctx context.Context, instance *heatv parentHeatName := heat.GetOwningHeatName(instance) configMaps := []string{ - fmt.Sprintf("%s-scripts", parentHeatName), //ScriptsConfigMap - fmt.Sprintf("%s-config-data", parentHeatName), //ConfigMap + fmt.Sprintf("%s-scripts", parentHeatName), // ScriptsConfigMap + fmt.Sprintf("%s-config-data", parentHeatName), // ConfigMap } _, err = configmap.GetConfigMaps(ctx, helper, instance, configMaps, instance.Namespace, &configMapVars) diff --git a/controllers/heatcfnapi_controller.go b/controllers/heatcfnapi_controller.go index ffb4358a..e33a74a1 100644 --- a/controllers/heatcfnapi_controller.go +++ b/controllers/heatcfnapi_controller.go @@ -37,7 +37,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/source" "github.com/go-logr/logr" heatv1beta1 "github.com/openstack-k8s-operators/heat-operator/api/v1beta1" @@ -66,15 +65,13 @@ type HeatCfnAPIReconciler struct { Kclient kubernetes.Interface } -var ( - keystoneCfnServices = []map[string]string{ - { - "type": heat.CfnServiceType, - "name": heat.CfnServiceName, - "desc": "Heat Cloudformation API service", - }, - } -) +var keystoneCfnServices = []map[string]string{ + { + "type": heat.CfnServiceType, + "name": heat.CfnServiceName, + "desc": "Heat Cloudformation API service", + }, +} // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatcfnapis,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=heat.openstack.org,resources=heatcfnapis/status,verbs=get;update;patch @@ -186,7 +183,6 @@ func (r *HeatCfnAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) // SetupWithManager sets up the controller with the Manager. func (r *HeatCfnAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { - // index passwordSecretField if err := mgr.GetFieldIndexer().IndexField(context.Background(), &heatv1beta1.HeatCfnAPI{}, passwordSecretField, func(rawObj client.Object) []string { // Extract the secret name from the spec, if one is provided @@ -247,7 +243,7 @@ func (r *HeatCfnAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { return err } - configMapFn := func(o client.Object) []reconcile.Request { + configMapFn := func(ctx context.Context, o client.Object) []reconcile.Request { result := []reconcile.Request{} apis := &heatv1beta1.HeatCfnAPIList{} @@ -287,17 +283,17 @@ func (r *HeatCfnAPIReconciler) SetupWithManager(mgr ctrl.Manager) error { Owns(&appsv1.Deployment{}). Owns(&corev1.Service{}). // watch the config CMs we don't own - Watches(&source.Kind{Type: &corev1.ConfigMap{}}, + Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(configMapFn)). Watches( - &source.Kind{Type: &corev1.Secret{}}, + &corev1.Secret{}, handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc), builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). Complete(r) } -func (r *HeatCfnAPIReconciler) findObjectsForSrc(src client.Object) []reconcile.Request { +func (r *HeatCfnAPIReconciler) findObjectsForSrc(ctx context.Context, src client.Object) []reconcile.Request { requests := []reconcile.Request{} l := log.FromContext(context.Background()).WithName("Controllers").WithName("HeatCfnAPI") @@ -611,8 +607,8 @@ func (r *HeatCfnAPIReconciler) reconcileNormal(ctx context.Context, instance *he parentHeatName := heat.GetOwningHeatName(instance) configMaps := []string{ - fmt.Sprintf("%s-scripts", parentHeatName), //ScriptsConfigMap - fmt.Sprintf("%s-config-data", parentHeatName), //ConfigMap + fmt.Sprintf("%s-scripts", parentHeatName), // ScriptsConfigMap + fmt.Sprintf("%s-config-data", parentHeatName), // ConfigMap } _, err = configmap.GetConfigMaps(ctx, helper, instance, configMaps, instance.Namespace, &configMapVars) diff --git a/controllers/heatengine_controller.go b/controllers/heatengine_controller.go index 0038169a..0b3b9ecb 100644 --- a/controllers/heatengine_controller.go +++ b/controllers/heatengine_controller.go @@ -36,7 +36,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/source" "github.com/go-logr/logr" heatv1beta1 "github.com/openstack-k8s-operators/heat-operator/api/v1beta1" @@ -157,7 +156,6 @@ func (r *HeatEngineReconciler) Reconcile(ctx context.Context, req ctrl.Request) // SetupWithManager sets up the controller with the Manager. func (r *HeatEngineReconciler) SetupWithManager(mgr ctrl.Manager) error { - // index passwordSecretField if err := mgr.GetFieldIndexer().IndexField(context.Background(), &heatv1beta1.HeatEngine{}, passwordSecretField, func(rawObj client.Object) []string { // Extract the secret name from the spec, if one is provided @@ -194,7 +192,7 @@ func (r *HeatEngineReconciler) SetupWithManager(mgr ctrl.Manager) error { return err } - configMapFn := func(o client.Object) []reconcile.Request { + configMapFn := func(ctx context.Context, o client.Object) []reconcile.Request { result := []reconcile.Request{} engines := &heatv1beta1.HeatEngineList{} @@ -218,7 +216,6 @@ func (r *HeatEngineReconciler) SetupWithManager(mgr ctrl.Manager) error { r.Log.Info(fmt.Sprintf("ConfigMap object %s and CR %s marked with label: %s", o.GetName(), cr.Name, l)) result = append(result, reconcile.Request{NamespacedName: name}) } - } } if len(result) > 0 { @@ -230,17 +227,17 @@ func (r *HeatEngineReconciler) SetupWithManager(mgr ctrl.Manager) error { For(&heatv1beta1.HeatEngine{}). Owns(&corev1.ConfigMap{}). Owns(&appsv1.Deployment{}). - Watches(&source.Kind{Type: &corev1.ConfigMap{}}, + Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(configMapFn)). Watches( - &source.Kind{Type: &corev1.Secret{}}, + &corev1.Secret{}, handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc), builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). Complete(r) } -func (r *HeatEngineReconciler) findObjectsForSrc(src client.Object) []reconcile.Request { +func (r *HeatEngineReconciler) findObjectsForSrc(ctx context.Context, src client.Object) []reconcile.Request { requests := []reconcile.Request{} l := log.FromContext(context.Background()).WithName("Controllers").WithName("HeatEngine") @@ -299,8 +296,8 @@ func (r *HeatEngineReconciler) reconcileInit( func (r *HeatEngineReconciler) reconcileNormal( ctx context.Context, instance *heatv1beta1.HeatEngine, - helper *helper.Helper) (ctrl.Result, error) { - + helper *helper.Helper, +) (ctrl.Result, error) { r.Log.Info("Reconciling Heat Engine") // TODO(bshephar) Write the reconcile logic for Heat engine. Let's just create @@ -363,8 +360,8 @@ func (r *HeatEngineReconciler) reconcileNormal( parentHeatName := heat.GetOwningHeatName(instance) configMaps := []string{ - fmt.Sprintf("%s-scripts", parentHeatName), //ScriptsConfigMap - fmt.Sprintf("%s-config-data", parentHeatName), //ConfigMap + fmt.Sprintf("%s-scripts", parentHeatName), // ScriptsConfigMap + fmt.Sprintf("%s-config-data", parentHeatName), // ConfigMap } _, err = configmap.GetConfigMaps(ctx, helper, instance, configMaps, instance.Namespace, &configMapVars)