diff --git a/operator/pkg/config/multiclusterglobalhub_config.go b/operator/pkg/config/multiclusterglobalhub_config.go index 3d7a82223..ce521a05b 100644 --- a/operator/pkg/config/multiclusterglobalhub_config.go +++ b/operator/pkg/config/multiclusterglobalhub_config.go @@ -38,6 +38,7 @@ import ( "github.com/stolostron/multicluster-global-hub/operator/api/operator/v1alpha4" operatorconstants "github.com/stolostron/multicluster-global-hub/operator/pkg/constants" + "github.com/stolostron/multicluster-global-hub/pkg/constants" "github.com/stolostron/multicluster-global-hub/pkg/logger" ) @@ -131,6 +132,27 @@ var MGHPred = predicate.Funcs{ }, } +// watch globalhub applied services +var GeneralPredicate = predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + return false + }, + UpdateFunc: func(e event.UpdateEvent) bool { + if e.ObjectNew.GetLabels()[constants.GlobalHubOwnerLabelKey] != + constants.GHOperatorOwnerLabelVal { + return false + } + return e.ObjectNew.GetGeneration() != e.ObjectOld.GetGeneration() + }, + DeleteFunc: func(e event.DeleteEvent) bool { + if e.Object.GetLabels()[constants.GlobalHubOwnerLabelKey] == + constants.GHOperatorOwnerLabelVal { + return true + } + return false + }, +} + // getAnnotation returns the annotation value for a given key, or an empty string if not set func getAnnotation(mgh *v1alpha4.MulticlusterGlobalHub, annotationKey string) string { annotations := mgh.GetAnnotations() diff --git a/operator/pkg/controllers/acm/resources.go b/operator/pkg/controllers/acm/resources.go index 55f0d05e0..bcfff71f3 100644 --- a/operator/pkg/controllers/acm/resources.go +++ b/operator/pkg/controllers/acm/resources.go @@ -90,10 +90,11 @@ func (r *ACMResourceController) readyToWatchACMResources() bool { } func StartController(opts config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start acm controller") if acmResourceController != nil { return acmResourceController, nil } + log.Info("start acm controller") + acmController := &ACMResourceController{ Manager: opts.Manager, Resources: make(map[string]bool), diff --git a/operator/pkg/controllers/agent/addon_manager.go b/operator/pkg/controllers/agent/addon_manager.go index 20396eee9..a534e9c5c 100644 --- a/operator/pkg/controllers/agent/addon_manager.go +++ b/operator/pkg/controllers/agent/addon_manager.go @@ -61,11 +61,11 @@ func ReadyToEnableAddonManager(mgh *v1alpha4.MulticlusterGlobalHub) bool { } func StartAddonManagerController(initOption config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start addon manager controller") - if addonManagerController != nil { return addonManagerController, nil } + log.Info("start addon manager controller") + if !ReadyToEnableAddonManager(initOption.MulticlusterGlobalHub) { return nil, nil } diff --git a/operator/pkg/controllers/agent/default_agent_controller.go b/operator/pkg/controllers/agent/default_agent_controller.go index 3347162fb..b30346133 100644 --- a/operator/pkg/controllers/agent/default_agent_controller.go +++ b/operator/pkg/controllers/agent/default_agent_controller.go @@ -128,10 +128,11 @@ func NewDefaultAgentController(c client.Client) *DefaultAgentController { } func StartDefaultAgentController(initOption config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start default agent controller") if defaultAgentController != nil { return defaultAgentController, nil } + log.Info("start default agent controller") + if !ReadyToEnableAddonManager(initOption.MulticlusterGlobalHub) { return nil, nil } diff --git a/operator/pkg/controllers/agent/hosted_agent_controller.go b/operator/pkg/controllers/agent/hosted_agent_controller.go index a7fd1a8a9..6cdba56e3 100644 --- a/operator/pkg/controllers/agent/hosted_agent_controller.go +++ b/operator/pkg/controllers/agent/hosted_agent_controller.go @@ -56,14 +56,18 @@ var ( ) func StartHostedAgentController(initOption config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start hosted agent controller") - if hostedAgentController != nil { return hostedAgentController, nil } + if !config.IsACMResourceReady() { + return nil, nil + } if !config.GetImportClusterInHosted() { return nil, nil } + + log.Info("start hosted agent controller") + if !ReadyToEnableAddonManager(initOption.MulticlusterGlobalHub) { return nil, nil } diff --git a/operator/pkg/controllers/backup/backup_start.go b/operator/pkg/controllers/backup/backup_start.go index 1088346f1..a4ad19745 100644 --- a/operator/pkg/controllers/backup/backup_start.go +++ b/operator/pkg/controllers/backup/backup_start.go @@ -69,13 +69,14 @@ func GetBackupController() *BackupReconciler { } func StartController(initOption config.ControllerOption) (config.ControllerInterface, error) { - log.Infof("start backup controller") if backupController != nil { return backupController, nil } if !config.IsACMResourceReady() { return nil, nil } + log.Infof("start backup controller") + c := &BackupReconciler{ Manager: initOption.Manager, Client: initOption.Manager.GetClient(), diff --git a/operator/pkg/controllers/grafana/grafana_reconciler.go b/operator/pkg/controllers/grafana/grafana_reconciler.go index 2a026eda9..35abd827e 100644 --- a/operator/pkg/controllers/grafana/grafana_reconciler.go +++ b/operator/pkg/controllers/grafana/grafana_reconciler.go @@ -15,6 +15,7 @@ import ( "gopkg.in/yaml.v2" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + rbacv1 "k8s.io/api/rbac/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -51,6 +52,7 @@ import ( // +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=clusterroles,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=clusterrolebindings,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update;delete +// +kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;delete;patch // +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="apps",resources=deployments,verbs=get;list;watch;create;update;delete @@ -125,7 +127,6 @@ func (r *GrafanaReconciler) IsResourceRemoved() bool { } func StartController(initOption config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start grafana controller") if grafanaController != nil { return grafanaController, nil } @@ -135,6 +136,8 @@ func StartController(initOption config.ControllerOption) (config.ControllerInter if config.GetStorageConnection() == nil { return nil, nil } + log.Info("start grafana controller") + grafanaController = NewGrafanaReconciler(initOption.Manager, initOption.KubeClient) err := grafanaController.SetupWithManager(initOption.Manager) @@ -156,7 +159,17 @@ func (r *GrafanaReconciler) SetupWithManager(mgr ctrl.Manager) error { Watches(&corev1.ConfigMap{}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(configmapPred)). Watches(&appsv1.Deployment{}, - &handler.EnqueueRequestForObject{}, builder.WithPredicates(deplomentPred)) + &handler.EnqueueRequestForObject{}, builder.WithPredicates(deploymentPred)). + Watches(&corev1.Service{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&corev1.ServiceAccount{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&rbacv1.ClusterRole{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&rbacv1.ClusterRoleBinding{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&routev1.Route{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)) if _, err := mgr.GetRESTMapper().KindFor(schema.GroupVersionResource{ Group: "image.openshift.io", @@ -202,21 +215,6 @@ var imageStreamPred = predicate.Funcs{ }, } -var deplomentPred = predicate.Funcs{ - CreateFunc: func(e event.CreateEvent) bool { - return e.Object.GetNamespace() == commonutils.GetDefaultNamespace() && - e.Object.GetName() == config.COMPONENTS_GRAFANA_NAME - }, - UpdateFunc: func(e event.UpdateEvent) bool { - return e.ObjectNew.GetNamespace() == commonutils.GetDefaultNamespace() && - e.ObjectNew.GetName() == config.COMPONENTS_GRAFANA_NAME - }, - DeleteFunc: func(e event.DeleteEvent) bool { - return e.Object.GetNamespace() == commonutils.GetDefaultNamespace() && - e.Object.GetName() == config.COMPONENTS_GRAFANA_NAME - }, -} - var configmapPred = predicate.Funcs{ CreateFunc: func(e event.CreateEvent) bool { return WatchedConfigMap.Has(e.Object.GetName()) diff --git a/operator/pkg/controllers/inventory/inventory_reconciler.go b/operator/pkg/controllers/inventory/inventory_reconciler.go index f0fd3d985..94f88fc18 100644 --- a/operator/pkg/controllers/inventory/inventory_reconciler.go +++ b/operator/pkg/controllers/inventory/inventory_reconciler.go @@ -68,7 +68,6 @@ func (r *InventoryReconciler) IsResourceRemoved() bool { } func StartController(initOption config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start inventory controller") if inventoryReconciler != nil { return inventoryReconciler, nil } @@ -81,6 +80,7 @@ func StartController(initOption config.ControllerOption) (config.ControllerInter if config.GetStorageConnection() == nil { return nil, nil } + log.Info("start inventory controller") inventoryReconciler = NewInventoryReconciler(initOption.Manager, initOption.KubeClient) @@ -100,6 +100,12 @@ func (r *InventoryReconciler) SetupWithManager(mgr ctrl.Manager) error { builder.WithPredicates(config.MGHPred)). Watches(&appsv1.Deployment{}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(deploymentPred)). + Watches(&corev1.Secret{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&corev1.Service{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&corev1.ServiceAccount{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). Complete(r) } diff --git a/operator/pkg/controllers/managedhub/managedhub_controller.go b/operator/pkg/controllers/managedhub/managedhub_controller.go index 85c6c63fc..1fbdd5e17 100644 --- a/operator/pkg/controllers/managedhub/managedhub_controller.go +++ b/operator/pkg/controllers/managedhub/managedhub_controller.go @@ -55,13 +55,14 @@ func (r *ManagedHubController) IsResourceRemoved() bool { } func StartController(initOption config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start managedhub controller") if managedHubController != nil { return managedHubController, nil } if !config.IsACMResourceReady() { return nil, nil } + log.Info("start managedhub controller") + managedHubController = NewManagedHubController(initOption.Manager) err := managedHubController.SetupWithManager(initOption.Manager) if err != nil { diff --git a/operator/pkg/controllers/manager/manager_reconciler.go b/operator/pkg/controllers/manager/manager_reconciler.go index 631c3a818..99a18fafe 100644 --- a/operator/pkg/controllers/manager/manager_reconciler.go +++ b/operator/pkg/controllers/manager/manager_reconciler.go @@ -9,9 +9,11 @@ import ( "strconv" "time" + routev1 "github.com/openshift/api/route/v1" promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + rbacv1 "k8s.io/api/rbac/v1" "k8s.io/apimachinery/pkg/api/equality" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -19,6 +21,7 @@ import ( "k8s.io/client-go/discovery/cached/memory" "k8s.io/client-go/kubernetes" "k8s.io/client-go/restmapper" + "open-cluster-management.io/api/addon/v1alpha1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -39,9 +42,11 @@ import ( ) // +kubebuilder:rbac:groups=operator.open-cluster-management.io,resources=multiclusterglobalhubs,verbs=get;list;watch; +// +kubebuilder:rbac:groups="route.openshift.io",resources=routes,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=clusterroles,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=clusterrolebindings,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update;delete +// +kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;delete;patch // +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;delete // +kubebuilder:rbac:groups="apps",resources=deployments,verbs=get;list;watch;create;update;delete @@ -85,11 +90,11 @@ var ( ) func StartController(initOption config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start manager controller") - if managerController != nil { return managerController, nil } + log.Info("start manager controller") + if config.GetTransporterConn() == nil { return nil, nil } @@ -115,12 +120,32 @@ func (r *ManagerReconciler) IsResourceRemoved() bool { // SetupWithManager sets up the controller with the Manager. func (r *ManagerReconciler) SetupWithManager(mgr ctrl.Manager) error { - return ctrl.NewControllerManagedBy(mgr).Named("manager"). + mgrBuilder := ctrl.NewControllerManagedBy(mgr).Named("manager"). For(&v1alpha4.MulticlusterGlobalHub{}, builder.WithPredicates(config.MGHPred)). Watches(&appsv1.Deployment{}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(deploymentPred)). - Complete(r) + Watches(&corev1.Service{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&corev1.ServiceAccount{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&rbacv1.ClusterRole{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&rbacv1.ClusterRoleBinding{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&rbacv1.Role{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&rbacv1.RoleBinding{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&routev1.Route{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)) + + if config.IsACMResourceReady() { + mgrBuilder = mgrBuilder. + Watches(&v1alpha1.ClusterManagementAddOn{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)) + } + return mgrBuilder.Complete(r) } var deploymentPred = predicate.Funcs{ diff --git a/operator/pkg/controllers/storage/storage_reconciler.go b/operator/pkg/controllers/storage/storage_reconciler.go index 7ade3404e..f3b97ad36 100644 --- a/operator/pkg/controllers/storage/storage_reconciler.go +++ b/operator/pkg/controllers/storage/storage_reconciler.go @@ -12,6 +12,7 @@ import ( "time" "github.com/jackc/pgx/v4" + promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -63,6 +64,7 @@ type StorageReconciler struct { upgrade bool databaseReconcileCount int enableGlobalResource bool + enableMetrics bool } var WatchedSecret = sets.NewString( @@ -71,10 +73,6 @@ var WatchedSecret = sets.NewString( config.PostgresCertName, ) -var WatchedConfigMap = sets.NewString( - constants.PostgresCAConfigMap, -) - var ( storageReconciler *StorageReconciler updateConnection bool @@ -85,13 +83,13 @@ func (r *StorageReconciler) IsResourceRemoved() bool { } func StartController(initOption config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start storage controller") - if storageReconciler != nil { return storageReconciler, nil } + log.Info("start storage controller") + storageReconciler = NewStorageReconciler(initOption.Manager, - initOption.OperatorConfig.GlobalResourceEnabled) + initOption.OperatorConfig.GlobalResourceEnabled, initOption.MulticlusterGlobalHub.Spec.EnableMetrics) err := storageReconciler.SetupWithManager(initOption.Manager) if err != nil { storageReconciler = nil @@ -101,12 +99,13 @@ func StartController(initOption config.ControllerOption) (config.ControllerInter return storageReconciler, nil } -func NewStorageReconciler(mgr ctrl.Manager, enableGlobalResource bool) *StorageReconciler { +func NewStorageReconciler(mgr ctrl.Manager, enableGlobalResource, enableMetrics bool) *StorageReconciler { return &StorageReconciler{ Manager: mgr, upgrade: false, databaseReconcileCount: 0, enableGlobalResource: enableGlobalResource, + enableMetrics: enableMetrics, } } @@ -117,9 +116,15 @@ func (r *StorageReconciler) SetupWithManager(mgr ctrl.Manager) error { Watches(&corev1.Secret{}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(secretPred)). Watches(&corev1.ConfigMap{}, - &handler.EnqueueRequestForObject{}, builder.WithPredicates(configmapPred)). + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). Watches(&appsv1.StatefulSet{}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(statefulSetPred)). + Watches(&corev1.ServiceAccount{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&promv1.PrometheusRule{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&promv1.ServiceMonitor{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). Complete(r) } @@ -138,26 +143,22 @@ var statefulSetPred = predicate.Funcs{ }, } -var configmapPred = predicate.Funcs{ - CreateFunc: func(e event.CreateEvent) bool { - return WatchedConfigMap.Has(e.Object.GetName()) - }, - UpdateFunc: func(e event.UpdateEvent) bool { - return WatchedConfigMap.Has(e.ObjectNew.GetName()) - }, - DeleteFunc: func(e event.DeleteEvent) bool { - return WatchedConfigMap.Has(e.Object.GetName()) - }, -} - var secretPred = predicate.Funcs{ CreateFunc: func(e event.CreateEvent) bool { return WatchedSecret.Has(e.Object.GetName()) }, UpdateFunc: func(e event.UpdateEvent) bool { + if e.ObjectNew.GetLabels()[constants.GlobalHubOwnerLabelKey] == + constants.GHOperatorOwnerLabelVal { + return true + } return WatchedSecret.Has(e.ObjectNew.GetName()) }, DeleteFunc: func(e event.DeleteEvent) bool { + if e.Object.GetLabels()[constants.GlobalHubOwnerLabelKey] == + constants.GHOperatorOwnerLabelVal { + return true + } return WatchedSecret.Has(e.Object.GetName()) }, } diff --git a/operator/pkg/controllers/transporter/protocol/strimzi_kafka_controller.go b/operator/pkg/controllers/transporter/protocol/strimzi_kafka_controller.go index 1030a0059..7f6d65a1c 100644 --- a/operator/pkg/controllers/transporter/protocol/strimzi_kafka_controller.go +++ b/operator/pkg/controllers/transporter/protocol/strimzi_kafka_controller.go @@ -148,10 +148,10 @@ var kafkaPred = predicate.Funcs{ } func StartKafkaController(ctx context.Context, mgr ctrl.Manager, transporter transport.Transporter) error { - log.Info("start kafka controller") if startedKafkaController { return nil } + log.Info("start kafka controller") r := &KafkaController{ c: mgr.GetClient(), trans: transporter.(*strimziTransporter), diff --git a/operator/pkg/controllers/transporter/transport_reconciler.go b/operator/pkg/controllers/transporter/transport_reconciler.go index f4019573a..2a25b50a5 100644 --- a/operator/pkg/controllers/transporter/transport_reconciler.go +++ b/operator/pkg/controllers/transporter/transport_reconciler.go @@ -51,10 +51,11 @@ func (c *TransportReconciler) IsResourceRemoved() bool { } func StartController(controllerOption config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start transport controller") if transportReconciler != nil { return transportReconciler, nil } + log.Info("start transport controller") + transportReconciler = NewTransportReconciler(controllerOption.Manager) err := transportReconciler.SetupWithManager(controllerOption.Manager) if err != nil { diff --git a/operator/pkg/controllers/webhook/webhook_controller.go b/operator/pkg/controllers/webhook/webhook_controller.go index bad59a531..b72c310e5 100644 --- a/operator/pkg/controllers/webhook/webhook_controller.go +++ b/operator/pkg/controllers/webhook/webhook_controller.go @@ -12,15 +12,17 @@ import ( "k8s.io/client-go/discovery" "k8s.io/client-go/discovery/cached/memory" "k8s.io/client-go/restmapper" + addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" + clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1" + clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/predicate" - "sigs.k8s.io/controller-runtime/pkg/source" - globalhubv1alpha4 "github.com/stolostron/multicluster-global-hub/operator/api/operator/v1alpha4" + "github.com/stolostron/multicluster-global-hub/operator/api/operator/v1alpha4" "github.com/stolostron/multicluster-global-hub/operator/pkg/config" "github.com/stolostron/multicluster-global-hub/operator/pkg/deployer" "github.com/stolostron/multicluster-global-hub/operator/pkg/renderer" @@ -59,10 +61,14 @@ func NewWebhookReconciler(mgr ctrl.Manager, } func StartController(opts config.ControllerOption) (config.ControllerInterface, error) { - log.Info("start webhook controller") if webhookReconciler != nil { return webhookReconciler, nil } + if !config.IsACMResourceReady() { + return nil, nil + } + log.Info("start webhook controller") + webhookReconciler = &WebhookReconciler{ c: opts.Manager.GetClient(), } @@ -131,29 +137,33 @@ type WebhookVariables struct { // SetupWithManager sets up the controller with the Manager. func (r *WebhookReconciler) SetupWithManager(mgr ctrl.Manager) error { - c, err := controller.New("webhook-controller", mgr, controller.Options{ - Reconciler: r, - }) - if err != nil { - return err - } - return c.Watch(source.Kind(mgr.GetCache(), &globalhubv1alpha4.MulticlusterGlobalHub{}, - &handler.TypedEnqueueRequestForObject[*globalhubv1alpha4.MulticlusterGlobalHub]{}, - predicate.TypedFuncs[*globalhubv1alpha4.MulticlusterGlobalHub]{ - CreateFunc: func(e event.TypedCreateEvent[*globalhubv1alpha4.MulticlusterGlobalHub]) bool { - return true - }, - UpdateFunc: func(e event.TypedUpdateEvent[*globalhubv1alpha4.MulticlusterGlobalHub]) bool { - if e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration() { - return true - } - return !reflect.DeepEqual(e.ObjectOld.GetAnnotations(), e.ObjectNew.GetAnnotations()) - }, - DeleteFunc: func(e event.TypedDeleteEvent[*globalhubv1alpha4.MulticlusterGlobalHub]) bool { - return false - }, - }, - )) + return ctrl.NewControllerManagedBy(mgr).Named("webhook-controller"). + For(&v1alpha4.MulticlusterGlobalHub{}, + builder.WithPredicates(mghPred)). + Watches(&addonv1alpha1.AddOnDeploymentConfig{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&clusterv1beta2.ManagedClusterSetBinding{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&admissionregistrationv1.MutatingWebhookConfiguration{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Watches(&clusterv1beta1.Placement{}, + &handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)). + Complete(r) +} + +var mghPred = predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + return true + }, + UpdateFunc: func(e event.UpdateEvent) bool { + if e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration() { + return true + } + return !reflect.DeepEqual(e.ObjectOld.GetAnnotations(), e.ObjectNew.GetAnnotations()) + }, + DeleteFunc: func(e event.DeleteEvent) bool { + return true + }, } func (r *WebhookReconciler) pruneWebhookResources(ctx context.Context) error { diff --git a/test/integration/operator/controllers/grafana_test.go b/test/integration/operator/controllers/grafana_test.go index 6a70e8ea9..e14399af7 100644 --- a/test/integration/operator/controllers/grafana_test.go +++ b/test/integration/operator/controllers/grafana_test.go @@ -12,7 +12,6 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/rand" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/stolostron/multicluster-global-hub/operator/api/operator/v1alpha4" "github.com/stolostron/multicluster-global-hub/operator/pkg/config" @@ -57,13 +56,9 @@ var _ = Describe("grafana", Ordered, func() { grafanaReconciler := grafana.NewGrafanaReconciler(runtimeManager, kubeClient) - _, err := grafanaReconciler.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: mgh.Namespace, - Name: mgh.Name, - }, - }) + err := grafanaReconciler.SetupWithManager(runtimeManager) Expect(err).To(Succeed()) + // deployment Eventually(func() error { deployment := &appsv1.Deployment{} diff --git a/test/integration/operator/controllers/inventory_test.go b/test/integration/operator/controllers/inventory_test.go index f4e0c6760..77ed026a4 100644 --- a/test/integration/operator/controllers/inventory_test.go +++ b/test/integration/operator/controllers/inventory_test.go @@ -12,7 +12,6 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/rand" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/stolostron/multicluster-global-hub/operator/api/operator/v1alpha4" "github.com/stolostron/multicluster-global-hub/operator/pkg/config" @@ -62,12 +61,7 @@ var _ = Describe("inventory-api", Ordered, func() { It("should generate the inventory resources", func() { reconciler := inventory.NewInventoryReconciler(runtimeManager, kubeClient) - _, err := reconciler.Reconcile(ctx, reconcile.Request{ - NamespacedName: types.NamespacedName{ - Namespace: mgh.Namespace, - Name: mgh.Name, - }, - }) + err := reconciler.SetupWithManager(runtimeManager) Expect(err).To(Succeed()) // deployment diff --git a/test/integration/operator/controllers/storage_test.go b/test/integration/operator/controllers/storage_test.go index 84a1a7c2f..caf15cc86 100644 --- a/test/integration/operator/controllers/storage_test.go +++ b/test/integration/operator/controllers/storage_test.go @@ -68,7 +68,9 @@ var _ = Describe("storage", Ordered, func() { } Expect(runtimeClient.Create(ctx, storageSecret)).To(Succeed()) - storageReconciler := storage.NewStorageReconciler(runtimeManager, true) + storageReconciler := storage.NewStorageReconciler(runtimeManager, true, false) + Expect(err).To(Succeed()) + err = storageReconciler.SetupWithManager(runtimeManager) Expect(err).To(Succeed()) // the subscription @@ -123,7 +125,7 @@ var _ = Describe("storage", Ordered, func() { Expect(runtimeClient.Create(ctx, mgh)).To(Succeed()) Expect(runtimeClient.Get(ctx, client.ObjectKeyFromObject(mgh), mgh)).To(Succeed()) - storageReconciler := storage.NewStorageReconciler(runtimeManager, true) + storageReconciler := storage.NewStorageReconciler(runtimeManager, true, false) // blocking until get the connection go func() { @@ -192,7 +194,7 @@ var _ = Describe("storage", Ordered, func() { Expect(runtimeClient.Create(ctx, mgh)).To(Succeed()) Expect(runtimeClient.Get(ctx, client.ObjectKeyFromObject(mgh), mgh)).To(Succeed()) - storageReconciler := storage.NewStorageReconciler(runtimeManager, true) + storageReconciler := storage.NewStorageReconciler(runtimeManager, true, false) // blocking until get the connection go func() { diff --git a/test/script/util.sh b/test/script/util.sh index f45784ff6..005df1249 100755 --- a/test/script/util.sh +++ b/test/script/util.sh @@ -376,8 +376,9 @@ install_crds() { # cluster managers kubectl --context "$ctx" apply -f ${CURRENT_DIR}/../manifest/crd/0000_01_operator.open-cluster-management.io_clustermanagers.crd.yaml - # service monitor + # monitor kubectl --context "$1" apply -f "$CURRENT_DIR"/../manifest/crd/0000_04_monitoring.coreos.com_servicemonitors.crd.yaml + kubectl --context "$1" apply -f "$CURRENT_DIR"/../manifest/crd/0000_04_monitoring.coreos.com_prometheusrules.yaml # addons kubectl --context "$1" apply -f "$CURRENT_DIR"/../manifest/crd/0000_01_addon.open-cluster-management.io_managedclusteraddons.crd.yaml