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(CNV-28185): add required labels #608

Merged
merged 1 commit into from
Aug 13, 2023
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
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