Add PrintPluginEnv plugin.

This commit is contained in:
jregan
2019-11-30 09:27:38 -08:00
parent d942e6fa59
commit d98af3f06a
11 changed files with 161 additions and 148 deletions

View File

@@ -0,0 +1,48 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package target
import (
"fmt"
"strings"
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/api/konfig"
)
type errMissingKustomization struct {
path string
}
func (e *errMissingKustomization) Error() string {
return fmt.Sprintf(
"unable to find one of %v in directory '%s'",
commaOr(quoted(konfig.RecognizedKustomizationFileNames())),
e.path)
}
func IsMissingKustomizationFileError(err error) bool {
_, ok := err.(*errMissingKustomization)
if ok {
return true
}
_, ok = errors.Cause(err).(*errMissingKustomization)
return ok
}
func NewErrMissingKustomization(p string) *errMissingKustomization {
return &errMissingKustomization{path: p}
}
func quoted(l []string) []string {
r := make([]string, len(l))
for i, v := range l {
r[i] = "'" + v + "'"
}
return r
}
func commaOr(q []string) string {
return strings.Join(q[:len(q)-1], ", ") + " or " + q[len(q)-1]
}

View File

@@ -70,18 +70,6 @@ func NewKustTarget(
}, nil }, nil
} }
func quoted(l []string) []string {
r := make([]string, len(l))
for i, v := range l {
r[i] = "'" + v + "'"
}
return r
}
func commaOr(q []string) string {
return strings.Join(q[:len(q)-1], ", ") + " or " + q[len(q)-1]
}
func loadKustFile(ldr ifc.Loader) ([]byte, error) { func loadKustFile(ldr ifc.Loader) ([]byte, error) {
var content []byte var content []byte
match := 0 match := 0
@@ -103,30 +91,6 @@ func loadKustFile(ldr ifc.Loader) ([]byte, error) {
} }
} }
type errMissingKustomization struct {
path string
}
func (e *errMissingKustomization) Error() string {
return fmt.Sprintf(
"unable to find one of %v in directory '%s'",
commaOr(quoted(konfig.RecognizedKustomizationFileNames())),
e.path)
}
func NewErrMissingKustomization(p string) *errMissingKustomization {
return &errMissingKustomization{path: p}
}
func IsMissingKustomizationFileError(err error) bool {
_, ok := err.(*errMissingKustomization)
if ok {
return true
}
_, ok = errors.Cause(err).(*errMissingKustomization)
return ok
}
func unmarshal(y []byte, o interface{}) error { func unmarshal(y []byte, o interface{}) error {
j, err := yaml.YAMLToJSON(y) j, err := yaml.YAMLToJSON(y)
if err != nil { if err != nil {

View File

@@ -1,93 +0,0 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package target_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/internal/k8sdeps/transformer"
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
. "sigs.k8s.io/kustomize/api/internal/target"
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/api/konfig"
fLdr "sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
)
func TestPluginDir(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.PrepExecPlugin(
"someteam.example.com", "v1", "PrintWorkDir")
base, err := os.Getwd()
if err != nil {
t.Fatalf("err %v", err)
}
dir, err := ioutil.TempDir(base, "kustomize-")
if err != nil {
t.Fatalf("err %v", err)
}
defer os.RemoveAll(dir)
fSys := filesys.MakeFsOnDisk()
err = fSys.WriteFile(filepath.Join(dir, "kustomization.yaml"), []byte(`
generators:
- config.yaml
`))
if err != nil {
t.Fatalf("err %v", err)
}
err = fSys.WriteFile(filepath.Join(dir, "config.yaml"), []byte(`
apiVersion: someteam.example.com/v1
kind: PrintWorkDir
metadata:
name: some-random-name
`))
if err != nil {
t.Fatalf("err %v", err)
}
ldr, err := fLdr.NewLoader(
fLdr.RestrictionRootOnly, dir, fSys)
if err != nil {
t.Fatalf("Err: %v", err)
}
rf := resmap.NewFactory(resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl()), nil)
c, err := konfig.EnabledPluginConfig()
if err != nil {
t.Fatal(err)
}
pl := pLdr.NewLoader(c, rf)
tg, err := NewKustTarget(
ldr, valtest_test.MakeFakeValidator(), rf, transformer.NewFactoryImpl(), pl)
if err != nil {
t.Fatalf("err %v", err)
}
m, err := tg.MakeCustomizedResMap()
if err != nil {
t.Fatalf("Err: %v", err)
}
th := kusttest_test.NewKustTestHarness(t, filesys.SelfDir)
th.AssertActualEqualsExpected(m, `
apiVersion: v1
kind: WorkDir
metadata:
name: `+dir+`
spec:
path: `+dir+`
`)
}

View File

@@ -0,0 +1,80 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package krusty_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/konfig"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
)
// The PrintPluginEnv plugin is a toy plugin that emits
// its working directory and some environment variables,
// to add regression protection to plugin loading logic.
func TestPluginEnvironment(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.PrepExecPlugin(
"someteam.example.com", "v1", "PrintPluginEnv")
confirmBehavior(
makeTestHarnessWithFs(t, filesys.MakeFsInMemory()),
filesys.Separator)
dir := makeTmpDir(t)
defer os.RemoveAll(dir)
confirmBehavior(
makeTestHarnessWithFs(t, filesys.MakeFsOnDisk()),
dir)
}
func confirmBehavior(th testingHarness, dir string) {
th.WriteK(dir, `
generators:
- config.yaml
`)
th.WriteF(filepath.Join(dir, "config.yaml"), `
apiVersion: someteam.example.com/v1
kind: PrintPluginEnv
metadata:
name: irrelevantHere
`)
m := th.Run(dir, th.MakeOptionsPluginsEnabled())
pHome, ok := os.LookupEnv(konfig.KustomizePluginHomeEnv)
if !ok {
th.GetT().Fatalf(
"expected env var '%s' to be defined",
konfig.KustomizePluginHomeEnv)
}
th.AssertActualEqualsExpected(m, `
apiVersion: v1
env:
kustomize_plugin_config_root: `+dir+`
kustomize_plugin_home: `+pHome+`
pwd: `+dir+`
kind: GeneratedEnv
metadata:
name: hello
`)
}
func makeTmpDir(t *testing.T) string {
base, err := os.Getwd()
if err != nil {
t.Fatalf("err %v", err)
}
dir, err := ioutil.TempDir(base, "kustomize-tmp-test-")
if err != nil {
t.Fatalf("err %v", err)
}
return dir
}

View File

@@ -23,12 +23,21 @@ type testingHarness struct {
} }
func makeTestHarness(t *testing.T) testingHarness { func makeTestHarness(t *testing.T) testingHarness {
return makeTestHarnessWithFs(t, filesys.MakeFsInMemory())
}
func makeTestHarnessWithFs(
t *testing.T, fSys filesys.FileSystem) testingHarness {
return testingHarness{ return testingHarness{
t: t, t: t,
fSys: filesys.MakeFsInMemory(), fSys: fSys,
} }
} }
func (th testingHarness) GetT() *testing.T {
return th.t
}
func (th testingHarness) WriteK(path string, content string) { func (th testingHarness) WriteK(path string, content string) {
th.fSys.WriteFile( th.fSys.WriteFile(
filepath.Join( filepath.Join(

View File

@@ -53,6 +53,11 @@ func (x *PluginTestEnv) Reset() {
x.removeWorkDir() x.removeWorkDir()
} }
// WorkDir allows inspection of the temp working directory.
func (x *PluginTestEnv) WorkDir() string {
return x.workDir
}
// BuildGoPlugin compiles a Go plugin, leaving the newly // BuildGoPlugin compiles a Go plugin, leaving the newly
// created object code in the right place - a temporary // created object code in the right place - a temporary
// working directory pointed to by KustomizePluginHomeEnv. // working directory pointed to by KustomizePluginHomeEnv.

View File

@@ -0,0 +1,13 @@
#!/bin/bash
set -e
echo "
kind: GeneratedEnv
apiVersion: v1
metadata:
name: hello
env:
pwd: $PWD
kustomize_plugin_home: $KUSTOMIZE_PLUGIN_HOME
kustomize_plugin_config_root: $KUSTOMIZE_PLUGIN_CONFIG_ROOT
"

View File

@@ -16,18 +16,18 @@ func shouldContain(t *testing.T, s []byte, x string) {
} }
} }
func TestPrintWorkDirPlugin(t *testing.T) { func TestPrintPluginEnvPlugin(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set() tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset() defer tc.Reset()
tc.PrepExecPlugin( tc.PrepExecPlugin(
"someteam.example.com", "v1", "PrintWorkDir") "someteam.example.com", "v1", "PrintPluginEnv")
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/theAppRoot") th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/theAppRoot")
m := th.LoadAndRunGenerator(` m := th.LoadAndRunGenerator(`
apiVersion: someteam.example.com/v1 apiVersion: someteam.example.com/v1
kind: PrintWorkDir kind: PrintPluginEnv
metadata: metadata:
name: whatever name: whatever
`) `)
@@ -35,6 +35,6 @@ metadata:
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
shouldContain(t, a, "path: /theAppRoot") shouldContain(t, a, "kustomize_plugin_config_root: /theAppRoot")
shouldContain(t, a, "plugin/someteam.example.com/v1/printworkdir") shouldContain(t, a, "plugin/someteam.example.com/v1/printpluginenv")
} }

View File

@@ -1,13 +0,0 @@
#!/bin/bash
set -e
dir=`pwd`
echo "
kind: WorkDir
apiVersion: v1
metadata:
name: $dir
spec:
path: ${KUSTOMIZE_PLUGIN_CONFIG_ROOT}
"