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

inner templates context #817

Closed
eddycharly opened this issue Aug 21, 2019 · 15 comments
Closed

inner templates context #817

eddycharly opened this issue Aug 21, 2019 · 15 comments

Comments

@eddycharly
Copy link
Contributor

eddycharly commented Aug 21, 2019

Is it possible to pass a context to an inner template ?

My use case is like this :

{{ if .Environment.Values.ingress.alb }}
{{ range $ingress := .Values.ingress.alb }}
  - name: alb-{{ $ingress.class }}
    namespace: ingress
    chart: incubator/aws-alb-ingress-controller
    values:
      - templates/ingress/alb.yaml.gotmpl
{{ end }}
{{ end }}

In alb.yaml.gotmpl i have :

scope:
  ingressClass: {{ $ingress.class }}

So i need to access $ingress, that is, the current element that is processed in the main helmfile.yaml file.

@eddycharly
Copy link
Contributor Author

@mumoshu do you have any advice for such use case please ?

@mumoshu
Copy link
Collaborator

mumoshu commented Aug 23, 2019

Nope! It isn't possible. We're discussing about a potential feature to allow you to pass arbitrary context to templates in #745. See templateFile mentioned in #745 (comment) to see how it would work.

@eddycharly
Copy link
Contributor Author

@mumoshu thanks for clarifying it out.
Why not introduce a tpl function like in helm ?

@mumoshu
Copy link
Collaborator

mumoshu commented Aug 23, 2019

@eddycharly Adding tpl is ok. I thought we already have the issue for that. But How tpl helps in your use-case?

@eddycharly
Copy link
Contributor Author

@mumoshu i could write something like :

{{ if .Environment.Values.ingress.alb }}
{{ range $ingress := .Values.ingress.alb }}
  - name: alb-{{ $ingress.class }}
    namespace: ingress
    chart: incubator/aws-alb-ingress-controller
    values:
      - {{ tpl (readFile "templates/ingress/alb.yaml.gotmpl") $ingress | fromYaml }}
{{ end }}
{{ end }}

This way i can control the inner template processing and context.

@mumoshu
Copy link
Collaborator

mumoshu commented Aug 23, 2019

Ah that makes sense! You need indent but yeah it should generally work.

    values:
      -
{{ tpl (readFile "templates/ingress/alb.yaml.gotmpl") $ingress | fromYaml | indent 8}}

@mumoshu
Copy link
Collaborator

mumoshu commented Aug 23, 2019

Ah, see #504 - It seems like we already have tpl :)

@eddycharly
Copy link
Contributor Author

Not sure if i need to indent as it is manipulating objects but even if we need to specify the correct indent that's not a big deal, we do this with helm all the time.
I think it gives a lot of power to let the end user control templating programmatically rather than declaratively.

@mumoshu
Copy link
Collaborator

mumoshu commented Aug 23, 2019

Not sure if i need to indent as it is manipulating objects

It is manipulating objects to be rendered as text. Please note that Helmfile templates are rendered before loading helmfile.yaml as YAML.

@mumoshu
Copy link
Collaborator

mumoshu commented Aug 23, 2019

I think it gives a lot of power to let the end user control templating programmatically rather than declaratively.

Makes sense :)

As we already have tpl implemented. Would you mind trying what you've described? I appreciate any feedback if it doesn't work as expected.

@eddycharly
Copy link
Contributor Author

I'm testing it right now, it seems to be working, i just need to make an additionnal test to ensure it's working the same way as in helm and come back to you.

@eddycharly
Copy link
Contributor Author

@mumoshu it's almost working ;)
The case that doesn't work (and i believe it works in helm) is this one :

autoDiscoverAwsRegion: true
autoDiscoverAwsVpcID: true
clusterName: {{ $.Values.clusterName }}
nodeSelector:
  kops.k8s.io/instancegroup: infra
scope:
  ingressClass: {{ .class }}

In the inner template, we should be able to refer to the root context by prefixing the accessor with $ as in clusterName: {{ $.Values.clusterName }}.

The ingressClass: {{ .class }} part is working as expected, it is using the context that is passed to the tpl function, but the root context seems to be unavailable.

@mumoshu
Copy link
Collaborator

mumoshu commented Aug 23, 2019

@eddycharly Can you try {{ tpl (readFile "templates/ingress/alb.yaml.gotmpl") (merge .Values $ingress)}} so that {{ .Values.clusterName }} would work?

And based on my knowledge about how tpl is implemented in helm and helmfile, I can't figure out how it can work in helm but helmfile. If you have any insight on that I'd appreciate your help.

@eddycharly
Copy link
Contributor Author

@mumoshu I looked through the helm source code and i don't see anyting related to the root context i was talking about, i guess i was wrong.
I'll go with the tpl + indent solution, thank you for your patience ! :)

@eddycharly
Copy link
Contributor Author

I close this issue, all is clear and already implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants