From 14298a762562ccdf2da2e28ef3d214dc148eb86a Mon Sep 17 00:00:00 2001 From: tuunit Date: Fri, 1 Dec 2023 17:38:39 +0100 Subject: [PATCH] reduce code duplication --- internal/compose/helm.go | 141 +++++++++------------------------------ 1 file changed, 32 insertions(+), 109 deletions(-) diff --git a/internal/compose/helm.go b/internal/compose/helm.go index 047d82a..0f8e289 100644 --- a/internal/compose/helm.go +++ b/internal/compose/helm.go @@ -36,25 +36,33 @@ var ( minVersion = semver.MustParse("v3.0.0") ) +type HelmCommand string + +const ( + HelmInstall HelmCommand = "upgrade" + HelmUninstall HelmCommand = "uninstall" + HelmTemplate HelmCommand = "template" +) + func CompatibleHelmVersion() error { cmd := exec.Command(helm, "version") util.DebugPrint("Executing %s", strings.Join(cmd.Args, " ")) output, err := cmd.CombinedOutput() if err != nil { - return fmt.Errorf("Failed to run `%s version`: %v", os.Getenv("HELM_BIN"), err) + return fmt.Errorf("failed to run `%s version`: %v", os.Getenv("HELM_BIN"), err) } versionOutput := string(output) matches := versionRE.FindStringSubmatch(versionOutput) if matches == nil { - return fmt.Errorf("Failed to find version in output %#v", versionOutput) + return fmt.Errorf("failed to find version in output %#v", versionOutput) } helmVersion, err := semver.NewVersion(matches[1]) if err != nil { - return fmt.Errorf("Failed to parse version %#v: %v", matches[1], err) + return fmt.Errorf("failed to parse version %#v: %v", matches[1], err) } if minVersion.GreaterThan(helmVersion) { @@ -74,109 +82,12 @@ func addHelmRepository(name string, url string) error { } func installHelmRelease(name string, release *cfg.Release) { - var args []string - - args = append(args, "upgrade") - args = append(args, "--install") - - if release.ChartVersion != "" { - args = append(args, fmt.Sprintf("--version=%s", release.ChartVersion)) - } - - if release.Namespace != "" { - args = append(args, fmt.Sprintf("--namespace=%s", release.Namespace)) - } - - if release.ForceUpdate { - args = append(args, "--force") - } - - if release.HistoryMax < 0 { - args = append(args, fmt.Sprintf("--history-max=%d", 0)) - } else if release.HistoryMax > 0 { - args = append(args, fmt.Sprintf("--history-max=%d", release.HistoryMax)) - } - - if release.CreateNamespace { - args = append(args, "--create-namespace") - } - - if release.CleanUpOnFail { - args = append(args, "--cleanup-on-fail") - } - - if release.DependencyUpdate { - args = append(args, "--dependency-update") - } - - if release.SkipTLSVerify { - args = append(args, "--insecure-skip-tls-verify") - } - - if release.SkipCRDs { - args = append(args, "--skip-crds") - } - - if release.PostRenderer != "" { - args = append(args, fmt.Sprintf("--post-renderer=%s", release.PostRenderer)) - } - - if len(release.PostRendererArgs) > 0 { - args = append(args, fmt.Sprintf("--post-renderer-args=[%s]", strings.Join(release.PostRendererArgs, ","))) - } - - if release.CAFile != "" { - args = append(args, fmt.Sprintf("--ca-file=%s", release.CAFile)) - } - - if release.CertFile != "" { - args = append(args, fmt.Sprintf("--cert-file=%s", release.CertFile)) - } - - if release.KeyFile != "" { - args = append(args, fmt.Sprintf("--key-file=%s", release.KeyFile)) - } - - if release.Timeout != "" { - args = append(args, fmt.Sprintf("--timeout=%s", release.Timeout)) - } - - if release.Wait { - args = append(args, "--wait") - } - - if release.KubeConfig != "" { - args = append(args, fmt.Sprintf("--kubeconfig=%s", release.KubeConfig)) - } - - if release.KubeContext != "" { - args = append(args, fmt.Sprintf("--kube-context=%s", release.KubeContext)) - } - - for _, file := range release.ValueFiles { - args = append(args, fmt.Sprintf("--values=%s", file)) - } - - var jsonValues []string - for key := range release.Values { - data := util.ConvertJson(release.Values[key]) - values, err := json.Marshal(data) - if err != nil { - cp := util.NewColorPrinter(name) - cp.Printf("%s |\t\t%s", name, err) - return - } - - jsonValues = append(jsonValues, fmt.Sprintf("%s=%s", key, values)) - } - - if len(jsonValues) > 0 { - args = append(args, fmt.Sprintf("--set-json=%s", strings.Join(jsonValues, ","))) + args, err := createHelmArguments(HelmInstall, name, release) + if err != nil { + cp := util.NewColorPrinter(name) + cp.Printf("%s |\t\t%s", name, err) } - args = append(args, name) - args = append(args, release.Chart) - helmExec(name, args) } @@ -235,9 +146,23 @@ func helmExec(name string, args []string) { } func templateHelmRelease(name string, release *cfg.Release) { + args, err := createHelmArguments(HelmTemplate, name, release) + if err != nil { + cp := util.NewColorPrinter(name) + cp.Printf("%s |\t\t%s", name, err) + } + + helmExec(name, args) +} + +func createHelmArguments(command HelmCommand, name string, release *cfg.Release) ([]string, error) { var args []string - args = append(args, "template") + args = append(args, string(command)) + + if command == HelmInstall { + args = append(args, "--install") + } if release.ChartVersion != "" { args = append(args, fmt.Sprintf("--version=%s", release.ChartVersion)) @@ -322,9 +247,7 @@ func templateHelmRelease(name string, release *cfg.Release) { data := util.ConvertJson(release.Values[key]) values, err := json.Marshal(data) if err != nil { - cp := util.NewColorPrinter(name) - cp.Printf("%s |\t\t%s", name, err) - return + return nil, err } jsonValues = append(jsonValues, fmt.Sprintf("%s=%s", key, values)) @@ -337,5 +260,5 @@ func templateHelmRelease(name string, release *cfg.Release) { args = append(args, name) args = append(args, release.Chart) - helmExec(name, args) + return args, nil }