Skip to content

Applied namespace to Cleanup and getArgs #3787

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

Merged
merged 3 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pkg/skaffold/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ func (h *HelmDeployer) Cleanup(ctx context.Context, out io.Writer) error {
args := []string{"delete", releaseName}
if hv.LT(helm3Version) {
args = append(args, "--purge")
} else if r.Namespace != "" {
args = append(args, "--namespace", r.Namespace)
}
if err := h.exec(ctx, out, false, args...); err != nil {
return errors.Wrapf(err, "deleting %s", releaseName)
Expand Down Expand Up @@ -255,7 +257,7 @@ func (h *HelmDeployer) deployRelease(ctx context.Context, out io.Writer, r lates
helmVersion: helmVersion,
}

if err := h.exec(ctx, ioutil.Discard, false, getArgs(helmVersion, releaseName)...); err != nil {
if err := h.exec(ctx, ioutil.Discard, false, getArgs(helmVersion, releaseName, r.Namespace)...); err != nil {
color.Yellow.Fprintf(out, "Helm release %s not installed. Installing...\n", releaseName)

opts.upgrade = false
Expand Down Expand Up @@ -312,7 +314,7 @@ func (h *HelmDeployer) deployRelease(ctx context.Context, out io.Writer, r lates
var b bytes.Buffer

// Be accepting of failure
if err := h.exec(ctx, &b, false, getArgs(helmVersion, releaseName)...); err != nil {
if err := h.exec(ctx, &b, false, getArgs(helmVersion, releaseName, r.Namespace)...); err != nil {
logrus.Warnf(err.Error())
return nil, nil
}
Expand Down Expand Up @@ -487,10 +489,13 @@ func installArgs(r latest.HelmRelease, builds []build.Artifact, valuesSet map[st
}

// getArgs calculates the correct arguments to "helm get"
func getArgs(v semver.Version, releaseName string) []string {
func getArgs(v semver.Version, releaseName string, namespace string) []string {
args := []string{"get"}
if v.GTE(helm3Version) {
args = append(args, "all")
if namespace != "" {
args = append(args, "--namespace", namespace)
}
}
return append(args, releaseName)
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/skaffold/deploy/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ var testDeployConfig = latest.HelmDeploy{
}},
}

var testDeployNamespacedConfig = latest.HelmDeploy{
Releases: []latest.HelmRelease{{
Name: "skaffold-helm",
ChartPath: "examples/test",
Values: map[string]string{
"image": "skaffold-helm",
},
Overrides: schemautil.HelmOverrides{Values: map[string]interface{}{"foo": "bar"}},
SetValues: map[string]string{
"some.key": "somevalue",
},
Namespace: "testNamespace",
}},
}

var testDeployConfigTemplated = latest.HelmDeploy{
Releases: []latest.HelmRelease{{
Name: "skaffold-helm",
Expand Down Expand Up @@ -376,6 +391,17 @@ func TestHelmDeploy(t *testing.T) {
runContext: makeRunContext(testDeployConfig, false),
builds: testBuilds,
},
{
description: "helm3.0 namespaced deploy success",
commands: testutil.
CmdRunWithOutput("helm version", version30).
AndRun("helm --kube-context kubecontext get all --namespace testNamespace skaffold-helm --kubeconfig kubeconfig").
AndRun("helm --kube-context kubecontext dep build examples/test --kubeconfig kubeconfig").
AndRun("helm --kube-context kubecontext upgrade skaffold-helm examples/test --namespace testNamespace -f skaffold-overrides.yaml --set-string image=docker.io:5000/skaffold-helm:3605e7bc17cf46e53f4d81c4cbc24e5b4c495184 --set some.key=somevalue --kubeconfig kubeconfig").
AndRun("helm --kube-context kubecontext get all --namespace testNamespace skaffold-helm --kubeconfig kubeconfig"),
runContext: makeRunContext(testDeployNamespacedConfig, false),
builds: testBuilds,
},
{
description: "helm3.1 deploy success",
commands: testutil.
Expand All @@ -387,6 +413,17 @@ func TestHelmDeploy(t *testing.T) {
runContext: makeRunContext(testDeployConfig, false),
builds: testBuilds,
},
{
description: "helm3.1 namespaced deploy success",
commands: testutil.
CmdRunWithOutput("helm version", version31).
AndRun("helm --kube-context kubecontext get all --namespace testNamespace skaffold-helm --kubeconfig kubeconfig").
AndRun("helm --kube-context kubecontext dep build examples/test --kubeconfig kubeconfig").
AndRun("helm --kube-context kubecontext upgrade skaffold-helm examples/test --namespace testNamespace -f skaffold-overrides.yaml --set-string image=docker.io:5000/skaffold-helm:3605e7bc17cf46e53f4d81c4cbc24e5b4c495184 --set some.key=somevalue --kubeconfig kubeconfig").
AndRun("helm --kube-context kubecontext get all --namespace testNamespace skaffold-helm --kubeconfig kubeconfig"),
runContext: makeRunContext(testDeployNamespacedConfig, false),
builds: testBuilds,
},
{
description: "helm3 unparseable version",
commands: testutil.CmdRunWithOutput("helm version", "gobbledygook"),
Expand Down Expand Up @@ -696,6 +733,14 @@ func TestHelmCleanup(t *testing.T) {
runContext: makeRunContext(testDeployConfig, false),
builds: testBuilds,
},
{
description: "helm3 namespace cleanup success",
commands: testutil.
CmdRunWithOutput("helm version", version31).
AndRun("helm --kube-context kubecontext delete skaffold-helm --namespace testNamespace --kubeconfig kubeconfig"),
runContext: makeRunContext(testDeployNamespacedConfig, false),
builds: testBuilds,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
Expand Down