You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After testing the lookup function introduced in #6752, I found calling lookup in a template caused the rendered output to be empty.
Here's what I used to test. After creating a new chart with helm create foo, I modified foo/templates/deployment.yaml to add a new label that performs a lookup:
><> ./bin/helm template --validate ./foo/ --debug
install.go:156: [debug] Original chart version: ""
install.go:173: [debug] CHART PATH: /home/bacongobbler/code/go/src/helm.sh/helm/foo
lookup_func.go:51: got err: the server could not find the requested resource
It looks as though we need to actually capture the case where a lookup fails and report the expected error.
If no fourth parameter is ever provided, lookup is never called. Users may expect this to be the case when trying to lookup non-namespaced resources, like namespaces.
After playing around a little more, I found that this breaks with a very common use case.
With this function, I want to check if a certain resource exists in the cluster. If it does not exist, create it. This could be used to solve cases like #6794.
Here's the example template:
{{ if not (lookup "v1" "Namespace" .Release.Namespace .Release.Namespace) }}
apiVersion: v1
kind: Namespace
metadata:
name: {{ .Release.Namespace }}
{{ end }}
However, if the resource does not exist in the cluster, then all output is removed and no error is displayed.
@bacongobbler thanks for the detailed explanation. docs where in the todo list. @thomastaylor312 what is the right way to create docs? @bacongobbler the intended way to invoke lookup for a cluster scoped resource was lookup "v1" "Namespace" "" .Release.Namespace. Also you may find this useful:
namespaced true + non-empty namespace + non empty name -> kubectl get xxx -n -> single object
namespaced true + non-empty namespace + empty name -> kubectl get xxx -n -> list
namespaced true + empty namespace (name is ignored) -> kubectl get xxx --all-namespaces -> list
namespaced false (namespace is ignored) + non-empty name -> kubectl get xxx -> single object
namespaced false (namespace is ignored) + empty name -> kubectl get xxx -> list
Given the current implementation of lookup, perhaps a better way to check if an object esists is:
Given the current implementation of lookup, perhaps a better way to check if an object esists is:
That's what I was testing for earlier with #7473 (comment). if not is the way you check for a nil value in text/template land. There is no != operand :)
Finally what should lookup return when the requested resource does not exist? here are some options:
1. empty map (current implementation)
2. fail the call (this seems a bit draconian)
As it currently stands, #6752 will fail the call. PR #7474 fixes this case by checking if the error returned from client.Get was of the type NotFound. That way, lookup will now return an empty map, and the example listed above works. If you wouldn't mind looking at that PR, that would be appreciated.
Yes, I tried both #7474 and now master (since it has been merged), with the same result:
⋊> ~/g/s/g/h/helm on master ⨯ helm version
version.BuildInfo{Version:"v3.0+unreleased", GitCommit:"5c7bf8e0150a7c4c62441581718f17b5a892fdbd", GitTreeState:"dirty", GoVersion:"go1.12.12"}
⋊> ~/g/s/g/h/helm on master ⨯ helm install foo ./foo -n bar
Error: create: failed to create: namespaces "bar" not found
@bacongobbler is that the correct "install" ^ command to set Release.Namespace?
Activity
bacongobbler commentedon Jan 27, 2020
Found the issue with a few println statements. No error was returned to the user when the requested resource was not found.
Adding a
log.Printf("got err: %s", err)afterhelm/pkg/engine/lookup_func.go
Line 48 in 9c680e6
It looks as though we need to actually capture the case where a lookup fails and report the expected error.
bacongobbler commentedon Jan 27, 2020
Something else that was confusing...
If no fourth parameter is ever provided,
lookupis never called. Users may expect this to be the case when trying to lookup non-namespaced resources, like namespaces.The correct invocation was
@raffaelespazzoli can you please write some documentation on how to use the
lookupfunction and its expected use cases? https://github.com/helm/helm-wwwbacongobbler commentedon Jan 27, 2020
After playing around a little more, I found that this breaks with a very common use case.
With this function, I want to check if a certain resource exists in the cluster. If it does not exist, create it. This could be used to solve cases like #6794.
Here's the example template:
However, if the resource does not exist in the cluster, then all output is removed and no error is displayed.
with a namespace that already exists:
With a namespace that does not already exist:
This doesn't seem like the right output and exit code. How should we proceed?
[-]`lookup` function removes all rendered output[/-][+]a failed `lookup` removes all rendered output[/+]raffaelespazzoli commentedon Jan 28, 2020
@bacongobbler thanks for the detailed explanation. docs where in the todo list.
@thomastaylor312 what is the right way to create docs?
@bacongobbler the intended way to invoke lookup for a cluster scoped resource was
lookup "v1" "Namespace" "" .Release.Namespace. Also you may find this useful:Given the current implementation of lookup, perhaps a better way to check if an object esists is:
I haven't tested it though.
Finally what should lookup return when the requested resource does not exist? here are some options:
What else? do we want to return a structure with the error? This will probably make the templating code complex to manage.
Also should we print anything to stdout/stderr when the lookup fails?
bacongobbler commentedon Jan 28, 2020
Thanks for the follow-up!
That's what I was testing for earlier with #7473 (comment).
if notis the way you check for a nil value intext/templateland. There is no!=operand :)As it currently stands, #6752 will fail the call. PR #7474 fixes this case by checking if the error returned from
client.Getwas of the typeNotFound. That way,lookupwill now return an empty map, and the example listed above works. If you wouldn't mind looking at that PR, that would be appreciated.bacongobbler commentedon Jan 28, 2020
https://github.com/helm/helm-www#how-to-edit-the-helm-docs
ichekrygin commentedon Jan 30, 2020
@bacongobbler in
How do I define
.Releas.Namespace?I am trying ^ exact template and getting:
bacongobbler commentedon Jan 30, 2020
Are you using #7474?
That field is injected into the template engine. See https://helm.sh/docs/chart_template_guide/builtin_objects/
ichekrygin commentedon Jan 30, 2020
Not sure where to comment :), continue here.
Yes, I tried both #7474 and now
master(since it has been merged), with the same result:@bacongobbler is that the correct "install" ^ command to set
Release.Namespace?