forked from kubernetes-sigs/kustomize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configmap.go
151 lines (128 loc) · 4.32 KB
/
configmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2023 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package set
import (
"fmt"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
func newCmdSetConfigMap(
fSys filesys.FileSystem,
ldr ifc.KvLoader,
rf *resource.Factory,
) *cobra.Command {
var flags util.ConfigMapSecretFlagsAndArgs
cmd := &cobra.Command{
Use: "configmap NAME [--from-literal=key1=value1] [--namespace=namespace-name] [--new-namespace=new-namespace-name]",
Short: "Edits the value for an existing key for a configmap in the kustomization file",
Long: `Edits the value for an existing key in an existing configmap in the kustomization file.
Both configmap name and key name must exist for this command to succeed.`,
Example: `
# Edits an existing configmap in the kustomization file, changing value of key1 to 2
kustomize edit set configmap my-configmap --from-literal=key1=2
# Edits an existing configmap in the kustomization file, changing namespace to 'new-namespace'
kustomize edit set configmap my-configmap --namespace=current-namespace --new-namespace=new-namespace
`,
RunE: func(_ *cobra.Command, args []string) error {
return runEditSetConfigMap(flags, fSys, args, ldr, rf)
},
}
cmd.Flags().StringArrayVar(
&flags.LiteralSources,
util.FromLiteralFlag,
[]string{},
"Specify an existing key and a new value to update a ConfigMap (i.e. mykey=newvalue)")
cmd.Flags().StringVar(
&flags.Namespace,
util.NamespaceFlag,
"",
"Current namespace of the target ConfigMap")
cmd.Flags().StringVar(
&flags.NewNamespace,
util.NewNamespaceFlag,
"",
"New namespace value for the target ConfigMap")
return cmd
}
func runEditSetConfigMap(
flags util.ConfigMapSecretFlagsAndArgs,
fSys filesys.FileSystem,
args []string,
ldr ifc.KvLoader,
rf *resource.Factory,
) error {
err := flags.ExpandFileSource(fSys)
if err != nil {
return fmt.Errorf("failed to expand file source: %w", err)
}
err = flags.ValidateSet(args)
if err != nil {
return fmt.Errorf("failed to validate flags: %w", err)
}
// Load the kustomization file.
mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil {
return fmt.Errorf("failed to load kustomization file: %w", err)
}
kustomization, err := mf.Read()
if err != nil {
return fmt.Errorf("failed to read kustomization file: %w", err)
}
// Updates the existing ConfigMap
err = setConfigMap(ldr, kustomization, flags, rf)
if err != nil {
return fmt.Errorf("failed to create configmap: %w", err)
}
// Write out the kustomization file with added configmap.
err = mf.Write(kustomization)
if err != nil {
return fmt.Errorf("failed to write kustomization file: %w", err)
}
return nil
}
func setConfigMap(
ldr ifc.KvLoader,
k *types.Kustomization,
flags util.ConfigMapSecretFlagsAndArgs,
rf *resource.Factory,
) error {
args, err := findConfigMapArgs(k, flags.Name, flags.Namespace)
if err != nil {
return fmt.Errorf("could not set new ConfigMap value: %w", err)
}
if len(flags.LiteralSources) > 0 {
err := util.UpdateLiteralSources(&args.GeneratorArgs, flags)
if err != nil {
return fmt.Errorf("failed to update literal sources: %w", err)
}
}
// update namespace to new one
if flags.NewNamespace != "" {
args.Namespace = flags.NewNamespace
}
// Validate by trying to create corev1.configmap.
args.Options = types.MergeGlobalOptionsIntoLocal(
args.Options, k.GeneratorOptions)
_, err = rf.MakeConfigMap(ldr, args)
if err != nil {
return fmt.Errorf("failed to validate ConfigMap structure: %w", err)
}
return nil
}
// findConfigMapArgs finds the generator arguments corresponding to the specified
// ConfigMap name. ConfigMap must exist for this command to be successful.
func findConfigMapArgs(m *types.Kustomization, name, namespace string) (*types.ConfigMapArgs, error) {
cmIndex := slices.IndexFunc(m.ConfigMapGenerator, func(cmArgs types.ConfigMapArgs) bool {
return name == cmArgs.Name && util.NamespaceEqual(namespace, cmArgs.Namespace)
})
if cmIndex == -1 {
return nil, fmt.Errorf("unable to find ConfigMap with name '%q'", name)
}
return &m.ConfigMapGenerator[cmIndex], nil
}