From 48a415191e5d8dcf794c2316e2d49f3cfa970253 Mon Sep 17 00:00:00 2001 From: Andrej Petras Date: Tue, 12 Dec 2023 17:53:42 +0100 Subject: [PATCH] feat: update chart lock, switch to helm dependency build command (#16) --- cmd/helm.go | 1 + cmd/helm_build.go | 4 ++- cmd/helm_lock_update.go | 54 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 cmd/helm_lock_update.go diff --git a/cmd/helm.go b/cmd/helm.go index 6ad8960..f9e5a2d 100644 --- a/cmd/helm.go +++ b/cmd/helm.go @@ -54,6 +54,7 @@ func createHelmCmd() *cobra.Command { addChildCmd(cmd, createHelmReleaseCmd()) addChildCmd(cmd, createHelmDepsValidateCmd()) addChildCmd(cmd, createHelmDepsUpdateCmd()) + addChildCmd(cmd, createHelmLockUpdateCmd()) return cmd } diff --git a/cmd/helm_build.go b/cmd/helm_build.go index 04f5b3e..f0faf16 100644 --- a/cmd/helm_build.go +++ b/cmd/helm_build.go @@ -12,6 +12,7 @@ import ( type helmBuildFlags struct { Helm helmFlags `mapstructure:",squash"` Source string `mapstructure:"helm-source-dir"` + DepsCmd string `mapstructure:"helm-deps-cmd"` Copy bool `mapstructure:"helm-source-copy"` ChartFilterTemplate string `mapstructure:"helm-chart-template-list"` ValuesFilterTemplate string `mapstructure:"helm-values-template-list"` @@ -32,6 +33,7 @@ func createHelmBuildCmd() *cobra.Command { } addStringFlag(cmd, "helm-source-dir", "", "", "project helm chart source directory") + addStringFlag(cmd, "helm-deps-cmd", "", "build", "helm dependency command.") addBoolFlag(cmd, "helm-source-copy", "", false, "copy helm source to helm directory") addStringFlag(cmd, "helm-chart-template-list", "", "version={{ .Version }},appVersion={{ .Version }},name={{ .Name }}", `list of key value to be replaced in the Chart.yaml Values: `+templateValues+` @@ -56,7 +58,7 @@ func helmBuild(project *Project, flags helmBuildFlags) { updateHelmValues(project, flags.Helm, flags.ValuesFilterTemplate) // update helm dependencies - tools.ExecCmd("helm", "dependency", "update", helmDir(project, flags.Helm)) + tools.ExecCmd("helm", "dependency", flags.DepsCmd, helmDir(project, flags.Helm)) // package helm chart helmPackage(project, flags.Helm) diff --git a/cmd/helm_lock_update.go b/cmd/helm_lock_update.go new file mode 100644 index 0000000..2d95dd9 --- /dev/null +++ b/cmd/helm_lock_update.go @@ -0,0 +1,54 @@ +package cmd + +import ( + "github.com/lorislab/samo/log" + "github.com/lorislab/samo/tools" + "github.com/spf13/cobra" + "os" +) + +type helmLockUpdateFlags struct { + Helm helmFlags `mapstructure:",squash"` + KeepCharts bool `mapstructure:"helm-keep-charts"` +} + +func createHelmLockUpdateCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "lock-update", + Short: "Update Chart.lock file", + Long: `Update the Helm chart Chart.lock or create a new one if it doesn't exist`, + Run: func(cmd *cobra.Command, args []string) { + flags := helmLockUpdateFlags{} + readOptions(&flags) + project := loadProject(flags.Helm.Project) + helmLockUpdate(project, flags) + }, + TraverseChildren: true, + } + + addBoolFlag(cmd, "helm-clean-charts", "", false, "keep chart directory after Chart.lock update") + return cmd +} + +func helmLockUpdate(project *Project, flags helmLockUpdateFlags) { + + dir := helmDir(project, flags.Helm) + + // update helm Chart.lock + tools.ExecCmd("helm", "dependency", "update", dir) + + // keep chart directory? + if flags.KeepCharts { + return + } + + charts := dir + "/charts" + if _, err := os.Stat(charts); !os.IsNotExist(err) { + log.Debug("Clean helm dependencies directory", log.F("dir", charts)) + err := os.RemoveAll(charts) + if err != nil { + log.Panic("error delete directory", log.F("output", charts).E(err)) + } + } + +}