Skip to content

Commit 3f337c9

Browse files
committed
Support configuring default namespace for kubectl/kustomize deployers
1 parent d18604e commit 3f337c9

File tree

7 files changed

+93
-16
lines changed

7 files changed

+93
-16
lines changed

pkg/skaffold/deploy/helm_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ var testTwoReleases = latest.HelmDeploy{
261261
}
262262

263263
var testNamespace = "testNamespace"
264+
var testNamespace2 = "testNamespace2"
264265

265266
var validDeployYaml = `
266267
# Source: skaffold-helm/templates/deployment.yaml

pkg/skaffold/deploy/kubectl.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ type KubectlDeployer struct {
5959
// NewKubectlDeployer returns a new KubectlDeployer for a DeployConfig filled
6060
// with the needed configuration for `kubectl apply`
6161
func NewKubectlDeployer(runCtx *runcontext.RunContext) *KubectlDeployer {
62+
defaultNamespace := ""
63+
if runCtx.Cfg.Deploy.KubectlDeploy.DefaultNamespace != nil {
64+
defaultNamespace = *runCtx.Cfg.Deploy.KubectlDeploy.DefaultNamespace
65+
}
66+
6267
return &KubectlDeployer{
6368
KubectlDeploy: runCtx.Cfg.Deploy.KubectlDeploy,
6469
workingDir: runCtx.WorkingDir,
6570
globalConfig: runCtx.Opts.GlobalConfig,
6671
defaultRepo: runCtx.Opts.DefaultRepo.Value(),
6772
kubectl: deploy.CLI{
68-
CLI: kubectl.NewFromRunContext(runCtx),
73+
CLI: kubectl.NewFromRunContextAndDefaultNamespace(runCtx, defaultNamespace),
6974
Flags: runCtx.Cfg.Deploy.KubectlDeploy.Flags,
7075
ForceDeploy: runCtx.Opts.Force,
7176
},

pkg/skaffold/deploy/kubectl_test.go

+29-7
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ spec:
6161

6262
func TestKubectlDeploy(t *testing.T) {
6363
tests := []struct {
64-
description string
65-
cfg *latest.KubectlDeploy
66-
builds []build.Artifact
67-
commands util.Command
68-
shouldErr bool
69-
forceDeploy bool
64+
description string
65+
cfg *latest.KubectlDeploy
66+
builds []build.Artifact
67+
commands util.Command
68+
shouldErr bool
69+
forceDeploy bool
70+
skipSkaffoldNamespaceOption bool
7071
}{
7172
{
7273
description: "no manifest",
@@ -133,6 +134,22 @@ func TestKubectlDeploy(t *testing.T) {
133134
Tag: "leeroy-web:123",
134135
}},
135136
},
137+
{
138+
description: "deploy success (default namespace)",
139+
cfg: &latest.KubectlDeploy{
140+
Manifests: []string{"deployment.yaml"},
141+
DefaultNamespace: &testNamespace2,
142+
},
143+
commands: testutil.
144+
CmdRunOut("kubectl version --client -ojson", kubectlVersion118).
145+
AndRunOut("kubectl --context kubecontext --namespace testNamespace2 create --dry-run=client -oyaml -f deployment.yaml", deploymentWebYAML).
146+
AndRun("kubectl --context kubecontext --namespace testNamespace2 apply -f -"),
147+
builds: []build.Artifact{{
148+
ImageName: "leeroy-web",
149+
Tag: "leeroy-web:123",
150+
}},
151+
skipSkaffoldNamespaceOption: true,
152+
},
136153
{
137154
description: "http manifest",
138155
cfg: &latest.KubectlDeploy{
@@ -191,6 +208,11 @@ func TestKubectlDeploy(t *testing.T) {
191208
Touch("empty.ignored").
192209
Chdir()
193210

211+
skaffoldNamespaceOption := ""
212+
if !test.skipSkaffoldNamespaceOption {
213+
skaffoldNamespaceOption = testNamespace
214+
}
215+
194216
k := NewKubectlDeployer(&runcontext.RunContext{
195217
WorkingDir: ".",
196218
Cfg: latest.Pipeline{
@@ -202,7 +224,7 @@ func TestKubectlDeploy(t *testing.T) {
202224
},
203225
KubeContext: testKubeContext,
204226
Opts: config.SkaffoldOptions{
205-
Namespace: testNamespace,
227+
Namespace: skaffoldNamespaceOption,
206228
Force: test.forceDeploy,
207229
},
208230
})

pkg/skaffold/deploy/kustomize.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ type KustomizeDeployer struct {
9696
}
9797

9898
func NewKustomizeDeployer(runCtx *runcontext.RunContext) *KustomizeDeployer {
99+
defaultNamespace := ""
100+
if runCtx.Cfg.Deploy.KustomizeDeploy.DefaultNamespace != nil {
101+
defaultNamespace = *runCtx.Cfg.Deploy.KustomizeDeploy.DefaultNamespace
102+
}
103+
99104
return &KustomizeDeployer{
100105
KustomizeDeploy: runCtx.Cfg.Deploy.KustomizeDeploy,
101106
kubectl: deploy.CLI{
102-
CLI: kubectl.NewFromRunContext(runCtx),
107+
CLI: kubectl.NewFromRunContextAndDefaultNamespace(runCtx, defaultNamespace),
103108
Flags: runCtx.Cfg.Deploy.KustomizeDeploy.Flags,
104109
ForceDeploy: runCtx.Opts.Force,
105110
},

pkg/skaffold/deploy/kustomize_test.go

+30-7
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ import (
3434

3535
func TestKustomizeDeploy(t *testing.T) {
3636
tests := []struct {
37-
description string
38-
cfg *latest.KustomizeDeploy
39-
builds []build.Artifact
40-
commands util.Command
41-
shouldErr bool
42-
forceDeploy bool
37+
description string
38+
cfg *latest.KustomizeDeploy
39+
builds []build.Artifact
40+
commands util.Command
41+
shouldErr bool
42+
forceDeploy bool
43+
skipSkaffoldNamespaceOption bool
4344
}{
4445
{
4546
description: "no manifest",
@@ -65,6 +66,23 @@ func TestKustomizeDeploy(t *testing.T) {
6566
}},
6667
forceDeploy: true,
6768
},
69+
{
70+
description: "deploy success (default namespace)",
71+
cfg: &latest.KustomizeDeploy{
72+
KustomizePaths: []string{"."},
73+
DefaultNamespace: &testNamespace2,
74+
},
75+
commands: testutil.
76+
CmdRunOut("kubectl version --client -ojson", kubectlVersion112).
77+
AndRunOut("kustomize build .", deploymentWebYAML).
78+
AndRun("kubectl --context kubecontext --namespace testNamespace2 apply -f - --force --grace-period=0"),
79+
builds: []build.Artifact{{
80+
ImageName: "leeroy-web",
81+
Tag: "leeroy-web:123",
82+
}},
83+
forceDeploy: true,
84+
skipSkaffoldNamespaceOption: true,
85+
},
6886
{
6987
description: "deploy success with multiple kustomizations",
7088
cfg: &latest.KustomizeDeploy{
@@ -94,6 +112,11 @@ func TestKustomizeDeploy(t *testing.T) {
94112
t.NewTempDir().
95113
Chdir()
96114

115+
skaffoldNamespaceOption := ""
116+
if !test.skipSkaffoldNamespaceOption {
117+
skaffoldNamespaceOption = testNamespace
118+
}
119+
97120
k := NewKustomizeDeployer(&runcontext.RunContext{
98121
WorkingDir: ".",
99122
Cfg: latest.Pipeline{
@@ -105,7 +128,7 @@ func TestKustomizeDeploy(t *testing.T) {
105128
},
106129
KubeContext: testKubeContext,
107130
Opts: config.SkaffoldOptions{
108-
Namespace: testNamespace,
131+
Namespace: skaffoldNamespaceOption,
109132
Force: test.forceDeploy,
110133
},
111134
})

pkg/skaffold/kubectl/cli.go

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ func NewFromRunContext(runCtx *runcontext.RunContext) *CLI {
4444
}
4545
}
4646

47+
// NewFromRunContextAndDefaultNamespace creates a new kubectl CLI whereby the namespace from
48+
// command line / environment variable takes precedence over "default namespace" defined in
49+
// configuration
50+
func NewFromRunContextAndDefaultNamespace(runCtx *runcontext.RunContext, defaultNamespace string) *CLI {
51+
ns := defaultNamespace
52+
if runCtx.Opts.Namespace != "" {
53+
ns = runCtx.Opts.Namespace
54+
}
55+
return &CLI{
56+
KubeContext: runCtx.KubeContext,
57+
KubeConfig: runCtx.Opts.KubeConfig,
58+
Namespace: ns,
59+
}
60+
}
61+
4762
// Command creates the underlying exec.CommandContext. This allows low-level control of the executed command.
4863
func (c *CLI) Command(ctx context.Context, command string, arg ...string) *exec.Cmd {
4964
args := c.args(command, "", arg...)

pkg/skaffold/schema/latest/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ type KubectlDeploy struct {
438438

439439
// Flags are additional flags passed to `kubectl`.
440440
Flags KubectlFlags `yaml:"flags,omitempty"`
441+
442+
// DefaultNamespace is the default namespace passed to kubectl on deployment if no other override is given.
443+
DefaultNamespace *string `yaml:"defaultNamespace,omitempty"`
441444
}
442445

443446
// KubectlFlags are additional flags passed on the command
@@ -492,6 +495,9 @@ type KustomizeDeploy struct {
492495

493496
// BuildArgs are additional args passed to `kustomize build`.
494497
BuildArgs []string `yaml:"buildArgs,omitempty"`
498+
499+
// DefaultNamespace is the default namespace passed to kubectl on deployment if no other override is given.
500+
DefaultNamespace *string `yaml:"defaultNamespace,omitempty"`
495501
}
496502

497503
// HelmRelease describes a helm release to be deployed.

0 commit comments

Comments
 (0)