Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use helmBin() for kustomize build for helmChart in kustomization.yaml #64

Merged
merged 3 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}
3 changes: 2 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,7 @@ 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", "--helm-command="+u.HelmBinary)
yxxhero marked this conversation as resolved.
Show resolved Hide resolved

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