Skip to content

Commit

Permalink
Merge pull request #658 from kubevirt-bot/cherry-pick-608-to-release-…
Browse files Browse the repository at this point in the history
…v0.18

[release-v0.18] fix(CNV-28185): add required labels
  • Loading branch information
kubevirt-bot authored Aug 16, 2023
2 parents 0c2e035 + 923be58 commit 80b85af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
30 changes: 20 additions & 10 deletions controllers/services_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion tests/service_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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")
Expand Down

0 comments on commit 80b85af

Please sign in to comment.