Skip to content

Lookup template function - #6752

Merged
bacongobbler merged 9 commits into
helm:masterfrom
raffaelespazzoli:lookup
Jan 14, 2020
Merged

bacongobbler merged 9 commits into
helm:masterfrom
raffaelespazzoli:lookup

Conversation

@raffaelespazzoli

Copy link
Copy Markdown
Contributor

I created a lookup template function to address #949 .
The lookup up function takes for string parameters:

  1. the apiVersion, mandatory
  2. the Kind, mandatory
  3. the namespace, optional
  4. the name, optional

and returns a map[string]interface{}

if namespace is missing the lookup function assumes we are looking up a cluster resource.
if name is missing the lookup function assumes we are looking up an list of resources.

for example the expression to lookup the value of a loadbalancer service would be:

{{ (index (lookup "v1" "Service" "mynamespace" "myservice").status.loadBalancer.ingress 0).ip }}

The way the lookup function works is the following:

  1. at init time the restClient is initialized. I assume helm has already a way to initialized the rest client, so I'd gladly reuse that approach is someone can indicate to me how to do it.
  2. the lookup function is added to the template functions and can therefore be used.

This is a work in progress and I being a new contributor request some assistance (and patience) while I comply with all the rules.

@helm-bot helm-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 23, 2019
@raffaelespazzoli
raffaelespazzoli force-pushed the lookup branch 2 times, most recently from 401c5e7 to 2583fd6 Compare October 23, 2019 10:34
@thomastaylor312 thomastaylor312 added feature v3.x Issues and Pull Requests related to the major version v3 wip work in progress labels Oct 23, 2019
@thomastaylor312

Copy link
Copy Markdown
Contributor

@raffaelespazzoli Thanks for taking this on! Please let us know if you have any questions or need any feedback. Also, just as an FYI, because this is a new feature, we will probably not get around to a full review until after 3.0 is released

@raffaelespazzoli

Copy link
Copy Markdown
Contributor Author

@thomastaylor312 if you could help with two things:

  1. when I renamed my commits to comply with DCO, I inadvertently brought in also the last commit from the master branch. Is there a way to drop that commit from the PR?
  2. circle ci is failing because, I think, I have introduced a new command line flag. This is due to the fact that I am not creating the kube rest client the right way. I assume there is a way to create the client that uses already existing flags. Can you show me how to do it?

@thomastaylor312

Copy link
Copy Markdown
Contributor

@raffaelespazzoli You should be able to rebase your branch off of master and it should take care of it. As for the command line flag, let me dig into it some more. I haven't touched the engine package in a while and there might be an easier way

@thomastaylor312

Copy link
Copy Markdown
Contributor

Ok, so after digging around in the dynamic client docs I'm thinking the best way around this is to add a new top level Render method (probably something like RenderWithClient or something like that) that takes a Factory interface which should already exist when it is called from the action package in Helm. You might will probably need to expand the Helm Factory interface to include the ToDiscoveryClient method.

Any way you do it, I would recommend against trying to regenerate the config separately like you are doing it currently

@raffaelespazzoli

Copy link
Copy Markdown
Contributor Author

@thomastaylor312 I fixed the rebase issue, thanks for the suggestion.

I also took a look at how the kube client config is generated/managed.
It seems to me that the kube client config is embedded in a large config object (action.Configuration) that is created at the cobra command instantiation and passed down to each command. But there doesn't seem to be a globally visible config object reachable from every part of the code.
The engine package can be called by many actions but has no access to each action's config.
I maybe wrong in my summary, but this is what I understood reading the code.
So, I need some help finding a way to make the config object or the more specific kube client Factory visible in the engine package.

@thomastaylor312

Copy link
Copy Markdown
Contributor

The action.Configuration object has the RESTClientGetter which should get you what you need. You could pass the getter or the configuration object down into the engine. Still trying to think of other options

@raffaelespazzoli

Copy link
Copy Markdown
Contributor Author

@thomastaylor312 you say I can pass the action.Configuration or RESTClientGetter to the engine package. How would you do that? Keep in mind I am really new to the helm code base. with my limited understanding it seems to me that one of the issues there is that template processing will be called by multiple actions (I can think at least of install and template). I'd like to find a solution where we wound't have to modify and keep track of all the actions that can use the engine. Would it be better to create a global variable for the RESTClientGetter?

@raffaelespazzoli

Copy link
Copy Markdown
Contributor Author

@thomastaylor312 re-based and added the fix about getting the rest client from the action configuration.

@thomastaylor312 thomastaylor312 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @raffaelespazzoli! Finally back from vacation and looking through this again. This looks really concise and straightforward. I have a handful of requested changes and a few nits (that I'll leave up to you to decide if you want to fix). I'll start pinging some of the other maintainers to take a look at this. Although I don't see any problems security-wise as I look at this now, I want to make sure that we've made sure we understand any security implications of adding this feature

Comment thread pkg/action/install.go Outdated
if c.RESTClientGetter != nil {
rest, err := c.RESTClientGetter.ToRESTConfig()
if err != nil {
files, err2 = engine.Render(ch, values)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this just return the error? If the RESTClientGetter is not nil, then this shouldn't be failing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to maintain the previous behavior as much as possible. I don't have a specific opinion either way, so let me know how you prefer to handle this. It's been a while but some of the unit tests run without the client initialized. I don't remember exactly in which of the ifs they were failing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be best to return an error if ToRESTConfig errors. It shouldn't cause any failures with unit tests because most of them don't have RESTClientGetter set. If it does cause failures, let's review the list of those failures and see what the best solution would be

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to return an error

Comment thread pkg/engine/lookup_func.go Resolved
Comment thread pkg/engine/lookup_func.go Resolved
Comment thread pkg/engine/lookup_func.go Resolved
Comment thread pkg/engine/lookup_func.go Resolved
Comment thread pkg/engine/lookup_func.go Resolved
@jdolitsky

Copy link
Copy Markdown
Contributor

How will these values be rendered on helm template, with the consideration that the user would not have access to a running cluster?

@etsauer

etsauer commented Dec 12, 2019

Copy link
Copy Markdown

@jdolitsky I'm working with @raffaelespazzoli a bit on this, doing some use case testing. Currently helm template would throw an error if there's no access to a cluster:

$ helm template ./charts/lookup-test/
lookup_func.go:99: [ERROR] unable to retrieve resource list for: v1 , error: Unauthorized
lookup_func.go:73: [ERROR] unable to get apiresource from unstructured: /v1, Kind=Service , error Unauthorized

I personally don't see any issue with that as its reasonable to expect a running cluster if you are going to use the lookup function.

@thomastaylor312

Copy link
Copy Markdown
Contributor

@etsauer Helm template should run successfully without needing a connection to a Kubernetes, otherwise it would be a backwards incompatible change. If --validate is passed, it should then connect to k8s. I would suggest a filler value for when you can't connect

@raffaelespazzoli

raffaelespazzoli commented Dec 13, 2019 •

Copy link
Copy Markdown
Contributor Author

@jdolitsky @etsauer @thomastaylor312 in the current implementation there is some logic (see the pkg/action/install.go file) to decide whether to inject or no the lookup function. The lookup function is injected if the restclient is initialized.
From my experience the restclient is not initialized only during unit test and always when helm is run from the CLI. My experience may not be comprehensive but I added that logic anyway after a @thomastaylor312 to try to preserve the current behavior as much as possible.
If you run a template with the lookup function, without having injected the lookup function in the template engine (i.e. the restclient is not initialized), you get an golang template error as it is to be expected.
If you run a template with the lookup function and the client is initialized but it gets an error when establishing the connection, you get an error as shown by @etsauer.
It is kind of implied that in order to use this function you need a valid connection. I'm open discussing returning a default value instead of an error but I don't think it would be a good approach. This is not like the Capability family of variables that can be initialized if there is a connection or get a default if there isn't. The lookup function really can lookup any value of any type. It can return arrays, maps, string integer boo leans etc., so there wouldn't even be a one size fits all default type/value combination. And templates that get a type they didn't expect would fail anyway.

@raffaelespazzoli

Copy link
Copy Markdown
Contributor Author

@thomastaylor312 all you comment should be addressed

Signed-off-by: raffaelespazzoli <[email protected]>
@raffaelespazzoli

Copy link
Copy Markdown
Contributor Author

@thomastaylor312 ping...

1 similar comment
@raffaelespazzoli

Copy link
Copy Markdown
Contributor Author

@thomastaylor312 ping...

@thomastaylor312

Copy link
Copy Markdown
Contributor

@raffaelespazzoli Sorry. All back from the holidays and finally digging myself out of the last of the backlog. Re-reviewing now and bringing it back up in the dev call this week

@thomastaylor312 thomastaylor312 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good. Holding off on final approval until we can talk about what the behavior should be with helm template in the dev call

@technosophos

Copy link
Copy Markdown
Member

I am the one who rejected a similar proposal a few years ago. I am in favor of going forward with this PR. But I will call out one of the reasons I rejected a similar proposal in 2016:

One of the ideas behind helm rollback was that we could move a release back into the state it was in at that time. This required that all of the main dependencies were controlled by the chart (because if there were external dependencies, a rollback could not be reliable).

This feature will definitely put rollbacks at risk. But my feeling is that over time we now have a lot of external dependencies anyway. (CRDs are an example) So I think that while we should be aware of this and educate users about this, we should go ahead with this PR.

@thomastaylor312 thomastaylor312 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also approving as is. After a good discussion in the dev call, we were in agreement that your logic is correct. If there are lookups in a chart, it should fail if used with helm template (without the --validate flag). So this LGTM. We will need a follow up doc PR in the docs repo to document all the things here

@thomastaylor312

Copy link
Copy Markdown
Contributor

@raffaelespazzoli If you are feeling good with merging this, please remove the "WIP" and we'll go ahead and merge

@thomastaylor312 thomastaylor312 added this to the 3.1.0 milestone Jan 14, 2020
@raffaelespazzoli raffaelespazzoli changed the title WIP - Lookup template function Lookup template function Jan 14, 2020
@raffaelespazzoli

Copy link
Copy Markdown
Contributor Author

@thomastaylor312 WIP removed, please merge.

@jpettit

jpettit commented Mar 24, 2020 •

Copy link
Copy Markdown

@raffaelespazzoli

Just in case anyone else is wondering... the original proposed example only works for type: LoadBalancer. With a ClusterIP you'll get a nil record for .status.loadBalancer.ingress.

When using a ClusterIP it's a similar simple lookup:

{{ (lookup "v1" "Service" "namespace" "service name").spec.clusterIP }}

@huineng

huineng commented Mar 25, 2021

Copy link
Copy Markdown

is this working in a post-install,post-upgrade method ?
i have a chart that creates a loadBalancer service , it has an ip
nevertheless when i want to create a configmap in a post hook to fill in that ip , i cannot get it to work

{{- if (lookup "v1" "Service" .Release.Namespace $service).status }}
  NATS__IP: {{ (index (lookup "v1" "Service" .Release.Namespace $service).status.loadBalancer.ingress 0).ip }}
  {{- else }}
  NATS__IP: 0.0.0.0
  {{- end }}

if i don't do the if i get

at <$service>: nil pointer evaluating interface {}.loadBalancer

if i just do

lookup "v1" "Service" .Release.Namespace $service

i get the value map[] which means it's empty

so I4m wondering if the lookup is not immediately done instead if being done as part of the post hook

thanks

@BAGELreflex

Copy link
Copy Markdown

I wanted to do the same thing as huineng however it appears the lookup occurs prior to deploying any of the k8s resources defined in your template. This includes the post-install jobs too, the lookup / template resolution is occurring before any resources are created, even though it's a post-install job. I have instead just created a basic job which curl's the load balancer IP address from the local kubernetes API. This required me to create a ClusterRoleBinding and ClusterRole (I suspect RoleBinding and Role would have worked too) with permission to get information about the service.

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

Labels

feature size/L Denotes a PR that changes 100-499 lines, ignoring generated files. v3.x Issues and Pull Requests related to the major version v3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants