helm: default function treats boolean false as not defined, and applies default
values.yaml:
someKey: false
in template:
{{ default true .Values.someKey }}
I would expect this to return false
, as someKey
is defined, and its value is false, however it returns true.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 45
- Comments: 42 (5 by maintainers)
Commits related to this issue
- [stable/prometheus] Fix incorrect rendering of enableServiceLinks cf. https://github.com/helm/helm/issues/3308 Signed-off-by: Yong Wen Chua <lawliet89@users.noreply.github.com> — committed to aicadium-archive/helm-charts by lawliet89 4 years ago
- [stable/prometheus] Fix incorrect rendering of enableServiceLinks (#22391) cf. https://github.com/helm/helm/issues/3308 Signed-off-by: Yong Wen Chua <lawliet89@users.noreply.github.com> — committed to helm/charts by lawliet89 4 years ago
- [stable/prometheus] Fix incorrect rendering of enableServiceLinks (#22391) cf. https://github.com/helm/helm/issues/3308 Signed-off-by: Yong Wen Chua <lawliet89@users.noreply.github.com> — committed to iwilltry42/charts by lawliet89 4 years ago
- [stable/prometheus] Fix incorrect rendering of enableServiceLinks (#22391) cf. https://github.com/helm/helm/issues/3308 Signed-off-by: Yong Wen Chua <lawliet89@users.noreply.github.com> — committed to quantopian/charts by lawliet89 4 years ago
- [stable/prometheus] Fix incorrect rendering of enableServiceLinks (#22391) cf. https://github.com/helm/helm/issues/3308 Signed-off-by: Yong Wen Chua <lawliet89@users.noreply.github.com> — committed to includerandom/helm_charts by lawliet89 4 years ago
- [stable/prometheus] Fix incorrect rendering of enableServiceLinks (#22391) cf. https://github.com/helm/helm/issues/3308 Signed-off-by: Yong Wen Chua <lawliet89@users.noreply.github.com> Signed-off-b... — committed to li-adrienloiseau/charts by lawliet89 4 years ago
- [stable/prometheus] Fix incorrect rendering of enableServiceLinks (#22391) cf. https://github.com/helm/helm/issues/3308 Signed-off-by: Yong Wen Chua <lawliet89@users.noreply.github.com> — committed to prometheus-community/helm-charts by lawliet89 4 years ago
- [stable/prometheus] Fix incorrect rendering of enableServiceLinks (#22391) cf. https://github.com/helm/helm/issues/3308 Signed-off-by: Yong Wen Chua <lawliet89@users.noreply.github.com> Signed-off-b... — committed to mmingorance-dh/charts by lawliet89 4 years ago
- helm: default values don't work with booleans * See https://github.com/helm/helm/issues/3308. — committed to diegodelemos/reana by deleted user 4 years ago
- helm: default values don't work with booleans * See https://github.com/helm/helm/issues/3308. — committed to diegodelemos/reana by deleted user 4 years ago
- helm: default values don't work with booleans * See https://github.com/helm/helm/issues/3308. — committed to diegodelemos/reana by deleted user 4 years ago
- updated LiveReload toggle logic @MikeGoldsmith found an issue in testing related to https://github.com/helm/helm/issues/3308 — committed to bixu/helm-charts by bixu 2 years ago
- Allow operators to disable live reload of Refinery related to changed sampling rules (#104) @MikeGoldsmith found an issue in testing related to https://github.com/helm/helm/issues/3308 — committed to honeycombio/helm-charts by bixu 2 years ago
- {{ .boolean | default true }} doesnt work in helm templating. For more details please see: https://github.com/helm/helm/issues/3308 — committed to furmanyk/helm-charts by deleted user 2 years ago
- {{ .boolean | default true }} doesnt work in helm templating. For more details please see: https://github.com/helm/helm/issues/3308 (#55) Co-authored-by: Eugene Furmanyk <eugene.furmanyk@ringcentral.... — committed to mvisonneau/helm-charts by furmanyk 2 years ago
- Allow services to be exposed via HTTP. The use of the `default` function had the unintended side-effect of never allowing the value to be `false`. See the following for more details: - [helm/helm#33... — committed to ezcater/tonic_helm_charts by jbeard6 9 months ago
- Support HTTP (#10) * Allow services to be exposed via HTTP. The use of the `default` function had the unintended side-effect of never allowing the value to be `false`. See the following for mo... — committed to ezcater/tonic_helm_charts by jbeard6 9 months ago
- Allow services to be exposed via HTTP. The use of the `default` function had the unintended side-effect of never allowing the value to be `false`. See the following for more details: - [helm/helm#33... — committed to ezcater/tonic_helm_charts by jbeard6 9 months ago
You have the arguments flipped in the wrong direction. It should be
{{ default .Values.somekey false }}
.default
takes its first argument as the value to check if it exists, then defaults to the second argument if it’s not defined.Hey guys, you can try to use this one solution, which we are using:
@bacongobbler If this is working as intended (that you cannot use
default
with booleans). Can it be added to documentation, as a warning? Or reopen the issue if it isn’t working as intended.@gabordk and anyone else that my encounter this issue, this is my workaround that I am using is a few templates:
in action:
I use this:
This basically says “render the block if the value is true, OR if the value is false because the key does not exist”.
I find this easier to understand than the ternary function and does not depend on the variable’s value.
I can’t believe this hasn’t been reopened
Hi @bacongobbler ,
What if I’d like to use this in a block
if
in my template? Something like:and I want the
someSetting
to not render only when$foo
is explicitly set tofalse
.simplest workaround I’ve come up is this
Thanks @schollii, worked for me. The documentation should definitely state that values that evaluate to false cannot be used with the
default
function, or the function should be changed to work similarly to Jinja2’sdefault
filter:I am surprised that I still see this bug in 2021…
The “if + default” make me confused since it behaviors differently with different default value.
with
and explicitly define “somekey: true” , then
<code-block>
will be executed as expected.But on the other hand,
and explicitly define “somekey: false” , then
<code-block>
is still be executed which is not expected at all.I personally picked up the workaround provided by @sshishov in https://github.com/helm/helm/issues/3308#issuecomment-701367019, but I still wonder why helm works with
if/default
in such a confusing behavior as mentioned above.Currently using this. Convert all to lower case string and make a direct comparison. If not specified use
"true"
This is the workaround I use:
Obviously, if the dictionary you’re checking is something like
$foo
:The reason I’m using
coalesce
is becauseeq .enabled true
fails (error calling eq: invalid type for comparison
) withnull
errors for some dastardly reason.Obviously, the only works if
enabled
is either a boolean, or absent. If it’s actuallynull
, then the above won’t work.five years later and i fall into the same hole now, this issue should be fixed
{{if ((.Values.health).enabled)}}
This works well for me, in case anyone still cares about this.
That’s wrong.
default
takes the default value as its first argument, and the value to be tested as the second. I use this all the time. Yes, the order is weird, but the OP is right.There’s a bug here and this needs to reopened.
I’m going to put this solution to my problem here in case it helps anyone:
I wanted to render part of a template (a deployment label) by default unless the values.yaml explicitly stated that a value was false:
Here is the explanation of using the parenthesis on my evaluated value > https://stackoverflow.com/questions/59795596/how-to-make-nested-variables-optional-in-helm/68807258#68807258
And I am simply use a “not equals” which fits the logic very nicely. The users of my chart will have to explicitly state the specific value as
false
in order to not include this label in the deployment.I do:
Which looks in .Values for the key, “somekey”. If it’s there, it uses that value. If it’s not, it uses (in the above example) true as the default.
In some cases it may be useful to also add the
"true"
string to the checked listlist nil true "true" | has .Values.vaultEnabled
A new issue to update the docs for the
default
function probably needs to be opened. It should contain a warning and as a recommended workaround I suggest something generic:This is still open, istn’t it? The workaround provided by @bacongobbler does not seem to work. There must be some way to distinguish
null
fromfalse
? How else can you set a default totrue
and make it overridable by afalse
value?@bacongobbler apologies, i noticed this too but it’s still incorrect the other way… I also tried
{{ $deployment.enabled | default true }}
just for the hell of itvalues.yaml
template:
You would expect that to be false however:
Almost six years and it is still an issue. I want to explicitly change default-
true
in subchart in my values, and I cannot usefalse
.IMO the solution from @joejulian that is using the “dig” function is the most elegant, readable and simple solution.
Update: it is indeed mentioned in the documentation
How is this not re-opened or fixed? This is a really helpful thread with some workarounds, but we shouldn’t need it. This functionality with
default
and Booleans seems really simple and should work straight awayIt works. But can you explain why the bracket can work it?
This whole thread is unbelievable. 4 years… 👎