From feef492ca676edcc3103f3cc537a99f0dcbd8fd6 Mon Sep 17 00:00:00 2001 From: peefy Date: Wed, 2 Aug 2023 10:27:38 +0800 Subject: [PATCH] feat: impl patch with alpha plugin flag Signed-off-by: peefy --- chartify.go | 1 + integration_test.go | 23 ++++ kustomize.go | 43 ++++++-- patch.go | 18 +++- .../want | 100 ++++++++++++++++++ .../annotations.yaml | 10 ++ 6 files changed, 184 insertions(+), 11 deletions(-) create mode 100644 testdata/integration/testcases/kube_manifest_transformer_alpha_plugin/want create mode 100644 testdata/kube_manifest_transformer_alpha_plugin/annotations.yaml diff --git a/chartify.go b/chartify.go index 9c15b8d..8092feb 100644 --- a/chartify.go +++ b/chartify.go @@ -444,6 +444,7 @@ func (r *Runner) Chartify(release, dirOrChart string, opts ...ChartifyOption) (s JsonPatches: u.JsonPatches, StrategicMergePatches: u.StrategicMergePatches, Transformers: u.Transformers, + EnableAlphaPlugins: u.EnableKustomizeAlphaPlugins, } if err := r.Patch(tempDir, generatedManifestFiles, patchOpts); err != nil { return "", err diff --git a/integration_test.go b/integration_test.go index ff90c0f..9e05024 100644 --- a/integration_test.go +++ b/integration_test.go @@ -300,6 +300,29 @@ func TestIntegration(t *testing.T) { }, }, }) + + // SAVE_SNAPSHOT=1 go1.20 test -run ^TestIntegration/kube_manifest_transformer_alpha_plugin$ ./ + runTest(t, integrationTestCase{ + description: "kube_manifest_transformer_alpha_plugin", + release: "myapp", + chart: "./testdata/kube_manifest_yml", + opts: ChartifyOpts{ + AdhocChartDependencies: []ChartDependency{ + { + Alias: "log", + Chart: repo + "/log", + Version: "0.1.0", + }, + }, + Transformers: []string{ + "./testdata/kube_manifest_transformer_alpha_plugin/annotations.yaml", + }, + SetFlags: []string{ + "--set", "log.enabled=true", + }, + EnableKustomizeAlphaPlugins: true, + }, + }) } func setupHelmConfig(t *testing.T) { diff --git a/kustomize.go b/kustomize.go index be79722..a79da9e 100644 --- a/kustomize.go +++ b/kustomize.go @@ -141,16 +141,7 @@ func (r *Runner) KustomizeBuild(srcDir string, tempDir string, opts ...Kustomize outputFile := filepath.Join(tempDir, "templates", "kustomized.yaml") kustomizeArgs := []string{"-o", outputFile, "build"} - versionInfo, err := r.run(r.kustomizeBin(), "version") - if err != nil { - return "", err - } - - vi, err := FindSemVerInfo(versionInfo) - if err != nil { - return "", err - } - version, err := semver.NewVersion(vi) + version, err := r.kustomizeVersion() if err != nil { return "", err } @@ -179,3 +170,35 @@ func (r *Runner) KustomizeBuild(srcDir string, tempDir string, opts ...Kustomize return outputFile, nil } + +// kustomizeVersion returns the kustomize binary version. +func (r *Runner) kustomizeVersion() (*semver.Version, error) { + versionInfo, err := r.run(r.kustomizeBin(), "version") + if err != nil { + return nil, err + } + + vi, err := FindSemVerInfo(versionInfo) + if err != nil { + return nil, err + } + version, err := semver.NewVersion(vi) + if err != nil { + return nil, err + } + return version, nil +} + +// kustomizeEnableAlphaPluginsFlag returns the kustomize binary alpha plugin argument. +// Above Kustomize v3, it is `--enable-alpha-plugins`. +// Below Kustomize v3 (including v3), it is `--enable_alpha_plugins`. +func (r *Runner) kustomizeEnableAlphaPluginsFlag() (string, error) { + version, err := r.kustomizeVersion() + if err != nil { + return "", err + } + if version.Major() > 3 { + return "--enable-alpha-plugins", nil + } + return "--enable_alpha_plugins", nil +} diff --git a/patch.go b/patch.go index bc975a7..b335426 100644 --- a/patch.go +++ b/patch.go @@ -17,6 +17,11 @@ type PatchOpts struct { StrategicMergePatches []string Transformers []string + + // Kustomize alpha plugin enable flag. + // Above Kustomize v3, it is `--enable-alpha-plugins`. + // Below Kustomize v3 (including v3), it is `--enable_alpha_plugins`. + EnableAlphaPlugins bool } func (o *PatchOpts) SetPatchOption(opts *PatchOpts) error { @@ -162,7 +167,18 @@ resources: renderedFileName := "all.patched.yaml" renderedFile := filepath.Join(tempDir, renderedFileName) r.Logf("Generating %s", renderedFile) - _, err := r.run(r.kustomizeBin(), "build", tempDir, "--output", renderedFile) + + kustomizeArgs := []string{"build", tempDir, "--output", renderedFile} + + if u.EnableAlphaPlugins { + f, err := r.kustomizeEnableAlphaPluginsFlag() + if err != nil { + return err + } + kustomizeArgs = append(kustomizeArgs, f) + } + + _, err := r.run(r.kustomizeBin(), kustomizeArgs...) if err != nil { return err } diff --git a/testdata/integration/testcases/kube_manifest_transformer_alpha_plugin/want b/testdata/integration/testcases/kube_manifest_transformer_alpha_plugin/want new file mode 100644 index 0000000..85705d4 --- /dev/null +++ b/testdata/integration/testcases/kube_manifest_transformer_alpha_plugin/want @@ -0,0 +1,100 @@ +--- +# Source: kube_manifest_yml/templates/patched_resources.yaml +apiVersion: v1 +data: + bar: | + -----BEGIN CERTIFICATE----- + FOO + -----END CERTIFICATE----- + foo: bar +kind: ConfigMap +metadata: + annotations: + app: myApp + greeting/morning: a string with blanks + name: myconfig1 +--- +# Source: kube_manifest_yml/templates/patched_resources.yaml +apiVersion: v1 +data: + bar: | + -----BEGIN CERTIFICATE----- + FOO + -----END CERTIFICATE----- + foo: bar +kind: ConfigMap +metadata: + annotations: + app: myApp + greeting/morning: a string with blanks + name: myconfig2 +--- +# Source: kube_manifest_yml/templates/patched_resources.yaml +apiVersion: v1 +data: + baz: | + -----BEGIN CERTIFICATE----- + FOO + -----END CERTIFICATE----- + foo: baz +kind: ConfigMap +metadata: + annotations: + app: myApp + greeting/morning: a string with blanks + name: myconfig3 +--- +# Source: kube_manifest_yml/templates/patched_resources.yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + app: myApp + greeting/morning: a string with blanks + labels: + app.kubernetes.io/instance: myapp + 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: myapp-log +spec: + replicas: 1 + selector: + matchLabels: + app.kubernetes.io/instance: myapp + app.kubernetes.io/name: log + template: + metadata: + labels: + app.kubernetes.io/instance: myapp + app.kubernetes.io/name: log + spec: + containers: + - image: nginx:1.16.0 + name: log +--- +# Source: kube_manifest_yml/templates/patched_resources.yaml +apiVersion: v1 +kind: Pod +metadata: + annotations: + app: myApp + greeting/morning: a string with blanks + helm.sh/hook: test + labels: + app.kubernetes.io/instance: myapp + 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: myapp-log-test-connection +spec: + containers: + - args: + - myapp-log:80 + command: + - wget + image: busybox + name: wget + restartPolicy: Never diff --git a/testdata/kube_manifest_transformer_alpha_plugin/annotations.yaml b/testdata/kube_manifest_transformer_alpha_plugin/annotations.yaml new file mode 100644 index 0000000..17ce28a --- /dev/null +++ b/testdata/kube_manifest_transformer_alpha_plugin/annotations.yaml @@ -0,0 +1,10 @@ +apiVersion: builtin +kind: AnnotationsTransformer +metadata: + name: not-important-to-example +annotations: + app: myApp + greeting/morning: a string with blanks +fieldSpecs: +- path: metadata/annotations + create: true