From 7da5efd6d1dd8f09584267935c08bc677360e1f2 Mon Sep 17 00:00:00 2001 From: moririnson Date: Sun, 12 Nov 2023 16:03:32 +0900 Subject: [PATCH] feat: use helmBin() for kustomize build for helmChart in kustomization.yaml (#64) * feat: test for kustomize build using helmCharts Signed-off-by: moririnson * feat: use helmBin() for kustomize build for helmChart in kustomization.yaml This adds support --helm-binary for helmChart in kustomization.yaml by: - Changing use helmBin() for kustomize build Resolves https://github.com/helmfile/helmfile/issues/1121 Signed-off-by: moririnson * feat: u.HelmBinary empty check for kustomize build Signed-off-by: moririnson --------- Signed-off-by: moririnson --- .gitignore | 3 + chartify.go | 5 +- chartify_test.go | 55 +++++++++++++++++++ kustomize.go | 7 ++- .../testcases/kustomize_with_helm_charts/want | 25 +++++++++ .../kustomization.yaml | 6 ++ 6 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 testdata/integration/testcases/kustomize_with_helm_charts/want create mode 100644 testdata/kustomize_with_helm_charts/kustomization.yaml diff --git a/.gitignore b/.gitignore index f166652..a3b1a14 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ *~ .idea + +# testdata +testdata/kustomize_with_helm_charts/charts/ diff --git a/chartify.go b/chartify.go index 8092feb..55e45e3 100644 --- a/chartify.go +++ b/chartify.go @@ -251,14 +251,15 @@ func (r *Runner) Chartify(release, dirOrChart string, opts ...ChartifyOption) (s generatedManifestsUnderTemplatesDir := []string{} if isKustomization { - kustomOpts := &KustomizeBuildOpts{ + kustomizeOpts := &KustomizeBuildOpts{ ValuesFiles: u.ValuesFiles, SetValues: u.SetValues, SetFlags: u.SetFlags, EnableAlphaPlugins: u.EnableKustomizeAlphaPlugins, Namespace: u.Namespace, + HelmBinary: r.helmBin(), } - kustomizeFile, err := r.KustomizeBuild(dirOrChart, tempDir, kustomOpts) + kustomizeFile, err := r.KustomizeBuild(dirOrChart, tempDir, kustomizeOpts) if err != nil { return "", err } diff --git a/chartify_test.go b/chartify_test.go index c0742b6..4516449 100644 --- a/chartify_test.go +++ b/chartify_test.go @@ -1,7 +1,9 @@ package chartify import ( + "context" "os" + "os/exec" "testing" "github.com/google/go-cmp/cmp" @@ -111,3 +113,56 @@ func TestReadAdhocDependencies(t *testing.T) { }, }) } + +func TestUseHelmChartsInKustomize(t *testing.T) { + repo := "myrepo" + startServer(t, repo) + + r := New(UseHelm3(true), HelmBin(helm)) + + tests := []struct { + name string + opts ChartifyOpts + }{ + { + name: "--enable_alpha_plugins is ON", + opts: ChartifyOpts{ + EnableKustomizeAlphaPlugins: true, + }, + }, + { + name: "--enable_alpha_plugins is OFF", + opts: ChartifyOpts{ + EnableKustomizeAlphaPlugins: false, + }, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Helper() + + release := "myapp" + tmpDir, err := r.Chartify(release, "./testdata/kustomize_with_helm_charts", &tc.opts) + t.Cleanup(func() { + if err := os.RemoveAll(tmpDir); err != nil { + panic("unable to remove chartify tmpDir: " + err.Error()) + } + }) + require.NoError(t, err) + + ctx := context.Background() + args := []string{"template", release, tmpDir} + cmd := exec.CommandContext(ctx, helm, args...) + out, err := cmd.CombinedOutput() + require.NoError(t, err) + got := string(out) + + snapshotFile := "./testdata/integration/testcases/kustomize_with_helm_charts/want" + snapshot, err := os.ReadFile(snapshotFile) + require.NoError(t, err, "reading snapshot %s", snapshotFile) + + want := string(snapshot) + require.Equal(t, want, got) + }) + } +} diff --git a/kustomize.go b/kustomize.go index e17d932..1815cfc 100644 --- a/kustomize.go +++ b/kustomize.go @@ -44,6 +44,7 @@ type KustomizeBuildOpts struct { SetFlags []string EnableAlphaPlugins bool Namespace string + HelmBinary string } func (o *KustomizeBuildOpts) SetKustomizeBuildOption(opts *KustomizeBuildOpts) error { @@ -153,7 +154,11 @@ func (r *Runner) KustomizeBuild(srcDir string, tempDir string, opts ...Kustomize if err != nil { return "", err } - kustomizeArgs = append(kustomizeArgs, f) + kustomizeArgs = append(kustomizeArgs, f, "--enable-helm") + + if u.HelmBinary != "" { + kustomizeArgs = append(kustomizeArgs, "--helm-command="+u.HelmBinary) + } out, err := r.runInDir(tempDir, r.kustomizeBin(), append(kustomizeArgs, tempDir)...) if err != nil { diff --git a/testdata/integration/testcases/kustomize_with_helm_charts/want b/testdata/integration/testcases/kustomize_with_helm_charts/want new file mode 100644 index 0000000..65193d9 --- /dev/null +++ b/testdata/integration/testcases/kustomize_with_helm_charts/want @@ -0,0 +1,25 @@ + +--- +# Source: kustomize_with_helm_charts/templates/kustomized.yaml +# Source: kustomize_with_helm_charts/templates/kustomized.yaml +apiVersion: v1 +kind: Pod +metadata: + annotations: + helm.sh/hook: test + labels: + app.kubernetes.io/instance: release-name + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: log + app.kubernetes.io/version: 1.16.0 + helm.sh/chart: log-0.1.0 + name: release-name-log-test-connection +spec: + containers: + - args: + - release-name-log:80 + command: + - wget + image: busybox + name: wget + restartPolicy: Never diff --git a/testdata/kustomize_with_helm_charts/kustomization.yaml b/testdata/kustomize_with_helm_charts/kustomization.yaml new file mode 100644 index 0000000..fdc535f --- /dev/null +++ b/testdata/kustomize_with_helm_charts/kustomization.yaml @@ -0,0 +1,6 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +helmCharts: + - repo: http://localhost:18080/ + name: log + version: 0.1.0