Skip to content

Commit 0cdfa5b

Browse files
yufeimindsstormqueen1990
authored andcommitted
feat: add remove configmap command
(cherry picked from commit 0d7c56d) fix: add logging when configmap not exists (cherry picked from commit 0235f10) fix: correct lint issues (cherry picked from commit 8ca1f38) fix: Resolve conversation (cherry picked from commit 927804d)
1 parent 7c36ed2 commit 0cdfa5b

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

kustomize/commands/edit/remove/all.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func NewCmdRemove(
2222
kustomize edit remove resource {filepath} {filepath}
2323
kustomize edit remove resource {pattern}
2424
25+
# Removes one or more configmap from the kustomization file
26+
kustomize edit remove configmap {name1},{name2}
27+
2528
# Removes one or more patches from the kustomization file
2629
kustomize edit remove patch --path {filepath} --group {target group name} --version {target version}
2730
@@ -37,6 +40,7 @@ func NewCmdRemove(
3740
Args: cobra.MinimumNArgs(1),
3841
}
3942
c.AddCommand(
43+
newCmdRemoveConfigMap(fSys),
4044
newCmdRemoveResource(fSys),
4145
newCmdRemoveLabel(fSys, v.MakeLabelNameValidator()),
4246
newCmdRemoveAnnotation(fSys, v.MakeAnnotationNameValidator()),
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2019 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package remove
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"log"
10+
"strings"
11+
12+
"github.com/spf13/cobra"
13+
"sigs.k8s.io/kustomize/api/konfig"
14+
"sigs.k8s.io/kustomize/api/types"
15+
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/kustfile"
16+
"sigs.k8s.io/kustomize/kyaml/filesys"
17+
)
18+
19+
type removeConfigMapOptions struct {
20+
configMapNamesToRemove []string
21+
}
22+
23+
// newCmdRemoveResource remove the name of a file containing a resource to the kustomization file.
24+
func newCmdRemoveConfigMap(fSys filesys.FileSystem) *cobra.Command {
25+
var o removeConfigMapOptions
26+
27+
cmd := &cobra.Command{
28+
Use: "configmap",
29+
Short: "Removes specified configmap" +
30+
konfig.DefaultKustomizationFileName(),
31+
Example: `
32+
remove configmap my-configmap
33+
`,
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
err := o.Validate(args)
36+
if err != nil {
37+
return err
38+
}
39+
return o.RunRemoveConfigMap(fSys)
40+
},
41+
}
42+
return cmd
43+
}
44+
45+
// Validate validates removeConfigMap command.
46+
func (o *removeConfigMapOptions) Validate(args []string) error {
47+
if len(args) == 0 {
48+
return errors.New("must specify a ConfigMap name")
49+
}
50+
if len(args) > 1 {
51+
return fmt.Errorf("too many arguments: %s; to provide multiple ConfigMaps to remove, please separate ConfigMap names by commas", args)
52+
}
53+
o.configMapNamesToRemove = strings.Split(args[0], ",")
54+
return nil
55+
}
56+
57+
// RunRemoveConfigMap runs ConfigMap command (do real work).
58+
func (o *removeConfigMapOptions) RunRemoveConfigMap(fSys filesys.FileSystem) error {
59+
mf, err := kustfile.NewKustomizationFile(fSys)
60+
if err != nil {
61+
return fmt.Errorf("could not read kustomization file: %w", err)
62+
}
63+
64+
m, err := mf.Read()
65+
if err != nil {
66+
return fmt.Errorf("could not read kustomization file: %w", err)
67+
}
68+
69+
foundConfigMaps := make(map[string]struct{})
70+
71+
newConfigMaps := make([]types.ConfigMapArgs, 0, len(m.ConfigMapGenerator))
72+
for _, currentConfigMap := range m.ConfigMapGenerator {
73+
if kustfile.StringInSlice(currentConfigMap.Name, o.configMapNamesToRemove) {
74+
foundConfigMaps[currentConfigMap.Name] = struct{}{}
75+
continue
76+
}
77+
newConfigMaps = append(newConfigMaps, currentConfigMap)
78+
}
79+
80+
for _, name := range o.configMapNamesToRemove {
81+
if _, found := foundConfigMaps[name]; !found {
82+
log.Printf("configmap %s doesn't exist in kustomization file", name)
83+
}
84+
}
85+
86+
m.ConfigMapGenerator = newConfigMaps
87+
err = mf.Write(m)
88+
if err != nil {
89+
return fmt.Errorf("configmap cannot write back to file, got %w", err)
90+
}
91+
return nil
92+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2019 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package remove //nolint:testpackage
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
testutils_test "sigs.k8s.io/kustomize/kustomize/v4/commands/internal/testutils"
13+
"sigs.k8s.io/kustomize/kyaml/filesys"
14+
)
15+
16+
func TestRemoveConfigMap(t *testing.T) {
17+
const configMapName01 = "example-configmap-01"
18+
const configMapName02 = "example-configmap-02"
19+
20+
tests := map[string]struct {
21+
input string
22+
args []string
23+
expectedErr string
24+
}{
25+
"happy path": {
26+
input: fmt.Sprintf(`
27+
apiVersion: kustomize.config.k8s.io/v1beta1
28+
kind: Kustomization
29+
configMapGenerator:
30+
- name: %s
31+
files:
32+
- application.properties
33+
`, configMapName01),
34+
args: []string{configMapName01},
35+
},
36+
"multiple": {
37+
input: fmt.Sprintf(`
38+
apiVersion: kustomize.config.k8s.io/v1beta1
39+
kind: Kustomization
40+
configMapGenerator:
41+
- name: %s
42+
files:
43+
- application.properties
44+
- name: %s
45+
files:
46+
- application.properties
47+
`, configMapName01, configMapName02),
48+
args: []string{
49+
fmt.Sprintf("%s,%s", configMapName01, configMapName02),
50+
},
51+
},
52+
"miss": {
53+
input: fmt.Sprintf(`
54+
apiVersion: kustomize.config.k8s.io/v1beta1
55+
kind: Kustomization
56+
configMapGenerator:
57+
- name: %s
58+
files:
59+
- application.properties
60+
`, configMapName01),
61+
args: []string{"foo"},
62+
},
63+
}
64+
65+
for name, tc := range tests {
66+
t.Run(name, func(t *testing.T) {
67+
fSys := filesys.MakeFsInMemory()
68+
testutils_test.WriteTestKustomizationWith(fSys, []byte(tc.input))
69+
cmd := newCmdRemoveConfigMap(fSys)
70+
err := cmd.RunE(cmd, tc.args)
71+
if tc.expectedErr != "" {
72+
assert.Error(t, err)
73+
assert.Contains(t, err.Error(), tc.expectedErr)
74+
} else {
75+
assert.NoError(t, err)
76+
content, err := testutils_test.ReadTestKustomization(fSys)
77+
assert.NoError(t, err)
78+
for _, opt := range strings.Split(tc.args[0], ",") {
79+
assert.NotContains(t, string(content), opt)
80+
}
81+
}
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)