Skip to content

Commit

Permalink
proposal v2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
aabouzaid committed Oct 22, 2022
1 parent ea21b37 commit 3e447da
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 22 deletions.
6 changes: 5 additions & 1 deletion api/internal/plugins/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ func TestLoaderWithWorkingDir(t *testing.T) {
c := types.EnabledPluginConfig(types.BploLoadFromFileSys)
pLdr := NewLoader(c, rmF, fsys)
npLdr := pLdr.LoaderWithWorkingDir("/tmp/dummy")
require.Equal(t,
"",
pLdr.Config().FnpLoadingOptions.WorkingDir,
"the plugin working dir should not change")
require.Equal(t,
"/tmp/dummy",
npLdr.Config().FnpLoadingOptions.WorkingDir,
"plugin working dir is not set correctly")
"the plugin working dir is not updated")
}
197 changes: 176 additions & 21 deletions api/krusty/fnplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@ spec:
EOF
`

const krmEchoDotSh = `#!/bin/bash
resourceList=$(cat)
echo "$resourceList"
const krmTransformerDotSh = `#!/bin/bash
cat << EOF
apiVersion: v1
kind: Secret
metadata:
name: dummyTransformed
stringData:
foo: bar
type: Opaque
EOF
`

func TestFnExecGenerator(t *testing.T) {
func TestFnExecGeneratorInBase(t *testing.T) {
fSys := filesys.MakeFsOnDisk()

th := kusttest_test.MakeHarnessWithFs(t, fSys)
Expand Down Expand Up @@ -93,7 +99,6 @@ spec:
`)

m := th.Run(tmpDir.String(), o)
assert.NoError(t, err)
yml, err := m.AsYaml()
assert.NoError(t, err)
assert.Equal(t, `apiVersion: v1
Expand Down Expand Up @@ -132,7 +137,7 @@ spec:
assert.NoError(t, fSys.RemoveAll(tmpDir.String()))
}

func TestFnExecGeneratorInOverlay(t *testing.T) {
func TestFnExecGeneratorInBaseWithOverlay(t *testing.T) {
fSys := filesys.MakeFsOnDisk()

th := kusttest_test.MakeHarnessWithFs(t, fSys)
Expand Down Expand Up @@ -223,7 +228,98 @@ spec:
assert.NoError(t, fSys.RemoveAll(tmpDir.String()))
}

func TestFnExecTransformer(t *testing.T) {
func TestFnExecGeneratorInOverlay(t *testing.T) {
fSys := filesys.MakeFsOnDisk()

th := kusttest_test.MakeHarnessWithFs(t, fSys)
o := th.MakeOptionsPluginsEnabled()
o.PluginConfig.FnpLoadingOptions.EnableExec = true

tmpDir, err := filesys.NewTmpConfirmedDir()
assert.NoError(t, err)
base := filepath.Join(tmpDir.String(), "base")
prod := filepath.Join(tmpDir.String(), "prod")
assert.NoError(t, fSys.Mkdir(base))
assert.NoError(t, fSys.Mkdir(prod))
th.WriteK(base, `
resources:
- short_secret.yaml
`)
th.WriteK(prod, `
resources:
- ../base
generators:
- gener.yaml
`)
th.WriteF(filepath.Join(base, "short_secret.yaml"),
`
apiVersion: v1
kind: Secret
metadata:
labels:
airshipit.org/ephemeral-user-data: "true"
name: node1-bmc-secret
type: Opaque
stringData:
userData: |
bootcmd:
- mkdir /mnt/vda
`)
th.WriteF(filepath.Join(prod, "generateDeployment.sh"), generateDeploymentDotSh)

assert.NoError(t, os.Chmod(filepath.Join(prod, "generateDeployment.sh"), 0777))
th.WriteF(filepath.Join(prod, "gener.yaml"), `
kind: executable
metadata:
name: demo
annotations:
config.kubernetes.io/function: |
exec:
path: ./generateDeployment.sh
spec:
`)

m := th.Run(prod, o)
assert.NoError(t, err)
yml, err := m.AsYaml()
assert.NoError(t, err)
assert.Equal(t, `apiVersion: v1
kind: Secret
metadata:
labels:
airshipit.org/ephemeral-user-data: "true"
name: node1-bmc-secret
stringData:
userData: |
bootcmd:
- mkdir /mnt/vda
type: Opaque
---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
tshirt-size: small
labels:
app: nginx
name: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
`, string(yml))
assert.NoError(t, fSys.RemoveAll(tmpDir.String()))
}

func TestFnExecTransformerInBase(t *testing.T) {
fSys := filesys.MakeFsOnDisk()

th := kusttest_test.MakeHarnessWithFs(t, fSys)
Expand All @@ -238,7 +334,7 @@ func TestFnExecTransformer(t *testing.T) {
resources:
- secret.yaml
transformers:
- krm-echo.yaml
- krm-transformer.yaml
`)
th.WriteF(filepath.Join(base, "secret.yaml"),
`
Expand All @@ -250,28 +346,88 @@ type: Opaque
stringData:
foo: bar
`)
th.WriteF(filepath.Join(base, "krmEcho.sh"), krmEchoDotSh)
th.WriteF(filepath.Join(base, "krmTransformer.sh"), krmTransformerDotSh)

assert.NoError(t, os.Chmod(filepath.Join(base, "krmEcho.sh"), 0777))
th.WriteF(filepath.Join(base, "krm-echo.yaml"), `
assert.NoError(t, os.Chmod(filepath.Join(base, "krmTransformer.sh"), 0777))
th.WriteF(filepath.Join(base, "krm-transformer.yaml"), `
apiVersion: examples.config.kubernetes.io/v1beta1
kind: MyPlugin
metadata:
name: notImportantHere
annotations:
config.kubernetes.io/function: |
exec:
path: ./krmEcho.sh
path: ./krmTransformer.sh
`)

m := th.Run(base, o)
assert.NoError(t, err)
yml, err := m.AsYaml()
assert.NoError(t, err)
assert.Equal(t, `apiVersion: v1
kind: Secret
metadata:
name: dummyTransformed
stringData:
foo: bar
type: Opaque
`, string(yml))
assert.NoError(t, fSys.RemoveAll(tmpDir.String()))
}

func TestFnExecTransformerInBaseWithOverlay(t *testing.T) {
fSys := filesys.MakeFsOnDisk()

th := kusttest_test.MakeHarnessWithFs(t, fSys)
o := th.MakeOptionsPluginsEnabled()
o.PluginConfig.FnpLoadingOptions.EnableExec = true

tmpDir, err := filesys.NewTmpConfirmedDir()
assert.NoError(t, err)
base := filepath.Join(tmpDir.String(), "base")
prod := filepath.Join(tmpDir.String(), "prod")
assert.NoError(t, fSys.Mkdir(base))
assert.NoError(t, fSys.Mkdir(prod))
th.WriteK(base, `
resources:
- secret.yaml
transformers:
- krm-transformer.yaml
`)
th.WriteK(prod, `
resources:
- ../base
`)
th.WriteF(filepath.Join(base, "secret.yaml"),
`
apiVersion: v1
kind: Secret
metadata:
name: dummy
type: Opaque
stringData:
foo: bar
`)
th.WriteF(filepath.Join(base, "krmTransformer.sh"), krmTransformerDotSh)

assert.NoError(t, os.Chmod(filepath.Join(base, "krmTransformer.sh"), 0777))
th.WriteF(filepath.Join(base, "krm-transformer.yaml"), `
apiVersion: examples.config.kubernetes.io/v1beta1
kind: MyPlugin
metadata:
name: notImportantHere
annotations:
config.kubernetes.io/function: |
exec:
path: ./krmTransformer.sh
`)

m := th.Run(prod, o)
yml, err := m.AsYaml()
assert.NoError(t, err)
assert.Equal(t, `apiVersion: v1
kind: Secret
metadata:
name: dummyTransformed
stringData:
foo: bar
type: Opaque
Expand Down Expand Up @@ -300,7 +456,7 @@ resources:
resources:
- ../base
transformers:
- krm-echo.yaml
- krm-transformer.yaml
`)
th.WriteF(filepath.Join(base, "secret.yaml"),
`
Expand All @@ -312,28 +468,27 @@ type: Opaque
stringData:
foo: bar
`)
th.WriteF(filepath.Join(prod, "krmEcho.sh"), krmEchoDotSh)
th.WriteF(filepath.Join(prod, "krmTransformer.sh"), krmTransformerDotSh)

assert.NoError(t, os.Chmod(filepath.Join(prod, "krmEcho.sh"), 0777))
th.WriteF(filepath.Join(prod, "krm-echo.yaml"), `
assert.NoError(t, os.Chmod(filepath.Join(prod, "krmTransformer.sh"), 0777))
th.WriteF(filepath.Join(prod, "krm-transformer.yaml"), `
apiVersion: examples.config.kubernetes.io/v1beta1
kind: MyPlugin
metadata:
name: notImportantHere
annotations:
config.kubernetes.io/function: |
exec:
path: ./krmEcho.sh
path: ./krmTransformer.sh
`)

m := th.Run(prod, o)
assert.NoError(t, err)
yml, err := m.AsYaml()
assert.NoError(t, err)
assert.Equal(t, `apiVersion: v1
kind: Secret
metadata:
name: dummy
name: dummyTransformed
stringData:
foo: bar
type: Opaque
Expand Down

0 comments on commit 3e447da

Please # to comment.