Skip to content

Commit

Permalink
fix watch resources in each controller
Browse files Browse the repository at this point in the history
Signed-off-by: ldpliu <[email protected]>
  • Loading branch information
ldpliu committed Dec 4, 2024
1 parent e23ee1f commit 92599b4
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 44 deletions.
22 changes: 22 additions & 0 deletions operator/pkg/config/multiclusterglobalhub_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion operator/pkg/controllers/acm/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions operator/pkg/controllers/agent/addon_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion operator/pkg/controllers/agent/default_agent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions operator/pkg/controllers/agent/hosted_agent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ var (
)

func StartHostedAgentController(initOption config.ControllerOption) (config.ControllerInterface, error) {
log.Info("start hosted agent controller")

if hostedAgentController != nil {
return hostedAgentController, nil
}
if !config.GetImportClusterInHosted() {
return nil, nil
}

log.Info("start hosted agent controller")

if !ReadyToEnableAddonManager(initOption.MulticlusterGlobalHub) {
return nil, nil
}
Expand Down
3 changes: 2 additions & 1 deletion operator/pkg/controllers/backup/backup_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ func GetBackupController() *BackupReconciler {
}

func StartController(initOption config.ControllerOption) (config.ControllerInterface, error) {
log.Infof("start backup controller")
if backupController != nil {
return backupController, nil
}
log.Infof("start backup controller")

if !config.IsACMResourceReady() {
return nil, nil
}
Expand Down
24 changes: 22 additions & 2 deletions operator/pkg/controllers/grafana/grafana_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -125,10 +126,11 @@ func (r *GrafanaReconciler) IsResourceRemoved() bool {
}

func StartController(initOption config.ControllerOption) (config.ControllerInterface, error) {
log.Info("start grafana controller")
if grafanaController != nil {
return grafanaController, nil
}
log.Info("start grafana controller")

if !config.IsACMResourceReady() {
return nil, nil
}
Expand Down Expand Up @@ -156,7 +158,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(deplomentPred)).
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",
Expand Down Expand Up @@ -242,9 +254,17 @@ var secretPred = predicate.Funcs{
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())
},
}
Expand Down
10 changes: 9 additions & 1 deletion operator/pkg/controllers/inventory/inventory_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ func (r *InventoryReconciler) IsResourceRemoved() bool {
}

func StartController(initOption config.ControllerOption) (config.ControllerInterface, error) {
log.Info("start inventory controller")
if inventoryReconciler != nil {
return inventoryReconciler, nil
}

if !config.WithInventory(initOption.MulticlusterGlobalHub) {
return nil, nil
}
log.Info("start inventory controller")

if config.GetTransporterConn() == nil {
return nil, nil
}
Expand All @@ -100,6 +102,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)
}

Expand Down
3 changes: 2 additions & 1 deletion operator/pkg/controllers/managedhub/managedhub_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ func (r *ManagedHubController) IsResourceRemoved() bool {
}

func StartController(initOption config.ControllerOption) (config.ControllerInterface, error) {
log.Info("start managedhub controller")
if managedHubController != nil {
return managedHubController, nil
}
log.Info("start managedhub controller")

if !config.IsACMResourceReady() {
return nil, nil
}
Expand Down
23 changes: 21 additions & 2 deletions operator/pkg/controllers/manager/manager_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ 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"
"k8s.io/client-go/discovery"
"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"
Expand Down Expand Up @@ -85,11 +88,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
}
Expand Down Expand Up @@ -120,6 +123,22 @@ func (r *ManagerReconciler) SetupWithManager(mgr ctrl.Manager) error {
builder.WithPredicates(config.MGHPred)).
Watches(&appsv1.Deployment{},
&handler.EnqueueRequestForObject{}, builder.WithPredicates(deploymentPred)).
Watches(&v1alpha1.ClusterManagementAddOn{},
&handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)).
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)).
Complete(r)
}

Expand Down
27 changes: 25 additions & 2 deletions operator/pkg/controllers/storage/storage_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -85,11 +86,11 @@ 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)
err := storageReconciler.SetupWithManager(initOption.Manager)
Expand Down Expand Up @@ -120,6 +121,12 @@ func (r *StorageReconciler) SetupWithManager(mgr ctrl.Manager) error {
&handler.EnqueueRequestForObject{}, builder.WithPredicates(configmapPred)).
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)
}

Expand All @@ -143,9 +150,17 @@ var configmapPred = predicate.Funcs{
return WatchedConfigMap.Has(e.Object.GetName())
},
UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectNew.GetLabels()[constants.GlobalHubOwnerLabelKey] ==
constants.GHOperatorOwnerLabelVal {
return true
}
return WatchedConfigMap.Has(e.ObjectNew.GetName())
},
DeleteFunc: func(e event.DeleteEvent) bool {
if e.Object.GetLabels()[constants.GlobalHubOwnerLabelKey] ==
constants.GHOperatorOwnerLabelVal {
return true
}
return WatchedConfigMap.Has(e.Object.GetName())
},
}
Expand All @@ -155,9 +170,17 @@ var secretPred = predicate.Funcs{
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())
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ var kafkaPred = predicate.Funcs{
}

func StartKafkaController(ctx context.Context, mgr ctrl.Manager, transporter transport.Transporter) error {
log.Info("start kafka controller")
log.Debug("start kafka controller")
if startedKafkaController {
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion operator/pkg/controllers/transporter/transport_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 92599b4

Please sign in to comment.