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

Fallback to alternative env variable #1574

Open
paichinger opened this issue Nov 6, 2020 · 2 comments
Open

Fallback to alternative env variable #1574

paichinger opened this issue Nov 6, 2020 · 2 comments

Comments

@paichinger
Copy link
Contributor

paichinger commented Nov 6, 2020

Let's say we have the following:

- name: my-service
  namespace: {{ env "MY_SERVICE_NAMESPACE" | default "default" }}

Now, I'd like to check on another env variable for the namespace before the default fallback is used. Something like:

- name: my-service
  namespace: {{ env "MY_SERVICE_NAMESPACE" <OR ELSE> env "DEFAULT_NAMESPACE"  | default "default" }}

Unfortunately I couldn't figure out how to do this <OR ELSE> in an elegant (not too verbose) way.
This is working, but I was wondering if there's a better way to do this, it feels very cluttered:

{{ $MY_SERVICE_NAMESPACE := "" }}
{{ if env "MY_SERVICE_NAMESPACE" }}
  {{ $myServiceNamespace = env "MY_SERVICE_NAMESPACE" }} 
{{ else }}
  {{ $myServiceNamespace = env "DEFAULT_NAMESPACE"  }}
{{ end }}
- name: my-service
  namespace: {{ $myServiceNamespace  | default "default" }}

Any hints are appreciated!

@paichinger paichinger changed the title Check for alternative env variable Fallback to alternative env variable Nov 6, 2020
@andrewnazarov
Copy link
Contributor

andrewnazarov commented Nov 6, 2020

Maybe coalesce will help

Probably something like this:

namespace: {{ printf "%s" (coalesce (env "MY_SERVICE_NAMESPACE") (env "DEFAULT_NAMESPACE") "default") }}

@paichinger
Copy link
Contributor Author

coalesce did the trick. I think printf "%s" is not even needed. For me this is working:
namespace: {{ coalesce (env "MY_SERVICE_NAMESPACE") (env "DEFAULT_NAMESPACE") "default") }}
Much nicer than what I had, thanks you very much!

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