diff --git a/cmd/crd-replicator/main.go b/cmd/crd-replicator/main.go index 65c8e9b3e1..813902c7d8 100644 --- a/cmd/crd-replicator/main.go +++ b/cmd/crd-replicator/main.go @@ -95,13 +95,13 @@ func main() { clusterID, namespaceManager), } if err = d.SetupWithManager(mgr); err != nil { - klog.Error(err, "unable to setup the crdreplicator-operator") + klog.Error(err, "unable to setup the crdReplicator controller") os.Exit(1) } - klog.Info("Starting crdreplicator-operator") + klog.Info("Starting crdReplicator manager") if err := mgr.Start(ctx); err != nil { - klog.Error(err, "problem running manager") + klog.Error(err, "unable to start the crdReplicator manager") os.Exit(1) } } diff --git a/internal/crdReplicator/crdReplicator-operator.go b/internal/crdReplicator/crdReplicator-operator.go index 71de735882..720a785ce7 100644 --- a/internal/crdReplicator/crdReplicator-operator.go +++ b/internal/crdReplicator/crdReplicator-operator.go @@ -43,8 +43,7 @@ import ( ) const ( - operatorName = "crdReplicator-operator" - finalizer = "crdreplicator.liqo.io/operator" + finalizer = "crdreplicator.liqo.io/operator" ) // Controller reconciles identity Secrets to start/stop the reflection of registered resources to remote clusters. @@ -166,10 +165,9 @@ func (c *Controller) SetupWithManager(mgr ctrl.Manager) error { return err } - return ctrl.NewControllerManagedBy(mgr). - Named(operatorName). - WithEventFilter(resourceToBeProccesedPredicate). + return ctrl.NewControllerManagedBy(mgr).Named(consts.CtrlSecretsCRDReplicator). For(&corev1.Secret{}, builder.WithPredicates(secretsFilter)). + WithEventFilter(resourceToBeProccesedPredicate). Complete(c) } diff --git a/pkg/consts/controllers.go b/pkg/consts/controllers.go new file mode 100644 index 0000000000..e5254aec50 --- /dev/null +++ b/pkg/consts/controllers.go @@ -0,0 +1,42 @@ +// Copyright 2019-2024 The Liqo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package consts + +// Constants used to name and identify controllers. +// Controller-runtime requires that each controller has a unique name within the container. +// This name is used, for example, to identify a controller univocally in the logs +// and must be a prometheus compatible name (underscores and alphanumeric characters only). +// As a convention to avoid conflicts, we use the name of the reconciled resource (lowercase version of their kind), +// and, if already used, we add a recognizable identifier, separated by the underscore character. +// To catch duplicated names, we name the constant as its value (in CamelCase and stripping the separator character), +// with the prefix "Ctrl". +const ( + // Core. + CtrlSecretsCRDReplicator = "secrets_crdreplicator" + + // Networking. + CtrlInternalFabricsFabric = "internalfabrics_fabric" + CtrlInternalFabricsCM = "internalfabrics_cm" + + // Authentication. + CtrlResourceSlicesLocal = "resourceslices_local" + CtrlResourceSlicesRemote = "resourceslices_remote" + + // Offloading. + CtrlShadowPods = "shadowpods" + + // Cross modules. + CtrlResourceSlicesQuotaCreator = "resourceslices_quotacreator" +) diff --git a/pkg/fabric/internalfabric_controller.go b/pkg/fabric/internalfabric_controller.go index 575c6822ca..b3e43b7115 100644 --- a/pkg/fabric/internalfabric_controller.go +++ b/pkg/fabric/internalfabric_controller.go @@ -28,6 +28,7 @@ import ( ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1" + "github.com/liqotech/liqo/pkg/consts" "github.com/liqotech/liqo/pkg/utils/network/geneve" ) @@ -126,7 +127,7 @@ func (r *InternalFabricReconciler) Reconcile(ctx context.Context, req ctrl.Reque // SetupWithManager register the InternalFabricReconciler to the manager. func (r *InternalFabricReconciler) SetupWithManager(mgr ctrl.Manager) error { - return ctrl.NewControllerManagedBy(mgr). + return ctrl.NewControllerManagedBy(mgr).Named(consts.CtrlInternalFabricsFabric). For(&networkingv1beta1.InternalFabric{}). Complete(r) } diff --git a/pkg/liqo-controller-manager/authentication/localresourceslice-controller/localresourceslice_controller.go b/pkg/liqo-controller-manager/authentication/localresourceslice-controller/localresourceslice_controller.go index be80e3547f..3f9a0d9f54 100644 --- a/pkg/liqo-controller-manager/authentication/localresourceslice-controller/localresourceslice_controller.go +++ b/pkg/liqo-controller-manager/authentication/localresourceslice-controller/localresourceslice_controller.go @@ -36,9 +36,6 @@ import ( tenantnamespace "github.com/liqotech/liqo/pkg/tenantNamespace" ) -// ControllerName is the name of the controller. -const ControllerName = "localResourceSlice" - // NewLocalResourceSliceReconciler returns a new LocalResourceSliceReconciler. func NewLocalResourceSliceReconciler(cl client.Client, s *runtime.Scheme, recorder record.EventRecorder, liqoNamespace string, @@ -120,7 +117,7 @@ func (r *LocalResourceSliceReconciler) Reconcile(ctx context.Context, req ctrl.R return ctrl.Result{}, err } - if resourceSlice.Spec.CSR == nil || len(resourceSlice.Spec.CSR) == 0 { + if len(resourceSlice.Spec.CSR) == 0 { // Generate a CSR for the remote cluster. CSR, err := authentication.GenerateCSRForResourceSlice(privateKey, &resourceSlice) if err != nil { @@ -153,7 +150,7 @@ func (r *LocalResourceSliceReconciler) SetupWithManager(mgr ctrl.Manager) error return err } - return ctrl.NewControllerManagedBy(mgr).Named(ControllerName). + return ctrl.NewControllerManagedBy(mgr).Named(consts.CtrlResourceSlicesLocal). For(&authv1beta1.ResourceSlice{}, builder.WithPredicates(localResSliceFilter)). Complete(r) } diff --git a/pkg/liqo-controller-manager/authentication/remoteresourceslice-controller/remoteresourceslice_controller.go b/pkg/liqo-controller-manager/authentication/remoteresourceslice-controller/remoteresourceslice_controller.go index 39da5f318e..cfe4cf4f08 100644 --- a/pkg/liqo-controller-manager/authentication/remoteresourceslice-controller/remoteresourceslice_controller.go +++ b/pkg/liqo-controller-manager/authentication/remoteresourceslice-controller/remoteresourceslice_controller.go @@ -36,15 +36,13 @@ import ( authv1beta1 "github.com/liqotech/liqo/apis/authentication/v1beta1" "github.com/liqotech/liqo/internal/crdReplicator/reflection" + "github.com/liqotech/liqo/pkg/consts" identitymanager "github.com/liqotech/liqo/pkg/identityManager" "github.com/liqotech/liqo/pkg/liqo-controller-manager/authentication" "github.com/liqotech/liqo/pkg/utils/getters" liqolabels "github.com/liqotech/liqo/pkg/utils/labels" ) -// ControllerName is the name of the controller. -const ControllerName = "remoteResourceSlice" - // NewRemoteResourceSliceReconciler returns a new RemoteResourceSliceReconciler. func NewRemoteResourceSliceReconciler(cl client.Client, s *runtime.Scheme, config *rest.Config, recorder record.EventRecorder, @@ -257,7 +255,7 @@ func (r *RemoteResourceSliceReconciler) SetupWithManager(mgr ctrl.Manager) error return err } - return ctrl.NewControllerManagedBy(mgr).Named(ControllerName). + return ctrl.NewControllerManagedBy(mgr).Named(consts.CtrlResourceSlicesRemote). For(&authv1beta1.ResourceSlice{}, builder.WithPredicates(predicate.And(remoteResSliceFilter, withCSR()))). Watches(&authv1beta1.Tenant{}, handler.EnqueueRequestsFromMapFunc(r.resourceSlicesEnquer())). Complete(r) @@ -300,7 +298,7 @@ func withCSR() predicate.Funcs { if !ok { return false } - return rs.Spec.CSR != nil && len(rs.Spec.CSR) > 0 + return len(rs.Spec.CSR) > 0 }) } diff --git a/pkg/liqo-controller-manager/networking/internal-network/internalfabric-controller/internalfabric_controller.go b/pkg/liqo-controller-manager/networking/internal-network/internalfabric-controller/internalfabric_controller.go index fbf87dd000..a80246d3dd 100644 --- a/pkg/liqo-controller-manager/networking/internal-network/internalfabric-controller/internalfabric_controller.go +++ b/pkg/liqo-controller-manager/networking/internal-network/internalfabric-controller/internalfabric_controller.go @@ -121,10 +121,10 @@ func (r *InternalFabricReconciler) SetupWithManager(mgr ctrl.Manager) error { }, ) - return ctrl.NewControllerManagedBy(mgr). + return ctrl.NewControllerManagedBy(mgr).Named(consts.CtrlInternalFabricsCM). + For(&networkingv1beta1.InternalFabric{}). Watches(&networkingv1beta1.InternalNode{}, internalNodeEnqueuer). Owns(&networkingv1beta1.RouteConfiguration{}). Owns(&networkingv1beta1.GeneveTunnel{}). - For(&networkingv1beta1.InternalFabric{}). Complete(r) } diff --git a/pkg/liqo-controller-manager/offloading/shadowpod-controller/shadowpod_controller.go b/pkg/liqo-controller-manager/offloading/shadowpod-controller/shadowpod_controller.go index 6ba8427e62..16f1d788a2 100644 --- a/pkg/liqo-controller-manager/offloading/shadowpod-controller/shadowpod_controller.go +++ b/pkg/liqo-controller-manager/offloading/shadowpod-controller/shadowpod_controller.go @@ -154,7 +154,7 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, workers int) error { UpdateFunc: func(_ event.UpdateEvent) bool { return true }, GenericFunc: func(_ event.GenericEvent) bool { return false }, } - return ctrl.NewControllerManagedBy(mgr). + return ctrl.NewControllerManagedBy(mgr).Named(consts.CtrlShadowPods). For(&offloadingv1beta1.ShadowPod{}). Owns(&corev1.Pod{}, builder.WithPredicates(reconciledPredicates)). WithOptions(controller.Options{MaxConcurrentReconciles: workers}). diff --git a/pkg/liqo-controller-manager/quotacreator-controller/quotacreator_controller.go b/pkg/liqo-controller-manager/quotacreator-controller/quotacreator_controller.go index a841604ab4..bcc0298b7c 100644 --- a/pkg/liqo-controller-manager/quotacreator-controller/quotacreator_controller.go +++ b/pkg/liqo-controller-manager/quotacreator-controller/quotacreator_controller.go @@ -37,11 +37,6 @@ import ( "github.com/liqotech/liqo/pkg/liqo-controller-manager/authentication" ) -const ( - // ControllerName is the name of the controller. - ControllerName = "quotaCreator" -) - // QuotaCreatorReconciler manage Quota lifecycle. type QuotaCreatorReconciler struct { client.Client @@ -141,9 +136,8 @@ func (r *QuotaCreatorReconciler) SetupWithManager(mgr ctrl.Manager) error { klog.Error(err) return err } - return ctrl.NewControllerManagedBy(mgr).Named(ControllerName). - For(&authv1beta1.ResourceSlice{}, - builder.WithPredicates(remoteResSliceFilter)). + return ctrl.NewControllerManagedBy(mgr).Named(consts.CtrlResourceSlicesQuotaCreator). + For(&authv1beta1.ResourceSlice{}, builder.WithPredicates(remoteResSliceFilter)). Owns(&offloadingv1beta1.Quota{}). Complete(r) }