Skip to content

Commit

Permalink
feat: use helmBin() for kustomize build for helmChart in kustomizatio…
Browse files Browse the repository at this point in the history
…n.yaml (#64)

* feat: test for kustomize build using helmCharts

Signed-off-by: moririnson <[email protected]>

* 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 helmfile/helmfile#1121

Signed-off-by: moririnson <[email protected]>

* feat: u.HelmBinary empty check for kustomize build

Signed-off-by: moririnson <[email protected]>

---------

Signed-off-by: moririnson <[email protected]>
  • Loading branch information
moririnson authored Nov 12, 2023
1 parent a67147a commit 7da5efd
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
*~
.idea

# testdata
testdata/kustomize_with_helm_charts/charts/
5 changes: 3 additions & 2 deletions chartify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
55 changes: 55 additions & 0 deletions chartify_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package chartify

import (
"context"
"os"
"os/exec"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -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)
})
}
}
7 changes: 6 additions & 1 deletion kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type KustomizeBuildOpts struct {
SetFlags []string
EnableAlphaPlugins bool
Namespace string
HelmBinary string
}

func (o *KustomizeBuildOpts) SetKustomizeBuildOption(opts *KustomizeBuildOpts) error {
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions testdata/integration/testcases/kustomize_with_helm_charts/want
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions testdata/kustomize_with_helm_charts/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
helmCharts:
- repo: http://localhost:18080/
name: log
version: 0.1.0

0 comments on commit 7da5efd

Please sign in to comment.