Skip to content

Commit

Permalink
Merge pull request #5799 from chaosi-zju/ctrl-name
Browse files Browse the repository at this point in the history
Ensure controller names are unique when emitting metrics
  • Loading branch information
karmada-bot authored Nov 11, 2024
2 parents c694088 + b605d9d commit ed3f48c
Show file tree
Hide file tree
Showing 32 changed files with 82 additions and 41 deletions.
3 changes: 2 additions & 1 deletion operator/pkg/controller/karmada/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
)

const (
// ControllerName is the controller name that will be used when reporting events.
// ControllerName is the controller name that will be used when reporting events and metrics.
ControllerName = "karmada-operator-controller"

// ControllerFinalizerName is the name of the karmada controller finalizer
Expand Down Expand Up @@ -185,6 +185,7 @@ func (ctrl *Controller) ensureKarmada(ctx context.Context, karmada *operatorv1al
// SetupWithManager creates a controller and register to controller manager.
func (ctrl *Controller) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).
Named(ControllerName).
For(&operatorv1alpha1.Karmada{},
builder.WithPredicates(predicate.Funcs{
UpdateFunc: ctrl.onKarmadaUpdate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/helper"
)

// CRBApplicationFailoverControllerName is the controller name that will be used when reporting events.
// CRBApplicationFailoverControllerName is the controller name that will be used when reporting events and metrics.
const CRBApplicationFailoverControllerName = "cluster-resource-binding-application-failover-controller"

// CRBApplicationFailoverController is to sync ClusterResourceBinding's application failover behavior.
Expand Down Expand Up @@ -230,6 +230,7 @@ func (c *CRBApplicationFailoverController) SetupWithManager(mgr controllerruntim
}

return controllerruntime.NewControllerManagedBy(mgr).
Named(CRBApplicationFailoverControllerName).
For(&workv1alpha2.ClusterResourceBinding{}, builder.WithPredicates(clusterResourceBindingPredicateFn)).
WithOptions(controller.Options{RateLimiter: ratelimiterflag.DefaultControllerRateLimiter(c.RateLimiterOptions)}).
Complete(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/helper"
)

// RBApplicationFailoverControllerName is the controller name that will be used when reporting events.
// RBApplicationFailoverControllerName is the controller name that will be used when reporting events and metrics.
const RBApplicationFailoverControllerName = "resource-binding-application-failover-controller"

// RBApplicationFailoverController is to sync ResourceBinding's application failover behavior.
Expand Down Expand Up @@ -232,6 +232,7 @@ func (c *RBApplicationFailoverController) SetupWithManager(mgr controllerruntime
}

return controllerruntime.NewControllerManagedBy(mgr).
Named(RBApplicationFailoverControllerName).
For(&workv1alpha2.ResourceBinding{}, builder.WithPredicates(resourceBindingPredicateFn)).
WithOptions(controller.Options{RateLimiter: ratelimiterflag.DefaultControllerRateLimiter(c.RateLimiterOptions)}).
Complete(c)
Expand Down
6 changes: 4 additions & 2 deletions pkg/controllers/binding/binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/overridemanager"
)

// ControllerName is the controller name that will be used when reporting events.
// ControllerName is the controller name that will be used when reporting events and metrics.
const ControllerName = "binding-controller"

// ResourceBindingController is to sync ResourceBinding.
Expand Down Expand Up @@ -165,7 +165,9 @@ func (c *ResourceBindingController) removeOrphanWorks(ctx context.Context, bindi

// SetupWithManager creates a controller and register to controller manager.
func (c *ResourceBindingController) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha2.ResourceBinding{}).
return controllerruntime.NewControllerManagedBy(mgr).
Named(ControllerName).
For(&workv1alpha2.ResourceBinding{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
Watches(&policyv1alpha1.OverridePolicy{}, handler.EnqueueRequestsFromMapFunc(c.newOverridePolicyFunc())).
Watches(&policyv1alpha1.ClusterOverridePolicy{}, handler.EnqueueRequestsFromMapFunc(c.newOverridePolicyFunc())).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/overridemanager"
)

// ClusterResourceBindingControllerName is the controller name that will be used when reporting events.
// ClusterResourceBindingControllerName is the controller name that will be used when reporting events and metrics.
const ClusterResourceBindingControllerName = "cluster-resource-binding-controller"

// ClusterResourceBindingController is to sync ClusterResourceBinding.
Expand Down Expand Up @@ -161,7 +161,9 @@ func (c *ClusterResourceBindingController) removeOrphanWorks(ctx context.Context

// SetupWithManager creates a controller and register to controller manager.
func (c *ClusterResourceBindingController) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha2.ClusterResourceBinding{}).
return controllerruntime.NewControllerManagedBy(mgr).
Named(ClusterResourceBindingControllerName).
For(&workv1alpha2.ClusterResourceBinding{}).
Watches(&policyv1alpha1.ClusterOverridePolicy{}, handler.EnqueueRequestsFromMapFunc(c.newOverridePolicyFunc())).
WithEventFilter(predicate.GenerationChangedPredicate{}).
WithOptions(controller.Options{RateLimiter: ratelimiterflag.DefaultControllerRateLimiter(c.RateLimiterOptions)}).
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/certificate/cert_rotation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import (
)

const (
// CertRotationControllerName is the controller name that will be used when reporting events.
// CertRotationControllerName is the controller name that will be used when reporting events and metrics.
CertRotationControllerName = "cert-rotation-controller"

// SignerName defines the signer name for csr, 'kubernetes.io/kube-apiserver-client-kubelet' can sign the csr automatically
Expand Down Expand Up @@ -129,6 +129,7 @@ func (c *CertRotationController) Reconcile(ctx context.Context, req controllerru
// SetupWithManager creates a controller and register to controller manager.
func (c *CertRotationController) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).
Named(CertRotationControllerName).
For(&clusterv1alpha1.Cluster{}, builder.WithPredicates(c.PredicateFunc)).
WithEventFilter(predicate.GenerationChangedPredicate{}).
WithOptions(controller.Options{
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import (
)

const (
// ControllerName is the controller name that will be used when reporting events.
// ControllerName is the controller name that will be used when reporting events and metrics.
ControllerName = "cluster-controller"
// MonitorRetrySleepTime is the amount of time the cluster controller that should
// sleep between retrying cluster health updates.
Expand Down Expand Up @@ -215,7 +215,7 @@ func (c *Controller) Start(ctx context.Context) error {
func (c *Controller) SetupWithManager(mgr controllerruntime.Manager) error {
c.clusterHealthMap = newClusterHealthMap()
return utilerrors.NewAggregate([]error{
controllerruntime.NewControllerManagedBy(mgr).For(&clusterv1alpha1.Cluster{}).Complete(c),
controllerruntime.NewControllerManagedBy(mgr).Named(ControllerName).For(&clusterv1alpha1.Cluster{}).Complete(c),
mgr.Add(c),
})
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/cluster/taint_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/helper"
)

// TaintManagerName is the controller name that will be used for taint management.
// TaintManagerName is the controller name that will be used when reporting events and metrics.
const TaintManagerName = "taint-manager"

// NoExecuteTaintManager listens to Taint/Toleration changes and is responsible for removing objects
Expand Down Expand Up @@ -291,7 +291,7 @@ func (tc *NoExecuteTaintManager) needEviction(clusterName string, annotations ma
// SetupWithManager creates a controller and register to controller manager.
func (tc *NoExecuteTaintManager) SetupWithManager(mgr controllerruntime.Manager) error {
return utilerrors.NewAggregate([]error{
controllerruntime.NewControllerManagedBy(mgr).For(&clusterv1alpha1.Cluster{}).Complete(tc),
controllerruntime.NewControllerManagedBy(mgr).Named(TaintManagerName).For(&clusterv1alpha1.Cluster{}).Complete(tc),
mgr.Add(tc),
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
)

const (
// ControllerName is the controller name that will be used when reporting events.
// ControllerName is the controller name that will be used when reporting events and metrics.
ControllerName = "cronfederatedhpa-controller"
)

Expand Down Expand Up @@ -118,6 +118,7 @@ func (c *CronFHPAController) Reconcile(ctx context.Context, req controllerruntim
func (c *CronFHPAController) SetupWithManager(mgr controllerruntime.Manager) error {
c.CronHandler = NewCronHandler(mgr.GetClient(), mgr.GetEventRecorderFor(ControllerName))
return controllerruntime.NewControllerManagedBy(mgr).
Named(ControllerName).
For(&autoscalingv1alpha1.CronFederatedHPA{}).
WithOptions(controller.Options{RateLimiter: ratelimiterflag.DefaultControllerRateLimiter(c.RateLimiterOptions)}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
)

const (
// ControllerName is the controller name that will be used when reporting events.
// ControllerName is the controller name that will be used when reporting events and metrics.
ControllerName = "deployment-replicas-syncer"

waitDeploymentStatusInterval = 1 * time.Second
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/execution/execution_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import (
)

const (
// ControllerName is the controller name that will be used when reporting events.
// ControllerName is the controller name that will be used when reporting events and metrics.
ControllerName = "execution-controller"
// WorkSuspendDispatchingConditionMessage is the condition and event message when dispatching is suspended.
WorkSuspendDispatchingConditionMessage = "Work dispatching is in a suspended state."
Expand Down Expand Up @@ -133,6 +133,7 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
// SetupWithManager creates a controller and register to controller manager.
func (c *Controller) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).
Named(ControllerName).
For(&workv1alpha1.Work{}, builder.WithPredicates(c.PredicateFunc)).
WithEventFilter(predicate.GenerationChangedPredicate{}).
WithOptions(controller.Options{
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/federatedhpa/federatedhpa_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import (
// FederatedHPA-controller is borrowed from the HPA controller of Kubernetes.
// The referenced code has been marked in the comment.

// ControllerName is the controller name that will be used when reporting events.
// ControllerName is the controller name that will be used when reporting events and metrics.
const ControllerName = "federatedHPA-controller"

var (
Expand Down Expand Up @@ -128,6 +128,7 @@ func (c *FHPAController) SetupWithManager(mgr controllerruntime.Manager) error {
c.hpaSelectors = selectors.NewBiMultimap()
c.monitor = monitor.New()
return controllerruntime.NewControllerManagedBy(mgr).
Named(ControllerName).
For(&autoscalingv1alpha1.FederatedHPA{}).
WithOptions(controller.Options{RateLimiter: ratelimiterflag.DefaultControllerRateLimiter(c.RateLimiterOptions)}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import (
)

const (
// StatusControllerName is the controller name that will be used when reporting events.
// StatusControllerName is the controller name that will be used when reporting events and metrics.
StatusControllerName = "federated-resource-quota-status-controller"
)

Expand Down Expand Up @@ -129,6 +129,7 @@ func (c *StatusController) SetupWithManager(mgr controllerruntime.Manager) error
},
})
return controllerruntime.NewControllerManagedBy(mgr).
Named(StatusControllerName).
For(&policyv1alpha1.FederatedResourceQuota{}).
Watches(&workv1alpha1.Work{}, handler.EnqueueRequestsFromMapFunc(fn), workPredicate).
Complete(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
)

const (
// SyncControllerName is the controller name that will be used when reporting events.
// SyncControllerName is the controller name that will be used when reporting events and metrics.
SyncControllerName = "federated-resource-quota-sync-controller"
)

Expand Down Expand Up @@ -129,6 +129,7 @@ func (c *SyncController) SetupWithManager(mgr controllerruntime.Manager) error {
})

return controllerruntime.NewControllerManagedBy(mgr).
Named(SyncControllerName).
For(&policyv1alpha1.FederatedResourceQuota{}).
Watches(&clusterv1alpha1.Cluster{}, handler.EnqueueRequestsFromMapFunc(fn), clusterPredicate).
Complete(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/helper"
)

// CRBGracefulEvictionControllerName is the controller name that will be used when reporting events.
// CRBGracefulEvictionControllerName is the controller name that will be used when reporting events and metrics.
const CRBGracefulEvictionControllerName = "cluster-resource-binding-graceful-eviction-controller"

// CRBGracefulEvictionController is to sync ClusterResourceBinding.spec.gracefulEvictionTasks.
Expand Down Expand Up @@ -125,6 +125,7 @@ func (c *CRBGracefulEvictionController) SetupWithManager(mgr controllerruntime.M
}

return controllerruntime.NewControllerManagedBy(mgr).
Named(CRBGracefulEvictionControllerName).
For(&workv1alpha2.ClusterResourceBinding{}, builder.WithPredicates(clusterResourceBindingPredicateFn)).
WithOptions(controller.Options{RateLimiter: ratelimiterflag.DefaultControllerRateLimiter(c.RateLimiterOptions)}).
Complete(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/helper"
)

// RBGracefulEvictionControllerName is the controller name that will be used when reporting events.
// RBGracefulEvictionControllerName is the controller name that will be used when reporting events and metrics.
const RBGracefulEvictionControllerName = "resource-binding-graceful-eviction-controller"

// RBGracefulEvictionController is to sync ResourceBinding.spec.gracefulEvictionTasks.
Expand Down Expand Up @@ -125,6 +125,7 @@ func (c *RBGracefulEvictionController) SetupWithManager(mgr controllerruntime.Ma
}

return controllerruntime.NewControllerManagedBy(mgr).
Named(RBGracefulEvictionControllerName).
For(&workv1alpha2.ResourceBinding{}, builder.WithPredicates(resourceBindingPredicateFn)).
WithOptions(controller.Options{RateLimiter: ratelimiterflag.DefaultControllerRateLimiter(c.RateLimiterOptions)}).
Complete(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

const (
// ControllerName is the controller name that will be used when reporting events.
// ControllerName is the controller name that will be used when reporting events and metrics.
ControllerName = "hpa-scale-target-marker"
// scaleTargetWorkerNum is the async Worker number
scaleTargetWorkerNum = 1
Expand Down
7 changes: 5 additions & 2 deletions pkg/controllers/mcs/endpointslice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/names"
)

// EndpointSliceControllerName is the controller name that will be used when reporting events.
// EndpointSliceControllerName is the controller name that will be used when reporting events and metrics.
const EndpointSliceControllerName = "endpointslice-controller"

// EndpointSliceController is to collect EndpointSlice which reported by member cluster from executionNamespace to serviceexport namespace.
Expand Down Expand Up @@ -115,7 +115,10 @@ func (c *EndpointSliceController) SetupWithManager(mgr controllerruntime.Manager
return false
},
}
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha1.Work{}, builder.WithPredicates(serviceImportPredicateFun)).Complete(c)
return controllerruntime.NewControllerManagedBy(mgr).
Named(EndpointSliceControllerName).
For(&workv1alpha1.Work{}, builder.WithPredicates(serviceImportPredicateFun)).
Complete(c)
}

func (c *EndpointSliceController) collectEndpointSliceFromWork(ctx context.Context, work *workv1alpha1.Work) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/mcs/service_export_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/names"
)

// ServiceExportControllerName is the controller name that will be used when reporting events.
// ServiceExportControllerName is the controller name that will be used when reporting events and metrics.
const ServiceExportControllerName = "service-export-controller"

// ServiceExportController is to sync ServiceExport and report EndpointSlices of exported service to control-plane.
Expand Down Expand Up @@ -131,7 +131,7 @@ func (c *ServiceExportController) Reconcile(ctx context.Context, req controllerr

// SetupWithManager creates a controller and register to controller manager.
func (c *ServiceExportController) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha1.Work{}, builder.WithPredicates(c.PredicateFunc)).Complete(c)
return controllerruntime.NewControllerManagedBy(mgr).Named(ServiceExportControllerName).For(&workv1alpha1.Work{}, builder.WithPredicates(c.PredicateFunc)).Complete(c)
}

// RunWorkQueue initializes worker and run it, worker will process resource asynchronously.
Expand Down
7 changes: 5 additions & 2 deletions pkg/controllers/mcs/service_import_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/names"
)

// ServiceImportControllerName is the controller name that will be used when reporting events.
// ServiceImportControllerName is the controller name that will be used when reporting events and metrics.
const ServiceImportControllerName = "service-import-controller"

// ServiceImportController is to sync derived service from ServiceImport.
Expand Down Expand Up @@ -71,7 +71,10 @@ func (c *ServiceImportController) Reconcile(ctx context.Context, req controllerr

// SetupWithManager creates a controller and register to controller manager.
func (c *ServiceImportController) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).For(&mcsv1alpha1.ServiceImport{}).Complete(c)
return controllerruntime.NewControllerManagedBy(mgr).
Named(ServiceImportControllerName).
For(&mcsv1alpha1.ServiceImport{}).
Complete(c)
}

func (c *ServiceImportController) deleteDerivedService(ctx context.Context, svcImport types.NamespacedName) (controllerruntime.Result, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ var (
multiClusterServiceGVK = networkingv1alpha1.SchemeGroupVersion.WithKind("MultiClusterService")
)

// EndpointSliceCollectControllerName is the controller name that will be used when reporting events and metrics.
const EndpointSliceCollectControllerName = "endpointslice-collect-controller"

// Reconcile performs a full reconciliation for the object referred to by the Request.
func (c *EndpointSliceCollectController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
klog.V(4).Infof("Reconciling Work %s", req.NamespacedName.String())
Expand Down Expand Up @@ -115,6 +118,7 @@ func (c *EndpointSliceCollectController) Reconcile(ctx context.Context, req cont
// SetupWithManager creates a controller and register to controller manager.
func (c *EndpointSliceCollectController) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).
Named(EndpointSliceCollectControllerName).
For(&workv1alpha1.Work{}, builder.WithPredicates(c.PredicateFunc)).Complete(c)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (
"github.com/karmada-io/karmada/pkg/util/names"
)

// EndpointsliceDispatchControllerName is the controller name that will be used when reporting events.
// EndpointsliceDispatchControllerName is the controller name that will be used when reporting events and metrics.
const EndpointsliceDispatchControllerName = "endpointslice-dispatch-controller"

// EndpointsliceDispatchController will reconcile a MultiClusterService object
Expand Down Expand Up @@ -160,7 +160,9 @@ func (c *EndpointsliceDispatchController) SetupWithManager(mgr controllerruntime
return false
},
}
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha1.Work{}, builder.WithPredicates(workPredicateFun)).
return controllerruntime.NewControllerManagedBy(mgr).
Named(EndpointsliceDispatchControllerName).
For(&workv1alpha1.Work{}, builder.WithPredicates(workPredicateFun)).
Watches(&networkingv1alpha1.MultiClusterService{}, handler.EnqueueRequestsFromMapFunc(c.newMultiClusterServiceFunc())).
Watches(&clusterv1alpha1.Cluster{}, handler.EnqueueRequestsFromMapFunc(c.newClusterFunc())).
Complete(c)
Expand Down
Loading

0 comments on commit ed3f48c

Please sign in to comment.