Repository navigation
Closed
Description
Hello!
While reviewing recent charts PRs (example: helm/charts#2004), I see this interesting pattern:
{{- if .Values.persistence.storageClass }}
{{- if (eq "-" .Values.persistence.storageClass) }}
storageClassName: ""
{{- else }}
storageClassName: "{{ .Values.persistence.storageClass }}"
{{- end }}
{{- end }}
The explanation is:
## mariadb data Persistent Volume Storage Class
## If defined, storageClassName: <storageClass>
## If set to "-", storageClassName: "", which disables dynamic provisioning
## If undefined (the default) or set to null, no storageClassName spec is
## set, choosing the default provisioner. (gp2 on AWS, standard on
## GKE, AWS & OpenStack)
##
# storageClass: "-"
My question was, since the template-rendered YAML will either be a storage class name, an empty string (to disable dynamic provisioning), or not set at all, can't we just do this instead?
## mariadb data Persistent Volume Storage Class
## If defined, storageClassName: <storageClass>
## If set to empty string "", this disables dynamic provisioning
## If undefined (the default) or set to null, no storageClassName spec is
## set, choosing the default provisioner. (gp2 on AWS, standard on
## GKE, AWS & OpenStack)
##
# storageClass: ""
Then the template logic could be simplified to:
{{- if .Values.persistence.storageClass }}
storageClassName: "{{ .Values.persistence.storageClass }}"
{{- end }}
But this would only work if an empty string is different from not set (nil) when checking if .Values.some.value in go template. Can someone help clarify this for me, or point to the relevant part of the docs? I seem to not be able to find this info.
Thanks!
Reacted by Max Baz, Alexander A. Prokhorov, IAXES, Zoltán Zvara and ngadde
Metadata
Metadata
Assignees
Labels
No labels
Activity
scottrigby commentedon Sep 7, 2017
OK, I found the relevant helm issue: helm/helm#2600
But just checking here, is there really no way for sprig to differentiate the difference between
niland""(empty string) in a template? If not, would that be a desirable PR?technosophos commentedon Sep 7, 2017
Wow. That issue actually helped me nail down an issue I was seeing in a local PVC! Ha!
Okay, so an
{{ if $value }}will check for both""andnil, considering either one to be an "empty value". There is also theemptyfunction, which essentially behaves the same way.It might be nice to be able to test whether something is
niland possibly empty ({{ if isNil $foo }}). I don't think we have a way of checking that. Maybeeq $foo nilwould work... I've never tried.Does that answer your question, or am I misunderstanding.
mattfarina commentedon Sep 11, 2017
@technosophos If something is a string, isn't the initialized value on a struct
""instead ofnil? How would you get thenilstate without the template engine complaining about the missing field?I imagine I'm missing something which is why I ask.
jkroepke commentedon Apr 15, 2019
Found a solution
{{ kindIs "invalid" $value }}mattfarina commentedon Sep 30, 2019
@scottrigby @technosophos Here's an example of the previous comment... https://play.golang.org/p/9yJW5iGT4qa
When I looked at this in the past I'd not thought of this. Thanks @jkroepke
scottrigby commentedon Oct 1, 2019
Love it 😄
8 remaining items
helm: document missing bpf helm option
thesuperzapper commentedon Aug 28, 2023
@jkroepke While this solution might work, I imagine there are other cases where the "kind" of something might be "invalid" without it being
nil.I think better option is to use a direct equality check against
nil:PS: the reason that the kind of
nilequals "invalid" is actually becausenildoes not have a type in Go, because it is a "predeclared identifier".