Skip to content

Commit 61d49ed

Browse files
committed
feat: add better support and messaging around using helm with skaffold apply
1 parent 1dbc7a2 commit 61d49ed

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

pkg/skaffold/runner/deployer.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,45 @@ func (d *deployerCtx) JSONParseConfig() v1.JSONParseConfig {
6666

6767
// GetDeployer creates a deployer from a given RunContext and deploy pipeline definitions.
6868
func GetDeployer(ctx context.Context, runCtx *runcontext.RunContext, labeller *label.DefaultLabeller) (deploy.Deployer, error) {
69+
deployerCfg := runCtx.Deployers()
70+
6971
if runCtx.Opts.Apply {
72+
// helmNamespaceValue := ""
73+
helmNamespaces := make(map[string]bool)
74+
// helmReleasesWithDiffNamespacesFound := false
75+
nonHelmDeployFound := false
76+
77+
for _, d := range deployerCfg {
78+
if d.DockerDeploy != nil || d.KptDeploy != nil || d.KubectlDeploy != nil || d.KustomizeDeploy != nil {
79+
nonHelmDeployFound = true
80+
}
81+
82+
if d.HelmDeploy != nil {
83+
for _, release := range d.HelmDeploy.Releases {
84+
if release.Namespace != "" {
85+
helmNamespaces[release.Namespace] = true
86+
}
87+
}
88+
}
89+
}
90+
91+
if len(helmNamespaces) > 1 || (nonHelmDeployFound && len(helmNamespaces) == 1) {
92+
return nil, errors.New("skaffold apply called with conflicting namespaces set via skaffold.yaml. This is likely due to the use of the 'deploy.helm.releases.*.namespace' field which is not supported in apply. Remove the 'deploy.helm.releases.*.namespace' field(s) and run skaffold apply again")
93+
}
94+
95+
if len(helmNamespaces) == 1 && !nonHelmDeployFound {
96+
if runCtx.Opts.Namespace == "" {
97+
// if skaffold --namespace flag not set, use the helm namespace value
98+
for k := range helmNamespaces {
99+
// map only has 1 (k,v) from length check in if condition
100+
runCtx.Opts.Namespace = k
101+
}
102+
}
103+
}
104+
70105
return getDefaultDeployer(runCtx, labeller)
71106
}
72107

73-
deployerCfg := runCtx.Deployers()
74108
localDeploy := false
75109
remoteDeploy := false
76110

pkg/skaffold/runner/deployer_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,59 @@ func TestGetDeployer(tOuter *testing.T) {
133133
&kpt.Deployer{},
134134
}, false),
135135
},
136+
{
137+
description: "apply does not allow multiple deployers when a helm namespace is set",
138+
apply: true,
139+
cfg: latestV1.DeployType{
140+
HelmDeploy: &latestV1.HelmDeploy{
141+
Releases: []latestV1.HelmRelease{
142+
{
143+
Namespace: "foo",
144+
},
145+
},
146+
},
147+
KubectlDeploy: &latestV1.KubectlDeploy{},
148+
},
149+
shouldErr: true,
150+
},
151+
{
152+
description: "apply does not allow multiple helm releases with different namespaces set",
153+
apply: true,
154+
cfg: latestV1.DeployType{
155+
HelmDeploy: &latestV1.HelmDeploy{
156+
Releases: []latestV1.HelmRelease{
157+
{
158+
Namespace: "foo",
159+
},
160+
{
161+
Namespace: "bar",
162+
},
163+
},
164+
},
165+
},
166+
shouldErr: true,
167+
},
168+
{
169+
description: "apply does allow multiple helm releases with the same namespace set",
170+
apply: true,
171+
cfg: latestV1.DeployType{
172+
HelmDeploy: &latestV1.HelmDeploy{
173+
Releases: []latestV1.HelmRelease{
174+
{
175+
Namespace: "foo",
176+
},
177+
{
178+
Namespace: "foo",
179+
},
180+
},
181+
},
182+
},
183+
expected: t.RequireNonNilResult(kubectl.NewDeployer(&runcontext.RunContext{
184+
Pipelines: runcontext.NewPipelines([]latestV1.Pipeline{{}}),
185+
}, &label.DefaultLabeller{}, &latestV1.KubectlDeploy{
186+
Flags: latestV1.KubectlFlags{},
187+
})).(deploy.Deployer),
188+
},
136189
}
137190
for _, test := range tests {
138191
testutil.Run(tOuter, test.description, func(t *testutil.T) {

0 commit comments

Comments
 (0)