Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix watch resources in each controller #1263

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 6 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,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
}
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,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(),
Expand Down
32 changes: 15 additions & 17 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 @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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{},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add more permissions for this controller

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

&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 @@ -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())
Expand Down
8 changes: 7 additions & 1 deletion operator/pkg/controllers/inventory/inventory_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
}

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,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 {
Expand Down
33 changes: 29 additions & 4 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 All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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{},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add more permissions for this controller

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

&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{
Expand Down
Loading
Loading