examples icon indicating copy to clipboard operation
examples copied to clipboard

Example of generating multiple resources from a single template file

Open oxr463 opened this issue 3 years ago • 2 comments

In the values.yaml file, we could specify several sites:

sites:
  - name: foo
    source: "foo.com"
  - name: bar
    source: "bar.com
  - name: baz
    source: "baz.com

Then the chart would generate three deployments and three services, one for each site.

Then we could add an ingress that includes each of the services:

spec:
  rules:
  - host: hello-world.com
    http:
      paths:
      - backend:
          service:
            name: foo
            port:
              name: http
        path: /foo
        pathType: Prefix
      - backend:
          service:
            name: bar
            port:
              name: http
        path: /bar
        pathType: Prefix
      - backend:
          service:
            name: baz
            port:
              name: http
        path: /baz
        pathType: Prefix

Then each site would be accessible via the following URLs:

  • hello-world.com/foo
  • hello-world.com/bar
  • hello-world.com/baz

oxr463 avatar May 08 '22 13:05 oxr463

I created a new chart:

helm create hello-world-multiple
cd hello-world-multiple
helm template hello-world .

Here is the modified template/deployment.yaml file:

{{- range $key, $value := .Values.repositories }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $value.name }}
  labels:
    {{- include "hello-world-multiple.labels" $ | nindent 4 }}
spec:
  {{- if not $.Values.autoscaling.enabled }}
  replicas: {{ $.Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "hello-world-multiple.selectorLabels" $ | nindent 6 }}
  template:
    metadata:
      {{- with $.Values.podAnnotations }}
      annotations:
        {{- toYaml $ | nindent 8 }}
      {{- end }}
      labels:
        {{- include "hello-world-multiple.selectorLabels" $ | nindent 8 }}
    spec:
      {{- with $.Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml $ | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "hello-world-multiple.serviceAccountName" $ }}
      securityContext:
        {{- toYaml $.Values.podSecurityContext | nindent 8 }}
      containers:
        - name: hello-world
          securityContext:
            {{- toYaml $.Values.securityContext | nindent 12 }}
          image: "{{ $.Values.image.repository }}:{{ $.Values.image.tag | default $.Chart.AppVersion }}"
          imagePullPolicy: {{ $.Values.image.pullPolicy }}
{{- end }}

oxr463 avatar May 10 '22 20:05 oxr463

Reference(s):

  • https://github.com/helm/helm/issues/3684
  • https://stackoverflow.com/a/56225254/8507637

oxr463 avatar May 10 '22 20:05 oxr463