-1

I am new to helm charts and I am building helm chart to deploy an app on kubernetes, as part of it I have created a deployment template as below,

{{- $outer := . -}}
{{- range $index, $service := .Values.myservices}}
{{- with $outer }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $service.name }}
  labels:
    {{- include "myhelm.labels" $ | nindent 4 }}
spec:
.
.
.
  {{- end }}
{{- end }}

Here I am using a template "myhelm.labels", which is defined in _helpers.tpl as below,

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

I like to include some more labels provided in the values.yaml as below

myservices:
  api:
    name: "com-api"
    labels:
      app: "com-api"
    selectorLabels:
      app: "com-cp"
    podAnnotations: {}
    container:
      image: "com-api"
      port: 24000
      name: "api"
    nodeSelector:
      app: "com-cp-api"
    affinity: {}
    tolerations: {}
  ui:
    name: "com-ui"
    labels:
      app: "com-ui"
    selectorLabels:
      app: "com-ui"
    podAnnotations: {}
    container:
      image: "com-ui"
      port: 23000
      name: "ui"
    nodeSelector:
      app: "com-cp-ui"
    affinity: {}
    tolerations: {}

Along with "myhelm.labels" (common labels) I also want to include service specific labels like $service.labels. Please help me, how can I do it ?

I am able to add specific labels one by one like,

  labels:
    {{- include "dlc-project-service-control-plane.labels" $ | nindent 4 }}
    app: {{ $service.labels.app }}

But, I am looking for a solution, if I have multiple labels under $service.labels in values.yaml and want to add all of them in a single statement in deployment template.

Please share the code snippet if you already know the solution, it helps.

Thanks

Pradeep
  • 350
  • 1
  • 3
  • 13
  • You should be able to add more lines to your Deployment's `labels:` block; the syntax is not at all rigid. What have you already tried? – David Maze Nov 23 '22 at 11:32
  • Hi @DavidMaze, I am able to add specific labels one by one like, labels: {{- include "dlc-project-service-control-plane.labels" $ | nindent 4 }} app: {{ $service.labels.app }} But, I am looking for a solution, if I have multiple labels under $service.labels in values.yaml and want to add all of them in a single statement in deployment template. Please share the code snippet if you already know he solution, it helps. Thanks – Pradeep Nov 28 '22 at 07:12

1 Answers1

1

I am able to achieve it using the toYaml, below is the code snippet,

{{- $outer := . -}}
{{- range $index, $service := .Values.myservices}}
{{- with $outer }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $service.name }}
  labels:
    {{- include "myhelm.labels" $ | nindent 4 }}
    {{- toYaml $service.labels | nindent 4 }}
spec:
.
.
.
  {{- end }}
{{- end }}

Pradeep
  • 350
  • 1
  • 3
  • 13