Skip to content

Commit 2fa4a34

Browse files
committed
replace commands/envcommand by DataSource in SecretGenerator
1 parent a5c6938 commit 2fa4a34

File tree

11 files changed

+166
-296
lines changed

11 files changed

+166
-296
lines changed

docs/kustomization.yaml

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,35 +85,23 @@ configMapGenerator:
8585

8686
# Each entry in this list results in the creation of
8787
# one Secret resource (it's a generator of n secrets).
88-
# A command can do anything to get a secret,
89-
# e.g. prompt the user directly, start a webserver to
90-
# initate an oauth dance, etc.
9188
secretGenerator:
9289
- name: app-tls
93-
commands:
94-
tls.crt: "cat secret/tls.cert"
95-
tls.key: "cat secret/tls.key"
90+
files:
91+
- secret/tls.cert
92+
- secret/tls.key
9693
type: "kubernetes.io/tls"
9794
- name: app-tls-namespaced
9895
# you can define a namespace to generate secret in, defaults to: "default"
9996
namespace: apps
100-
commands:
101-
tls.crt: "cat secret/tls.cert"
102-
tls.key: "cat secret/tls.key"
97+
files:
98+
- tls.crt=catsecret/tls.cert
99+
- tls.key=secret/tls.key
103100
type: "kubernetes.io/tls"
104-
- name: downloaded_secret
105-
# timeoutSeconds specifies the number of seconds to
106-
# wait for the commands below. It defaults to 5 seconds.
107-
timeoutSeconds: 30
108-
commands:
109-
username: "curl -s https://path/to/secrets/username.yaml"
110-
password: "curl -s https://path/to/secrets/password.yaml"
111-
type: Opaque
112101
- name: env_file_secret
113-
# envCommand is similar to command but outputs lines of key=val pairs
114-
# i.e. a Docker .env file or a .ini file.
115-
# you can only specify one envCommand per secret.
116-
envCommand: printf \"DB_USERNAME=admin\nDB_PASSWORD=somepw\"
102+
# env is a path to a file to read lines of key=val
103+
# you can only specify one env file per secret.
104+
env: env.txt
117105
type: Opaque
118106

119107
# generatorOptions modify behavior of all ConfigMap and Secret generators
@@ -124,11 +112,6 @@ generatorOptions:
124112
# annotations to add to all generated resources
125113
annotations:
126114
kustomize.generated.resource: somevalue
127-
# timeoutSeconds specifies the timeout for commands
128-
timeoutSeconds: 30
129-
# shell and arguments to use as a context for commands used in resource
130-
# generation. Default at time of writing: ["sh", "-c"]
131-
shell: ["sh", "-c"]
132115
# disableNameSuffixHash is true disables the default behavior of adding a
133116
# suffix to the names of generated resources that is a hash of
134117
# the resource contents.

examples/combineConfigs.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ secret holding them (not covering that here).
9292
<!--
9393
secretGenerator:
9494
- name: app-tls
95-
commands:
96-
tls.crt: "cat tls.cert"
97-
tls.key: "cat tls.key"
95+
files:
96+
tls.crt=tls.cert
97+
tls.key=tls.key
9898
type: "kubernetes.io/tls"
9999
EOF
100100
-->

examples/generatorOptions.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ Kustomize provides options to modify the behavior of ConfigMap and Secret genera
55
- disable appending a content hash suffix to the names of generated resources
66
- adding labels to generated resources
77
- adding annotations to generated resources
8-
- changing shell and arguments for getting data from commands
9-
- changing timeout for executing commands
108

119
This demo shows how to use these options. First create a workspace.
1210
```

k8sdeps/configmapandsecret/secretfactory.go

Lines changed: 23 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,26 @@ limitations under the License.
1717
package configmapandsecret
1818

1919
import (
20-
"context"
2120
"fmt"
22-
"log"
23-
"os/exec"
24-
"path/filepath"
2521
"strings"
26-
"time"
2722

2823
"github.com/pkg/errors"
2924
corev1 "k8s.io/api/core/v1"
3025
"k8s.io/apimachinery/pkg/util/validation"
3126
"sigs.k8s.io/kustomize/pkg/fs"
27+
"sigs.k8s.io/kustomize/pkg/ifc"
3228
"sigs.k8s.io/kustomize/pkg/types"
3329
)
3430

35-
const (
36-
defaultCommandTimeout = 5 * time.Second
37-
)
38-
3931
// SecretFactory makes Secrets.
4032
type SecretFactory struct {
4133
fSys fs.FileSystem
42-
wd string
34+
ldr ifc.Loader
4335
}
4436

4537
// NewSecretFactory returns a new SecretFactory.
46-
func NewSecretFactory(fSys fs.FileSystem, wd string) *SecretFactory {
47-
return &SecretFactory{fSys: fSys, wd: wd}
38+
func NewSecretFactory(fSys fs.FileSystem, ldr ifc.Loader) *SecretFactory {
39+
return &SecretFactory{fSys: fSys, ldr: ldr}
4840
}
4941

5042
func (f *SecretFactory) makeFreshSecret(args *types.SecretArgs) *corev1.Secret {
@@ -67,28 +59,28 @@ func (f *SecretFactory) MakeSecret(args *types.SecretArgs, options *types.Genera
6759
var err error
6860
s := f.makeFreshSecret(args)
6961

70-
timeout := defaultCommandTimeout
71-
if args.TimeoutSeconds != nil {
72-
log.Println("SecretArgs.TimeoutSeconds will be deprected in next release. Please use GeneratorOptions.TimeoutSeconds instread.")
73-
timeout = time.Duration(*args.TimeoutSeconds) * time.Second
62+
pairs, err := keyValuesFromEnvFile(f.ldr, args.EnvSource)
63+
if err != nil {
64+
return nil, errors.Wrap(err, fmt.Sprintf(
65+
"env source file: %s",
66+
args.EnvSource))
7467
}
75-
if args.EnvCommand != "" {
76-
pairs, err := f.keyValuesFromEnvFileCommand(args.EnvCommand, timeout, options)
77-
if err != nil {
78-
return nil, errors.Wrap(err, fmt.Sprintf(
79-
"env source file: %s",
80-
args.EnvCommand))
81-
}
82-
all = append(all, pairs...)
68+
all = append(all, pairs...)
69+
70+
pairs, err = keyValuesFromLiteralSources(args.LiteralSources)
71+
if err != nil {
72+
return nil, errors.Wrap(err, fmt.Sprintf(
73+
"literal sources %v", args.LiteralSources))
8374
}
84-
if len(args.Commands) != 0 {
85-
pairs, err := f.keyValuesFromCommands(args.Commands, timeout, options)
86-
if err != nil {
87-
return nil, errors.Wrap(err, fmt.Sprintf(
88-
"commands %v", args.Commands))
89-
}
90-
all = append(all, pairs...)
75+
all = append(all, pairs...)
76+
77+
pairs, err = keyValuesFromFileSources(f.ldr, args.FileSources)
78+
if err != nil {
79+
return nil, errors.Wrap(err, fmt.Sprintf(
80+
"file sources: %v", args.FileSources))
9181
}
82+
all = append(all, pairs...)
83+
9284
for _, kv := range all {
9385
err = addKvToSecret(s, kv.key, kv.value)
9486
if err != nil {
@@ -113,52 +105,3 @@ func addKvToSecret(secret *corev1.Secret, keyName, data string) error {
113105
secret.Data[keyName] = []byte(data)
114106
return nil
115107
}
116-
117-
func (f *SecretFactory) keyValuesFromEnvFileCommand(cmd string, timeout time.Duration, options *types.GeneratorOptions) ([]kvPair, error) {
118-
content, err := f.createSecretKey(cmd, timeout, options)
119-
if err != nil {
120-
return nil, err
121-
}
122-
return keyValuesFromLines(content)
123-
}
124-
125-
func (f *SecretFactory) keyValuesFromCommands(sources map[string]string, timeout time.Duration, options *types.GeneratorOptions) ([]kvPair, error) {
126-
var kvs []kvPair
127-
for k, cmd := range sources {
128-
content, err := f.createSecretKey(cmd, timeout, options)
129-
if err != nil {
130-
return nil, err
131-
}
132-
kvs = append(kvs, kvPair{key: k, value: string(content)})
133-
}
134-
return kvs, nil
135-
}
136-
137-
// Run a command, return its output as the secret.
138-
func (f *SecretFactory) createSecretKey(command string, timeout time.Duration, options *types.GeneratorOptions) ([]byte, error) {
139-
if !f.fSys.IsDir(f.wd) {
140-
f.wd = filepath.Dir(f.wd)
141-
if !f.fSys.IsDir(f.wd) {
142-
return nil, errors.New("not a directory: " + f.wd)
143-
}
144-
}
145-
146-
if options != nil && options.TimeoutSeconds != nil {
147-
t := time.Duration(*options.TimeoutSeconds) * time.Second
148-
if t > timeout {
149-
timeout = t
150-
}
151-
}
152-
153-
var commands []string
154-
if options == nil || len(options.Shell) == 0 {
155-
commands = []string{"sh", "-c", command}
156-
} else {
157-
commands = append(options.Shell, command)
158-
}
159-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
160-
defer cancel()
161-
cmd := exec.CommandContext(ctx, commands[0], commands[1:]...)
162-
cmd.Dir = f.wd
163-
return cmd.Output()
164-
}

0 commit comments

Comments
 (0)