@@ -17,34 +17,26 @@ limitations under the License.
17
17
package configmapandsecret
18
18
19
19
import (
20
- "context"
21
20
"fmt"
22
- "log"
23
- "os/exec"
24
- "path/filepath"
25
21
"strings"
26
- "time"
27
22
28
23
"github.com/pkg/errors"
29
24
corev1 "k8s.io/api/core/v1"
30
25
"k8s.io/apimachinery/pkg/util/validation"
31
26
"sigs.k8s.io/kustomize/pkg/fs"
27
+ "sigs.k8s.io/kustomize/pkg/ifc"
32
28
"sigs.k8s.io/kustomize/pkg/types"
33
29
)
34
30
35
- const (
36
- defaultCommandTimeout = 5 * time .Second
37
- )
38
-
39
31
// SecretFactory makes Secrets.
40
32
type SecretFactory struct {
41
33
fSys fs.FileSystem
42
- wd string
34
+ ldr ifc. Loader
43
35
}
44
36
45
37
// 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 }
48
40
}
49
41
50
42
func (f * SecretFactory ) makeFreshSecret (args * types.SecretArgs ) * corev1.Secret {
@@ -67,28 +59,28 @@ func (f *SecretFactory) MakeSecret(args *types.SecretArgs, options *types.Genera
67
59
var err error
68
60
s := f .makeFreshSecret (args )
69
61
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 ))
74
67
}
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 ))
83
74
}
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 ))
91
81
}
82
+ all = append (all , pairs ... )
83
+
92
84
for _ , kv := range all {
93
85
err = addKvToSecret (s , kv .key , kv .value )
94
86
if err != nil {
@@ -113,52 +105,3 @@ func addKvToSecret(secret *corev1.Secret, keyName, data string) error {
113
105
secret .Data [keyName ] = []byte (data )
114
106
return nil
115
107
}
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