Skip to content
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

feature(pkg/engine): introduce RenderWithClientProvider #12617

Merged
merged 2 commits into from Jan 9, 2024

Conversation

porridge
Copy link
Contributor

@porridge porridge commented Dec 7, 2023

What this PR does / why we need it:

In order to fully test charts that use the lookup function in environments that do not provide a kube API server (e.g. with helmtest), we need to inject a (fake) kubernetes client to the engine.

This change adds a RenderWithClientProvider function parallel to the already existing (somewhat unfortunately named) RenderWithClient, to make this possible.

Special notes for your reviewer:

Existing functionality is untouched.

If applicable:

  • this PR contains documentation
  • this PR contains unit tests
  • this PR has been tested for backwards compatibility: I made sure that the lookup template function still works, by running a locally built helm binary against a chart which uses this functionality, on a kind cluster

Statement coverage with tests:

file before after
engine.go 89.2% 88.7%
files.go 85.3% 85.3%
funcs.go 85.4% 85.4%
lookup_func.go 0% 33.3%

Comment on lines 48 to 50
// This is suboptimal but the best kind->resource translation we can do without a discovery client.
gvr, _ := meta.UnsafeGuessKindToResource(schema.FromAPIVersionAndKind(apiVersion, kind))
namespaced := true // we infer this from the namespace argument to the lookup function
Copy link
Contributor

Choose a reason for hiding this comment

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

can we detail what is happening here please. Any limitations of this functionality? And how come we can't use the resources gvk when invoked from the lookup function.

(the three levels of function closures are bending my brain a little; I think I might be missing something)

Copy link
Contributor

@gjenkins8 gjenkins8 Dec 15, 2023

Choose a reason for hiding this comment

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

Thinking about this more, if a non-Helmtest user started to use this RenderWithDynamicClient interface, they might (I think likely) be surprised by the meta.UnsafeGuessKindToResource usage here. Given my (admittedly superficial) understanding, that this function is trying to "guess" the resource name from the Version, Kind info (meta.UnsafeGuessKindToResource source).

It seems to me, that it would be better to hoist the clientProviderFunc functionality to external Render interface? Rather than passing in a dynamic client, which gets only some of the way. Then needing to build in a workaround to use it.

Something like:

func RenderWithClientLookupFunc(chrt *chart.Chart, values chartutil.Values, clientLookupFunc ClientLookupFunc) (map[string]string, error) {
	return Engine{
		clientLookupFunc: ClientLookupFunc,
	}.Render(chrt, values)
}

...

if e.clientLookupFunc != nil {
    funcMap["lookup"] = return newLookupFunction(clientLookupFunc)
}			

edit: Is there perhaps a better name than RenderWithClientLookupFunc? I choose the obvious here for the example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gjenkins8 thank you for the prompt review! You formulated clearly the concerns I had myself floating somewhere on the edge of my perception 😅

I updated the PR to follow your advice. This also has the advantages of:

  • not introducing any new function closures, at the expense of adding a new struct-based type, which I think is an acceptable overhead in this place,
  • eradicating the somewhat dubious UnsafeGuessKindToResource call to the helmtest codebase, where it's appropriate.

Signed-off-by: Marcin Owsiany <porridge@redhat.com>
@porridge porridge changed the title feature(pkg/engine): introduce RenderWithDynamicClient feature(pkg/engine): introduce RenderWithClientProvider Dec 18, 2023
Copy link
Contributor

@gjenkins8 gjenkins8 left a comment

Choose a reason for hiding this comment

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

Changes LGTMe, will ask another maintainer to review. Thanks for updating the PR

return Engine{
config: config,
clientProvider: &clientProvider,
Copy link
Contributor

@gjenkins8 gjenkins8 Dec 21, 2023

Choose a reason for hiding this comment

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

Nice! IMHO it is great to be able to re-write the existing implementation in terms of this new, more generic, interface

@joejulian
Copy link
Contributor

joejulian commented Dec 27, 2023

@porridge There are no tests with this. Are the existing tests sufficient? How can I manually validate this?

@joejulian joejulian self-assigned this Dec 27, 2023
@gjenkins8
Copy link
Contributor

@porridge There are no tests with this. Are the existing tests sufficient? How can I manually validate this?

I also realized tests were missing. @porridge was going to add after the holidays iirc

Signed-off-by: Marcin Owsiany <porridge@redhat.com>
@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jan 3, 2024
@porridge
Copy link
Contributor Author

porridge commented Jan 3, 2024

@gjenkins8 @joejulian tests added.

I think they now cover everything that is testable without having a real API server connection.

PTAL

Copy link
Contributor

@joejulian joejulian left a comment

Choose a reason for hiding this comment

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

Thanks!

@joejulian joejulian added the Has One Approval This PR has one approval. It still needs a second approval to be merged. label Jan 3, 2024
@joejulian joejulian added this to the 3.14.0 milestone Jan 3, 2024
@mattfarina mattfarina merged commit 7fd0804 into helm:main Jan 9, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Has One Approval This PR has one approval. It still needs a second approval to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants