helmfile icon indicating copy to clipboard operation
helmfile copied to clipboard

Namespace template doesn't render any value

Open dtshepherd opened this issue 6 years ago • 14 comments

When I try to use {{ .Namespace }} in a values.yaml.gotmpl template, it just renders as an empty string. It doesn't throw an error. This happens if I let helmfile use the default namespace or specify a namespace override with --namespace on the CLI.

Maybe I'm missing something?

dtshepherd avatar Sep 19 '18 23:09 dtshepherd

@dtshepherd Hey! Thanks for the report. Yeah {{.Namespace}} is available only in helmfile.yaml right now. For which purpose you want to use it from within values.yaml.gotmpls?

mumoshu avatar Sep 19 '18 23:09 mumoshu

I wanted to use it to build a URL that includes the namespace. That value is used in my chart templates.

dtshepherd avatar Sep 20 '18 00:09 dtshepherd

@dtshepherd Makes sense! Meanwhile you can use something like below as a work-around:

releases:
- name: myapp
   # ...
   set:
   - name: foo.bar.ns
      value: {{.Namespace}}

mumoshu avatar Sep 20 '18 00:09 mumoshu

Yeah once you said I could use in the helmfile, that pointed to a workaround. Thanks!

dtshepherd avatar Sep 20 '18 01:09 dtshepherd

Nice! Glad it worked for you.

One gotcha is that {{.Namespace}} results in the value passed via --namespace. That is, you can't refer to what was set in namespace: key in your helmfile.yaml.

So, do you expect {{.Namespace}} to result in foo in the below example?

releases:
- name: myapp
   # ...
   namespace: foo

mumoshu avatar Sep 20 '18 01:09 mumoshu

Yeah, that's a good point. Ideally, it would work that way. I'm not quite clear on the --namespace flag and how it interacts with namespace: being specified on a release given that there can be multiple namespaces specified if there are multiple. Does the --namespace flag override all specified namespace: fields in the releases?

dtshepherd avatar Sep 20 '18 14:09 dtshepherd

I'm after this functionality too - My use case is related to two different environments:

environments:
  default:
    values:
      - helm_vars/minikube.yaml

  test1:
    values:
      - helm_vars/test1.yaml

default - minikube - Everything is in the default namespace to ease developer use test1 - test cluster - Everything is in a namespace per app, i.e. fluentd

So for example helm_vars/minikube.yaml has:

releaseNamespace: {{ env "RELEASE_NAMESPACE" | default "default" }}

so it can be overriden with env vars if necessary, and then I refer to it like so:

releases:
  - name: fluentd
    namespace: {{ .Environment.Values | getOrNil "releaseNamespace" | default "fluentd" }}
    chart: stable/fluentd-elasticsearch
    values:
      - helm_vars/apps/fluentd.yaml.gotmpl

Finally, this is how i'm trying to use {{ .Namespace }} in ./helm_vars/apps/fluentd.yaml.gotmpl without having to duplicate the previous value again:

elasticsearch:
  host: 'elasticsearch-client.{{ .Namespace }}'

acaire avatar Oct 04 '18 08:10 acaire

Is there a different workaround? I'm looking for a similar setup as @acaire and since it's quite a few settings with a substring of namespace (also release name) it's a bit heavy handed to set all of those specifically in the helmfile.

naphta avatar Apr 12 '19 14:04 naphta

Is there a way to get access to the namespace in values.gotmpl?:

...
releases:
...
  namespace: bla
  values:
    - namespace: bla
    - values.gotmpl

In the context of values.gotmpl both .Environment.Values.namespace and .Namespace are empty...what am I missing?

Morriz avatar Jul 28 '19 18:07 Morriz

@Morriz I think you're confusing the context of values.gotmpl and helmfile.yaml. They're different and you have no way to alter the values.gotmpl context from within each release under releases.

We'll need what we're discussing in #745 in order to elegantly write it.

Until then, https://github.com/roboll/helmfile/issues/756#issuecomment-514013382 is the only known work-around.

mumoshu avatar Jul 29 '19 07:07 mumoshu

Even in helmfile.yaml I got the issue, except if I specify a --namespace arg in the helmfile command.

The context I use got a namespace

ghost avatar Nov 18 '21 15:11 ghost

Yep, I could not find a way to pick the targeting namespace and use it on the values section.

My use case is to specify the value for default-ssl-certificate for the nginx-ingress, which is on the form of namespace/secretName.

daper avatar Apr 01 '22 18:04 daper

@daper Here's what works for me.

values.351.yaml.gotmpl:

myns2: {{ .Release.Namespace }}
myns3: {{ .Namespace }}

helmfile.351.yaml:

releases:
- name: foo
  chart: stable/nginx
  namespace: bar
  values:
  - myns1: {{ .Namespace }}
  - values.351.yaml.gotmpl
$ helmfile -f helmfile.351.yaml --namespace foo build --embed-values
---
#  Source: helmfile.351.yaml

filepath: helmfile.351.yaml
helmBinary: helm
namespace: foo
releases:
- chart: stable/nginx
  name: foo
  namespace: bar
  values:
  - myns1: foo
  - myns2: bar
    myns3: foo
templates: {}
renderedvalues: {}

mumoshu avatar Apr 03 '22 00:04 mumoshu

@mumoshu, as @Tlzps commented, it works when you specify the --namespace option. But it comes as an empty value if not.

daper avatar Apr 04 '22 09:04 daper