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
Fix the Helm trick that we use to differentiate between 0 and an empty value #6713
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see lots of discussion of this problem in:
- Wrong interpretation of Zero in templates files when having a default value helm/helm#3164
- Clarification: nil vs empty string Masterminds/sprig#53
And the work arounds mentioned in the comments of those issues are:
- Wrong interpretation of Zero in templates files when having a default value helm/helm#3164 (comment) by @aczajkowski
if has (kindOf $value) (list "int" "int64" "float" "float64")
- Clarification: nil vs empty string Masterminds/sprig#53 (comment) by @jkroepke (one of our recent contributors)
if kindIs "invalid" $value
Neither of those issues seems to mention the hasKey $parent "key"
trick which is already used in our chart, but I wonder why.
Happy to approve this PR if you are sure your's is the best fix and clearly it does fix the problem - thanks for showing all your testing - but also please check out those issues and look at the other tricks used already in our chart and explain why this new trick is necessary here.
{{- if ne (trimAll "'" (squote .Values.global.revisionHistoryLimit)) "" }} | ||
revisionHistoryLimit: {{ .Values.global.revisionHistoryLimit }} | ||
{{- end }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Elsewhere in this chart we use if hasKey .Values.global "revisionHistoryLimit
:
{{- if hasKey .Values.webhook "automountServiceAccountToken" }} | |
automountServiceAccountToken: {{ .Values.webhook.automountServiceAccountToken }} | |
{{- end }} |
Would that technique work here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wallrj we also want to skip the property when the value is not set or set to an empty value in the values.yaml file or using a command line flag. This solution would also set the property when the value is just an empty string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the user has used --set global.revisionHistoryLimit=
then revisionHistoryLimit:
should appear in the rendered manifests, since that is what they have asked for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you unset the value from the command line then?
I guess it wont work, if the default already contains the key and as end user I want to override the key with nil. Technically, the key is present with an nil value. |
I used this variant in a recent PR to another chart: {{- if or (kindIs "float64" .Values.revisionHistoryLimit) (kindIs "int64" .Values.revisionHistoryLimit) }}
revisionHistoryLimit: {{ .Values.revisionHistoryLimit | int64 }}
{{- end }} which I guess is a variant of the |
…y value Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
ff370ed
to
2b14b32
Compare
Thanks for providing this alternative solution. @wallrj I also just realised that it is not possible to put this logic in a template, because it is the argument of an if statement. Templates can only be used to template part of the output yaml. It cannot be used to define boolean functions. |
Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for updating the PR description with the new variant of the trick (which no longer uses squot).
And thanks for explaining to me in person why those other work arounds do not work here.
Please add comments above the tricks in the code, explaining why the if
logic is written in this tricky way.
@@ -16,7 +16,7 @@ metadata: | |||
{{- end }} | |||
spec: | |||
replicas: {{ .Values.cainjector.replicaCount }} | |||
{{- if ne (quote .Values.global.revisionHistoryLimit) (quote "") }} | |||
{{- if not (has (quote .Values.global.revisionHistoryLimit) (list "" (quote ""))) }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment above all these explaining why it is written this way and link to the upstream bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that you already added the comments.
/lgtm
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: wallrj The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/cherrypick release-1.14 |
@inteon: new pull request created: #6729 In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
Closes #6712
This trick was introduced in #5860 and #6248.
The root issue is that
{{ if "" }}
and{{ if 0 }}
both returnfalse
. The trick was introduced to fix this issue.The trick uses the
quote
function to differentiate between a empty string and 0 or "0".The result from
quote
can be compared with(quote "")
to detect empty strings.However, the fix is not complete. In case the value is empty/ null, the result from quote is an empty string not
(quote "")
.The final solution that fixes the original and the new problem is to:
(quote value)
: yields these strings"0"
,""
and an empty string""
or an empty stringbefore:
after:
Kind
/kind bug
Release Note