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

10/14/2020

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.

-- Frederigo
helmfile
kubernetes
kubernetes-helm

2 Answers

10/15/2020

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:

-- Mark
Source: StackOverflow

10/14/2020

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 is values.yaml

# Jenkins
master:
  podLabels:
    label1Name: label1Value

# Sonar
podLabels:
  label1Name: label1Value
-- rkosegi
Source: StackOverflow