Skip to content

Commit

Permalink
Add force-deploy option to helm deployer
Browse files Browse the repository at this point in the history
Signed-off-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com>
  • Loading branch information
corneliusweig committed Apr 1, 2019
1 parent f8ec8d9 commit 0c7f59c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
7 changes: 6 additions & 1 deletion pkg/skaffold/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ type HelmDeployer struct {
kubeContext string
namespace string
defaultRepo string
forceDeploy bool
}

// NewHelmDeployer returns a new HelmDeployer for a DeployConfig filled
// with the needed configuration for `helm`
func NewHelmDeployer(cfg *latest.HelmDeploy, kubeContext string, namespace string, defaultRepo string) *HelmDeployer {
func NewHelmDeployer(cfg *latest.HelmDeploy, kubeContext string, namespace string, forceDeploy bool, defaultRepo string) *HelmDeployer {
return &HelmDeployer{
HelmDeploy: cfg,
kubeContext: kubeContext,
namespace: namespace,
defaultRepo: defaultRepo,
forceDeploy: forceDeploy,
}
}

Expand Down Expand Up @@ -189,6 +191,9 @@ func (h *HelmDeployer) deployRelease(ctx context.Context, out io.Writer, r lates
} else {
args = append(args, "upgrade", releaseName)
args = append(args, h.Flags.Upgrade...)
if h.forceDeploy {
args = append(args, "--force")
}
if r.RecreatePods {
args = append(args, "--recreate-pods")
}
Expand Down
65 changes: 46 additions & 19 deletions pkg/skaffold/deploy/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,32 +284,32 @@ func TestHelmDeploy(t *testing.T) {
{
description: "deploy success",
cmd: &MockHelm{t: t},
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
},
{
description: "deploy success with recreatePods",
cmd: &MockHelm{t: t},
deployer: NewHelmDeployer(testDeployRecreatePodsConfig, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeployRecreatePodsConfig, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
},
{
description: "deploy success with skipBuildDependencies",
cmd: &MockHelm{t: t},
deployer: NewHelmDeployer(testDeploySkipBuildDependenciesConfig, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeploySkipBuildDependenciesConfig, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
},
{
description: "deploy error unmatched parameter",
cmd: &MockHelm{t: t},
deployer: NewHelmDeployer(testDeployConfigParameterUnmatched, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeployConfigParameterUnmatched, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
shouldErr: true,
},
{
description: "deploy success remote chart with skipBuildDependencies",
cmd: &MockHelm{t: t},
deployer: NewHelmDeployer(testDeploySkipBuildDependencies, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeploySkipBuildDependencies, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
},
{
Expand All @@ -318,7 +318,7 @@ func TestHelmDeploy(t *testing.T) {
t: t,
depResult: fmt.Errorf("unexpected error"),
},
deployer: NewHelmDeployer(testDeployRemoteChart, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeployRemoteChart, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
shouldErr: true,
},
Expand All @@ -328,17 +328,17 @@ func TestHelmDeploy(t *testing.T) {
t: t,
getResult: fmt.Errorf("not found"),
installMatcher: func(cmd *exec.Cmd) bool {
expected := map[string]bool{fmt.Sprintf("image=%s", testBuilds[0].Tag): true}
expected := fmt.Sprintf("image=%s", testBuilds[0].Tag)
for _, arg := range cmd.Args {
if expected[arg] {
if expected == arg {
return true
}
}
return false
},
upgradeResult: fmt.Errorf("should not have called upgrade"),
},
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
},
{
Expand All @@ -352,26 +352,51 @@ func TestHelmDeploy(t *testing.T) {
return false
}

expected := map[string]bool{fmt.Sprintf("image.repository=%s,image.tag=%s", dockerRef.BaseName, dockerRef.Tag): true}
expected := fmt.Sprintf("image.repository=%s,image.tag=%s", dockerRef.BaseName, dockerRef.Tag)
for _, arg := range cmd.Args {
if expected[arg] {
if expected == arg {
return true
}
}
return false
},
upgradeResult: fmt.Errorf("should not have called upgrade"),
},
deployer: NewHelmDeployer(testDeployHelmStyleConfig, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeployHelmStyleConfig, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
},
{
description: "get success should upgrade not install",
description: "get success should upgrade by force, not install",
cmd: &MockHelm{
t: t,
t: t,
upgradeMatcher: func(cmd *exec.Cmd) bool {
for _, arg := range cmd.Args {
if arg == "--force" {
return true
}
}
return false
},
installResult: fmt.Errorf("should not have called install"),
},
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, true, ""),
builds: testBuilds,
},
{
description: "get success should upgrade without force, not install",
cmd: &MockHelm{
t: t,
upgradeMatcher: func(cmd *exec.Cmd) bool {
for _, arg := range cmd.Args {
if arg == "--force" {
return false
}
}
return true
},
installResult: fmt.Errorf("should not have called install"),
},
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
},
{
Expand All @@ -381,7 +406,7 @@ func TestHelmDeploy(t *testing.T) {
upgradeResult: fmt.Errorf("unexpected error"),
},
shouldErr: true,
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
},
{
Expand All @@ -391,7 +416,7 @@ func TestHelmDeploy(t *testing.T) {
depResult: fmt.Errorf("unexpected error"),
},
shouldErr: true,
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
},
{
Expand All @@ -405,6 +430,7 @@ func TestHelmDeploy(t *testing.T) {
testDeployFooWithPackaged,
testKubeContext,
testNamespace,
false,
"",
),
builds: testBuildsFoo,
Expand All @@ -420,14 +446,15 @@ func TestHelmDeploy(t *testing.T) {
testDeployFooWithPackaged,
testKubeContext,
testNamespace,
false,
"",
),
builds: testBuildsFoo,
},
{
description: "deploy and get templated release name",
cmd: &MockHelm{t: t},
deployer: NewHelmDeployer(testDeployWithTemplatedName, testKubeContext, testNamespace, ""),
deployer: NewHelmDeployer(testDeployWithTemplatedName, testKubeContext, testNamespace, false, ""),
builds: testBuilds,
},
}
Expand Down Expand Up @@ -599,7 +626,7 @@ func TestHelmDependencies(t *testing.T) {
SetValues: map[string]string{"some.key": "somevalue"},
},
},
}, testKubeContext, testNamespace, "")
}, testKubeContext, testNamespace, false, "")

deps, err := deployer.Dependencies()

Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func getDeployer(cfg *latest.DeployConfig, kubeContext string, namespace string,

switch {
case cfg.HelmDeploy != nil:
return deploy.NewHelmDeployer(cfg.HelmDeploy, kubeContext, namespace, defaultRepo), nil
return deploy.NewHelmDeployer(cfg.HelmDeploy, kubeContext, namespace, forceDeploy, defaultRepo), nil

case cfg.KubectlDeploy != nil:
return deploy.NewKubectlDeployer(cwd, cfg.KubectlDeploy, kubeContext, namespace, forceDeploy, defaultRepo), nil
Expand Down

0 comments on commit 0c7f59c

Please sign in to comment.