Skip to content

Commit 0571a2f

Browse files
Incorporate feedback from original PR
* Incorporate feedback left on original PR. * Add more test cases. * Minor refactoring and update to copyright notice.
1 parent 0cdfa5b commit 0571a2f

File tree

5 files changed

+78
-28
lines changed

5 files changed

+78
-28
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
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
4958
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=

kustomize/commands/edit/remove/removeconfigmap.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 The Kubernetes Authors.
1+
// Copyright 2022 The Kubernetes Authors.
22
// SPDX-License-Identifier: Apache-2.0
33

44
package remove
@@ -12,7 +12,7 @@ import (
1212
"github.com/spf13/cobra"
1313
"sigs.k8s.io/kustomize/api/konfig"
1414
"sigs.k8s.io/kustomize/api/types"
15-
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/kustfile"
15+
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
1616
"sigs.k8s.io/kustomize/kyaml/filesys"
1717
)
1818

@@ -26,8 +26,9 @@ func newCmdRemoveConfigMap(fSys filesys.FileSystem) *cobra.Command {
2626

2727
cmd := &cobra.Command{
2828
Use: "configmap",
29-
Short: "Removes specified configmap" +
29+
Short: "Removes the specified configmap(s) from " +
3030
konfig.DefaultKustomizationFileName(),
31+
Long: "",
3132
Example: `
3233
remove configmap my-configmap
3334
`,
@@ -44,12 +45,13 @@ func newCmdRemoveConfigMap(fSys filesys.FileSystem) *cobra.Command {
4445

4546
// Validate validates removeConfigMap command.
4647
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)
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)
5253
}
54+
5355
o.configMapNamesToRemove = strings.Split(args[0], ",")
5456
return nil
5557
}
@@ -63,7 +65,7 @@ func (o *removeConfigMapOptions) RunRemoveConfigMap(fSys filesys.FileSystem) err
6365

6466
m, err := mf.Read()
6567
if err != nil {
66-
return fmt.Errorf("could not read kustomization file: %w", err)
68+
return fmt.Errorf("could not read kustomization file contents: %w", err)
6769
}
6870

6971
foundConfigMaps := make(map[string]struct{})
@@ -77,6 +79,11 @@ func (o *removeConfigMapOptions) RunRemoveConfigMap(fSys filesys.FileSystem) err
7779
newConfigMaps = append(newConfigMaps, currentConfigMap)
7880
}
7981

82+
if len(foundConfigMaps) == 0 {
83+
return fmt.Errorf("no specified configmap(s) were found in the %s file",
84+
konfig.DefaultKustomizationFileName())
85+
}
86+
8087
for _, name := range o.configMapNamesToRemove {
8188
if _, found := foundConfigMaps[name]; !found {
8289
log.Printf("configmap %s doesn't exist in kustomization file", name)
@@ -86,7 +93,7 @@ func (o *removeConfigMapOptions) RunRemoveConfigMap(fSys filesys.FileSystem) err
8693
m.ConfigMapGenerator = newConfigMaps
8794
err = mf.Write(m)
8895
if err != nil {
89-
return fmt.Errorf("configmap cannot write back to file, got %w", err)
96+
return fmt.Errorf("failed to write kustomization file: %w", err)
9097
}
9198
return nil
9299
}

kustomize/commands/edit/remove/removeconfigmap_test.go

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
// Copyright 2019 The Kubernetes Authors.
1+
// Copyright 2022 The Kubernetes Authors.
22
// SPDX-License-Identifier: Apache-2.0
33

44
package remove //nolint:testpackage
55

66
import (
77
"fmt"
8-
"strings"
98
"testing"
109

10+
"github.com/stretchr/testify/require"
11+
1112
"github.com/stretchr/testify/assert"
12-
testutils_test "sigs.k8s.io/kustomize/kustomize/v4/commands/internal/testutils"
13+
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
1314
"sigs.k8s.io/kustomize/kyaml/filesys"
1415
)
1516

@@ -18,9 +19,10 @@ func TestRemoveConfigMap(t *testing.T) {
1819
const configMapName02 = "example-configmap-02"
1920

2021
tests := map[string]struct {
21-
input string
22-
args []string
23-
expectedErr string
22+
input string
23+
args []string
24+
expectedOutput string
25+
expectedErr string
2426
}{
2527
"happy path": {
2628
input: fmt.Sprintf(`
@@ -32,6 +34,10 @@ configMapGenerator:
3234
- application.properties
3335
`, configMapName01),
3436
args: []string{configMapName01},
37+
expectedOutput: `
38+
apiVersion: kustomize.config.k8s.io/v1beta1
39+
kind: Kustomization
40+
`,
3541
},
3642
"multiple": {
3743
input: fmt.Sprintf(`
@@ -48,6 +54,10 @@ configMapGenerator:
4854
args: []string{
4955
fmt.Sprintf("%s,%s", configMapName01, configMapName02),
5056
},
57+
expectedOutput: `
58+
apiVersion: kustomize.config.k8s.io/v1beta1
59+
kind: Kustomization
60+
`,
5161
},
5262
"miss": {
5363
input: fmt.Sprintf(`
@@ -58,7 +68,31 @@ configMapGenerator:
5868
files:
5969
- application.properties
6070
`, configMapName01),
61-
args: []string{"foo"},
71+
args: []string{"foo"},
72+
expectedErr: "no specified configmap(s) were found",
73+
},
74+
"no configmap name specified": {
75+
args: []string{},
76+
expectedErr: "at least one configmap name must be specified",
77+
},
78+
"too many configmap names specified": {
79+
args: []string{"test1", "test2"},
80+
expectedErr: "too many arguments",
81+
},
82+
"one existing and one non-existing": {
83+
input: fmt.Sprintf(`
84+
apiVersion: kustomize.config.k8s.io/v1beta1
85+
kind: Kustomization
86+
configMapGenerator:
87+
- name: %s
88+
files:
89+
- application.properties
90+
`, configMapName01),
91+
args: []string{fmt.Sprintf("%s,%s", configMapName01, "foo")},
92+
expectedOutput: `
93+
apiVersion: kustomize.config.k8s.io/v1beta1
94+
kind: Kustomization
95+
`,
6296
},
6397
}
6498

@@ -68,17 +102,17 @@ configMapGenerator:
68102
testutils_test.WriteTestKustomizationWith(fSys, []byte(tc.input))
69103
cmd := newCmdRemoveConfigMap(fSys)
70104
err := cmd.RunE(cmd, tc.args)
105+
71106
if tc.expectedErr != "" {
72-
assert.Error(t, err)
107+
require.Error(t, err)
73108
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-
}
109+
return
81110
}
111+
112+
require.NoError(t, err)
113+
content, err := testutils_test.ReadTestKustomization(fSys)
114+
require.NoError(t, err)
115+
require.Equal(t, tc.expectedOutput, string(content))
82116
})
83117
}
84118
}

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)