4
4
package add
5
5
6
6
import (
7
+ "fmt"
8
+
7
9
"github.com/spf13/cobra"
8
10
"sigs.k8s.io/kustomize/api/ifc"
9
11
"sigs.k8s.io/kustomize/api/resource"
@@ -16,8 +18,9 @@ import (
16
18
func newCmdAddConfigMap (
17
19
fSys filesys.FileSystem ,
18
20
ldr ifc.KvLoader ,
19
- rf * resource.Factory ) * cobra.Command {
20
- var flags flagsAndArgs
21
+ rf * resource.Factory ,
22
+ ) * cobra.Command {
23
+ var flags configmapSecretFlagsAndArgs
21
24
cmd := & cobra.Command {
22
25
Use : "configmap NAME [--behavior={create|merge|replace}] [--from-file=[key=]source] [--from-literal=key1=value1]" ,
23
26
Short : "Adds a configmap to the kustomization file" ,
@@ -36,63 +39,35 @@ func newCmdAddConfigMap(
36
39
kustomize edit add configmap my-configmap --behavior=merge --from-env-file=env/path.env
37
40
` ,
38
41
RunE : func (_ * cobra.Command , args []string ) error {
39
- err := flags .ExpandFileSource (fSys )
40
- if err != nil {
41
- return err
42
- }
43
-
44
- err = flags .Validate (args )
45
- if err != nil {
46
- return err
47
- }
48
-
49
- // Load the kustomization file.
50
- mf , err := kustfile .NewKustomizationFile (fSys )
51
- if err != nil {
52
- return err
53
- }
54
-
55
- kustomization , err := mf .Read ()
56
- if err != nil {
57
- return err
58
- }
59
-
60
- // Add the flagsAndArgs map to the kustomization file.
61
- err = addConfigMap (ldr , kustomization , flags , rf )
62
- if err != nil {
63
- return err
64
- }
65
-
66
- // Write out the kustomization file with added configmap.
67
- return mf .Write (kustomization )
42
+ return runEditAddConfigMap (flags , fSys , args , ldr , rf )
68
43
},
69
44
}
70
45
71
46
cmd .Flags ().StringSliceVar (
72
47
& flags .FileSources ,
73
- "from-file" ,
48
+ fromFileFlag ,
74
49
[]string {},
75
50
"Key file can be specified using its file path, in which case file basename will be used as configmap " +
76
51
"key, or optionally with a key and file path, in which case the given key will be used. Specifying a " +
77
52
"directory will iterate each named file in the directory whose basename is a valid configmap key." )
78
53
cmd .Flags ().StringArrayVar (
79
54
& flags .LiteralSources ,
80
- "from-literal" ,
55
+ fromLiteralFlag ,
81
56
[]string {},
82
57
"Specify a key and literal value to insert in configmap (i.e. mykey=somevalue)" )
83
58
cmd .Flags ().StringVar (
84
59
& flags .EnvFileSource ,
85
- "from-env-file" ,
60
+ fromEnvFileFlag ,
86
61
"" ,
87
62
"Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file)." )
88
63
cmd .Flags ().BoolVar (
89
64
& flags .DisableNameSuffixHash ,
90
- "disableNameSuffixHash" ,
65
+ flagDisableNameSuffixHash ,
91
66
false ,
92
67
"Disable the name suffix for the configmap" )
93
68
cmd .Flags ().StringVar (
94
69
& flags .Behavior ,
95
- "behavior" ,
70
+ flagBehavior ,
96
71
"" ,
97
72
"Specify the behavior for config map generation, i.e whether to create a new configmap (the default), " +
98
73
"to merge with a previously defined one, or to replace an existing one. Merge and replace should be used only " +
@@ -101,15 +76,60 @@ func newCmdAddConfigMap(
101
76
return cmd
102
77
}
103
78
79
+ func runEditAddConfigMap (
80
+ flags configmapSecretFlagsAndArgs ,
81
+ fSys filesys.FileSystem ,
82
+ args []string ,
83
+ ldr ifc.KvLoader ,
84
+ rf * resource.Factory ,
85
+ ) error {
86
+ err := flags .ExpandFileSource (fSys )
87
+ if err != nil {
88
+ return fmt .Errorf ("failed to expand file source: %w" , err )
89
+ }
90
+
91
+ err = flags .Validate (args )
92
+ if err != nil {
93
+ return fmt .Errorf ("failed to validate flags: %w" , err )
94
+ }
95
+
96
+ // Load the kustomization file.
97
+ mf , err := kustfile .NewKustomizationFile (fSys )
98
+ if err != nil {
99
+ return fmt .Errorf ("failed to load kustomization file: %w" , err )
100
+ }
101
+
102
+ kustomization , err := mf .Read ()
103
+ if err != nil {
104
+ return fmt .Errorf ("failed to read kustomization file: %w" , err )
105
+ }
106
+
107
+ // Add the configmapSecretFlagsAndArgs map to the kustomization file.
108
+ err = addConfigMap (ldr , kustomization , flags , rf )
109
+ if err != nil {
110
+ return fmt .Errorf ("failed to create configmap: %w" , err )
111
+ }
112
+
113
+ // Write out the kustomization file with added configmap.
114
+ err = mf .Write (kustomization )
115
+ if err != nil {
116
+ return fmt .Errorf ("failed to write kustomization file: %w" , err )
117
+ }
118
+
119
+ return nil
120
+ }
121
+
104
122
// addConfigMap adds a configmap to a kustomization file.
105
123
// Note: error may leave kustomization file in an undefined state.
106
124
// Suggest passing a copy of kustomization file.
107
125
func addConfigMap (
108
126
ldr ifc.KvLoader ,
109
127
k * types.Kustomization ,
110
- flags flagsAndArgs , rf * resource.Factory ) error {
128
+ flags configmapSecretFlagsAndArgs ,
129
+ rf * resource.Factory ,
130
+ ) error {
111
131
args := findOrMakeConfigMapArgs (k , flags .Name )
112
- mergeFlagsIntoCmArgs ( args , flags )
132
+ mergeFlagsIntoGeneratorArgs ( & args . GeneratorArgs , flags )
113
133
// Validate by trying to create corev1.configmap.
114
134
args .Options = types .MergeGlobalOptionsIntoLocal (
115
135
args .Options , k .GeneratorOptions )
@@ -124,30 +144,9 @@ func findOrMakeConfigMapArgs(m *types.Kustomization, name string) *types.ConfigM
124
144
}
125
145
}
126
146
// config map not found, create new one and add it to the kustomization file.
127
- cm := & types.ConfigMapArgs {GeneratorArgs : types.GeneratorArgs {Name : name }}
147
+ cm := & types.ConfigMapArgs {
148
+ GeneratorArgs : types.GeneratorArgs {Name : name },
149
+ }
128
150
m .ConfigMapGenerator = append (m .ConfigMapGenerator , * cm )
129
151
return & m .ConfigMapGenerator [len (m .ConfigMapGenerator )- 1 ]
130
152
}
131
-
132
- func mergeFlagsIntoCmArgs (args * types.ConfigMapArgs , flags flagsAndArgs ) {
133
- if len (flags .LiteralSources ) > 0 {
134
- args .LiteralSources = append (
135
- args .LiteralSources , flags .LiteralSources ... )
136
- }
137
- if len (flags .FileSources ) > 0 {
138
- args .FileSources = append (
139
- args .FileSources , flags .FileSources ... )
140
- }
141
- if flags .EnvFileSource != "" {
142
- args .EnvSources = append (
143
- args .EnvSources , flags .EnvFileSource )
144
- }
145
- if flags .DisableNameSuffixHash {
146
- args .Options = & types.GeneratorOptions {
147
- DisableNameSuffixHash : true ,
148
- }
149
- }
150
- if flags .Behavior != "" {
151
- args .Behavior = flags .Behavior
152
- }
153
- }
0 commit comments