Avoid unnecessary shell execution in secretFactory.

This commit is contained in:
Jeffrey Regan
2018-12-17 10:59:27 -08:00
parent 29694e5b6a
commit d4ba22191a
2 changed files with 17 additions and 20 deletions

View File

@@ -72,7 +72,7 @@ func (f *SecretFactory) MakeSecret(args *types.SecretArgs, options *types.Genera
log.Println("SecretArgs.TimeoutSeconds will be deprected in next release. Please use GeneratorOptions.TimeoutSeconds instread.") log.Println("SecretArgs.TimeoutSeconds will be deprected in next release. Please use GeneratorOptions.TimeoutSeconds instread.")
timeout = time.Duration(*args.TimeoutSeconds) * time.Second timeout = time.Duration(*args.TimeoutSeconds) * time.Second
} }
if args.EnvCommand != "" {
pairs, err := f.keyValuesFromEnvFileCommand(args.EnvCommand, timeout, options) pairs, err := f.keyValuesFromEnvFileCommand(args.EnvCommand, timeout, options)
if err != nil { if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf( return nil, errors.Wrap(err, fmt.Sprintf(
@@ -80,14 +80,15 @@ func (f *SecretFactory) MakeSecret(args *types.SecretArgs, options *types.Genera
args.EnvCommand)) args.EnvCommand))
} }
all = append(all, pairs...) all = append(all, pairs...)
}
pairs, err = f.keyValuesFromCommands(args.Commands, timeout, options) if len(args.Commands) != 0 {
pairs, err := f.keyValuesFromCommands(args.Commands, timeout, options)
if err != nil { if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf( return nil, errors.Wrap(err, fmt.Sprintf(
"commands %v", args.Commands)) "commands %v", args.Commands))
} }
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 {

View File

@@ -17,7 +17,6 @@ limitations under the License.
package configmapandsecret package configmapandsecret
import ( import (
"strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
@@ -55,11 +54,8 @@ func TestMakeSecretNoCommandsBadDir(t *testing.T) {
EnvCommand: "", EnvCommand: "",
}} }}
_, err := factory.MakeSecret(&args, nil) _, err := factory.MakeSecret(&args, nil)
if err == nil { if err != nil {
t.Fatalf("expected error: %v", err) t.Fatalf("Unexpected error: %v", err)
}
if !strings.Contains(err.Error(), "not a directory") {
t.Fatalf("unexpected error: %v", err)
} }
} }