Skip to content

fix(helm): handle templated namespaces consistently #6767

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

Conversation

aaron-prindle
Copy link
Contributor

@aaron-prindle aaron-prindle commented Oct 25, 2021

fixes #6749

Currently if there is a templated namespace with helm that does not currently exist in the cluster, skaffold will fail even though a regular deploy would succeed. As mentioned in #6749, this is because currently skaffold fetches the namespaces before we template which results in this failure. The fix here is to template the helm namespaces before fetching them.

@codecov
Copy link

codecov bot commented Oct 25, 2021

Codecov Report

Merging #6767 (8aadfd0) into main (290280e) will decrease coverage by 0.93%.
The diff coverage is 60.86%.

❗ Current head 8aadfd0 differs from pull request most recent head c268445. Consider uploading reports for the commit c268445 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6767      +/-   ##
==========================================
- Coverage   70.48%   69.54%   -0.94%     
==========================================
  Files         515      536      +21     
  Lines       23150    24419    +1269     
==========================================
+ Hits        16317    16982     +665     
- Misses       5776     6308     +532     
- Partials     1057     1129      +72     
Impacted Files Coverage Δ
cmd/skaffold/app/cmd/flags.go 89.00% <ø> (-1.82%) ⬇️
cmd/skaffold/skaffold.go 0.00% <ø> (ø)
cmd/skaffold/app/cmd/lint.go 52.94% <52.94%> (ø)
cmd/skaffold/app/cmd/cmd.go 70.49% <75.00%> (-0.57%) ⬇️
cmd/skaffold/app/cmd/debug.go 100.00% <100.00%> (ø)
cmd/skaffold/app/cmd/runner.go 64.17% <100.00%> (ø)
pkg/skaffold/initializer/build/builders.go 42.85% <0.00%> (-17.15%) ⬇️
pkg/skaffold/build/cluster/logs.go 0.00% <0.00%> (-16.67%) ⬇️
pkg/skaffold/event/v2/status_check.go 85.45% <0.00%> (-14.55%) ⬇️
pkg/skaffold/kubernetes/watcher.go 67.10% <0.00%> (-14.53%) ⬇️
... and 122 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 93e9a23...c268445. Read the comment docs.

Copy link
Member

@briandealwis briandealwis left a comment

Choose a reason for hiding this comment

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

Is there a way to test this?

@tejal29
Copy link
Contributor

tejal29 commented Nov 1, 2021

Is there a way to test this?

Either some unit tests or manual testing notes could help.

@@ -69,7 +70,11 @@ func collectHelmReleasesNamespaces(pipelines []latestV1.Pipeline) []string {
if cfg.Deploy.HelmDeploy != nil {
for _, release := range cfg.Deploy.HelmDeploy.Releases {
if release.Namespace != "" {
namespaces = append(namespaces, release.Namespace)
Copy link
Member

Choose a reason for hiding this comment

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

This should use the same approach as in releaseNamespace(), and it should surface any errors back up.
https://github.com/GoogleContainerTools/skaffold/blob/main/pkg/skaffold/deploy/helm/util.go#L159-L164

@briandealwis
Copy link
Member

Some unit tests of collectHelmReleasesNamespaces() looks easy enough?

@aaron-prindle aaron-prindle force-pushed the fix-6749_templated-helm-namespace-issue branch from 8aadfd0 to 8edbc56 Compare November 1, 2021 22:34
@pull-request-size pull-request-size bot added size/L and removed size/XS labels Nov 1, 2021
@aaron-prindle
Copy link
Contributor Author

aaron-prindle commented Nov 1, 2021

I've added the suggested changes and unit test for collectHelmReleasesNamespaces. I'm a bit confused though as to the suggested code change made. The suggestion was to reference logic at https://github.com/GoogleContainerTools/skaffold/blob/main/pkg/skaffold/deploy/helm/util.go#L159-L164. I just wanted to verify that the logic currently:

			for _, release := range cfg.Deploy.HelmDeploy.Releases {
				if release.Namespace != "" {
					templatedNamespace, err := util.ExpandEnvTemplateOrFail(release.Namespace, nil)
					if err != nil {
						return []string{}, fmt.Errorf("cannot parse the release namespace template: %w", err)
					}
					namespaces = append(namespaces, templatedNamespace)
				}
			}

is acceptable as theree was a suggested edit made that was essentially:

					if err != nil {
						namespaces = append(namespaces, release.Namespace)
                                                // which is now vvvv
						// return []string{}, fmt.Errorf("cannot parse the release namespace template: %w", err)
					}

but I'm a bit confused as this is what I believe should be an error case given the context. Thanks.

@aaron-prindle aaron-prindle force-pushed the fix-6749_templated-helm-namespace-issue branch from 8edbc56 to 7dcbf4b Compare November 1, 2021 22:43
@aaron-prindle aaron-prindle force-pushed the fix-6749_templated-helm-namespace-issue branch from 7dcbf4b to c268445 Compare November 1, 2021 22:44
@briandealwis
Copy link
Member

I was wrong with my comment. It happens more often than I'd like :-)

The important thing is that we treat inputs uniformly. It would seem to make sense to have collectHelmReleasesNamespaces() and Deployer.releaseNamespace() using the same function?

@aaron-prindle
Copy link
Contributor Author

aaron-prindle commented Nov 2, 2021

I'm not sure I understand, I believe the logic for namespace gathering from pipelines done in collectcollectHelmReleasesNamespaces is fundamentally different than the logic for Deployer.releaseNamespace. Reverting to the deployer namespace is only done in Deployer.releaseNamespace (code here). IIUC, the only shared logic would be something like:

func expandNamespace(ns string) (string, error) {
	expandedNs, err := util.ExpandEnvTemplateOrFail(r.Namespace, nil)
	if err != nil {
		return "", fmt.Errorf("cannot parse the release namespace template: %w", err)
	}
	return expandedNs, nil
}

which is essentially util.ExpandEnvTemplateOrFail

@briandealwis briandealwis changed the title fix: fix issue with templated helm namespaces when querying non-exist… fix(helm): handle templated namespaces consistently Nov 2, 2021
@aaron-prindle
Copy link
Contributor Author

aaron-prindle commented Nov 4, 2021

Currently I believe the implementation and tests are sufficient, was there any concern around better re-use of logic for Deployer.releaseNamespace and collectHelmReleasesNamespaces? From my comment above, I don't believe there is a worthwhile amount of logic to reuse but I may have missed something.

@briandealwis
Copy link
Member

We have util.Expand* calls in a few places in the code now and pulling it into a single place might reduce the likelihood of errors, like this issue, in the future. I mean, who would expect to find Helm-specific code outside of deploy/helm?

@aaron-prindle aaron-prindle merged commit fc06016 into GoogleContainerTools:main Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Skaffold dev with a templated helm namespace fails while querying a non-existent namespace
3 participants