mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-09-15 12:18:57 +00:00
replace commands/envcommand by DataSource in SecretGenerator
This commit is contained in:
@@ -85,35 +85,23 @@ configMapGenerator:
|
|||||||
|
|
||||||
# Each entry in this list results in the creation of
|
# Each entry in this list results in the creation of
|
||||||
# one Secret resource (it's a generator of n secrets).
|
# one Secret resource (it's a generator of n secrets).
|
||||||
# A command can do anything to get a secret,
|
|
||||||
# e.g. prompt the user directly, start a webserver to
|
|
||||||
# initate an oauth dance, etc.
|
|
||||||
secretGenerator:
|
secretGenerator:
|
||||||
- name: app-tls
|
- name: app-tls
|
||||||
commands:
|
files:
|
||||||
tls.crt: "cat secret/tls.cert"
|
- secret/tls.cert
|
||||||
tls.key: "cat secret/tls.key"
|
- secret/tls.key
|
||||||
type: "kubernetes.io/tls"
|
type: "kubernetes.io/tls"
|
||||||
- name: app-tls-namespaced
|
- name: app-tls-namespaced
|
||||||
# you can define a namespace to generate secret in, defaults to: "default"
|
# you can define a namespace to generate secret in, defaults to: "default"
|
||||||
namespace: apps
|
namespace: apps
|
||||||
commands:
|
files:
|
||||||
tls.crt: "cat secret/tls.cert"
|
- tls.crt=catsecret/tls.cert
|
||||||
tls.key: "cat secret/tls.key"
|
- tls.key=secret/tls.key
|
||||||
type: "kubernetes.io/tls"
|
type: "kubernetes.io/tls"
|
||||||
- name: downloaded_secret
|
|
||||||
# timeoutSeconds specifies the number of seconds to
|
|
||||||
# wait for the commands below. It defaults to 5 seconds.
|
|
||||||
timeoutSeconds: 30
|
|
||||||
commands:
|
|
||||||
username: "curl -s https://path/to/secrets/username.yaml"
|
|
||||||
password: "curl -s https://path/to/secrets/password.yaml"
|
|
||||||
type: Opaque
|
|
||||||
- name: env_file_secret
|
- name: env_file_secret
|
||||||
# envCommand is similar to command but outputs lines of key=val pairs
|
# env is a path to a file to read lines of key=val
|
||||||
# i.e. a Docker .env file or a .ini file.
|
# you can only specify one env file per secret.
|
||||||
# you can only specify one envCommand per secret.
|
env: env.txt
|
||||||
envCommand: printf \"DB_USERNAME=admin\nDB_PASSWORD=somepw\"
|
|
||||||
type: Opaque
|
type: Opaque
|
||||||
|
|
||||||
# generatorOptions modify behavior of all ConfigMap and Secret generators
|
# generatorOptions modify behavior of all ConfigMap and Secret generators
|
||||||
@@ -124,11 +112,6 @@ generatorOptions:
|
|||||||
# annotations to add to all generated resources
|
# annotations to add to all generated resources
|
||||||
annotations:
|
annotations:
|
||||||
kustomize.generated.resource: somevalue
|
kustomize.generated.resource: somevalue
|
||||||
# timeoutSeconds specifies the timeout for commands
|
|
||||||
timeoutSeconds: 30
|
|
||||||
# shell and arguments to use as a context for commands used in resource
|
|
||||||
# generation. Default at time of writing: ["sh", "-c"]
|
|
||||||
shell: ["sh", "-c"]
|
|
||||||
# disableNameSuffixHash is true disables the default behavior of adding a
|
# disableNameSuffixHash is true disables the default behavior of adding a
|
||||||
# suffix to the names of generated resources that is a hash of
|
# suffix to the names of generated resources that is a hash of
|
||||||
# the resource contents.
|
# the resource contents.
|
||||||
|
|||||||
@@ -92,9 +92,9 @@ secret holding them (not covering that here).
|
|||||||
<!--
|
<!--
|
||||||
secretGenerator:
|
secretGenerator:
|
||||||
- name: app-tls
|
- name: app-tls
|
||||||
commands:
|
files:
|
||||||
tls.crt: "cat tls.cert"
|
tls.crt=tls.cert
|
||||||
tls.key: "cat tls.key"
|
tls.key=tls.key
|
||||||
type: "kubernetes.io/tls"
|
type: "kubernetes.io/tls"
|
||||||
EOF
|
EOF
|
||||||
-->
|
-->
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ Kustomize provides options to modify the behavior of ConfigMap and Secret genera
|
|||||||
- disable appending a content hash suffix to the names of generated resources
|
- disable appending a content hash suffix to the names of generated resources
|
||||||
- adding labels to generated resources
|
- adding labels to generated resources
|
||||||
- adding annotations to generated resources
|
- adding annotations to generated resources
|
||||||
- changing shell and arguments for getting data from commands
|
|
||||||
- changing timeout for executing commands
|
|
||||||
|
|
||||||
This demo shows how to use these options. First create a workspace.
|
This demo shows how to use these options. First create a workspace.
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -17,34 +17,26 @@ limitations under the License.
|
|||||||
package configmapandsecret
|
package configmapandsecret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/validation"
|
"k8s.io/apimachinery/pkg/util/validation"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
defaultCommandTimeout = 5 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
// SecretFactory makes Secrets.
|
// SecretFactory makes Secrets.
|
||||||
type SecretFactory struct {
|
type SecretFactory struct {
|
||||||
fSys fs.FileSystem
|
fSys fs.FileSystem
|
||||||
wd string
|
ldr ifc.Loader
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSecretFactory returns a new SecretFactory.
|
// NewSecretFactory returns a new SecretFactory.
|
||||||
func NewSecretFactory(fSys fs.FileSystem, wd string) *SecretFactory {
|
func NewSecretFactory(fSys fs.FileSystem, ldr ifc.Loader) *SecretFactory {
|
||||||
return &SecretFactory{fSys: fSys, wd: wd}
|
return &SecretFactory{fSys: fSys, ldr: ldr}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *SecretFactory) makeFreshSecret(args *types.SecretArgs) *corev1.Secret {
|
func (f *SecretFactory) makeFreshSecret(args *types.SecretArgs) *corev1.Secret {
|
||||||
@@ -67,28 +59,28 @@ func (f *SecretFactory) MakeSecret(args *types.SecretArgs, options *types.Genera
|
|||||||
var err error
|
var err error
|
||||||
s := f.makeFreshSecret(args)
|
s := f.makeFreshSecret(args)
|
||||||
|
|
||||||
timeout := defaultCommandTimeout
|
pairs, err := keyValuesFromEnvFile(f.ldr, args.EnvSource)
|
||||||
if args.TimeoutSeconds != nil {
|
if err != nil {
|
||||||
log.Println("SecretArgs.TimeoutSeconds will be deprected in next release. Please use GeneratorOptions.TimeoutSeconds instread.")
|
return nil, errors.Wrap(err, fmt.Sprintf(
|
||||||
timeout = time.Duration(*args.TimeoutSeconds) * time.Second
|
"env source file: %s",
|
||||||
|
args.EnvSource))
|
||||||
}
|
}
|
||||||
if args.EnvCommand != "" {
|
all = append(all, pairs...)
|
||||||
pairs, err := f.keyValuesFromEnvFileCommand(args.EnvCommand, timeout, options)
|
|
||||||
if err != nil {
|
pairs, err = keyValuesFromLiteralSources(args.LiteralSources)
|
||||||
return nil, errors.Wrap(err, fmt.Sprintf(
|
if err != nil {
|
||||||
"env source file: %s",
|
return nil, errors.Wrap(err, fmt.Sprintf(
|
||||||
args.EnvCommand))
|
"literal sources %v", args.LiteralSources))
|
||||||
}
|
|
||||||
all = append(all, pairs...)
|
|
||||||
}
|
}
|
||||||
if len(args.Commands) != 0 {
|
all = append(all, pairs...)
|
||||||
pairs, err := f.keyValuesFromCommands(args.Commands, timeout, options)
|
|
||||||
if err != nil {
|
pairs, err = keyValuesFromFileSources(f.ldr, args.FileSources)
|
||||||
return nil, errors.Wrap(err, fmt.Sprintf(
|
if err != nil {
|
||||||
"commands %v", args.Commands))
|
return nil, errors.Wrap(err, fmt.Sprintf(
|
||||||
}
|
"file sources: %v", args.FileSources))
|
||||||
all = append(all, pairs...)
|
|
||||||
}
|
}
|
||||||
|
all = append(all, pairs...)
|
||||||
|
|
||||||
for _, kv := range all {
|
for _, kv := range all {
|
||||||
err = addKvToSecret(s, kv.key, kv.value)
|
err = addKvToSecret(s, kv.key, kv.value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -113,52 +105,3 @@ func addKvToSecret(secret *corev1.Secret, keyName, data string) error {
|
|||||||
secret.Data[keyName] = []byte(data)
|
secret.Data[keyName] = []byte(data)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *SecretFactory) keyValuesFromEnvFileCommand(cmd string, timeout time.Duration, options *types.GeneratorOptions) ([]kvPair, error) {
|
|
||||||
content, err := f.createSecretKey(cmd, timeout, options)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return keyValuesFromLines(content)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *SecretFactory) keyValuesFromCommands(sources map[string]string, timeout time.Duration, options *types.GeneratorOptions) ([]kvPair, error) {
|
|
||||||
var kvs []kvPair
|
|
||||||
for k, cmd := range sources {
|
|
||||||
content, err := f.createSecretKey(cmd, timeout, options)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
kvs = append(kvs, kvPair{key: k, value: string(content)})
|
|
||||||
}
|
|
||||||
return kvs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run a command, return its output as the secret.
|
|
||||||
func (f *SecretFactory) createSecretKey(command string, timeout time.Duration, options *types.GeneratorOptions) ([]byte, error) {
|
|
||||||
if !f.fSys.IsDir(f.wd) {
|
|
||||||
f.wd = filepath.Dir(f.wd)
|
|
||||||
if !f.fSys.IsDir(f.wd) {
|
|
||||||
return nil, errors.New("not a directory: " + f.wd)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if options != nil && options.TimeoutSeconds != nil {
|
|
||||||
t := time.Duration(*options.TimeoutSeconds) * time.Second
|
|
||||||
if t > timeout {
|
|
||||||
timeout = t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var commands []string
|
|
||||||
if options == nil || len(options.Shell) == 0 {
|
|
||||||
commands = []string{"sh", "-c", command}
|
|
||||||
} else {
|
|
||||||
commands = append(options.Shell, command)
|
|
||||||
}
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
||||||
defer cancel()
|
|
||||||
cmd := exec.CommandContext(ctx, commands[0], commands[1:]...)
|
|
||||||
cmd.Dir = f.wd
|
|
||||||
return cmd.Output()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -17,94 +17,129 @@ limitations under the License.
|
|||||||
package configmapandsecret
|
package configmapandsecret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/loader"
|
||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMakeSecretNoCommands(t *testing.T) {
|
func makeEnvSecret(name string) *corev1.Secret {
|
||||||
factory := NewSecretFactory(fs.MakeFakeFS(), "/")
|
return &corev1.Secret{
|
||||||
args := types.SecretArgs{
|
TypeMeta: metav1.TypeMeta{
|
||||||
GeneratorArgs: types.GeneratorArgs{Name: "apple"},
|
APIVersion: "v1",
|
||||||
Type: "Opaque",
|
Kind: "Secret",
|
||||||
CommandSources: types.CommandSources{
|
},
|
||||||
Commands: nil,
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
EnvCommand: "",
|
Name: name,
|
||||||
}}
|
},
|
||||||
s, err := factory.MakeSecret(&args, nil)
|
Data: map[string][]byte{
|
||||||
if err != nil {
|
"DB_PASSWORD": []byte("somepw"),
|
||||||
t.Fatalf("unexpected error: %v", err)
|
"DB_USERNAME": []byte("admin"),
|
||||||
}
|
},
|
||||||
if s.ObjectMeta.Name != "apple" {
|
Type: "Opaque",
|
||||||
t.Fatalf("unexpected name: %v", s.ObjectMeta.Name)
|
|
||||||
}
|
|
||||||
if len(s.Data) > 0 || len(s.StringData) > 0 {
|
|
||||||
t.Fatalf("unexpected data: %v", s)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMakeSecretNoCommandsBadDir(t *testing.T) {
|
func makeFileSecret(name string) *corev1.Secret {
|
||||||
factory := NewSecretFactory(fs.MakeFakeFS(), "/does/not/exist")
|
return &corev1.Secret{
|
||||||
args := types.SecretArgs{
|
TypeMeta: metav1.TypeMeta{
|
||||||
GeneratorArgs: types.GeneratorArgs{Name: "envConfigMap"},
|
APIVersion: "v1",
|
||||||
Type: "Opaque",
|
Kind: "Secret",
|
||||||
CommandSources: types.CommandSources{
|
},
|
||||||
Commands: nil,
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
EnvCommand: "",
|
Name: name,
|
||||||
}}
|
},
|
||||||
_, err := factory.MakeSecret(&args, nil)
|
Data: map[string][]byte{
|
||||||
if err != nil {
|
"app-init.ini": []byte(`FOO=bar
|
||||||
t.Fatalf("Unexpected error: %v", err)
|
BAR=baz
|
||||||
|
`),
|
||||||
|
},
|
||||||
|
Type: "Opaque",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMakeSecretEmptyCommandMap(t *testing.T) {
|
func makeLiteralSecret(name string) *corev1.Secret {
|
||||||
factory := NewSecretFactory(fs.MakeFakeFS(), "/")
|
s := &corev1.Secret{
|
||||||
args := types.SecretArgs{
|
TypeMeta: metav1.TypeMeta{
|
||||||
GeneratorArgs: types.GeneratorArgs{Name: "envConfigMap"},
|
APIVersion: "v1",
|
||||||
Type: "Opaque",
|
Kind: "Secret",
|
||||||
CommandSources: types.CommandSources{
|
},
|
||||||
// TODO try: map[string]string{"commandName": "bogusCommand bogusArg"},
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Commands: nil,
|
Name: name,
|
||||||
EnvCommand: "echo beans",
|
},
|
||||||
}}
|
Data: map[string][]byte{
|
||||||
s, err := factory.MakeSecret(&args, nil)
|
"a": []byte("x"),
|
||||||
if err != nil {
|
"b": []byte("y"),
|
||||||
t.Fatalf("unexpected error: %v", err)
|
},
|
||||||
}
|
Type: "Opaque",
|
||||||
if s == nil {
|
|
||||||
t.Fatalf("nil result")
|
|
||||||
}
|
|
||||||
v, ok := s.Data["beans"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("expected beans")
|
|
||||||
}
|
|
||||||
if len(v) > 0 {
|
|
||||||
t.Fatalf("unexpected data")
|
|
||||||
}
|
}
|
||||||
|
s.SetLabels(map[string]string{"foo": "bar"})
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMakeSecretWithCommandMap(t *testing.T) {
|
func TestConstructSecret(t *testing.T) {
|
||||||
factory := NewSecretFactory(fs.MakeFakeFS(), "/")
|
type testCase struct {
|
||||||
args := types.SecretArgs{
|
description string
|
||||||
GeneratorArgs: types.GeneratorArgs{Name: "envConfigMap"},
|
input types.SecretArgs
|
||||||
Type: "Opaque",
|
options *types.GeneratorOptions
|
||||||
CommandSources: types.CommandSources{
|
expected *corev1.Secret
|
||||||
Commands: map[string]string{"commandName": "echo beans"},
|
|
||||||
}}
|
|
||||||
s, err := factory.MakeSecret(&args, nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
}
|
||||||
if s == nil {
|
|
||||||
t.Fatalf("nil result")
|
testCases := []testCase{
|
||||||
|
{
|
||||||
|
description: "construct secret from env",
|
||||||
|
input: types.SecretArgs{
|
||||||
|
GeneratorArgs: types.GeneratorArgs{Name: "envSecret"},
|
||||||
|
DataSources: types.DataSources{
|
||||||
|
EnvSource: "secret/app.env",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
options: nil,
|
||||||
|
expected: makeEnvSecret("envSecret"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "construct secret from file",
|
||||||
|
input: types.SecretArgs{
|
||||||
|
GeneratorArgs: types.GeneratorArgs{Name: "fileSecret"},
|
||||||
|
DataSources: types.DataSources{
|
||||||
|
FileSources: []string{"secret/app-init.ini"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
options: nil,
|
||||||
|
expected: makeFileSecret("fileSecret"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "construct secret from literal",
|
||||||
|
input: types.SecretArgs{
|
||||||
|
GeneratorArgs: types.GeneratorArgs{Name: "literalSecret"},
|
||||||
|
DataSources: types.DataSources{
|
||||||
|
LiteralSources: []string{"a=x", "b=y"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
options: &types.GeneratorOptions{
|
||||||
|
Labels: map[string]string{
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: makeLiteralSecret("literalSecret"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
v, ok := s.Data["commandName"]
|
|
||||||
if !ok {
|
fSys := fs.MakeFakeFS()
|
||||||
t.Fatalf("expected something for commandName")
|
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"))
|
||||||
if string(v) != "beans\n" {
|
f := NewSecretFactory(fSys, loader.NewFileLoaderAtRoot(fSys))
|
||||||
t.Fatalf("unexpected data: %s", string(v))
|
for _, tc := range testCases {
|
||||||
|
cm, err := f.MakeSecret(&tc.input, tc.options)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(*cm, *tc.expected) {
|
||||||
|
t.Fatalf("in testcase: %q updated:\n%#v\ndoesn't match expected:\n%#v\n", tc.description, *cm, tc.expected)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ func (kf *KunstructuredFactoryImpl) MakeSecret(args *types.SecretArgs, options *
|
|||||||
// Set sets loader, filesystem and workdirectory
|
// Set sets loader, filesystem and workdirectory
|
||||||
func (kf *KunstructuredFactoryImpl) Set(fs fs.FileSystem, ldr ifc.Loader) {
|
func (kf *KunstructuredFactoryImpl) Set(fs fs.FileSystem, ldr ifc.Loader) {
|
||||||
kf.cmFactory = configmapandsecret.NewConfigMapFactory(fs, ldr)
|
kf.cmFactory = configmapandsecret.NewConfigMapFactory(fs, ldr)
|
||||||
kf.secretFactory = configmapandsecret.NewSecretFactory(fs, ldr.Root())
|
kf.secretFactory = configmapandsecret.NewSecretFactory(fs, ldr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate validates that u has kind and name
|
// validate validates that u has kind and name
|
||||||
|
|||||||
@@ -252,21 +252,14 @@ func TestNewResMapFromSecretArgs(t *testing.T) {
|
|||||||
secrets := []types.SecretArgs{
|
secrets := []types.SecretArgs{
|
||||||
{
|
{
|
||||||
GeneratorArgs: types.GeneratorArgs{Name: "apple"},
|
GeneratorArgs: types.GeneratorArgs{Name: "apple"},
|
||||||
CommandSources: types.CommandSources{
|
DataSources: types.DataSources{
|
||||||
Commands: map[string]string{
|
LiteralSources: []string{
|
||||||
"DB_USERNAME": "printf admin",
|
"DB_USERNAME=admin",
|
||||||
"DB_PASSWORD": "printf somepw",
|
"DB_PASSWORD=somepw",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Type: ifc.SecretTypeOpaque,
|
Type: ifc.SecretTypeOpaque,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{Name: "peanuts"},
|
|
||||||
CommandSources: types.CommandSources{
|
|
||||||
EnvCommand: "printf \"DB_USERNAME=admin\nDB_PASSWORD=somepw\"",
|
|
||||||
},
|
|
||||||
Type: ifc.SecretTypeOpaque,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
fakeFs := fs.MakeFakeFS()
|
fakeFs := fs.MakeFakeFS()
|
||||||
fakeFs.Mkdir(".")
|
fakeFs.Mkdir(".")
|
||||||
@@ -291,45 +284,8 @@ func TestNewResMapFromSecretArgs(t *testing.T) {
|
|||||||
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")),
|
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")),
|
||||||
},
|
},
|
||||||
}).SetBehavior(ifc.BehaviorCreate),
|
}).SetBehavior(ifc.BehaviorCreate),
|
||||||
resid.NewResId(secret, "peanuts"): rf.FromMap(
|
|
||||||
map[string]interface{}{
|
|
||||||
"apiVersion": "v1",
|
|
||||||
"kind": "Secret",
|
|
||||||
"metadata": map[string]interface{}{
|
|
||||||
"name": "peanuts",
|
|
||||||
},
|
|
||||||
"type": ifc.SecretTypeOpaque,
|
|
||||||
"data": map[string]interface{}{
|
|
||||||
"DB_USERNAME": base64.StdEncoding.EncodeToString([]byte("admin")),
|
|
||||||
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")),
|
|
||||||
},
|
|
||||||
}).SetBehavior(ifc.BehaviorCreate),
|
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
t.Fatalf("%#v\ndoesn't match expected:\n%#v", actual, expected)
|
t.Fatalf("%#v\ndoesn't match expected:\n%#v", actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSecretTimeout(t *testing.T) {
|
|
||||||
timeout := int64(1)
|
|
||||||
secrets := []types.SecretArgs{
|
|
||||||
{
|
|
||||||
GeneratorArgs: types.GeneratorArgs{Name: "slow"},
|
|
||||||
TimeoutSeconds: &timeout,
|
|
||||||
CommandSources: types.CommandSources{
|
|
||||||
Commands: map[string]string{
|
|
||||||
"USER": "sleep 2",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Type: ifc.SecretTypeOpaque,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
fakeFs := fs.MakeFakeFS()
|
|
||||||
fakeFs.Mkdir(".")
|
|
||||||
rmF.Set(fakeFs, loader.NewFileLoaderAtRoot(fakeFs))
|
|
||||||
_, err := rmF.NewResMapFromSecretArgs(secrets, nil)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("didn't get the expected timeout error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -181,9 +181,9 @@ configMapGenerator:
|
|||||||
- foo=bar
|
- foo=bar
|
||||||
secretGenerator:
|
secretGenerator:
|
||||||
- name: secret-in-base
|
- name: secret-in-base
|
||||||
commands:
|
literals:
|
||||||
username: "printf admin"
|
- username=admin
|
||||||
password: "printf somepw"
|
- password=somepw
|
||||||
`)
|
`)
|
||||||
th.writeF("/app/deployment.yaml", `
|
th.writeF("/app/deployment.yaml", `
|
||||||
apiVersion: apps/v1beta2
|
apiVersion: apps/v1beta2
|
||||||
@@ -362,8 +362,8 @@ configMapGenerator:
|
|||||||
secretGenerator:
|
secretGenerator:
|
||||||
- name: secret-in-base
|
- name: secret-in-base
|
||||||
behavior: merge
|
behavior: merge
|
||||||
commands:
|
literals:
|
||||||
proxy: "printf haproxy"
|
- proxy=haproxy
|
||||||
`)
|
`)
|
||||||
m, err := th.makeKustTarget().MakeCustomizedResMap()
|
m, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -52,9 +52,9 @@ configMapGenerator:
|
|||||||
- DB_PASSWORD=somepw
|
- DB_PASSWORD=somepw
|
||||||
secretGenerator:
|
secretGenerator:
|
||||||
- name: secret
|
- name: secret
|
||||||
commands:
|
literals:
|
||||||
DB_USERNAME: "printf admin"
|
- DB_USERNAME=admin
|
||||||
DB_PASSWORD: "printf somepw"
|
- DB_PASSWORD=somepw
|
||||||
type: Opaque
|
type: Opaque
|
||||||
patchesJson6902:
|
patchesJson6902:
|
||||||
- target:
|
- target:
|
||||||
@@ -63,16 +63,6 @@ patchesJson6902:
|
|||||||
kind: Deployment
|
kind: Deployment
|
||||||
name: dply1
|
name: dply1
|
||||||
path: jsonpatch.json
|
path: jsonpatch.json
|
||||||
`
|
|
||||||
kustomizationContent2 = `
|
|
||||||
apiVersion: v1beta1
|
|
||||||
kind: Kustomization
|
|
||||||
secretGenerator:
|
|
||||||
- name: secret
|
|
||||||
timeoutSeconds: 1
|
|
||||||
commands:
|
|
||||||
USER: "sleep 2"
|
|
||||||
type: Opaque
|
|
||||||
`
|
`
|
||||||
deploymentContent = `
|
deploymentContent = `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
@@ -217,18 +207,6 @@ func TestResourceNotFound(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSecretTimeout(t *testing.T) {
|
|
||||||
th := NewKustTestHarness(t, "/whatever")
|
|
||||||
th.writeK("/whatever", kustomizationContent2)
|
|
||||||
_, err := th.makeKustTarget().MakeCustomizedResMap()
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("Didn't get the expected error for an unknown resource")
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "killed") {
|
|
||||||
t.Fatalf("unexpected error: %q", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func findSecret(m resmap.ResMap) *resource.Resource {
|
func findSecret(m resmap.ResMap) *resource.Resource {
|
||||||
for id, res := range m {
|
for id, res := range m {
|
||||||
if id.Gvk().Kind == "Secret" {
|
if id.Gvk().Kind == "Secret" {
|
||||||
|
|||||||
@@ -39,11 +39,11 @@ configMapGenerator:
|
|||||||
secretGenerator:
|
secretGenerator:
|
||||||
- name: the-non-default-namespace-secret
|
- name: the-non-default-namespace-secret
|
||||||
namespace: non-default
|
namespace: non-default
|
||||||
commands:
|
literals:
|
||||||
password.txt: "echo verySecret"
|
- password.txt=verySecret
|
||||||
- name: the-secret
|
- name: the-secret
|
||||||
commands:
|
literals:
|
||||||
password.txt: "echo anotherSecret"
|
- password.txt=anotherSecret
|
||||||
`)
|
`)
|
||||||
m, err := th.makeKustTarget().MakeCustomizedResMap()
|
m, err := th.makeKustTarget().MakeCustomizedResMap()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -69,19 +69,19 @@ metadata:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
password.txt: dmVyeVNlY3JldAo=
|
password.txt: dmVyeVNlY3JldA==
|
||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
name: the-non-default-namespace-secret-9fgdmbbk5c
|
name: the-non-default-namespace-secret-h8d9hkgtb9
|
||||||
namespace: non-default
|
namespace: non-default
|
||||||
type: Opaque
|
type: Opaque
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
data:
|
data:
|
||||||
password.txt: YW5vdGhlclNlY3JldAo=
|
password.txt: YW5vdGhlclNlY3JldA==
|
||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
name: the-secret-7dd8hcgfhk
|
name: the-secret-fgb45h45bh
|
||||||
type: Opaque
|
type: Opaque
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -216,26 +216,12 @@ type SecretArgs struct {
|
|||||||
// This is the same field as the secret type field in v1/Secret:
|
// This is the same field as the secret type field in v1/Secret:
|
||||||
// It can be "Opaque" (default), or "kubernetes.io/tls".
|
// It can be "Opaque" (default), or "kubernetes.io/tls".
|
||||||
//
|
//
|
||||||
// If type is "kubernetes.io/tls", then "Commands" must have exactly two
|
// If type is "kubernetes.io/tls", then "literals" or "files" must have exactly two
|
||||||
// keys: "tls.key" and "tls.crt"
|
// keys: "tls.key" and "tls.crt"
|
||||||
Type string `json:"type,omitempty" yaml:"type,omitempty"`
|
Type string `json:"type,omitempty" yaml:"type,omitempty"`
|
||||||
|
|
||||||
// CommandSources for secret.
|
// DataSources for secret.
|
||||||
CommandSources `json:",inline,omitempty" yaml:",inline,omitempty"`
|
DataSources `json:",inline,omitempty" yaml:",inline,omitempty"`
|
||||||
|
|
||||||
// Deprecated.
|
|
||||||
// Replaced by GeneratorOptions.TimeoutSeconds
|
|
||||||
// TimeoutSeconds specifies the timeout for commands.
|
|
||||||
TimeoutSeconds *int64 `json:"timeoutSeconds,omitempty" yaml:"timeoutSeconds,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// CommandSources contains some generic sources for secrets.
|
|
||||||
type CommandSources struct {
|
|
||||||
// Map of keys to commands to generate the values
|
|
||||||
Commands map[string]string `json:"commands,omitempty" yaml:"commands,omitempty"`
|
|
||||||
// EnvCommand to output lines of key=val pairs to create a secret.
|
|
||||||
// i.e. a Docker .env file or a .ini file.
|
|
||||||
EnvCommand string `json:"envCommand,omitempty" yaml:"envCommand,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DataSources contains some generic sources for configmaps.
|
// DataSources contains some generic sources for configmaps.
|
||||||
@@ -282,15 +268,6 @@ type GeneratorOptions struct {
|
|||||||
// Annotations to add to all generated resources.
|
// Annotations to add to all generated resources.
|
||||||
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
|
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
|
||||||
|
|
||||||
// TimeoutSeconds specifies the timeout for commands, if any,
|
|
||||||
// used in resource generation. At time of writing, the default
|
|
||||||
// was specified in configmapandsecret.defaultCommandTimeout.
|
|
||||||
TimeoutSeconds *int64 `json:"timeoutSeconds,omitempty" yaml:"timeoutSeconds,omitempty"`
|
|
||||||
|
|
||||||
// Shell and arguments to use as a context for commands used in
|
|
||||||
// resource generation. Default at time of writing: {'sh', '-c'}.
|
|
||||||
Shell []string `json:"shell,omitempty" yaml:"shell,omitempty"`
|
|
||||||
|
|
||||||
// DisableNameSuffixHash if true disables the default behavior of adding a
|
// DisableNameSuffixHash if true disables the default behavior of adding a
|
||||||
// suffix to the names of generated resources that is a hash of the
|
// suffix to the names of generated resources that is a hash of the
|
||||||
// resource contents.
|
// resource contents.
|
||||||
|
|||||||
Reference in New Issue
Block a user