Skip to content

Commit 2f99707

Browse files
authored
Merge pull request #5367 from stormqueen1990/fix/edit-add-configmap-namespace
fix: add namespace option to 'edit add configmap' command
2 parents 81297df + 7a08dde commit 2f99707

File tree

5 files changed

+214
-111
lines changed

5 files changed

+214
-111
lines changed

api/types/generatorargs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package types
55

66
// GeneratorArgs contains arguments common to ConfigMap and Secret generators.
77
type GeneratorArgs struct {
8-
// Namespace for the configmap, optional
8+
// Namespace for the resource, optional
99
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
1010

1111
// Name - actually the partial name - of the generated resource.

kustomize/commands/edit/add/configmap.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func newCmdAddConfigMap(
2222
) *cobra.Command {
2323
var flags configmapSecretFlagsAndArgs
2424
cmd := &cobra.Command{
25-
Use: "configmap NAME [--behavior={create|merge|replace}] [--from-file=[key=]source] [--from-literal=key1=value1]",
25+
Use: "configmap NAME [--namespace=namespace-name] [--behavior={create|merge|replace}] [--from-file=[key=]source] [--from-literal=key1=value1]",
2626
Short: "Adds a configmap to the kustomization file",
2727
Long: "",
2828
Example: `
@@ -36,7 +36,10 @@ func newCmdAddConfigMap(
3636
kustomize edit add configmap my-configmap --from-env-file=env/path.env
3737
3838
# Adds a configmap from env-file with behavior merge
39-
kustomize edit add configmap my-configmap --behavior=merge --from-env-file=env/path.env
39+
kustomize edit add configmap my-configmap --behavior=merge --from-env-file=env/path.env
40+
41+
# Adds a configmap to the kustomization file with a specific namespace
42+
kustomize edit add configmap my-configmap --namespace test-ns --from-literal=my-key=my-value
4043
`,
4144
RunE: func(_ *cobra.Command, args []string) error {
4245
return runEditAddConfigMap(flags, fSys, args, ldr, rf)
@@ -62,16 +65,21 @@ func newCmdAddConfigMap(
6265
"Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).")
6366
cmd.Flags().BoolVar(
6467
&flags.DisableNameSuffixHash,
65-
flagDisableNameSuffixHash,
68+
disableNameSuffixHashFlag,
6669
false,
6770
"Disable the name suffix for the configmap")
6871
cmd.Flags().StringVar(
6972
&flags.Behavior,
70-
flagBehavior,
73+
behaviorFlag,
7174
"",
7275
"Specify the behavior for config map generation, i.e whether to create a new configmap (the default), "+
7376
"to merge with a previously defined one, or to replace an existing one. Merge and replace should be used only "+
7477
" when overriding an existing configmap defined in a base")
78+
cmd.Flags().StringVar(
79+
&flags.Namespace,
80+
namespaceFlag,
81+
"",
82+
"Specify the namespace of the ConfigMap")
7583

7684
return cmd
7785
}
@@ -128,7 +136,7 @@ func addConfigMap(
128136
flags configmapSecretFlagsAndArgs,
129137
rf *resource.Factory,
130138
) error {
131-
args := findOrMakeConfigMapArgs(k, flags.Name)
139+
args := findOrMakeConfigMapArgs(k, flags.Name, flags.Namespace)
132140
mergeFlagsIntoGeneratorArgs(&args.GeneratorArgs, flags)
133141
// Validate by trying to create corev1.configmap.
134142
args.Options = types.MergeGlobalOptionsIntoLocal(
@@ -137,15 +145,15 @@ func addConfigMap(
137145
return err
138146
}
139147

140-
func findOrMakeConfigMapArgs(m *types.Kustomization, name string) *types.ConfigMapArgs {
148+
func findOrMakeConfigMapArgs(m *types.Kustomization, name, namespace string) *types.ConfigMapArgs {
141149
for i, v := range m.ConfigMapGenerator {
142-
if name == v.Name {
150+
if name == v.Name && namespace == v.Namespace {
143151
return &m.ConfigMapGenerator[i]
144152
}
145153
}
146154
// config map not found, create new one and add it to the kustomization file.
147155
cm := &types.ConfigMapArgs{
148-
GeneratorArgs: types.GeneratorArgs{Name: name},
156+
GeneratorArgs: types.GeneratorArgs{Name: name, Namespace: namespace},
149157
}
150158
m.ConfigMapGenerator = append(m.ConfigMapGenerator, *cm)
151159
return &m.ConfigMapGenerator[len(m.ConfigMapGenerator)-1]

kustomize/commands/edit/add/configmapSecretFlagsAndArgs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
"sigs.k8s.io/kustomize/api/types"
11-
1211
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util"
1312
"sigs.k8s.io/kustomize/kyaml/filesys"
1413
)
@@ -17,8 +16,9 @@ const (
1716
fromFileFlag = "from-file"
1817
fromLiteralFlag = "from-literal"
1918
fromEnvFileFlag = "from-env-file"
20-
flagDisableNameSuffixHash = "disableNameSuffixHash"
21-
flagBehavior = "behavior"
19+
disableNameSuffixHashFlag = "disableNameSuffixHash"
20+
behaviorFlag = "behavior"
21+
namespaceFlag = "namespace"
2222
flagFormat = "--%s=%s"
2323
)
2424

0 commit comments

Comments
 (0)