Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for _helper.tpl so Go templates can be shared by all files in templates directory #1774

Open
andylholloway opened this issue Feb 14, 2021 · 0 comments

Comments

@andylholloway
Copy link

Hi
We have quite a bit of repetition in our templates. To try and reduce this repetition I have been looking at using the Go template function Kudo supports.

I have been able to define and use templates within the same template file e.g.

{{- define "creatorLabel" -}}
  creator: alh
{{- end -}}

{{- define "apiVersionLabel" -}}
  app-version: "{{ .AppVersion }}"
{{- end -}}

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Name }}-echoserver-deployment
  labels:
    {{ template "creatorLabel" }}
    {{ template "apiVersionLabel" . }}
spec:
  selector:
    matchLabels:
      app: {{ .Name }}-echoserver
  replicas: {{ .Params.replicas }}
  template:
    metadata:
      labels:
        app: {{ .Name }}-echoserver
        {{ template "creatorLabel" }}
        {{ template "apiVersionLabel" . }}
    spec:
      containers:
        - name: echoserver
          image: k8s.gcr.io/echoserver:1.4
          ports:
            - containerPort: 8080

This helps to reduce some of the repetition within the same template, but ideally I would be able to move the template definitions into a file which can be shared by all template files in the /templates directory in the same way helm uses _helper.tpl.

I have looked through the documentation but I can't see how to create and reference a "helper" file. I have tried referencing the a template in multiple yaml files, but I get an error saying: "... at <{{template "creatorLabel"}}>: template "creatorLabel" not defined".

Is it be possible to create an external template / helper file?

@andylholloway andylholloway changed the title Support external template function Support for _helper.tpl so Go templates can be shared by all files in templates directory Feb 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant