Search code examples
kubernetes-helm

Helm create template for template


Is it possible to create a helm template for a template file inside templates/ folder? My use case is following: there are several kubernetes deployment files which differs only in deployment name and docker image that is pulled from a repo(e.g. deployment for service1, service2 etc). I want to create one chart to deploy all that services. Currently there are a lot of copy-paste in my deployment templates. I want to have some kind of a template for that templates. Also all that deployment templates will have different Values.yaml on different environments(e.g. service1 and service2 will have values-prod.yaml on prod env and values-stage.yaml on staging env)

If not possible, what are the alternative solutions? Thanks


Solution

  • For those who are interseted in a solution: as proposed by David Maze in the comments to my question I should use somehing similar to {{ range .Values.resourceNames }} in the template file(e.g. deployment.yaml) and divide those resources with --- on the beginning of a loop(kubernetes allows to have several resources defined in a single file separated by ---), where .Values.resourceNames is just an example. So having that deployment.yaml template could look like following:

    {{- range .Values.resourceNames }}
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: {{ .name }}-deployment
      labels:
        app: {{ .name }}
    spec:
      replicas: {{ .replicaCount }}
      selector:
        ...
    {{- end }}
    
    

    where values.yaml file looks like folloiwng:

    resourceNames:
      - name: service1
        replicaCount: 2
      - name: service2
        replicaCount: 1