From 6a106546180b25592e3b977cde609ad0b3ca865b Mon Sep 17 00:00:00 2001 From: Jeffrey Regan Date: Fri, 24 May 2019 14:27:36 -0700 Subject: [PATCH] Delete the KV plugin code. --- docs/plugins.md | 5 +- examples/chart.md | 3 +- examples/secretGeneratorPlugin.md | 4 +- .../configmapfactory_test.go | 21 +--- k8sdeps/configmapandsecret/factory.go | 62 +---------- k8sdeps/configmapandsecret/factory_test.go | 100 +---------------- .../configmapandsecret/secretfactory_test.go | 21 +--- k8sdeps/kunstruct/factory.go | 36 +------ k8sdeps/kv/plugin/builtin/envfiles.go | 51 --------- k8sdeps/kv/plugin/builtin/files.go | 49 --------- k8sdeps/kv/plugin/builtin/literals.go | 39 ------- k8sdeps/kv/plugin/builtinplugin.go | 47 -------- k8sdeps/kv/plugin/goplugin.go | 94 ---------------- k8sdeps/kv/plugin/plugin.go | 28 ----- k8sdeps/kv/plugin/registry.go | 87 --------------- pkg/commands/build/build.go | 26 ++--- pkg/commands/commands.go | 39 +------ pkg/commands/edit/add/configmap.go | 45 +++----- pkg/commands/edit/add/configmap_test.go | 97 ++++++++--------- pkg/commands/edit/add/secret.go | 49 +++------ pkg/commands/edit/add/secret_test.go | 91 +++++++--------- pkg/kusttest/kusttestharness.go | 8 +- pkg/plugins/compiler.go | 3 +- pkg/plugins/config.go | 66 +++++++++--- pkg/plugins/execplugin_test.go | 3 +- pkg/plugins/loader.go | 22 +--- pkg/plugins/loader_test.go | 5 +- pkg/target/baseandoverlaysmall_test.go | 6 +- pkg/target/builtinplugins_test.go | 101 ------------------ pkg/target/generatoroptions_test.go | 86 +++++++-------- pkg/target/plugindir_test.go | 9 +- pkg/target/transformerplugin_test.go | 24 +++++ pkg/types/kustomization.go | 14 +-- 33 files changed, 278 insertions(+), 1063 deletions(-) delete mode 100644 k8sdeps/kv/plugin/builtin/envfiles.go delete mode 100644 k8sdeps/kv/plugin/builtin/files.go delete mode 100644 k8sdeps/kv/plugin/builtin/literals.go delete mode 100644 k8sdeps/kv/plugin/builtinplugin.go delete mode 100644 k8sdeps/kv/plugin/goplugin.go delete mode 100644 k8sdeps/kv/plugin/plugin.go delete mode 100644 k8sdeps/kv/plugin/registry.go delete mode 100644 pkg/target/builtinplugins_test.go diff --git a/docs/plugins.md b/docs/plugins.md index 86ab59048..e252efda8 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -173,13 +173,10 @@ transformers cannot be expected to be commutative. A `kustomize build` that tries to use plugins but omits the flag -_TODO: Change flag_ - -> `--enable_alpha_goplugins_accept_panic_risk` +> `--enable_alpha_plugins` will fail with a warning about plugin use. - Flag use is an opt-in acknowledging the absence of plugin provenance. It's meant to give pause to someone who blindly downloads a kustomization from diff --git a/examples/chart.md b/examples/chart.md index 99dd25562..483cdbc52 100644 --- a/examples/chart.md +++ b/examples/chart.md @@ -150,8 +150,7 @@ correct environment and flags for plugins: ``` function kustomizeIt { XDG_CONFIG_HOME=$DEMO_HOME \ - kustomize build \ - --enable_alpha_goplugins_accept_panic_risk \ + kustomize build --enable_alpha_plugins \ $DEMO_HOME/$1 } ``` diff --git a/examples/secretGeneratorPlugin.md b/examples/secretGeneratorPlugin.md index eee9ab42b..7f58f2fd2 100644 --- a/examples/secretGeneratorPlugin.md +++ b/examples/secretGeneratorPlugin.md @@ -201,9 +201,7 @@ can be found under `$DEMO_HOME`: ``` result=$( \ XDG_CONFIG_HOME=$DEMO_HOME \ - kustomize \ - --enable_alpha_goplugins_accept_panic_risk \ - build $DEMO_HOME ) + kustomize build --enable_alpha_plugins $DEMO_HOME ) echo "$result" # Spot check the result: test 1 == $(echo "$result" | grep -c "FRUIT: YXBwbGU=") diff --git a/k8sdeps/configmapandsecret/configmapfactory_test.go b/k8sdeps/configmapandsecret/configmapfactory_test.go index b13fb4588..a163d9031 100644 --- a/k8sdeps/configmapandsecret/configmapfactory_test.go +++ b/k8sdeps/configmapandsecret/configmapfactory_test.go @@ -1,18 +1,5 @@ -/* -Copyright 2018 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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package configmapandsecret @@ -22,7 +9,6 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/types" @@ -143,9 +129,8 @@ func TestConstructConfigMap(t *testing.T) { fSys.WriteFile("/configmap/app-init.ini", []byte("FOO=bar\nBAR=baz\n")) fSys.WriteFile("/configmap/app.bin", []byte{0xff, 0xfd}) ldr := loader.NewFileLoaderAtRoot(fSys) - reg := plugin.NewRegistry(ldr) for _, tc := range testCases { - f := NewFactory(ldr, tc.options, reg) + f := NewFactory(ldr, tc.options) cm, err := f.MakeConfigMap(&tc.input) if err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/k8sdeps/configmapandsecret/factory.go b/k8sdeps/configmapandsecret/factory.go index 2c67e4386..c52fcf04c 100644 --- a/k8sdeps/configmapandsecret/factory.go +++ b/k8sdeps/configmapandsecret/factory.go @@ -1,30 +1,15 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package configmapandsecret import ( "fmt" - "sort" "strings" "github.com/pkg/errors" "k8s.io/apimachinery/pkg/util/validation" "sigs.k8s.io/kustomize/k8sdeps/kv" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/types" ) @@ -33,25 +18,17 @@ import ( type Factory struct { ldr ifc.Loader options *types.GeneratorOptions - reg plugin.Registry } // NewFactory returns a new Factory. func NewFactory( - l ifc.Loader, o *types.GeneratorOptions, reg plugin.Registry) *Factory { - return &Factory{ldr: l, options: o, reg: reg} + l ifc.Loader, o *types.GeneratorOptions) *Factory { + return &Factory{ldr: l, options: o} } func (f *Factory) loadKvPairs( args types.GeneratorArgs) (all []kv.Pair, err error) { - pairs, err := f.keyValuesFromPlugins(args.KVSources) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf( - "plugins: %s", - args.KVSources)) - } - all = append(all, pairs...) - pairs, err = f.keyValuesFromEnvFiles(args.EnvSources) + pairs, err := f.keyValuesFromEnvFiles(args.EnvSources) if err != nil { return nil, errors.Wrap(err, fmt.Sprintf( "env source files: %v", @@ -96,35 +73,6 @@ func keyValuesFromLiteralSources(sources []string) ([]kv.Pair, error) { return kvs, nil } -func (f *Factory) keyValuesFromPlugins(sources []types.KVSource) ([]kv.Pair, error) { - var result []kv.Pair - for _, s := range sources { - plug, err := f.reg.Load(s.PluginType, s.Name) - if err != nil { - return nil, err - } - kvs, err := plug.Get(f.reg.Root(), s.Args) - if err != nil { - return nil, err - } - for _, k := range sortedKeys(kvs) { - result = append(result, kv.Pair{Key: k, Value: kvs[k]}) - } - } - return result, nil -} - -func sortedKeys(m map[string]string) []string { - keys := make([]string, len(m)) - i := 0 - for k := range m { - keys[i] = k - i++ - } - sort.Strings(keys) - return keys -} - func (f *Factory) keyValuesFromFileSources(sources []string) ([]kv.Pair, error) { var kvs []kv.Pair for _, s := range sources { diff --git a/k8sdeps/configmapandsecret/factory_test.go b/k8sdeps/configmapandsecret/factory_test.go index b11a57343..33891ce1b 100644 --- a/k8sdeps/configmapandsecret/factory_test.go +++ b/k8sdeps/configmapandsecret/factory_test.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package configmapandsecret @@ -21,10 +8,8 @@ import ( "testing" "sigs.k8s.io/kustomize/k8sdeps/kv" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/loader" - "sigs.k8s.io/kustomize/pkg/types" ) func TestKeyValuesFromFileSources(t *testing.T) { @@ -47,9 +32,7 @@ func TestKeyValuesFromFileSources(t *testing.T) { fSys := fs.MakeFakeFS() fSys.WriteFile("/files/app-init.ini", []byte("FOO=bar")) - ldr := loader.NewFileLoaderAtRoot(fSys) - reg := plugin.NewRegistry(ldr) - bf := NewFactory(loader.NewFileLoaderAtRoot(fSys), nil, reg) + bf := NewFactory(loader.NewFileLoaderAtRoot(fSys), nil) for _, tc := range tests { kvs, err := bf.keyValuesFromFileSources(tc.sources) if err != nil { @@ -60,80 +43,3 @@ func TestKeyValuesFromFileSources(t *testing.T) { } } } - -func TestKeyValuesFromPlugins(t *testing.T) { - tests := []struct { - description string - sources []types.KVSource - expected []kv.Pair - }{ - { - description: "Create kv.Pairs from builtin literals plugin", - sources: []types.KVSource{ - { - PluginType: "builtin", - Name: "literals", - Args: []string{"FOO=bar", "BAR=baz"}, - }, - }, - expected: []kv.Pair{ - { - Key: "BAR", - Value: "baz", - }, - { - Key: "FOO", - Value: "bar", - }, - }, - }, - { - description: "Create kv.Pairs from builtin files plugin", - sources: []types.KVSource{ - { - PluginType: "builtin", - Name: "files", - Args: []string{"files/app-init.ini"}, - }, - }, - expected: []kv.Pair{ - { - Key: "app-init.ini", - Value: "FOO=bar", - }, - }, - }, - { - description: "Create kv.Pairs from builtin envfiles plugin", - sources: []types.KVSource{ - { - PluginType: "builtin", - Name: "envfiles", - Args: []string{"files/app-init.ini"}, - }, - }, - expected: []kv.Pair{ - { - Key: "FOO", - Value: "bar", - }, - }, - }, - } - - fSys := fs.MakeFakeFS() - fSys.WriteFile("/files/app-init.ini", []byte("FOO=bar")) - ldr := loader.NewFileLoaderAtRoot(fSys) - reg := plugin.NewRegistry(ldr) - bf := NewFactory(ldr, nil, reg) - - for _, tc := range tests { - kvs, err := bf.keyValuesFromPlugins(tc.sources) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if !reflect.DeepEqual(kvs, tc.expected) { - t.Fatalf("in testcase: %q updated:\n%#v\ndoesn't match expected:\n%#v\n", tc.description, kvs, tc.expected) - } - } -} diff --git a/k8sdeps/configmapandsecret/secretfactory_test.go b/k8sdeps/configmapandsecret/secretfactory_test.go index 703de4257..b1e29fd4a 100644 --- a/k8sdeps/configmapandsecret/secretfactory_test.go +++ b/k8sdeps/configmapandsecret/secretfactory_test.go @@ -1,18 +1,5 @@ -/* -Copyright 2018 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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package configmapandsecret @@ -22,7 +9,6 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/types" @@ -140,9 +126,8 @@ func TestConstructSecret(t *testing.T) { fSys.WriteFile("/secret/app.env", []byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n")) fSys.WriteFile("/secret/app-init.ini", []byte("FOO=bar\nBAR=baz\n")) ldr := loader.NewFileLoaderAtRoot(fSys) - reg := plugin.NewRegistry(ldr) for _, tc := range testCases { - f := NewFactory(ldr, tc.options, reg) + f := NewFactory(ldr, tc.options) cm, err := f.MakeSecret(&tc.input) if err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/k8sdeps/kunstruct/factory.go b/k8sdeps/kunstruct/factory.go index 4cd945932..a2a2b3479 100644 --- a/k8sdeps/kunstruct/factory.go +++ b/k8sdeps/kunstruct/factory.go @@ -1,18 +1,5 @@ -/* -Copyright 2018 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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package kunstruct @@ -25,28 +12,19 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/util/yaml" "sigs.k8s.io/kustomize/k8sdeps/configmapandsecret" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/types" ) // KunstructuredFactoryImpl hides construction using apimachinery types. type KunstructuredFactoryImpl struct { - generatorMetaArgs *types.GeneratorMetaArgs } var _ ifc.KunstructuredFactory = &KunstructuredFactoryImpl{} // NewKunstructuredFactoryImpl returns a factory. func NewKunstructuredFactoryImpl() ifc.KunstructuredFactory { - return NewKunstructuredFactoryWithGeneratorArgs( - &types.GeneratorMetaArgs{}) -} - -// NewKunstructuredFactoryWithGeneratorArgs returns a factory. -func NewKunstructuredFactoryWithGeneratorArgs( - gma *types.GeneratorMetaArgs) ifc.KunstructuredFactory { - return &KunstructuredFactoryImpl{gma} + return &KunstructuredFactoryImpl{} } // SliceFromBytes returns a slice of Kunstructured. @@ -91,9 +69,7 @@ func (kf *KunstructuredFactoryImpl) MakeConfigMap( options *types.GeneratorOptions, args *types.ConfigMapArgs) (ifc.Kunstructured, error) { o, err := configmapandsecret.NewFactory( - ldr, options, - plugin.NewConfiguredRegistry( - ldr, kf.generatorMetaArgs.PluginConfig)).MakeConfigMap(args) + ldr, options).MakeConfigMap(args) if err != nil { return nil, err } @@ -106,9 +82,7 @@ func (kf *KunstructuredFactoryImpl) MakeSecret( options *types.GeneratorOptions, args *types.SecretArgs) (ifc.Kunstructured, error) { o, err := configmapandsecret.NewFactory( - ldr, options, - plugin.NewConfiguredRegistry( - ldr, kf.generatorMetaArgs.PluginConfig)).MakeSecret(args) + ldr, options).MakeSecret(args) if err != nil { return nil, err } diff --git a/k8sdeps/kv/plugin/builtin/envfiles.go b/k8sdeps/kv/plugin/builtin/envfiles.go deleted file mode 100644 index 816464f4f..000000000 --- a/k8sdeps/kv/plugin/builtin/envfiles.go +++ /dev/null @@ -1,51 +0,0 @@ -/* -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 builtin - -import ( - "sigs.k8s.io/kustomize/k8sdeps/kv" - "sigs.k8s.io/kustomize/pkg/ifc" -) - -// EnvFiles format should be a path to a file to read lines of key=val -// pairs to create a configmap. -// i.e. a Docker .env file or a .ini file. -type EnvFiles struct { - Ldr ifc.Loader -} - -// Get implements the interface for kv plugins. -func (p EnvFiles) Get(root string, args []string) (map[string]string, error) { - all := make(map[string]string) - for _, path := range args { - if path == "" { - return nil, nil - } - content, err := p.Ldr.Load(path) - if err != nil { - return nil, err - } - kvs, err := kv.KeyValuesFromLines(content) - if err != nil { - return nil, err - } - for _, pair := range kvs { - all[pair.Key] = pair.Value - } - } - return all, nil -} diff --git a/k8sdeps/kv/plugin/builtin/files.go b/k8sdeps/kv/plugin/builtin/files.go deleted file mode 100644 index 626dfb88c..000000000 --- a/k8sdeps/kv/plugin/builtin/files.go +++ /dev/null @@ -1,49 +0,0 @@ -/* -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 builtin - -import ( - "sigs.k8s.io/kustomize/k8sdeps/kv" - "sigs.k8s.io/kustomize/pkg/ifc" -) - -// Files is a list of file sources. -// Each file source can be specified using its file path, in which case file -// basename will be used as configmap key, or optionally with a key and file -// path, in which case the given key will be used. -// Specifying a directory will iterate each named file in the directory -// whose basename is a valid configmap key. -type Files struct { - Ldr ifc.Loader -} - -// Get implements the interface for kv plugins. -func (p Files) Get(root string, args []string) (map[string]string, error) { - kvs := make(map[string]string) - for _, s := range args { - k, fPath, err := kv.ParseFileSource(s) - if err != nil { - return nil, err - } - content, err := p.Ldr.Load(fPath) - if err != nil { - return nil, err - } - kvs[k] = string(content) - } - return kvs, nil -} diff --git a/k8sdeps/kv/plugin/builtin/literals.go b/k8sdeps/kv/plugin/builtin/literals.go deleted file mode 100644 index 364c1e8ff..000000000 --- a/k8sdeps/kv/plugin/builtin/literals.go +++ /dev/null @@ -1,39 +0,0 @@ -/* -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 builtin - -import ( - "sigs.k8s.io/kustomize/k8sdeps/kv" -) - -// Literals takes a list of literals. -// Each literal source should be a key and literal value, -// e.g. `somekey=somevalue` -type Literals struct{} - -// Get implements the interface for kv plugins. -func (p Literals) Get(root string, args []string) (map[string]string, error) { - kvs := make(map[string]string) - for _, s := range args { - k, v, err := kv.ParseLiteralSource(s) - if err != nil { - return nil, err - } - kvs[k] = v - } - return kvs, nil -} diff --git a/k8sdeps/kv/plugin/builtinplugin.go b/k8sdeps/kv/plugin/builtinplugin.go deleted file mode 100644 index 225de4d40..000000000 --- a/k8sdeps/kv/plugin/builtinplugin.go +++ /dev/null @@ -1,47 +0,0 @@ -/* -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 plugin - -import ( - "fmt" - - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin/builtin" - "sigs.k8s.io/kustomize/pkg/ifc" -) - -var _ Factory = &builtinFactory{} - -type builtinFactory struct { - plugins map[string]KVSource -} - -func newBuiltinFactory(ldr ifc.Loader) *builtinFactory { - return &builtinFactory{ - plugins: map[string]KVSource{ - "literals": builtin.Literals{}, - "files": builtin.Files{Ldr: ldr}, - "envfiles": builtin.EnvFiles{Ldr: ldr}, - }, - } -} - -func (p *builtinFactory) load(name string) (KVSource, error) { - if plug, ok := p.plugins[name]; ok { - return plug, nil - } - return nil, fmt.Errorf("plugin %s not found", name) -} diff --git a/k8sdeps/kv/plugin/goplugin.go b/k8sdeps/kv/plugin/goplugin.go deleted file mode 100644 index a4970f75f..000000000 --- a/k8sdeps/kv/plugin/goplugin.go +++ /dev/null @@ -1,94 +0,0 @@ -/* -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 plugin - -import ( - "fmt" - "path/filepath" - "plugin" - - "sigs.k8s.io/kustomize/pkg/types" -) - -var _ Factory = &goFactory{} - -const ( - kvSourcesDir = "kvSources" - EnableGoPluginsFlagName = "enable_alpha_goplugins_accept_panic_risk" - EnableGoPluginsFlagHelp = `The main program may panic and exit on an attempt -to use a goplugin that was compiled under conditions -differing from the those in effect when main was -compiled. It's safest to use this flag in the -context of a container image holding both the main -and the goplugins it needs, all built on the same -machine, with the same transitive libs and the same -compiler version.` - errorFmt = ` -enable go plugins by specifying flag - --%s -Place .so files in - %s -%s` -) - -func newGoFactory(c *types.PluginConfig) *goFactory { - return &goFactory{ - config: c, - plugins: make(map[string]KVSource), - } -} - -type goFactory struct { - config *types.PluginConfig - plugins map[string]KVSource -} - -func (p *goFactory) load(name string) (KVSource, error) { - if plug, ok := p.plugins[name]; ok { - return plug, nil - } - - dir := filepath.Join( - p.config.DirectoryPath, - kvSourcesDir) - if !p.config.GoEnabled { - return nil, fmt.Errorf( - errorFmt, - EnableGoPluginsFlagName, - dir, - EnableGoPluginsFlagHelp) - } - - goPlugin, err := plugin.Open( - filepath.Join(dir, name+".so")) - if err != nil { - return nil, err - } - - symbol, err := goPlugin.Lookup("KVSource") - if err != nil { - return nil, err - } - - plug, ok := symbol.(KVSource) - if !ok { - return nil, fmt.Errorf("plugin %s not found", name) - } - - p.plugins[name] = plug - return plug, nil -} diff --git a/k8sdeps/kv/plugin/plugin.go b/k8sdeps/kv/plugin/plugin.go deleted file mode 100644 index 29975999b..000000000 --- a/k8sdeps/kv/plugin/plugin.go +++ /dev/null @@ -1,28 +0,0 @@ -/* -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 plugin provides a plugin abstraction layer. -package plugin - -// KVSource is the interface for kv source plugins. -type KVSource interface { - Get(root string, args []string) (map[string]string, error) -} - -// Factory is the interface for new kv source plugin implementations. -type Factory interface { - load(string) (KVSource, error) -} diff --git a/k8sdeps/kv/plugin/registry.go b/k8sdeps/kv/plugin/registry.go deleted file mode 100644 index adadedcd4..000000000 --- a/k8sdeps/kv/plugin/registry.go +++ /dev/null @@ -1,87 +0,0 @@ -/* -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 plugin - -import ( - "fmt" - "path/filepath" - - "sigs.k8s.io/kustomize/pkg/ifc" - "sigs.k8s.io/kustomize/pkg/pgmconfig" - "sigs.k8s.io/kustomize/pkg/types" -) - -// Registry holds all the plugin factories. -type Registry struct { - factories map[types.PluginType]Factory - ldr ifc.Loader -} - -const ( - pluginTypeGo = types.PluginType("go") - pluginTypeBuiltIn = types.PluginType("builtin") -) - -func ActivePluginConfig() *types.PluginConfig { - pc := DefaultPluginConfig() - pc.GoEnabled = true - return pc -} - -func DefaultPluginConfig() *types.PluginConfig { - return &types.PluginConfig{ - GoEnabled: false, - DirectoryPath: filepath.Join( - pgmconfig.ConfigRoot(), pgmconfig.PluginRoot), - } -} - -// NewConfiguredRegistry returns a new Registry loaded -// with all the factories and a custom PluginConfig. -func NewConfiguredRegistry( - ldr ifc.Loader, pc *types.PluginConfig) Registry { - return Registry{ - ldr: ldr, - factories: map[types.PluginType]Factory{ - pluginTypeGo: newGoFactory(pc), - pluginTypeBuiltIn: newBuiltinFactory(ldr), - }, - } -} - -// NewRegistry returns a new Registry with default config. -func NewRegistry(ldr ifc.Loader) Registry { - return NewConfiguredRegistry(ldr, &types.PluginConfig{}) -} - -// Load returns a plugin by type and name. -func (r *Registry) Load( - pt types.PluginType, name string) (KVSource, error) { - if pt.IsUndefined() { - pt = pluginTypeBuiltIn - } - factory, exists := r.factories[pt] - if !exists { - return nil, fmt.Errorf("%s is not a valid plugin type", pt) - } - return factory.load(name) -} - -// Root returns the root of the plugins kustomization file. -func (r *Registry) Root() string { - return r.ldr.Root() -} diff --git a/pkg/commands/build/build.go b/pkg/commands/build/build.go index 05fcd2896..95d2f7cb8 100644 --- a/pkg/commands/build/build.go +++ b/pkg/commands/build/build.go @@ -1,18 +1,5 @@ -/* -Copyright 2017 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. -*/ +/// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package build @@ -69,10 +56,12 @@ url examples: func NewCmdBuild( out io.Writer, fs fs.FileSystem, rf *resmap.Factory, - ptf transformer.Factory, - pl *plugins.Loader) *cobra.Command { + ptf transformer.Factory) *cobra.Command { var o Options + pluginConfig := plugins.DefaultPluginConfig() + pl := plugins.NewLoader(pluginConfig, rf) + cmd := &cobra.Command{ Use: "build [path]", Short: "Print current configuration per contents of " + pgmconfig.KustomizationFileNames[0], @@ -86,11 +75,14 @@ func NewCmdBuild( return o.RunBuild(out, fs, rf, ptf, pl) }, } + cmd.Flags().StringVarP( &o.outputPath, "output", "o", "", "If specified, write the build output to this path.") loader.AddLoadRestrictionsFlag(cmd.Flags()) + plugins.AddEnablePluginsFlag( + cmd.Flags(), &pluginConfig.Enabled) cmd.AddCommand(NewCmdBuildPrune(out, fs, rf, ptf, pl)) return cmd diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index e6396f554..782a9c07b 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -1,18 +1,5 @@ -/* -Copyright 2017 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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 // Package commands holds the CLI glue mapping textual commands/args to method calls. package commands @@ -23,7 +10,6 @@ import ( "github.com/spf13/cobra" "sigs.k8s.io/kustomize/k8sdeps/kunstruct" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/k8sdeps/transformer" "sigs.k8s.io/kustomize/k8sdeps/validator" "sigs.k8s.io/kustomize/pkg/commands/build" @@ -31,10 +17,8 @@ import ( "sigs.k8s.io/kustomize/pkg/commands/misc" "sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/pgmconfig" - "sigs.k8s.io/kustomize/pkg/plugins" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" - "sigs.k8s.io/kustomize/pkg/types" ) // NewDefaultCommand returns the default (aka root) command for kustomize command. @@ -51,27 +35,12 @@ See https://sigs.k8s.io/kustomize `, } - pluginConfig := plugin.DefaultPluginConfig() - - c.PersistentFlags().BoolVar( - &pluginConfig.GoEnabled, - plugin.EnableGoPluginsFlagName, - false, plugin.EnableGoPluginsFlagHelp) - // Not advertising this alpha feature. - c.PersistentFlags().MarkHidden(plugin.EnableGoPluginsFlagName) - - // Configuration for ConfigMap and Secret generators. - genMetaArgs := types.GeneratorMetaArgs{ - PluginConfig: pluginConfig, - } - uf := kunstruct.NewKunstructuredFactoryWithGeneratorArgs(&genMetaArgs) + uf := kunstruct.NewKunstructuredFactoryImpl() rf := resmap.NewFactory(resource.NewFactory(uf)) c.AddCommand( build.NewCmdBuild( stdOut, fSys, - rf, - transformer.NewFactoryImpl(), - plugins.NewLoader(pluginConfig, rf)), + rf, transformer.NewFactoryImpl()), edit.NewCmdEdit(fSys, validator.NewKustValidator(), uf), misc.NewCmdConfig(fSys), misc.NewCmdVersion(stdOut), diff --git a/pkg/commands/edit/add/configmap.go b/pkg/commands/edit/add/configmap.go index 5e8361bdc..5e2e3924a 100644 --- a/pkg/commands/edit/add/configmap.go +++ b/pkg/commands/edit/add/configmap.go @@ -1,18 +1,5 @@ -/* -Copyright 2017 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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package add @@ -104,17 +91,17 @@ func addConfigMap( ldr ifc.Loader, k *types.Kustomization, flags flagsAndArgs, kf ifc.KunstructuredFactory) error { - cmArgs := makeConfigMapArgs(k, flags.Name) - mergeFlagsIntoCmArgs(&cmArgs.KVSources, flags) + args := findOrMakeConfigMapArgs(k, flags.Name) + mergeFlagsIntoCmArgs(args, flags) // Validate by trying to create corev1.configmap. - _, err := kf.MakeConfigMap(ldr, k.GeneratorOptions, cmArgs) + _, err := kf.MakeConfigMap(ldr, k.GeneratorOptions, args) if err != nil { return err } return nil } -func makeConfigMapArgs(m *types.Kustomization, name string) *types.ConfigMapArgs { +func findOrMakeConfigMapArgs(m *types.Kustomization, name string) *types.ConfigMapArgs { for i, v := range m.ConfigMapGenerator { if name == v.Name { return &m.ConfigMapGenerator[i] @@ -126,23 +113,17 @@ func makeConfigMapArgs(m *types.Kustomization, name string) *types.ConfigMapArgs return &m.ConfigMapGenerator[len(m.ConfigMapGenerator)-1] } -func mergeFlagsIntoCmArgs(src *[]types.KVSource, flags flagsAndArgs) { +func mergeFlagsIntoCmArgs(args *types.ConfigMapArgs, flags flagsAndArgs) { if len(flags.LiteralSources) > 0 { - *src = append(*src, types.KVSource{ - Name: "literals", - Args: flags.LiteralSources, - }) + args.LiteralSources = append( + args.LiteralSources, flags.LiteralSources...) } if len(flags.FileSources) > 0 { - *src = append(*src, types.KVSource{ - Name: "files", - Args: flags.FileSources, - }) + args.FileSources = append( + args.FileSources, flags.FileSources...) } if flags.EnvFileSource != "" { - *src = append(*src, types.KVSource{ - Name: "envfiles", - Args: []string{flags.EnvFileSource}, - }) + args.EnvSources = append( + args.EnvSources, flags.EnvFileSource) } } diff --git a/pkg/commands/edit/add/configmap_test.go b/pkg/commands/edit/add/configmap_test.go index 2aaa99e8f..5b1b2d7e7 100644 --- a/pkg/commands/edit/add/configmap_test.go +++ b/pkg/commands/edit/add/configmap_test.go @@ -1,18 +1,5 @@ -/* -Copyright 2017 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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package add @@ -39,7 +26,7 @@ func TestMakeConfigMapArgs(t *testing.T) { if len(kustomization.ConfigMapGenerator) != 0 { t.Fatal("Initial kustomization should not have any configmaps") } - args := makeConfigMapArgs(kustomization, cmName) + args := findOrMakeConfigMapArgs(kustomization, cmName) if args == nil { t.Fatalf("args should always be non-nil") @@ -53,7 +40,7 @@ func TestMakeConfigMapArgs(t *testing.T) { t.Fatalf("Pointer address for newly inserted configmap generator should be same") } - args2 := makeConfigMapArgs(kustomization, cmName) + args2 := findOrMakeConfigMapArgs(kustomization, cmName) if args2 != args { t.Fatalf("should have returned an existing args with name: %v", cmName) @@ -64,51 +51,53 @@ func TestMakeConfigMapArgs(t *testing.T) { } } -func TestMergeFlagsIntoCmArgs_LiteralSources(t *testing.T) { - var kv []types.KVSource - - mergeFlagsIntoCmArgs(&kv, flagsAndArgs{LiteralSources: []string{"k1=v1"}}) - - if len(kv) != 1 { - t.Fatalf("Initial literal source should have been added") +func TestMergeFlagsIntoConfigMapArgs_LiteralSources(t *testing.T) { + k := &types.Kustomization{} + args := findOrMakeConfigMapArgs(k, "foo") + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{LiteralSources: []string{"k1=v1"}}) + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{LiteralSources: []string{"k2=v2"}}) + if k.ConfigMapGenerator[0].LiteralSources[0] != "k1=v1" { + t.Fatalf("expected v1") } - - mergeFlagsIntoCmArgs(&kv, flagsAndArgs{LiteralSources: []string{"k2=v2"}}) - - if len(kv) != 2 { - t.Fatalf("Second literal source should have been added") + if k.ConfigMapGenerator[0].LiteralSources[1] != "k2=v2" { + t.Fatalf("expected v2") } } -func TestMergeFlagsIntoCmArgs_FileSources(t *testing.T) { - var kv []types.KVSource - - mergeFlagsIntoCmArgs(&kv, flagsAndArgs{FileSources: []string{"file1"}}) - - if len(kv) != 1 { - t.Fatalf("Initial file source should have been added") +func TestMergeFlagsIntoConfigMapArgs_FileSources(t *testing.T) { + k := &types.Kustomization{} + args := findOrMakeConfigMapArgs(k, "foo") + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{FileSources: []string{"file1"}}) + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{FileSources: []string{"file2"}}) + if k.ConfigMapGenerator[0].FileSources[0] != "file1" { + t.Fatalf("expected file1") } - - mergeFlagsIntoCmArgs(&kv, flagsAndArgs{FileSources: []string{"file2"}}) - - if len(kv) != 2 { - t.Fatalf("Second file source should have been added") + if k.ConfigMapGenerator[0].FileSources[1] != "file2" { + t.Fatalf("expected file2") } } -func TestMergeFlagsIntoCmArgs_EnvSource(t *testing.T) { - envFileName := "env1" - envFileName2 := "env2" - var kv []types.KVSource - - mergeFlagsIntoCmArgs(&kv, flagsAndArgs{EnvFileSource: envFileName}) - - if len(kv) != 1 { - t.Fatalf("Initial env source should have been added") +func TestMergeFlagsIntoConfigMapArgs_EnvSource(t *testing.T) { + k := &types.Kustomization{} + args := findOrMakeConfigMapArgs(k, "foo") + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{EnvFileSource: "env1"}) + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{EnvFileSource: "env2"}) + if k.ConfigMapGenerator[0].EnvSources[0] != "env1" { + t.Fatalf("expected env1") } - - mergeFlagsIntoCmArgs(&kv, flagsAndArgs{EnvFileSource: envFileName2}) - if len(kv) != 2 { - t.Fatalf("Second env source should have been added") + if k.ConfigMapGenerator[0].EnvSources[1] != "env2" { + t.Fatalf("expected env2") } } diff --git a/pkg/commands/edit/add/secret.go b/pkg/commands/edit/add/secret.go index aaaf2267b..8749f17d3 100644 --- a/pkg/commands/edit/add/secret.go +++ b/pkg/commands/edit/add/secret.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package add @@ -109,45 +96,41 @@ func addSecret( ldr ifc.Loader, k *types.Kustomization, flags flagsAndArgs, kf ifc.KunstructuredFactory) error { - secretArgs := makeSecretArgs(k, flags.Name, flags.Type) - mergeFlagsIntoSecretArgs(&secretArgs.KVSources, flags) + args := findOrMakeSecretArgs(k, flags.Name, flags.Type) + mergeFlagsIntoGeneratorArgs(&args.GeneratorArgs, flags) // Validate by trying to create corev1.secret. - _, err := kf.MakeSecret(ldr, k.GeneratorOptions, secretArgs) + _, err := kf.MakeSecret(ldr, k.GeneratorOptions, args) if err != nil { return err } return nil } -func makeSecretArgs(m *types.Kustomization, name, secretType string) *types.SecretArgs { +func findOrMakeSecretArgs(m *types.Kustomization, name, secretType string) *types.SecretArgs { for i, v := range m.SecretGenerator { if name == v.Name { return &m.SecretGenerator[i] } } // secret not found, create new one and add it to the kustomization file. - secret := &types.SecretArgs{GeneratorArgs: types.GeneratorArgs{Name: name}, Type: secretType} + secret := &types.SecretArgs{ + GeneratorArgs: types.GeneratorArgs{Name: name}, + Type: secretType} m.SecretGenerator = append(m.SecretGenerator, *secret) return &m.SecretGenerator[len(m.SecretGenerator)-1] } -func mergeFlagsIntoSecretArgs(src *[]types.KVSource, flags flagsAndArgs) { +func mergeFlagsIntoGeneratorArgs(args *types.GeneratorArgs, flags flagsAndArgs) { if len(flags.LiteralSources) > 0 { - *src = append(*src, types.KVSource{ - Name: "literals", - Args: flags.LiteralSources, - }) + args.LiteralSources = append( + args.LiteralSources, flags.LiteralSources...) } if len(flags.FileSources) > 0 { - *src = append(*src, types.KVSource{ - Name: "files", - Args: flags.FileSources, - }) + args.FileSources = append( + args.FileSources, flags.FileSources...) } if flags.EnvFileSource != "" { - *src = append(*src, types.KVSource{ - Name: "envfiles", - Args: []string{flags.EnvFileSource}, - }) + args.EnvSources = append( + args.EnvSources, flags.EnvFileSource) } } diff --git a/pkg/commands/edit/add/secret_test.go b/pkg/commands/edit/add/secret_test.go index 476e48ac1..6df3f90dd 100644 --- a/pkg/commands/edit/add/secret_test.go +++ b/pkg/commands/edit/add/secret_test.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package add @@ -41,7 +28,7 @@ func TestMakeSecretArgs(t *testing.T) { if len(kustomization.SecretGenerator) != 0 { t.Fatal("Initial kustomization should not have any secrets") } - args := makeSecretArgs(kustomization, secretName, secretType) + args := findOrMakeSecretArgs(kustomization, secretName, secretType) if args == nil { t.Fatalf("args should always be non-nil") @@ -55,7 +42,7 @@ func TestMakeSecretArgs(t *testing.T) { t.Fatalf("Pointer address for newly inserted secret generator should be same") } - args2 := makeSecretArgs(kustomization, secretName, secretType) + args2 := findOrMakeSecretArgs(kustomization, secretName, secretType) if args2 != args { t.Fatalf("should have returned an existing args with name: %v", secretName) @@ -67,50 +54,52 @@ func TestMakeSecretArgs(t *testing.T) { } func TestMergeFlagsIntoSecretArgs_LiteralSources(t *testing.T) { - var kv []types.KVSource - - mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{LiteralSources: []string{"k1=v1"}}) - - if len(kv) != 1 { - t.Fatalf("Initial literal source should have been added") + k := &types.Kustomization{} + args := findOrMakeSecretArgs(k, "foo", "forbidden") + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{LiteralSources: []string{"k1=v1"}}) + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{LiteralSources: []string{"k2=v2"}}) + if k.SecretGenerator[0].LiteralSources[0] != "k1=v1" { + t.Fatalf("expected v1") } - - mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{LiteralSources: []string{"k2=v2"}}) - - if len(kv) != 2 { - t.Fatalf("Second literal source should have been added") + if k.SecretGenerator[0].LiteralSources[1] != "k2=v2" { + t.Fatalf("expected v2") } } func TestMergeFlagsIntoSecretArgs_FileSources(t *testing.T) { - var kv []types.KVSource - - mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{FileSources: []string{"file1"}}) - - if len(kv) != 1 { - t.Fatalf("Initial file source should have been added") + k := &types.Kustomization{} + args := findOrMakeSecretArgs(k, "foo", "forbidden") + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{FileSources: []string{"file1"}}) + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{FileSources: []string{"file2"}}) + if k.SecretGenerator[0].FileSources[0] != "file1" { + t.Fatalf("expected file1") } - - mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{FileSources: []string{"file2"}}) - - if len(kv) != 2 { - t.Fatalf("Second file source should have been added") + if k.SecretGenerator[0].FileSources[1] != "file2" { + t.Fatalf("expected file2") } } func TestMergeFlagsIntoSecretArgs_EnvSource(t *testing.T) { - envFileName := "env1" - envFileName2 := "env2" - var kv []types.KVSource - - mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{EnvFileSource: envFileName}) - - if len(kv) != 1 { - t.Fatalf("Initial env source should have been added") + k := &types.Kustomization{} + args := findOrMakeSecretArgs(k, "foo", "forbidden") + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{EnvFileSource: "env1"}) + mergeFlagsIntoGeneratorArgs( + &args.GeneratorArgs, + flagsAndArgs{EnvFileSource: "env2"}) + if k.SecretGenerator[0].EnvSources[0] != "env1" { + t.Fatalf("expected env1") } - - mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{EnvFileSource: envFileName2}) - if len(kv) != 2 { - t.Fatalf("Second env source should have been added") + if k.SecretGenerator[0].EnvSources[1] != "env2" { + t.Fatalf("expected env2") } } diff --git a/pkg/kusttest/kusttestharness.go b/pkg/kusttest/kusttestharness.go index c22c93841..fa10aa645 100644 --- a/pkg/kusttest/kusttestharness.go +++ b/pkg/kusttest/kusttestharness.go @@ -11,7 +11,6 @@ import ( "sigs.k8s.io/kustomize/internal/loadertest" "sigs.k8s.io/kustomize/k8sdeps/kunstruct" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/k8sdeps/transformer" "sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/pgmconfig" @@ -33,12 +32,12 @@ type KustTestHarness struct { func NewKustTestHarness(t *testing.T, path string) *KustTestHarness { return NewKustTestHarnessWithPluginConfig( - t, path, plugin.DefaultPluginConfig()) + t, path, plugins.DefaultPluginConfig()) } func NewKustTestPluginHarness(t *testing.T, path string) *KustTestHarness { return NewKustTestHarnessWithPluginConfig( - t, path, plugin.ActivePluginConfig()) + t, path, plugins.ActivePluginConfig()) } func NewKustTestHarnessWithPluginConfig( @@ -51,8 +50,7 @@ func NewKustTestHarnessFull( t *testing.T, path string, lr loader.LoadRestrictorFunc, pc *types.PluginConfig) *KustTestHarness { rf := resmap.NewFactory(resource.NewFactory( - kunstruct.NewKunstructuredFactoryWithGeneratorArgs( - &types.GeneratorMetaArgs{PluginConfig: pc}))) + kunstruct.NewKunstructuredFactoryImpl())) return &KustTestHarness{ t: t, rf: rf, diff --git a/pkg/plugins/compiler.go b/pkg/plugins/compiler.go index 601b45065..2ae69507e 100644 --- a/pkg/plugins/compiler.go +++ b/pkg/plugins/compiler.go @@ -24,7 +24,6 @@ import ( "strings" "time" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/pgmconfig" ) @@ -56,7 +55,7 @@ func DefaultSrcRoot() (string, error) { } nope = append(nope, root) - root = plugin.DefaultPluginConfig().DirectoryPath + root = DefaultPluginConfig().DirectoryPath if FileExists(root) { return root, nil } diff --git a/pkg/plugins/config.go b/pkg/plugins/config.go index 24c94ba65..aea287dbd 100644 --- a/pkg/plugins/config.go +++ b/pkg/plugins/config.go @@ -1,19 +1,53 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package plugins -const PluginSymbol = "KustomizePlugin" +import ( + "fmt" + "github.com/spf13/pflag" + "path/filepath" + "sigs.k8s.io/kustomize/pkg/pgmconfig" + "sigs.k8s.io/kustomize/pkg/types" +) + +const ( + PluginSymbol = "KustomizePlugin" + flagEnablePluginsName = "enable_alpha_plugins" + flagEnablePluginsHelp = `enable plugins, an alpha feature. +See https://github.com/kubernetes-sigs/kustomize/blob/master/docs/plugins.md +` + flagErrorFmt = ` +unable to load plugin %s because plugins disabled +specify the flag + --%s +to %s` +) + +func ActivePluginConfig() *types.PluginConfig { + pc := DefaultPluginConfig() + pc.Enabled = true + return pc +} + +func DefaultPluginConfig() *types.PluginConfig { + return &types.PluginConfig{ + Enabled: false, + DirectoryPath: filepath.Join( + pgmconfig.ConfigRoot(), pgmconfig.PluginRoot), + } +} + +func PluginsNotEnabledErr(name string) error { + return fmt.Errorf( + flagErrorFmt, + name, + flagEnablePluginsName, + flagEnablePluginsHelp) +} + +func AddEnablePluginsFlag(set *pflag.FlagSet, v *bool) { + set.BoolVar( + v, flagEnablePluginsName, + false, flagEnablePluginsHelp) +} diff --git a/pkg/plugins/execplugin_test.go b/pkg/plugins/execplugin_test.go index 2b0daf8ad..ac04cd5ac 100644 --- a/pkg/plugins/execplugin_test.go +++ b/pkg/plugins/execplugin_test.go @@ -22,7 +22,6 @@ import ( "sigs.k8s.io/kustomize/internal/loadertest" "sigs.k8s.io/kustomize/k8sdeps/kunstruct" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" ) @@ -52,7 +51,7 @@ s/$BAR/bar/g p := NewExecPlugin( AbsolutePluginPath( - plugin.DefaultPluginConfig(), + DefaultPluginConfig(), pluginConfig.Id())) yaml, err := pluginConfig.AsYAML() diff --git a/pkg/plugins/loader.go b/pkg/plugins/loader.go index c5ef29ce0..97509a2ff 100644 --- a/pkg/plugins/loader.go +++ b/pkg/plugins/loader.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package plugins @@ -115,9 +102,8 @@ func (l *Loader) absolutePluginPath(id resid.ResId) string { func (l *Loader) loadAndConfigurePlugin( ldr ifc.Loader, res *resource.Resource) (c Configurable, err error) { - if !l.pc.GoEnabled { - return nil, errors.Errorf( - "plugins not enabled, but trying to load %s", res.Id()) + if !l.pc.Enabled { + return nil, PluginsNotEnabledErr(res.Id().Gvk().Kind) } if p := NewExecPlugin( l.absolutePluginPath(res.Id())); p.isAvailable() { diff --git a/pkg/plugins/loader_test.go b/pkg/plugins/loader_test.go index 69d12626a..89b30fbf2 100644 --- a/pkg/plugins/loader_test.go +++ b/pkg/plugins/loader_test.go @@ -8,8 +8,7 @@ import ( "sigs.k8s.io/kustomize/internal/loadertest" "sigs.k8s.io/kustomize/k8sdeps/kunstruct" - kvplugin "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" - "sigs.k8s.io/kustomize/pkg/plugins" + . "sigs.k8s.io/kustomize/pkg/plugins" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/plugin" @@ -54,7 +53,7 @@ func TestLoader(t *testing.T) { rmF := resmap.NewFactory(resource.NewFactory( kunstruct.NewKunstructuredFactoryImpl())) - l := plugins.NewLoader(kvplugin.ActivePluginConfig(), rmF) + l := NewLoader(ActivePluginConfig(), rmF) if l == nil { t.Fatal("expect non-nil loader") } diff --git a/pkg/target/baseandoverlaysmall_test.go b/pkg/target/baseandoverlaysmall_test.go index 3bbf12996..c06f9c4a3 100644 --- a/pkg/target/baseandoverlaysmall_test.go +++ b/pkg/target/baseandoverlaysmall_test.go @@ -17,10 +17,10 @@ limitations under the License. package target_test import ( + "sigs.k8s.io/kustomize/pkg/plugins" "strings" "testing" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/kusttest" "sigs.k8s.io/kustomize/pkg/loader" ) @@ -225,7 +225,7 @@ spec: func TestSharedPatchDisAllowed(t *testing.T) { th := kusttest_test.NewKustTestHarnessFull( t, "/app/overlay", - loader.RestrictionRootOnly, plugin.DefaultPluginConfig()) + loader.RestrictionRootOnly, plugins.DefaultPluginConfig()) writeSmallBase(th) th.WriteK("/app/overlay", ` commonLabels: @@ -257,7 +257,7 @@ spec: func TestSharedPatchAllowed(t *testing.T) { th := kusttest_test.NewKustTestHarnessFull( t, "/app/overlay", - loader.RestrictionNone, plugin.DefaultPluginConfig()) + loader.RestrictionNone, plugins.DefaultPluginConfig()) writeSmallBase(th) th.WriteK("/app/overlay", ` commonLabels: diff --git a/pkg/target/builtinplugins_test.go b/pkg/target/builtinplugins_test.go deleted file mode 100644 index c7a190cd7..000000000 --- a/pkg/target/builtinplugins_test.go +++ /dev/null @@ -1,101 +0,0 @@ -/* -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 ( - "testing" - - "sigs.k8s.io/kustomize/pkg/kusttest" -) - -const result = ` -apiVersion: v1 -data: - FRUIT: YXBwbGU= - MOUNTAIN: ZXZlcmVzdA== - OCEAN: cGFjaWZpYw== - VEGETABLE: Y2Fycm90 - foo.env: Ck1PVU5UQUlOPWV2ZXJlc3QKT0NFQU49cGFjaWZpYwo= - passphrase: ZGF0IHBocmFzZQ== -kind: Secret -metadata: - name: bob-kf5c9fccbt -type: Opaque -` - -func writeDataFiles(th *kusttest_test.KustTestHarness) { - th.WriteF("/app/foo.env", ` -MOUNTAIN=everest -OCEAN=pacific -`) - th.WriteF("/app/phrase.dat", "dat phrase") -} - -func TestBuiltinPlugins(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app") - th.WriteK("/app", ` -secretGenerator: -- name: bob - kvSources: - - pluginType: builtin - name: literals - args: - - FRUIT=apple - - VEGETABLE=carrot - - pluginType: builtin - name: files - args: - - foo.env - - passphrase=phrase.dat - - pluginType: builtin - name: envfiles - args: - - foo.env -`) - writeDataFiles(th) - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } - th.AssertActualEqualsExpected(m, result) -} - -func TestBuiltinIsTheDefault(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app") - th.WriteK("/app", ` -secretGenerator: -- name: bob - kvSources: - - name: literals - args: - - FRUIT=apple - - VEGETABLE=carrot - - name: files - args: - - foo.env - - passphrase=phrase.dat - - name: envfiles - args: - - foo.env -`) - writeDataFiles(th) - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } - th.AssertActualEqualsExpected(m, result) -} diff --git a/pkg/target/generatoroptions_test.go b/pkg/target/generatoroptions_test.go index 80eb85d03..b4a8ce578 100644 --- a/pkg/target/generatoroptions_test.go +++ b/pkg/target/generatoroptions_test.go @@ -17,14 +17,50 @@ limitations under the License. package target_test import ( - "path/filepath" - "strings" "testing" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/kusttest" ) +func TestSecretGenerator(t *testing.T) { + th := kusttest_test.NewKustTestHarness(t, "/app") + th.WriteK("/app", ` +secretGenerator: +- name: bob + literals: + - FRUIT=apple + - VEGETABLE=carrot + files: + - foo.env + - passphrase=phrase.dat + envs: + - foo.env +`) + th.WriteF("/app/foo.env", ` +MOUNTAIN=everest +OCEAN=pacific +`) + th.WriteF("/app/phrase.dat", "dat phrase") + m, err := th.MakeKustTarget().MakeCustomizedResMap() + if err != nil { + t.Fatalf("Err: %v", err) + } + th.AssertActualEqualsExpected(m, ` +apiVersion: v1 +data: + FRUIT: YXBwbGU= + MOUNTAIN: ZXZlcmVzdA== + OCEAN: cGFjaWZpYw== + VEGETABLE: Y2Fycm90 + foo.env: Ck1PVU5UQUlOPWV2ZXJlc3QKT0NFQU49cGFjaWZpYwo= + passphrase: ZGF0IHBocmFzZQ== +kind: Secret +metadata: + name: bob-kf5c9fccbt +type: Opaque +`) +} + func TestGeneratorOptionsWithBases(t *testing.T) { th := kusttest_test.NewKustTestHarness(t, "/app/overlay") th.WriteK("/app/base", ` @@ -77,47 +113,3 @@ metadata: name: shouldNotHaveHash `) } - -func TestGoPluginNotEnabled(t *testing.T) { - th := kusttest_test.NewKustTestHarness(t, "/app") - th.WriteK("/app", ` -secretGenerator: -- name: attemptGoPlugin - kvSources: - - name: foo - pluginType: go - args: - - someArg - - someOtherArg -`) - _, err := th.MakeKustTarget().MakeCustomizedResMap() - if err == nil { - t.Fatalf("expected error") - } - if !strings.Contains(err.Error(), "enable go plugins by ") { - t.Fatalf("unexpected err: %v", err) - } -} - -func TestGoPluginDoesNotExist(t *testing.T) { - th := kusttest_test.NewKustTestHarnessWithPluginConfig( - t, "/app", plugin.ActivePluginConfig()) - th.WriteK("/app", ` -secretGenerator: -- name: attemptGoPlugin - kvSources: - - name: foo - pluginType: go - args: - - someArg - - someOtherArg -`) - _, err := th.MakeKustTarget().MakeCustomizedResMap() - if err == nil { - t.Fatalf("expected error") - } - if !strings.Contains(err.Error(), - filepath.Join("kvSources", "foo.so")) { - t.Fatalf("unexpected err: %v", err) - } -} diff --git a/pkg/target/plugindir_test.go b/pkg/target/plugindir_test.go index bb9b100b3..5ff02de02 100644 --- a/pkg/target/plugindir_test.go +++ b/pkg/target/plugindir_test.go @@ -10,7 +10,6 @@ import ( "testing" "sigs.k8s.io/kustomize/k8sdeps/kunstruct" - kvplugin "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/k8sdeps/transformer" "sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/kusttest" @@ -19,7 +18,6 @@ import ( "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/target" - "sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/kustomize/plugin" ) @@ -63,12 +61,9 @@ metadata: t.Fatalf("Err: %v", err) } rf := resmap.NewFactory(resource.NewFactory( - kunstruct.NewKunstructuredFactoryWithGeneratorArgs( - &types.GeneratorMetaArgs{ - PluginConfig: kvplugin.ActivePluginConfig(), - }))) + kunstruct.NewKunstructuredFactoryImpl())) - pl := plugins.NewLoader(kvplugin.ActivePluginConfig(), rf) + pl := plugins.NewLoader(plugins.ActivePluginConfig(), rf) tg, err := target.NewKustTarget(ldr, rf, transformer.NewFactoryImpl(), pl) if err != nil { t.Fatalf("err %v", err) diff --git a/pkg/target/transformerplugin_test.go b/pkg/target/transformerplugin_test.go index 530365fc0..520f846b6 100644 --- a/pkg/target/transformerplugin_test.go +++ b/pkg/target/transformerplugin_test.go @@ -4,6 +4,7 @@ package target_test import ( + "strings" "testing" "sigs.k8s.io/kustomize/pkg/kusttest" @@ -91,6 +92,29 @@ spec: `) } +func TestPluginsNotEnabled(t *testing.T) { + tc := plugin.NewEnvForTest(t).Set() + defer tc.Reset() + + tc.BuildGoPlugin( + "someteam.example.com", "v1", "StringPrefixer") + + th := kusttest_test.NewKustTestHarness(t, "/app") + th.WriteK("/app", ` +transformers: +- stringPrefixer.yaml +`) + writeStringPrefixer(th, "/app/stringPrefixer.yaml") + + _, err := th.MakeKustTarget().MakeCustomizedResMap() + if err == nil { + t.Fatalf("expected error") + } + if !strings.Contains(err.Error(), "unable to load plugin StringPrefixer") { + t.Fatalf("unexpected err: %v", err) + } +} + func TestSedTransformer(t *testing.T) { tc := plugin.NewEnvForTest(t).Set() defer tc.Reset() diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index 3bba41e2a..5b9931b18 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -222,15 +222,6 @@ type GeneratorArgs struct { // DataSources for the generator. DataSources `json:",inline,omitempty" yaml:",inline,omitempty"` - - // KVSources for the generator. - KVSources []KVSource `json:",inline,omitempty" yaml:",inline,omitempty"` -} - -// GeneratorMetaArgs contains arguments common to generators -// that come from somewhere other than a kustomization file. -type GeneratorMetaArgs struct { - PluginConfig *PluginConfig } // PluginConfig holds plugin configuration. @@ -241,9 +232,8 @@ type PluginConfig struct { // further categorizing plugins. DirectoryPath string - // GoEnabled is true if goplugins are enabled. - // See https://golang.org/pkg/plugin - GoEnabled bool + // Enabled is true if plugins are enabled. + Enabled bool } // ConfigMapArgs contains the metadata of how to generate a configmap.