Skip to content

Commit

Permalink
Merge pull request #787 from codingben/template-validator-labels
Browse files Browse the repository at this point in the history
fix: add required labels to pods
  • Loading branch information
kubevirt-bot committed Jan 11, 2024
2 parents cc3f8d5 + c13b215 commit e40cbca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
21 changes: 12 additions & 9 deletions internal/common/labels.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package common

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
ssp "kubevirt.io/ssp-operator/api/v1beta2"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand Down Expand Up @@ -36,18 +36,13 @@ const (
// Name will translate into the AppKubernetesNameLabel
// Component will translate into the AppKubernetesComponentLabel
// Instance wide labels will be taken from the request if available
func AddAppLabels(requestInstance *ssp.SSP, name string, component AppComponent, obj client.Object) client.Object {
func AddAppLabels(requestInstance *ssp.SSP, name string, component AppComponent, obj metav1.Object) metav1.Object {
labels := getOrCreateLabels(obj)
addInstanceLabels(requestInstance, labels)

labels[AppKubernetesNameLabel] = name
labels[AppKubernetesComponentLabel] = component.String()
labels[AppKubernetesManagedByLabel] = AppKubernetesManagedByValue

addCommonLabels(labels, requestInstance, name, component)
return obj
}

func getOrCreateLabels(obj client.Object) map[string]string {
func getOrCreateLabels(obj metav1.Object) map[string]string {
labels := obj.GetLabels()
if labels == nil {
labels = map[string]string{}
Expand All @@ -56,6 +51,14 @@ func getOrCreateLabels(obj client.Object) map[string]string {
return labels
}

func addCommonLabels(labels map[string]string, requestInstance *ssp.SSP, name string, component AppComponent) {
addInstanceLabels(requestInstance, labels)

labels[AppKubernetesNameLabel] = name
labels[AppKubernetesComponentLabel] = component.String()
labels[AppKubernetesManagedByLabel] = AppKubernetesManagedByValue
}

func addInstanceLabels(requestInstance *ssp.SSP, to map[string]string) {
if requestInstance.Labels == nil {
return
Expand Down
1 change: 1 addition & 0 deletions internal/operands/template-validator/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func reconcileDeployment(request *common.Request) (common.ReconcileResult, error
}

deployment := newDeployment(request.Namespace, numberOfReplicas, image, sspTLSOptions)
common.AddAppLabels(request.Instance, operandName, operandComponent, &deployment.Spec.Template.ObjectMeta)
injectPlacementMetadata(&deployment.Spec.Template.Spec, validatorSpec)
return common.CreateOrUpdate(request).
NamespacedResource(deployment).
Expand Down
1 change: 1 addition & 0 deletions internal/operands/vm-console-proxy/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func reconcileDeployment(deployment apps.Deployment) common.ReconcileFunc {
return func(request *common.Request) (common.ReconcileResult, error) {
deployment.Namespace = request.Instance.Namespace
deployment.Spec.Template.Spec.Containers[0].Image = getVmConsoleProxyImage()
common.AddAppLabels(request.Instance, operandName, operandComponent, &deployment.Spec.Template.ObjectMeta)
return common.CreateOrUpdate(request).
NamespacedResource(&deployment).
WithAppLabels(operandName, operandComponent).
Expand Down

0 comments on commit e40cbca

Please sign in to comment.