Skip to content

Commit 1bd58bd

Browse files
authored
Merge pull request #5327 from stormqueen1990/feat/add-remove-configmap-command
feat: add remove configmap command
2 parents 59696d1 + 9107fa3 commit 1bd58bd

File tree

6 files changed

+231
-3
lines changed

6 files changed

+231
-3
lines changed

go.work.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,28 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
3131
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
3232
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
3333
go.starlark.net v0.0.0-20190528202925-30ae18b8564f/go.mod h1:c1/X6cHgvdXj6pUlmWKMkuqRnW4K8x2vwt6JAaaircg=
34+
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
3435
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
3536
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
37+
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
3638
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
39+
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
3740
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
41+
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
3842
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3943
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
44+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4045
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4146
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
47+
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
4248
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
49+
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
50+
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
4351
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
4452
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
4553
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
4654
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
55+
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
4756
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
4857
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
4958
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2023 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/v5/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 the specified configmap(s) from " +
30+
konfig.DefaultKustomizationFileName(),
31+
Long: "",
32+
Example: `
33+
remove configmap my-configmap
34+
`,
35+
RunE: func(cmd *cobra.Command, args []string) error {
36+
err := o.Validate(args)
37+
if err != nil {
38+
return err
39+
}
40+
return o.RunRemoveConfigMap(fSys)
41+
},
42+
}
43+
return cmd
44+
}
45+
46+
// Validate validates removeConfigMap command.
47+
func (o *removeConfigMapOptions) Validate(args []string) error {
48+
switch {
49+
case len(args) == 0:
50+
return errors.New("at least one configmap name must be specified")
51+
case len(args) > 1:
52+
return fmt.Errorf("too many arguments: %s; to provide multiple configmaps to remove, please separate configmap names by commas", args)
53+
}
54+
55+
o.configMapNamesToRemove = strings.Split(args[0], ",")
56+
return nil
57+
}
58+
59+
// RunRemoveConfigMap runs ConfigMap command (do real work).
60+
func (o *removeConfigMapOptions) RunRemoveConfigMap(fSys filesys.FileSystem) error {
61+
mf, err := kustfile.NewKustomizationFile(fSys)
62+
if err != nil {
63+
return fmt.Errorf("could not read kustomization file: %w", err)
64+
}
65+
66+
m, err := mf.Read()
67+
if err != nil {
68+
return fmt.Errorf("could not read kustomization file contents: %w", err)
69+
}
70+
71+
foundConfigMaps := make(map[string]struct{})
72+
73+
newConfigMaps := make([]types.ConfigMapArgs, 0, len(m.ConfigMapGenerator))
74+
for _, currentConfigMap := range m.ConfigMapGenerator {
75+
if kustfile.StringInSlice(currentConfigMap.Name, o.configMapNamesToRemove) {
76+
foundConfigMaps[currentConfigMap.Name] = struct{}{}
77+
continue
78+
}
79+
newConfigMaps = append(newConfigMaps, currentConfigMap)
80+
}
81+
82+
if len(foundConfigMaps) == 0 {
83+
return fmt.Errorf("no specified configmap(s) were found in the %s file",
84+
konfig.DefaultKustomizationFileName())
85+
}
86+
87+
for _, name := range o.configMapNamesToRemove {
88+
if _, found := foundConfigMaps[name]; !found {
89+
log.Printf("configmap %s doesn't exist in kustomization file", name)
90+
}
91+
}
92+
93+
m.ConfigMapGenerator = newConfigMaps
94+
err = mf.Write(m)
95+
if err != nil {
96+
return fmt.Errorf("failed to write kustomization file: %w", err)
97+
}
98+
return nil
99+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright 2023 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package remove //nolint:testpackage
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
12+
"sigs.k8s.io/kustomize/kyaml/filesys"
13+
)
14+
15+
func TestRemoveConfigMap(t *testing.T) {
16+
const configMapName01 = "example-configmap-01"
17+
const configMapName02 = "example-configmap-02"
18+
19+
tests := map[string]struct {
20+
input string
21+
args []string
22+
expectedOutput 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+
expectedOutput: `
36+
apiVersion: kustomize.config.k8s.io/v1beta1
37+
kind: Kustomization
38+
`,
39+
},
40+
"multiple": {
41+
input: fmt.Sprintf(`
42+
apiVersion: kustomize.config.k8s.io/v1beta1
43+
kind: Kustomization
44+
configMapGenerator:
45+
- name: %s
46+
files:
47+
- application.properties
48+
- name: %s
49+
files:
50+
- application.properties
51+
`, configMapName01, configMapName02),
52+
args: []string{
53+
fmt.Sprintf("%s,%s", configMapName01, configMapName02),
54+
},
55+
expectedOutput: `
56+
apiVersion: kustomize.config.k8s.io/v1beta1
57+
kind: Kustomization
58+
`,
59+
},
60+
"miss": {
61+
input: fmt.Sprintf(`
62+
apiVersion: kustomize.config.k8s.io/v1beta1
63+
kind: Kustomization
64+
configMapGenerator:
65+
- name: %s
66+
files:
67+
- application.properties
68+
`, configMapName01),
69+
args: []string{"foo"},
70+
expectedErr: "no specified configmap(s) were found",
71+
},
72+
"no configmap name specified": {
73+
args: []string{},
74+
expectedErr: "at least one configmap name must be specified",
75+
},
76+
"too many configmap names specified": {
77+
args: []string{"test1", "test2"},
78+
expectedErr: "too many arguments",
79+
},
80+
"one existing and one non-existing": {
81+
input: fmt.Sprintf(`
82+
apiVersion: kustomize.config.k8s.io/v1beta1
83+
kind: Kustomization
84+
configMapGenerator:
85+
- name: %s
86+
files:
87+
- application.properties
88+
`, configMapName01),
89+
args: []string{fmt.Sprintf("%s,%s", configMapName01, "foo")},
90+
expectedOutput: `
91+
apiVersion: kustomize.config.k8s.io/v1beta1
92+
kind: Kustomization
93+
`,
94+
},
95+
}
96+
97+
for name, tc := range tests {
98+
t.Run(name, func(t *testing.T) {
99+
fSys := filesys.MakeFsInMemory()
100+
testutils_test.WriteTestKustomizationWith(fSys, []byte(tc.input))
101+
cmd := newCmdRemoveConfigMap(fSys)
102+
err := cmd.RunE(cmd, tc.args)
103+
104+
if tc.expectedErr != "" {
105+
require.Error(t, err)
106+
require.Contains(t, err.Error(), tc.expectedErr)
107+
return
108+
}
109+
110+
require.NoError(t, err)
111+
content, err := testutils_test.ReadTestKustomization(fSys)
112+
require.NoError(t, err)
113+
require.Equal(t, tc.expectedOutput, string(content))
114+
})
115+
}
116+
}

kustomize/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ require (
3333
github.com/pmezard/go-difflib v1.0.0 // indirect
3434
github.com/xlab/treeprint v1.2.0 // indirect
3535
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
36-
golang.org/x/sys v0.8.0 // indirect
36+
golang.org/x/sys v0.12.0 // indirect
3737
google.golang.org/protobuf v1.30.0 // indirect
3838
gopkg.in/evanphx/json-patch.v5 v5.6.0 // indirect
3939
gopkg.in/inf.v0 v0.9.1 // indirect

kustomize/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd
6767
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 h1:+FNtrFTmVw0YZGpBGX56XDee331t6JAXeK2bcyhLOOc=
6868
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o=
6969
golang.org/x/sys v0.0.0-20191002063906-3421d5a6bb1c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
70-
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
71-
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
70+
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
71+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7272
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
7373
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
7474
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)