mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 10:00:56 +00:00
Cleanup plugin builds.
This commit is contained in:
@@ -15,9 +15,10 @@ package target_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"sigs.k8s.io/kustomize/pkg/types"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
|
||||
)
|
||||
|
||||
func TestGeneratorOptionsWithBases(t *testing.T) {
|
||||
@@ -96,7 +97,7 @@ secretGenerator:
|
||||
|
||||
func TestGoPluginDoesNotExist(t *testing.T) {
|
||||
th := NewKustTestHarnessWithPluginConfig(
|
||||
t, "/app", types.PluginConfig{GoEnabled: true})
|
||||
t, "/app", plugin.ActivePluginConfig())
|
||||
th.writeK("/app", `
|
||||
secretGenerator:
|
||||
- name: attemptGoPlugin
|
||||
|
||||
@@ -41,11 +41,11 @@ import (
|
||||
|
||||
// KustTarget encapsulates the entirety of a kustomization build.
|
||||
type KustTarget struct {
|
||||
kustomization *types.Kustomization
|
||||
ldr ifc.Loader
|
||||
rFactory *resmap.Factory
|
||||
tFactory transformer.Factory
|
||||
goPluginEnabled bool
|
||||
kustomization *types.Kustomization
|
||||
ldr ifc.Loader
|
||||
rFactory *resmap.Factory
|
||||
tFactory transformer.Factory
|
||||
pluginConfig *types.PluginConfig
|
||||
}
|
||||
|
||||
// NewKustTarget returns a new instance of KustTarget primed with a Loader.
|
||||
@@ -53,7 +53,7 @@ func NewKustTarget(
|
||||
ldr ifc.Loader,
|
||||
rFactory *resmap.Factory,
|
||||
tFactory transformer.Factory,
|
||||
b bool) (*KustTarget, error) {
|
||||
pc *types.PluginConfig) (*KustTarget, error) {
|
||||
content, err := loadKustFile(ldr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -71,11 +71,11 @@ func NewKustTarget(
|
||||
strings.Join(errs, "\n"), ldr.Root())
|
||||
}
|
||||
return &KustTarget{
|
||||
kustomization: &k,
|
||||
ldr: ldr,
|
||||
rFactory: rFactory,
|
||||
tFactory: tFactory,
|
||||
goPluginEnabled: b,
|
||||
kustomization: &k,
|
||||
ldr: ldr,
|
||||
rFactory: rFactory,
|
||||
tFactory: tFactory,
|
||||
pluginConfig: pc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -256,7 +256,7 @@ func (kt *KustTarget) accumulateBases() (
|
||||
continue
|
||||
}
|
||||
subKt, err := NewKustTarget(
|
||||
ldr, kt.rFactory, kt.tFactory, kt.goPluginEnabled)
|
||||
ldr, kt.rFactory, kt.tFactory, kt.pluginConfig)
|
||||
if err != nil {
|
||||
errs.Append(errors.Wrap(err, "couldn't make target for "+path))
|
||||
ldr.Cleanup()
|
||||
@@ -323,11 +323,13 @@ func (kt *KustTarget) newTransformer(
|
||||
}
|
||||
r = append(r, t)
|
||||
|
||||
tp, err := kt.loadTransformerPlugins()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if kt.pluginConfig.GoEnabled {
|
||||
tp, err := kt.loadTransformerPlugins()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r = append(r, tp...)
|
||||
}
|
||||
r = append(r, tp...)
|
||||
return transformers.NewMultiTransformer(r), nil
|
||||
}
|
||||
|
||||
@@ -337,6 +339,5 @@ func (kt *KustTarget) loadTransformerPlugins() ([]transformers.Transformer, erro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tl := plugins.NewTransformerLoader(kt.goPluginEnabled)
|
||||
return tl.Load(transformerPluginConfigs)
|
||||
return plugins.NewTransformerLoader(kt.pluginConfig).Load(transformerPluginConfigs)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
|
||||
"sigs.k8s.io/kustomize/pkg/gvk"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
|
||||
@@ -204,7 +205,9 @@ func TestResources(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKustomizationNotFound(t *testing.T) {
|
||||
_, err := NewKustTarget(loadertest.NewFakeLoader("/foo"), nil, nil, false)
|
||||
_, err := NewKustTarget(
|
||||
loadertest.NewFakeLoader("/foo"),
|
||||
nil, nil, plugin.DefaultPluginConfig())
|
||||
if err == nil {
|
||||
t.Fatalf("expected an error")
|
||||
}
|
||||
|
||||
@@ -21,11 +21,11 @@ package target_test
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
|
||||
"sigs.k8s.io/kustomize/k8sdeps/transformer"
|
||||
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
|
||||
"sigs.k8s.io/kustomize/pkg/pgmconfig"
|
||||
@@ -40,7 +40,7 @@ type KustTestHarness struct {
|
||||
t *testing.T
|
||||
rf *resmap.Factory
|
||||
ldr loadertest.FakeLoader
|
||||
b bool
|
||||
pc *types.PluginConfig
|
||||
}
|
||||
|
||||
func NewKustTestHarness(t *testing.T, path string) *KustTestHarness {
|
||||
@@ -50,19 +50,19 @@ func NewKustTestHarness(t *testing.T, path string) *KustTestHarness {
|
||||
|
||||
func NewKustTestHarnessWithPluginConfig(
|
||||
t *testing.T, path string,
|
||||
config types.PluginConfig) *KustTestHarness {
|
||||
pc *types.PluginConfig) *KustTestHarness {
|
||||
return &KustTestHarness{
|
||||
t: t,
|
||||
rf: resmap.NewFactory(resource.NewFactory(
|
||||
kunstruct.NewKunstructuredFactoryWithGeneratorArgs(
|
||||
&types.GeneratorMetaArgs{PluginConfig: config}))),
|
||||
&types.GeneratorMetaArgs{PluginConfig: pc}))),
|
||||
ldr: loadertest.NewFakeLoader(path),
|
||||
b: config.GoEnabled}
|
||||
pc: pc}
|
||||
}
|
||||
|
||||
func (th *KustTestHarness) makeKustTarget() *KustTarget {
|
||||
kt, err := NewKustTarget(
|
||||
th.ldr, th.rf, transformer.NewFactoryImpl(), th.b)
|
||||
th.ldr, th.rf, transformer.NewFactoryImpl(), th.pc)
|
||||
if err != nil {
|
||||
th.t.Fatalf("Unexpected construction error %v", err)
|
||||
}
|
||||
|
||||
165
pkg/target/testenvcontroller_test.go
Normal file
165
pkg/target/testenvcontroller_test.go
Normal file
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package target_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
|
||||
"sigs.k8s.io/kustomize/pkg/pgmconfig"
|
||||
)
|
||||
|
||||
// TestEnvController manages the KustTarget test environment.
|
||||
// It sets/resets XDG_CONFIG_HOME, makes/removes a temp objRoot.
|
||||
type TestEnvController struct {
|
||||
t *testing.T
|
||||
xdgConfigHome string
|
||||
oldXdg string
|
||||
wasSet bool
|
||||
}
|
||||
|
||||
func NewTestEnvController(t *testing.T) *TestEnvController {
|
||||
return &TestEnvController{t: t}
|
||||
}
|
||||
|
||||
func (x *TestEnvController) Set() *TestEnvController {
|
||||
x.makeTmpConfigHomeDir()
|
||||
x.makeObjectRootDir()
|
||||
x.setEnv()
|
||||
return x
|
||||
}
|
||||
|
||||
func (x *TestEnvController) Reset() {
|
||||
x.resetEnv()
|
||||
x.removeTmpConfigHomeDir()
|
||||
}
|
||||
|
||||
func (x *TestEnvController) fileExists(name string) bool {
|
||||
if _, err := os.Stat(name); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (x *TestEnvController) recentFileExists(path string) bool {
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
age := time.Now().Sub(fi.ModTime())
|
||||
return age.Minutes() < 1
|
||||
}
|
||||
|
||||
func (x *TestEnvController) BuildGoPlugin(plugin ...string) {
|
||||
obj := filepath.Join(
|
||||
append([]string{x.ObjectRoot()}, plugin...)...) + ".so"
|
||||
if x.recentFileExists(obj) {
|
||||
// Skip rebuilding it.
|
||||
return
|
||||
}
|
||||
src := filepath.Join(
|
||||
append([]string{x.SrcRoot()}, plugin...)...) + ".go"
|
||||
if !x.fileExists(src) {
|
||||
x.t.Errorf("cannot find go plugin source %s", src)
|
||||
}
|
||||
commands := []string{
|
||||
"build",
|
||||
"-buildmode",
|
||||
"plugin",
|
||||
"-tags=plugin",
|
||||
"-o", obj, src,
|
||||
}
|
||||
goBin := filepath.Join(os.Getenv("GOROOT"), "bin", "go")
|
||||
if !x.fileExists(src) {
|
||||
x.t.Errorf("cannot find go compiler %s", goBin)
|
||||
}
|
||||
cmd := exec.Command(goBin, commands...)
|
||||
cmd.Env = os.Environ()
|
||||
// cmd.Dir = filepath.Join(dir, "kustomize", "plugins")
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
x.t.Errorf("compiler error building %s: %v", src, err)
|
||||
}
|
||||
}
|
||||
|
||||
// ObjectRoot is the objRoot dir for plugin object files.
|
||||
func (x *TestEnvController) ObjectRoot() string {
|
||||
return filepath.Join(
|
||||
x.xdgConfigHome, pgmconfig.PgmName, plugin.PluginsDir)
|
||||
}
|
||||
|
||||
// SrcRoot is a objRoot directory for plugin source code
|
||||
// used by tests.
|
||||
//
|
||||
// Plugin object code files have to be in a particular
|
||||
// location to be found and loaded for security reasons,
|
||||
// but placement of plugin source code is up to the user.
|
||||
//
|
||||
// This function returns a location for storing example
|
||||
// plugins for tests. And maybe builtins at some point.
|
||||
func (x *TestEnvController) SrcRoot() string {
|
||||
dir := filepath.Join(
|
||||
os.Getenv("GOPATH"), "src",
|
||||
pgmconfig.Repo, pgmconfig.PgmName, plugin.PluginsDir)
|
||||
if _, err := os.Stat(dir); err != nil {
|
||||
x.t.Errorf("plugin source objRoot '%s' not found", dir)
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
func (x *TestEnvController) makeTmpConfigHomeDir() {
|
||||
var err error
|
||||
x.xdgConfigHome, err = ioutil.TempDir("", "kustomizetests")
|
||||
if err != nil {
|
||||
x.t.Errorf("failed to make temp objRoot: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *TestEnvController) makeObjectRootDir() {
|
||||
err := os.MkdirAll(x.ObjectRoot(), os.ModePerm)
|
||||
if err != nil {
|
||||
x.t.Errorf(
|
||||
"making temp object objRoot %s: %v", x.ObjectRoot(), err)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *TestEnvController) removeTmpConfigHomeDir() {
|
||||
err := os.RemoveAll(x.xdgConfigHome)
|
||||
if err != nil {
|
||||
x.t.Errorf(
|
||||
"removing temp object objRoot: %s %v", x.xdgConfigHome, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *TestEnvController) setEnv() {
|
||||
x.oldXdg, x.wasSet = os.LookupEnv(pgmconfig.XDG_CONFIG_HOME)
|
||||
os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.xdgConfigHome)
|
||||
}
|
||||
|
||||
func (x *TestEnvController) resetEnv() {
|
||||
if x.wasSet {
|
||||
os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.oldXdg)
|
||||
} else {
|
||||
os.Unsetenv(pgmconfig.XDG_CONFIG_HOME)
|
||||
}
|
||||
}
|
||||
@@ -14,14 +14,9 @@ limitations under the License.
|
||||
package target_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"fmt"
|
||||
"sigs.k8s.io/kustomize/pkg/pgmconfig"
|
||||
"sigs.k8s.io/kustomize/pkg/types"
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
|
||||
)
|
||||
|
||||
func writeDeployment(th *KustTestHarness, path string) {
|
||||
@@ -44,7 +39,7 @@ spec:
|
||||
|
||||
func writeStringPrefixer(th *KustTestHarness, path string) {
|
||||
th.writeF(path, `
|
||||
apiVersion: strings.microwoosh.com/v1
|
||||
apiVersion: someteam.example.com/v1
|
||||
kind: StringPrefixer
|
||||
metadata:
|
||||
name: myStringPrefixer
|
||||
@@ -54,54 +49,25 @@ prefix: apple-
|
||||
|
||||
func writeDatePrefixer(th *KustTestHarness, path string) {
|
||||
th.writeF(path, `
|
||||
apiVersion: team.dater.com/v1
|
||||
apiVersion: someteam.example.com/v1
|
||||
kind: DatePrefixer
|
||||
metadata:
|
||||
name: myDatePrefixer
|
||||
`)
|
||||
}
|
||||
|
||||
func buildGoPlugins(dir, filename string) error {
|
||||
commands := []string{
|
||||
"build",
|
||||
"-buildmode",
|
||||
"plugin",
|
||||
"-tags=plugin",
|
||||
"-o",
|
||||
filename + ".so",
|
||||
filename + ".go",
|
||||
}
|
||||
goBin := filepath.Join(os.Getenv("GOROOT"), "bin", "go")
|
||||
if _, err := os.Stat(goBin); err != nil {
|
||||
return fmt.Errorf("go binary not found %s", goBin)
|
||||
}
|
||||
cmd := exec.Command(goBin, commands...)
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Dir = filepath.Join(dir, "kustomize", "plugins")
|
||||
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func TestOrderedTransformers(t *testing.T) {
|
||||
dir, err := filepath.Abs("../../..")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error %v", err)
|
||||
}
|
||||
os.Setenv(pgmconfig.XDG_CONFIG_HOME, dir)
|
||||
defer os.Unsetenv(pgmconfig.XDG_CONFIG_HOME)
|
||||
tc := NewTestEnvController(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
err = buildGoPlugins(dir, "StringPrefixer")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error %v", err)
|
||||
}
|
||||
tc.BuildGoPlugin(
|
||||
"someteam.example.com", "v1", "StringPrefixer")
|
||||
|
||||
err = buildGoPlugins(dir, "DatePrefixer")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error %v", err)
|
||||
}
|
||||
tc.BuildGoPlugin(
|
||||
"someteam.example.com", "v1", "DatePrefixer")
|
||||
|
||||
th := NewKustTestHarnessWithPluginConfig(
|
||||
t, "/app", types.PluginConfig{GoEnabled: true})
|
||||
t, "/app", plugin.ActivePluginConfig())
|
||||
th.writeK("/app", `
|
||||
resources:
|
||||
- deployment.yaml
|
||||
@@ -134,7 +100,7 @@ spec:
|
||||
|
||||
func xTestTransformedTransformers(t *testing.T) {
|
||||
th := NewKustTestHarnessWithPluginConfig(
|
||||
t, "/app/overlay", types.PluginConfig{GoEnabled: true})
|
||||
t, "/app/overlay", plugin.ActivePluginConfig())
|
||||
|
||||
th.writeK("/app/base", `
|
||||
resources:
|
||||
|
||||
Reference in New Issue
Block a user