Frederigo
Frederigo

Reputation: 246

Helm, Is there a way to add kubernetes labels to values.yaml (without using template and _helpers.tpl)

So as in the title, I would like to add labels to my helms of my already running applications (sonarqube and jenkins from official helm charts). I don't have templates in them just values.yaml. I am afraid of adding templates, because as I said, application is already running and I just want to add few labels in metadata.

Upvotes: 6

Views: 20931

Answers (2)

rkosegi
rkosegi

Reputation: 14678

Both charts mentioned in comment under question has a way to define custom labels

https://github.com/jenkinsci/helm-charts/blob/main/charts/jenkins/templates/jenkins-master-deployment.yaml#L42

    {{- range $key, $val := .Values.master.podLabels }}
    {{ $key }}: {{ $val | quote }}
    {{- end}}

https://github.com/Oteemo/charts/blob/master/charts/sonarqube/templates/deployment.yaml#L31

{{- with .Values.podLabels }}
{{ toYaml . | indent 8 }}
{{- end }}

So you need something like this in values.yaml

# Jenkins
master:
  podLabels:
    label1Name: label1Value

# Sonar
podLabels:
  label1Name: label1Value

Upvotes: 12

Mark
Mark

Reputation: 4067

As described by @rkosegi this solution uses best practices while working with k8s configurations.

Please keep in mind, that users always should store configuration files (Configuration Best Practices). It allows us to quickly change/roll back any configuration in an easy way, using:

    kubectl apply -f ...
    kubectl replace ...

Another solutions:


a) adding or changing existing labels:

#patch-file.yaml
spec:
  template:
    metadata:
      labels:
        test: label
kubectl patch deployment <deployment-name>  --patch "$(cat patch-file.yaml)"

b) using a json patch approach, you can:

  • add one new label "NewTest" with value "TestValue":
kubectl patch deployment <deployment-name>  --type='json' -p='[{"op": "add", "path": "/spec/template/metadata/labels/NewTest", "value":"TestValue"}]'
  • replace label "NewTest" with value "OldTest":
kubectl patch deployment <deployment-name>  --type='json' -p='[{"op": "replace", "path": "/spec/template/metadata/labels/NewTest", "value": "OldTest"}]'
  • remove label "NewTest":
kubectl patch deployment <deployment-name>  --type='json' -p='[{"op": "remove", "path": "/spec/template/metadata/labels/NewTest"}]'        

Additional informations:

Upvotes: 1

Related Questions