Delete the KV plugin code.

This commit is contained in:
Jeffrey Regan
2019-05-24 14:27:36 -07:00
parent e4205c125c
commit 6a10654618
33 changed files with 278 additions and 1063 deletions

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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)
}
}
}

View File

@@ -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)

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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()
}