Skip to content

Commit

Permalink
Use helm upgrade --install for improved idempotency.
Browse files Browse the repository at this point in the history
This will install the chart if it doesn't exist, and (if necessary) upgrade it
if it does.
  • Loading branch information
Eric Wollesen committed Feb 22, 2023
1 parent 9418b06 commit 1f31ba1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pkg/executables/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (h *Helm) SaveChart(ctx context.Context, ociURI, version, folder string) er
}

func (h *Helm) InstallChartFromName(ctx context.Context, ociURI, kubeConfig, name, version string) error {
params := []string{"install", name, ociURI, "--version", version, "--kubeconfig", kubeConfig}
params := []string{"upgade", "--install", name, ociURI, "--version", version, "--kubeconfig", kubeConfig}
params = h.addInsecureFlagIfProvided(params)
_, err := h.executable.Command(ctx, params...).
WithEnvVars(h.env).Run()
Expand All @@ -126,7 +126,7 @@ func (h *Helm) InstallChartFromName(ctx context.Context, ociURI, kubeConfig, nam
// InstallChart installs a helm chart to the target cluster.
func (h *Helm) InstallChart(ctx context.Context, chart, ociURI, version, kubeconfigFilePath, namespace, valueFilePath string, values []string) error {
valueArgs := GetHelmValueArgs(values)
params := []string{"install", chart, ociURI, "--version", version}
params := []string{"upgrade", "--install", chart, ociURI, "--version", version}
params = append(params, valueArgs...)
params = append(params, "--kubeconfig", kubeconfigFilePath)
if len(namespace) > 0 {
Expand All @@ -145,7 +145,7 @@ func (h *Helm) InstallChart(ctx context.Context, chart, ociURI, version, kubecon
// InstallChartWithValuesFile installs a helm chart with the provided values file and waits for the chart deployment to be ready
// The default timeout for the chart to reach ready state is 5m.
func (h *Helm) InstallChartWithValuesFile(ctx context.Context, chart, ociURI, version, kubeconfigFilePath, valuesFilePath string) error {
params := []string{"install", chart, ociURI, "--version", version, "--values", valuesFilePath, "--kubeconfig", kubeconfigFilePath, "--wait"}
params := []string{"upgrade", "--install", chart, ociURI, "--version", version, "--values", valuesFilePath, "--kubeconfig", kubeconfigFilePath, "--wait"}
params = h.addInsecureFlagIfProvided(params)
_, err := h.executable.Command(ctx, params...).WithEnvVars(h.env).Run()
return err
Expand Down
12 changes: 6 additions & 6 deletions pkg/executables/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestHelmInstallChartSuccess(t *testing.T) {
kubeconfig := "/root/.kube/config"
values := []string{"key1=value1"}
expectCommand(
tt.e, tt.ctx, "install", chart, url, "--version", version, "--set", "key1=value1", "--kubeconfig", kubeconfig, "--create-namespace", "--namespace", "eksa-packages",
tt.e, tt.ctx, "upgrade", "--install", chart, url, "--version", version, "--set", "key1=value1", "--kubeconfig", kubeconfig, "--create-namespace", "--namespace", "eksa-packages",
).withEnvVars(tt.envVars).to().Return(bytes.Buffer{}, nil)

tt.Expect(tt.h.InstallChart(tt.ctx, chart, url, version, kubeconfig, "eksa-packages", "", values)).To(Succeed())
Expand All @@ -163,7 +163,7 @@ func TestHelmInstallChartSuccessWithValuesFile(t *testing.T) {
values := []string{"key1=value1"}
valuesFileName := "values.yaml"
expectCommand(
tt.e, tt.ctx, "install", chart, url, "--version", version, "--set", "key1=value1", "--kubeconfig", kubeconfig, "--create-namespace", "--namespace", "eksa-packages", "-f", valuesFileName,
tt.e, tt.ctx, "upgrade", "--install", chart, url, "--version", version, "--set", "key1=value1", "--kubeconfig", kubeconfig, "--create-namespace", "--namespace", "eksa-packages", "-f", valuesFileName,
).withEnvVars(tt.envVars).to().Return(bytes.Buffer{}, nil)

tt.Expect(tt.h.InstallChart(tt.ctx, chart, url, version, kubeconfig, "eksa-packages", valuesFileName, values)).To(Succeed())
Expand All @@ -177,7 +177,7 @@ func TestHelmInstallChartSuccessWithInsecure(t *testing.T) {
kubeconfig := "/root/.kube/config"
values := []string{"key1=value1"}
expectCommand(
tt.e, tt.ctx, "install", chart, url, "--version", version, "--set", "key1=value1", "--kubeconfig", kubeconfig, "--create-namespace", "--namespace", "eksa-packages", "--insecure-skip-tls-verify",
tt.e, tt.ctx, "upgrade", "--install", chart, url, "--version", version, "--set", "key1=value1", "--kubeconfig", kubeconfig, "--create-namespace", "--namespace", "eksa-packages", "--insecure-skip-tls-verify",
).withEnvVars(tt.envVars).to().Return(bytes.Buffer{}, nil)

tt.Expect(tt.h.InstallChart(tt.ctx, chart, url, version, kubeconfig, "eksa-packages", "", values)).To(Succeed())
Expand All @@ -192,7 +192,7 @@ func TestHelmInstallChartSuccessWithInsecureAndValuesFile(t *testing.T) {
values := []string{"key1=value1"}
valuesFileName := "values.yaml"
expectCommand(
tt.e, tt.ctx, "install", chart, url, "--version", version, "--set", "key1=value1", "--kubeconfig", kubeconfig, "--create-namespace", "--namespace", "eksa-packages", "-f", valuesFileName, "--insecure-skip-tls-verify",
tt.e, tt.ctx, "upgrade", "--install", chart, url, "--version", version, "--set", "key1=value1", "--kubeconfig", kubeconfig, "--create-namespace", "--namespace", "eksa-packages", "-f", valuesFileName, "--insecure-skip-tls-verify",
).withEnvVars(tt.envVars).to().Return(bytes.Buffer{}, nil)

tt.Expect(tt.h.InstallChart(tt.ctx, chart, url, version, kubeconfig, "eksa-packages", valuesFileName, values)).To(Succeed())
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestHelmInstallChartWithValuesFileSuccess(t *testing.T) {
kubeconfig := "/root/.kube/config"
valuesFileName := "values.yaml"
expectCommand(
tt.e, tt.ctx, "install", chart, url, "--version", version, "--values", valuesFileName, "--kubeconfig", kubeconfig, "--wait",
tt.e, tt.ctx, "upgrade", "--install", chart, url, "--version", version, "--values", valuesFileName, "--kubeconfig", kubeconfig, "--wait",
).withEnvVars(tt.envVars).to().Return(bytes.Buffer{}, nil)

tt.Expect(tt.h.InstallChartWithValuesFile(tt.ctx, chart, url, version, kubeconfig, valuesFileName)).To(Succeed())
Expand All @@ -246,7 +246,7 @@ func TestHelmInstallChartWithValuesFileSuccessWithInsecure(t *testing.T) {
kubeconfig := "/root/.kube/config"
valuesFileName := "values.yaml"
expectCommand(
tt.e, tt.ctx, "install", chart, url, "--version", version, "--values", valuesFileName, "--kubeconfig", kubeconfig, "--wait", "--insecure-skip-tls-verify",
tt.e, tt.ctx, "upgrade", "--install", chart, url, "--version", version, "--values", valuesFileName, "--kubeconfig", kubeconfig, "--wait", "--insecure-skip-tls-verify",
).withEnvVars(tt.envVars).to().Return(bytes.Buffer{}, nil)

tt.Expect(tt.h.InstallChartWithValuesFile(tt.ctx, chart, url, version, kubeconfig, valuesFileName)).To(Succeed())
Expand Down

0 comments on commit 1f31ba1

Please sign in to comment.