@@ -20,7 +20,6 @@ package configmapandsecret
20
20
import (
21
21
"encoding/json"
22
22
"fmt"
23
- "io/ioutil"
24
23
"path"
25
24
"strings"
26
25
@@ -51,7 +50,7 @@ func NewConfigMapFactory(
51
50
// MakeUnstructAndGenerateName returns an configmap and the name appended with a hash.
52
51
func (f * ConfigMapFactory ) MakeUnstructAndGenerateName (
53
52
args * types.ConfigMapArgs ) (* unstructured.Unstructured , string , error ) {
54
- cm , err := f .MakeConfigMap1 (args )
53
+ cm , err := f .MakeConfigMap (args )
55
54
if err != nil {
56
55
return nil , "" , err
57
56
}
@@ -84,31 +83,8 @@ func (f *ConfigMapFactory) makeFreshConfigMap(
84
83
return cm
85
84
}
86
85
87
- // MakeConfigMap1 returns a new ConfigMap, or nil and an error.
88
- func (f * ConfigMapFactory ) MakeConfigMap1 (
89
- args * types.ConfigMapArgs ) (* corev1.ConfigMap , error ) {
90
- cm := f .makeFreshConfigMap (args )
91
- if args .EnvSource != "" {
92
- if err := f .handleConfigMapFromEnvFileSource (cm , args ); err != nil {
93
- return nil , err
94
- }
95
- }
96
- if args .FileSources != nil {
97
- if err := f .handleConfigMapFromFileSources (cm , args ); err != nil {
98
- return nil , err
99
- }
100
- }
101
- if args .LiteralSources != nil {
102
- if err := f .handleConfigMapFromLiteralSources (cm , args .LiteralSources ); err != nil {
103
- return nil , err
104
- }
105
- }
106
- return cm , nil
107
- }
108
-
109
- // MakeConfigMap2 returns a new ConfigMap, or nil and an error.
110
- // TODO: Get rid of the nearly duplicated code in MakeConfigMap1 vs MakeConfigMap2
111
- func (f * ConfigMapFactory ) MakeConfigMap2 (
86
+ // MakeConfigMap returns a new ConfigMap, or nil and an error.
87
+ func (f * ConfigMapFactory ) MakeConfigMap (
112
88
args * types.ConfigMapArgs ) (* corev1.ConfigMap , error ) {
113
89
var all []kvPair
114
90
var err error
@@ -137,7 +113,7 @@ func (f *ConfigMapFactory) MakeConfigMap2(
137
113
all = append (all , pairs ... )
138
114
139
115
for _ , kv := range all {
140
- err = AddKv (cm , kv .key , kv .value )
116
+ err = addKvToConfigMap (cm , kv .key , kv .value )
141
117
if err != nil {
142
118
return nil , err
143
119
}
@@ -148,7 +124,7 @@ func (f *ConfigMapFactory) MakeConfigMap2(
148
124
func keyValuesFromLiteralSources (sources []string ) ([]kvPair , error ) {
149
125
var kvs []kvPair
150
126
for _ , s := range sources {
151
- k , v , err := ParseLiteralSource (s )
127
+ k , v , err := parseLiteralSource (s )
152
128
if err != nil {
153
129
return nil , err
154
130
}
@@ -157,27 +133,10 @@ func keyValuesFromLiteralSources(sources []string) ([]kvPair, error) {
157
133
return kvs , nil
158
134
}
159
135
160
- // handleConfigMapFromLiteralSources adds the specified literal source
161
- // information into the provided configMap.
162
- func (f * ConfigMapFactory ) handleConfigMapFromLiteralSources (
163
- configMap * v1.ConfigMap , sources []string ) error {
164
- for _ , s := range sources {
165
- k , v , err := ParseLiteralSource (s )
166
- if err != nil {
167
- return err
168
- }
169
- err = AddKv (configMap , k , v )
170
- if err != nil {
171
- return err
172
- }
173
- }
174
- return nil
175
- }
176
-
177
136
func keyValuesFromFileSources (ldr loader.Loader , sources []string ) ([]kvPair , error ) {
178
137
var kvs []kvPair
179
138
for _ , s := range sources {
180
- k , fPath , err := ParseFileSource (s )
139
+ k , fPath , err := parseFileSource (s )
181
140
if err != nil {
182
141
return nil , err
183
142
}
@@ -190,45 +149,6 @@ func keyValuesFromFileSources(ldr loader.Loader, sources []string) ([]kvPair, er
190
149
return kvs , nil
191
150
}
192
151
193
- // handleConfigMapFromFileSources adds the specified file source information
194
- // into the provided configMap
195
- func (f * ConfigMapFactory ) handleConfigMapFromFileSources (
196
- configMap * v1.ConfigMap , args * types.ConfigMapArgs ) error {
197
- for _ , fileSource := range args .FileSources {
198
- keyName , filePath , err := ParseFileSource (fileSource )
199
- if err != nil {
200
- return err
201
- }
202
- if ! f .fSys .Exists (filePath ) {
203
- return fmt .Errorf ("unable to read configmap source file %s" , filePath )
204
- }
205
- if f .fSys .IsDir (filePath ) {
206
- if strings .Contains (fileSource , "=" ) {
207
- return fmt .Errorf ("cannot give a key name for a directory path" )
208
- }
209
- fileList , err := ioutil .ReadDir (filePath )
210
- if err != nil {
211
- return fmt .Errorf ("error listing files in %s: %v" , filePath , err )
212
- }
213
- for _ , item := range fileList {
214
- itemPath := path .Join (filePath , item .Name ())
215
- if item .Mode ().IsRegular () {
216
- keyName = item .Name ()
217
- err = addKeyFromFileToConfigMap (configMap , keyName , itemPath )
218
- if err != nil {
219
- return err
220
- }
221
- }
222
- }
223
- } else {
224
- if err := addKeyFromFileToConfigMap (configMap , keyName , filePath ); err != nil {
225
- return err
226
- }
227
- }
228
- }
229
- return nil
230
- }
231
-
232
152
func keyValuesFromEnvFile (l loader.Loader , path string ) ([]kvPair , error ) {
233
153
if path == "" {
234
154
return nil , nil
@@ -240,34 +160,9 @@ func keyValuesFromEnvFile(l loader.Loader, path string) ([]kvPair, error) {
240
160
return keyValuesFromLines (content )
241
161
}
242
162
243
- // HandleConfigMapFromEnvFileSource adds the specified env file source information
244
- // into the provided configMap
245
- func (f * ConfigMapFactory ) handleConfigMapFromEnvFileSource (
246
- configMap * v1.ConfigMap , args * types.ConfigMapArgs ) error {
247
- if ! f .fSys .Exists (args .EnvSource ) {
248
- return fmt .Errorf ("unable to read configmap env file %s" , args .EnvSource )
249
- }
250
- if f .fSys .IsDir (args .EnvSource ) {
251
- return fmt .Errorf ("env config file %s cannot be a directory" , args .EnvSource )
252
- }
253
- return addFromEnvFile (args .EnvSource , func (key , value string ) error {
254
- return AddKv (configMap , key , value )
255
- })
256
- }
257
-
258
- // addKeyFromFileToConfigMap adds a key with the given name to a ConfigMap, populating
259
- // the value with the content of the given file path, or returns an error.
260
- func addKeyFromFileToConfigMap (configMap * v1.ConfigMap , keyName , filePath string ) error {
261
- data , err := ioutil .ReadFile (filePath )
262
- if err != nil {
263
- return err
264
- }
265
- return AddKv (configMap , keyName , string (data ))
266
- }
267
-
268
- // AddKv adds the given key and data to the given config map.
163
+ // addKvToConfigMap adds the given key and data to the given config map.
269
164
// Error if key invalid, or already exists.
270
- func AddKv (configMap * v1.ConfigMap , keyName , data string ) error {
165
+ func addKvToConfigMap (configMap * v1.ConfigMap , keyName , data string ) error {
271
166
// Note, the rules for ConfigMap keys are the exact same as the ones for SecretKeys.
272
167
if errs := validation .IsConfigMapKey (keyName ); len (errs ) != 0 {
273
168
return fmt .Errorf ("%q is not a valid key name for a ConfigMap: %s" , keyName , strings .Join (errs , ";" ))
@@ -279,15 +174,15 @@ func AddKv(configMap *v1.ConfigMap, keyName, data string) error {
279
174
return nil
280
175
}
281
176
282
- // ParseFileSource parses the source given.
177
+ // parseFileSource parses the source given.
283
178
//
284
179
// Acceptable formats include:
285
180
// 1. source-path: the basename will become the key name
286
181
// 2. source-name=source-path: the source-name will become the key name and
287
182
// source-path is the path to the key file.
288
183
//
289
184
// Key names cannot include '='.
290
- func ParseFileSource (source string ) (keyName , filePath string , err error ) {
185
+ func parseFileSource (source string ) (keyName , filePath string , err error ) {
291
186
numSeparators := strings .Count (source , "=" )
292
187
switch {
293
188
case numSeparators == 0 :
@@ -304,10 +199,10 @@ func ParseFileSource(source string) (keyName, filePath string, err error) {
304
199
}
305
200
}
306
201
307
- // ParseLiteralSource parses the source key=val pair into its component pieces.
202
+ // parseLiteralSource parses the source key=val pair into its component pieces.
308
203
// This functionality is distinguished from strings.SplitN(source, "=", 2) since
309
204
// it returns an error in the case of empty keys, values, or a missing equals sign.
310
- func ParseLiteralSource (source string ) (keyName , value string , err error ) {
205
+ func parseLiteralSource (source string ) (keyName , value string , err error ) {
311
206
// leading equal is invalid
312
207
if strings .Index (source , "=" ) == 0 {
313
208
return "" , "" , fmt .Errorf ("invalid literal source %v, expected key=value" , source )
0 commit comments