Skip to content

Commit

Permalink
feat: impl patch with alpha plugin flag
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Aug 2, 2023
1 parent b756b58 commit feef492
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 11 deletions.
1 change: 1 addition & 0 deletions chartify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 33 additions & 10 deletions kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
18 changes: 17 additions & 1 deletion patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions testdata/kube_manifest_transformer_alpha_plugin/annotations.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit feef492

Please sign in to comment.