diff --git a/cmd/template.go b/cmd/template.go new file mode 100644 index 0000000..95ac43a --- /dev/null +++ b/cmd/template.go @@ -0,0 +1,46 @@ +/* +Copyright © 2023 The Helm Compose Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "github.com/seacrew/helm-compose/internal/compose" + "github.com/seacrew/helm-compose/internal/config" + "github.com/spf13/cobra" +) + +var templateCmd = &cobra.Command{ + Use: "template", + Short: "Render templates for all releases locally and display the output.", + Long: ``, + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + + if err := compose.CompatibleHelmVersion(); err != nil { + return err + } + + config, err := config.ParseComposeFile(composeFile) + if err != nil { + return err + } + + return compose.Template(config) + }, +} + +func init() { + rootCmd.AddCommand(templateCmd) +} diff --git a/internal/compose/compose.go b/internal/compose/compose.go index 498d081..d3c96c4 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -126,3 +126,17 @@ func GetRevision(rev int, config *cfg.Config) error { return nil } + +func Template(config *cfg.Config) error { + for name, url := range config.Repositories { + if err := addHelmRepository(name, url); err != nil { + return err + } + } + + for name, release := range config.Releases { + templateHelmRelease(name, &release) + } + + return nil +} diff --git a/internal/compose/helm.go b/internal/compose/helm.go index 2cad205..047d82a 100644 --- a/internal/compose/helm.go +++ b/internal/compose/helm.go @@ -233,3 +233,109 @@ func helmExec(name string, args []string) { cp.Printf(err.Error()) } } + +func templateHelmRelease(name string, release *cfg.Release) { + var args []string + + args = append(args, "template") + + 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 = append(args, name) + args = append(args, release.Chart) + + helmExec(name, args) +} diff --git a/plugin.yaml b/plugin.yaml index 0b06294..45cbbad 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -1,5 +1,5 @@ name: compose -version: 1.2.0 +version: 1.3.0 usage: Compose is a helm plugin to define and manage multiple helm releases as single entity. command: $HELM_PLUGIN_DIR/bin/compose hooks: