Helm Chart Labels and Selectors

1/15/2020

I create a helm chart with helm createcommand for Helm v3 and it creates for me standart template.Because of I am new to using Helm, I have some confusion to use selector and labels.

So how should use the labels between the deployment.yaml and services.yaml?

Use include-road-dashboard.labels command already do the same thing as below or I need to specify the labels separate like as below command. My question is same as well for {{ include "road-dashboard.selectorLabels" . }} command.

Updated Template

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "road-dashboard.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "road-dashboard.name" . }}
    helm.sh/chart: {{ include "road-dashboard.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}

Helm Standart Template

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "road-dashboard.fullname" . }}
  labels:
    {{- include "road-dashboard.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "road-dashboard.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "road-dashboard.selectorLabels" . | nindent 8 }}
    spec:

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ include "road-dashboard.fullname" . }}
  labels:
    {{- include "road-dashboard.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    {{- include "road-dashboard.selectorLabels" . | nindent 4 }}

-helpers.tpl

  {{/*
Common labels
*/}}
{{- define "road-dashboard.labels" -}}
helm.sh/chart: {{ include "road-dashboard.chart" . }}
{{ include "road-dashboard.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}

{{/*
Selector labels
*/}}
{{- define "road-dashboard.selectorLabels" -}}
app.kubernetes.io/name: {{ include "road-dashboard.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}
-- semural
kubernetes-helm

1 Answer

1/15/2020

You continue using the ones you created in the helpers.tpl and use the Deployment/service/any resource or you use the one you posted in the beginning.

just a comment in your helpers.tpl in the define "road-dashboard.labels" is missing the app.kubernetes.io/name and app.kubernetes.io/instance you should add there as well otherwise the selector will not work

you also can check this documentation for guidelines: https://github.com/helm/charts/blob/master/REVIEW_GUIDELINES.md#names-and-labels

-- cpanato
Source: StackOverflow