Skip to content

a failed lookup removes all rendered output #7473

Closed

Description

@bacongobbler

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:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "foo.fullname" . }}
  labels:
    foo: {{ (lookup "v1" "Deployment" .Release.Namespace "nginx").metadata.name }}
...

And then created a deployment to match that lookup:

><> kubectl run nginx --image=nginx --replicas=5
deployment.apps/nginx created

No output is rendered.

><> ./bin/helm template --validate ./foo/

><> ./bin/helm version
version.BuildInfo{Version:"v3.0+unreleased", GitCommit:"9c680e604be8d516960eecbbc3718ace91643cea", GitTreeState:"dirty", GoVersion:"go1.13.6"}

Calling helm install fails to render, as no objects were rendered.

><> ./bin/helm install foo ./foo/
Error: no objects visited

I will look further into this, but @raffaelespazzoli, can you please look further into this and see where the issue may be stemming from?

Activity

  1. added
    bug
    on Jan 27, 2020
  2. added this to the 3.1.0 milestone on Jan 27, 2020
  3. bacongobbler commented on Jan 27, 2020

    @bacongobbler
    MemberAuthor

    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) after

    if err != nil {
    displayed the error:

    ><> ./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.

  4. bacongobbler commented on Jan 27, 2020

    @bacongobbler
    MemberAuthor

    Something else that was confusing...

    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.

    {{ (lookup "v1" "Namespace" .Release.Namespace).metadata.name }}
    

    The correct invocation was

    {{ (lookup "v1" "Namespace" .Release.Namespace .Release.Namespace).metadata.name }}
    

    @raffaelespazzoli can you please write some documentation on how to use the lookup function and its expected use cases? https://github.com/helm/helm-www

  5. bacongobbler commented on Jan 27, 2020

    @bacongobbler
    MemberAuthor

    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.

    with a namespace that already exists:

    ><> ./bin/helm template --validate ./foo/
    ---
    # Source: foo/templates/serviceaccount.yaml
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: RELEASE-NAME-foo
    

    With a namespace that does not already exist:

    ><> ./bin/helm template --validate ./foo/ --namespace does-not-exist
    
    ><> echo $?
    0
    

    This doesn't seem like the right output and exit code. How should we proceed?

  6. changed the title [-]`lookup` function removes all rendered output[/-] [+]a failed `lookup` removes all rendered output[/+] on Jan 27, 2020
  7. raffaelespazzoli commented on Jan 28, 2020

    @raffaelespazzoli
    Contributor

    @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:

    {{ if (lookup "v1" "Namespace" .Release.Namespace .Release.Namespace) != nil }}
    apiVersion: v1
    kind: Namespace
    metadata:
      name: {{ .Release.Namespace }}
    {{ end }}
    

    I haven't tested it though.

    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)

    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?

  8. bacongobbler commented on Jan 28, 2020

    @bacongobbler
    MemberAuthor

    Thanks for the follow-up!

    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.

  9. bacongobbler commented on Jan 28, 2020

    @bacongobbler
    MemberAuthor
  10. ichekrygin commented on Jan 30, 2020

    @ichekrygin

    @bacongobbler in

    {{ if not (lookup "v1" "Namespace" .Release.Namespace .Release.Namespace) }}
    apiVersion: v1
    kind: Namespace
    metadata:
      name: {{ .Release.Namespace }}
    {{ end }}
    

    How do I define .Releas.Namespace?

    I am trying ^ exact template and getting:

    helm install foo ./foo -n bar
    Error: create: failed to create: namespaces "bar" not found
    
  11. bacongobbler commented on Jan 30, 2020

    @bacongobbler
    MemberAuthor
  12. ichekrygin commented on Jan 30, 2020

    @ichekrygin

    Not sure where to comment :), continue here.

    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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions