Skip to content

Commit 7dcbf4b

Browse files
committed
fix: fix issue with templated helm namespaces when querying non-existent namespace
1 parent 15f88b6 commit 7dcbf4b

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

pkg/skaffold/deploy/util/namespaces.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
2424
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
25+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
2526
)
2627

2728
// GetAllPodNamespaces lists the namespaces that should be watched.
@@ -49,7 +50,11 @@ func GetAllPodNamespaces(configNamespace string, pipelines []latestV1.Pipeline)
4950
}
5051

5152
// Set additional namespaces each helm release referenced
52-
for _, namespace := range collectHelmReleasesNamespaces(pipelines) {
53+
helmReleasesNamespaces, err := collectHelmReleasesNamespaces(pipelines)
54+
if err != nil {
55+
return nil, fmt.Errorf("collecting helm releases namespaces: %w", err)
56+
}
57+
for _, namespace := range helmReleasesNamespaces {
5358
nsMap[namespace] = true
5459
}
5560

@@ -63,16 +68,20 @@ func GetAllPodNamespaces(configNamespace string, pipelines []latestV1.Pipeline)
6368
return namespaces, nil
6469
}
6570

66-
func collectHelmReleasesNamespaces(pipelines []latestV1.Pipeline) []string {
71+
func collectHelmReleasesNamespaces(pipelines []latestV1.Pipeline) ([]string, error) {
6772
var namespaces []string
6873
for _, cfg := range pipelines {
6974
if cfg.Deploy.HelmDeploy != nil {
7075
for _, release := range cfg.Deploy.HelmDeploy.Releases {
7176
if release.Namespace != "" {
72-
namespaces = append(namespaces, release.Namespace)
77+
templatedNamespace, err := util.ExpandEnvTemplateOrFail(release.Namespace, nil)
78+
if err != nil {
79+
return []string{}, fmt.Errorf("cannot parse the release namespace template: %w", err)
80+
}
81+
namespaces = append(namespaces, templatedNamespace)
7382
}
7483
}
7584
}
7685
}
77-
return namespaces
86+
return namespaces, nil
7887
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright 2019 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"testing"
21+
22+
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
23+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
24+
"github.com/GoogleContainerTools/skaffold/testutil"
25+
)
26+
27+
func TestCollectHelmReleasesNamespaces(t *testing.T) {
28+
tests := []struct {
29+
description string
30+
helmReleases []latestV1.HelmRelease
31+
env []string
32+
expected []string
33+
shouldErr bool
34+
}{
35+
{
36+
description: "namspaces are collected correctly",
37+
helmReleases: []latestV1.HelmRelease{
38+
{
39+
Namespace: "foo",
40+
},
41+
{
42+
Namespace: "bar",
43+
},
44+
{
45+
Namespace: "baz",
46+
},
47+
},
48+
expected: []string{"foo", "bar", "baz"},
49+
},
50+
{
51+
description: "namespaces are collected correctly with env expansion",
52+
helmReleases: []latestV1.HelmRelease{
53+
{
54+
Namespace: "{{.FOO}}",
55+
},
56+
{
57+
Namespace: "bar",
58+
},
59+
{
60+
Namespace: "baz",
61+
},
62+
},
63+
env: []string{"FOO=foo"},
64+
expected: []string{"foo", "bar", "baz"},
65+
},
66+
{
67+
description: "should error when template expansion fails",
68+
helmReleases: []latestV1.HelmRelease{
69+
{
70+
Namespace: "{{.DOESNT_EXIST_AND_SHOULD_ERROR_AS_SUCH}}",
71+
},
72+
},
73+
shouldErr: true,
74+
},
75+
}
76+
for _, test := range tests {
77+
testutil.Run(t, test.description, func(t *testutil.T) {
78+
t.Override(&util.OSEnviron, func() []string { return test.env })
79+
ns, err := collectHelmReleasesNamespaces([]latestV1.Pipeline{
80+
{
81+
Deploy: latestV1.DeployConfig{
82+
DeployType: latestV1.DeployType{
83+
HelmDeploy: &latestV1.HelmDeploy{
84+
Releases: test.helmReleases,
85+
},
86+
},
87+
},
88+
},
89+
})
90+
t.CheckError(test.shouldErr, err)
91+
if !test.shouldErr {
92+
t.CheckDeepEqual(test.expected, ns)
93+
}
94+
})
95+
}
96+
}

0 commit comments

Comments
 (0)