Skip to content

Commit 60ca26e

Browse files
author
priyawadhwa
authored
Merge pull request #2423 from dgageot/helm-namespaces
Watch namespaces for each Helm release
2 parents 71f5809 + aee0488 commit 60ca26e

File tree

4 files changed

+125
-5
lines changed

4 files changed

+125
-5
lines changed

pkg/skaffold/kubernetes/context/context.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import (
2424
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2525
)
2626

27+
// For testing
28+
var (
29+
CurrentConfig = getCurrentConfig
30+
)
31+
2732
var (
2833
currentConfigOnce sync.Once
2934
currentConfig clientcmdapi.Config
@@ -35,7 +40,7 @@ func ResetCurrentConfig() {
3540
currentConfigOnce = sync.Once{}
3641
}
3742

38-
func CurrentConfig() (clientcmdapi.Config, error) {
43+
func getCurrentConfig() (clientcmdapi.Config, error) {
3944
currentConfigOnce.Do(func() {
4045
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
4146
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})

pkg/skaffold/runner/context/context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func GetRunContext(opts *config.SkaffoldOptions, cfg *latest.Pipeline) (*RunCont
5353
return nil, errors.Wrap(err, "finding current directory")
5454
}
5555

56-
namespaces, err := runnerutil.GetAllPodNamespaces(opts.Namespace)
56+
namespaces, err := runnerutil.GetAllPodNamespaces(opts.Namespace, cfg)
5757
if err != nil {
5858
return nil, errors.Wrap(err, "getting namespace list")
5959
}

pkg/skaffold/runner/util/util.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@ limitations under the License.
1717
package util
1818

1919
import (
20+
"sort"
21+
2022
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
23+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
2124
"github.com/pkg/errors"
2225
)
2326

24-
func GetAllPodNamespaces(configNamespace string) ([]string, error) {
25-
// We also get the default namespace.
27+
// GetAllPodNamespaces lists the namespaces that should be watched.
28+
// + The namespace passed on the command line
29+
// + Current kube context's namespace
30+
// + Namespaces referenced in Helm releases
31+
func GetAllPodNamespaces(configNamespace string, cfg *latest.Pipeline) ([]string, error) {
2632
nsMap := make(map[string]bool)
33+
2734
if configNamespace == "" {
35+
// Get current kube context's namespace
2836
config, err := kubectx.CurrentConfig()
2937
if err != nil {
3038
return nil, errors.Wrap(err, "getting k8s configuration")
3139
}
40+
3241
context, ok := config.Contexts[config.CurrentContext]
3342
if ok {
3443
nsMap[context.Namespace] = true
@@ -39,12 +48,29 @@ func GetAllPodNamespaces(configNamespace string) ([]string, error) {
3948
nsMap[configNamespace] = true
4049
}
4150

42-
// FIXME: Set additional namespaces from the selected yamls.
51+
// Set additional namespaces each helm release referenced
52+
for _, namespace := range collectHelmReleasesNamespaces(cfg) {
53+
nsMap[namespace] = true
54+
}
4355

4456
// Collate the slice of namespaces.
4557
namespaces := make([]string, 0, len(nsMap))
4658
for ns := range nsMap {
4759
namespaces = append(namespaces, ns)
4860
}
61+
62+
sort.Strings(namespaces)
4963
return namespaces, nil
5064
}
65+
66+
func collectHelmReleasesNamespaces(cfg *latest.Pipeline) []string {
67+
var namespaces []string
68+
69+
if cfg != nil && cfg.Deploy.HelmDeploy != nil {
70+
for _, release := range cfg.Deploy.HelmDeploy.Releases {
71+
namespaces = append(namespaces, release.Namespace)
72+
}
73+
}
74+
75+
return namespaces
76+
}

pkg/skaffold/runner/util/util_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
23+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
24+
"github.com/GoogleContainerTools/skaffold/testutil"
25+
"k8s.io/client-go/tools/clientcmd/api"
26+
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
27+
)
28+
29+
func TestGetAllPodNamespaces(t *testing.T) {
30+
var tests = []struct {
31+
description string
32+
argNamespace string
33+
currentContext string
34+
cfg *latest.Pipeline
35+
expected []string
36+
}{
37+
{
38+
description: "namespace provided on the command line",
39+
argNamespace: "ns",
40+
expected: []string{"ns"},
41+
},
42+
{
43+
description: "kube context's namespace",
44+
currentContext: "prod-context",
45+
expected: []string{"prod"},
46+
},
47+
{
48+
description: "default namespace",
49+
currentContext: "unknown context",
50+
expected: []string{""},
51+
},
52+
{
53+
description: "add namespaces for helm",
54+
argNamespace: "ns",
55+
cfg: &latest.Pipeline{
56+
Deploy: latest.DeployConfig{
57+
DeployType: latest.DeployType{
58+
HelmDeploy: &latest.HelmDeploy{
59+
Releases: []latest.HelmRelease{
60+
{Namespace: "ns3"},
61+
{Namespace: ""},
62+
{Namespace: ""},
63+
{Namespace: "ns2"},
64+
},
65+
},
66+
},
67+
},
68+
},
69+
expected: []string{"", "ns", "ns2", "ns3"},
70+
},
71+
}
72+
for _, test := range tests {
73+
testutil.Run(t, "", func(t *testutil.T) {
74+
t.Override(&context.CurrentConfig, func() (clientcmdapi.Config, error) {
75+
return api.Config{
76+
CurrentContext: test.currentContext,
77+
Contexts: map[string]*api.Context{
78+
"prod-context": {Namespace: "prod"},
79+
},
80+
}, nil
81+
})
82+
83+
namespaces, err := GetAllPodNamespaces(test.argNamespace, test.cfg)
84+
85+
t.CheckNoError(err)
86+
t.CheckDeepEqual(test.expected, namespaces)
87+
})
88+
}
89+
}

0 commit comments

Comments
 (0)