Skip to content

Commit

Permalink
reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
tuunit committed Dec 1, 2023
1 parent 3bb453b commit 2cb082b
Showing 1 changed file with 32 additions and 109 deletions.
141 changes: 32 additions & 109 deletions internal/compose/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand All @@ -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
}

0 comments on commit 2cb082b

Please sign in to comment.