From d46e737cf111e69fb5946193095012d52db9aa3e Mon Sep 17 00:00:00 2001 From: Ben Oukhanov Date: Mon, 26 Jun 2023 16:59:12 +0300 Subject: [PATCH] fix(CNV-28185): add required labels Add required labels: - "app.kubernetes.io/managed-by" - "app.kubernetes.io/version" - "app.kubernetes.io/component" - "app.kubernetes.io/part-of" That are required by a specific resource: - ssp-operator-metrics Jira-Url: https://issues.redhat.com/browse/CNV-28185 Signed-off-by: Ben Oukhanov --- controllers/services_controller.go | 30 ++++++++++++++++++++---------- tests/service_controller_test.go | 13 ++++++++++++- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/controllers/services_controller.go b/controllers/services_controller.go index 2d84e1e1d..ba7553cda 100644 --- a/controllers/services_controller.go +++ b/controllers/services_controller.go @@ -21,21 +21,29 @@ import ( ) const ( - MetricsServiceName = "ssp-operator-metrics" - OperatorName = "ssp-operator" - serviceControllerName = "service-controller" + ServiceManagedByLabelValue = "ssp-operator-services" + MetricsServiceName = "ssp-operator-metrics" + OperatorName = "ssp-operator" + ServiceControllerName = "service-controller" ) -func ServiceObject(namespace string) *v1.Service { +func ServiceObject(namespace string, appKubernetesPartOfValue string) *v1.Service { policyCluster := v1.ServiceInternalTrafficPolicyCluster familyPolicy := v1.IPFamilyPolicySingleStack + labels := map[string]string{ + common.AppKubernetesManagedByLabel: ServiceManagedByLabelValue, + common.AppKubernetesVersionLabel: common.GetOperatorVersion(), + common.AppKubernetesComponentLabel: ServiceControllerName, + metrics.PrometheusLabelKey: metrics.PrometheusLabelValue, + } + if appKubernetesPartOfValue != "" { + labels[common.AppKubernetesPartOfLabel] = appKubernetesPartOfValue + } return &v1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: MetricsServiceName, Namespace: namespace, - Labels: map[string]string{ - metrics.PrometheusLabelKey: metrics.PrometheusLabelValue, - }, + Labels: labels, }, Spec: v1.ServiceSpec{ InternalTrafficPolicy: &policyCluster, @@ -67,7 +75,7 @@ func CreateServiceController(ctx context.Context, mgr ctrl.Manager) (*serviceRec } func (r *serviceReconciler) Name() string { - return serviceControllerName + return ServiceControllerName } func (r *serviceReconciler) Start(ctx context.Context, mgr ctrl.Manager) error { @@ -84,7 +92,8 @@ func (r *serviceReconciler) setServiceOwnerReference(service *v1.Service) error } func (r *serviceReconciler) createMetricsService(ctx context.Context) error { - service := ServiceObject(r.serviceNamespace) + appKubernetesPartOfValue := r.deployment.GetLabels()[common.AppKubernetesPartOfLabel] + service := ServiceObject(r.serviceNamespace, appKubernetesPartOfValue) err := r.setServiceOwnerReference(service) if err != nil { return fmt.Errorf("error setting owner reference: %w", err) @@ -144,7 +153,8 @@ func newServiceReconciler(ctx context.Context, mgr ctrl.Manager) (*serviceReconc func (r *serviceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, err error) { r.log.Info("Starting service reconciliation...", "request", req.String()) - service := ServiceObject(req.Namespace) + appKubernetesPartOfValue := r.deployment.GetLabels()[common.AppKubernetesPartOfLabel] + service := ServiceObject(req.Namespace, appKubernetesPartOfValue) var foundService v1.Service foundService.Name = service.Name foundService.Namespace = service.Namespace diff --git a/tests/service_controller_test.go b/tests/service_controller_test.go index d209924aa..5241c6ad0 100644 --- a/tests/service_controller_test.go +++ b/tests/service_controller_test.go @@ -14,12 +14,13 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "kubevirt.io/ssp-operator/controllers" + "kubevirt.io/ssp-operator/internal/common" "kubevirt.io/ssp-operator/internal/operands/metrics" "kubevirt.io/ssp-operator/tests/env" ) func getSspMetricsService() (*v1.Service, error) { - service := controllers.ServiceObject(strategy.GetSSPDeploymentNameSpace()) + service := controllers.ServiceObject(strategy.GetSSPDeploymentNameSpace(), "") err := apiClient.Get(ctx, client.ObjectKeyFromObject(service), service) return service, err } @@ -38,6 +39,16 @@ var _ = Describe("Service Controller", func() { Expect(serviceErr).ToNot(HaveOccurred(), "Failed to get ssp-operator-metrics service") }) + It("[test_id: TODO] Service ssp-operator-metrics should contain required labels", func() { + service, serviceErr := getSspMetricsService() + Expect(serviceErr).ToNot(HaveOccurred(), "Failed to get ssp-operator-metrics service") + + Expect(service.GetLabels()).To(HaveKeyWithValue(common.AppKubernetesManagedByLabel, controllers.ServiceManagedByLabelValue)) + Expect(service.GetLabels()).To(HaveKeyWithValue(common.AppKubernetesVersionLabel, common.GetOperatorVersion())) + Expect(service.GetLabels()).To(HaveKeyWithValue(common.AppKubernetesComponentLabel, controllers.ServiceControllerName)) + Expect(service.GetLabels()[common.AppKubernetesPartOfLabel]).To(BeEmpty()) + }) + It("[test_id: 8808] Should re-create ssp-operator-metrics service if deleted", func() { service, serviceErr := getSspMetricsService() Expect(serviceErr).ToNot(HaveOccurred(), "Failed to get ssp-operator-metrics service")