Skip to content

Commit

Permalink
feat: impl patch with alpha plugin flag. (#59)
Browse files Browse the repository at this point in the history
* feat: impl patch with alpha plugin flag

Signed-off-by: peefy <xpf6677@163.com>

* refactor: kustomize build arguments and remove duplicate code

Signed-off-by: peefy <xpf6677@163.com>

---------

Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy authored Aug 2, 2023
1 parent b756b58 commit e649b1d
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 20 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
73 changes: 54 additions & 19 deletions kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,41 +141,76 @@ 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 u.EnableAlphaPlugins {
f, err := r.kustomizeEnableAlphaPluginsFlag()
if err != nil {
return "", err
}
kustomizeArgs = append(kustomizeArgs, f)
}

f, err := r.kustomizeLoadRestrictionsNoneFlag()
if err != nil {
return "", err
}
kustomizeArgs = append(kustomizeArgs, f)

vi, err := FindSemVerInfo(versionInfo)
out, err := r.runInDir(tempDir, r.kustomizeBin(), append(kustomizeArgs, tempDir)...)
if err != nil {
return "", err
}
fmt.Println(out)

if err := os.RemoveAll(kustomizationPath); err != nil {
return "", fmt.Errorf("removing unnecessary kustomization.yaml after build: %v", err)
}

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 "", err
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 {
kustomizeArgs = append(kustomizeArgs, "--load-restrictor=LoadRestrictionsNone")
if u.EnableAlphaPlugins {
kustomizeArgs = append(kustomizeArgs, "--enable-alpha-plugins")
}
} else {
kustomizeArgs = append(kustomizeArgs, "--load_restrictor=none")
if u.EnableAlphaPlugins {
kustomizeArgs = append(kustomizeArgs, "--enable_alpha_plugins")
}
return "--enable-alpha-plugins", nil
}
return "--enable_alpha_plugins", nil
}

out, err := r.runInDir(tempDir, r.kustomizeBin(), append(kustomizeArgs, tempDir)...)
// kustomizeLoadRestrictionsNoneFlag returns the kustomize loading files from outside
// the root argument.
// Above Kustomize v3, it is `--load-restrictor=LoadRestrictionsNone`.
// Below Kustomize v3 (including v3), it is `--load_restrictor=none`.
func (r *Runner) kustomizeLoadRestrictionsNoneFlag() (string, error) {
version, err := r.kustomizeVersion()
if err != nil {
return "", err
}
fmt.Println(out)

if err := os.RemoveAll(kustomizationPath); err != nil {
return "", fmt.Errorf("removing unnecessary kustomization.yaml after build: %v", err)
if version.Major() > 3 {
return "--load-restrictor=LoadRestrictionsNone", nil
}

return outputFile, nil
return "--load_restrictor=none", 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 e649b1d

Please sign in to comment.