Skip to content

Commit 1ad9f9d

Browse files
authored
Merge pull request #6083 from 0xMH/fix/5103-remove-component
Add kustomize edit remove component command
2 parents 0343400 + c07b901 commit 1ad9f9d

3 files changed

Lines changed: 204 additions & 0 deletions

File tree

kustomize/commands/edit/remove/all.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func NewCmdRemove(
2828
# Removes one or more secret from the kustomization file
2929
kustomize edit remove secret {name1},{name2}
3030
31+
# Removes one or more components from the kustomization file
32+
kustomize edit remove component {filepath} {filepath}
33+
kustomize edit remove component {pattern}
34+
3135
# Removes one or more patches from the kustomization file
3236
kustomize edit remove patch --path {filepath} --group {target group name} --version {target version}
3337
@@ -46,6 +50,7 @@ func NewCmdRemove(
4650
newCmdRemoveConfigMap(fSys),
4751
newCmdRemoveSecret(fSys),
4852
newCmdRemoveResource(fSys),
53+
newCmdRemoveComponent(fSys),
4954
newCmdRemoveLabel(fSys, v.MakeLabelNameValidator()),
5055
newCmdRemoveAnnotation(fSys, v.MakeAnnotationNameValidator()),
5156
newCmdRemovePatch(fSys),
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
"slices"
10+
11+
"github.com/spf13/cobra"
12+
"sigs.k8s.io/kustomize/api/konfig"
13+
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
14+
"sigs.k8s.io/kustomize/kyaml/filesys"
15+
)
16+
17+
type removeComponentOptions struct {
18+
componentFilePaths []string
19+
}
20+
21+
// newCmdRemoveComponent remove the name of a file containing a component to the kustomization file.
22+
func newCmdRemoveComponent(fSys filesys.FileSystem) *cobra.Command {
23+
var o removeComponentOptions
24+
25+
cmd := &cobra.Command{
26+
Use: "component",
27+
Short: "Removes one or more components from " +
28+
konfig.DefaultKustomizationFileName(),
29+
Example: `
30+
remove component ../../components/component1
31+
remove component ../../components/component1 ../../components/component2
32+
remove component ../../components/component*
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.RunRemoveComponent(fSys)
40+
},
41+
}
42+
return cmd
43+
}
44+
45+
// Validate validates removeComponent command.
46+
func (o *removeComponentOptions) Validate(args []string) error {
47+
if len(args) == 0 {
48+
return errors.New("must specify a component file")
49+
}
50+
o.componentFilePaths = args
51+
return nil
52+
}
53+
54+
// RunRemoveComponent runs Component command (do real work).
55+
func (o *removeComponentOptions) RunRemoveComponent(fSys filesys.FileSystem) error {
56+
mf, err := kustfile.NewKustomizationFile(fSys)
57+
if err != nil {
58+
return fmt.Errorf("failed to read kustomization file: %w", err)
59+
}
60+
61+
m, err := mf.Read()
62+
if err != nil {
63+
return fmt.Errorf("failed to read kustomization file content: %w", err)
64+
}
65+
66+
components, err := globPatterns(m.Components, o.componentFilePaths)
67+
if err != nil {
68+
return err
69+
}
70+
71+
if len(components) == 0 {
72+
return nil
73+
}
74+
75+
newComponents := make([]string, 0, len(m.Components))
76+
for _, component := range m.Components {
77+
if slices.Contains(components, component) {
78+
continue
79+
}
80+
newComponents = append(newComponents, component)
81+
}
82+
83+
m.Components = newComponents
84+
if err := mf.Write(m); err != nil {
85+
return fmt.Errorf("failed to write kustomization file: %w", err)
86+
}
87+
return nil
88+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2019 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package remove
5+
6+
import (
7+
"errors"
8+
"testing"
9+
10+
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
11+
)
12+
13+
func TestRemoveComponents(t *testing.T) {
14+
testCases := []testutils_test.RemoveTestCase{
15+
{
16+
Description: "remove components",
17+
Given: testutils_test.RemoveTestGivenValues{
18+
Items: []string{
19+
"../../components/component1",
20+
"../../components/component2",
21+
"../../components/component3",
22+
},
23+
RemoveArgs: []string{"../../components/component2"},
24+
},
25+
Expected: testutils_test.RemoveTestExpectedValues{
26+
Items: []string{
27+
"../../components/component1",
28+
"../../components/component3",
29+
},
30+
Deleted: []string{
31+
"../../components/component2",
32+
},
33+
},
34+
},
35+
{
36+
Description: "remove component with pattern",
37+
Given: testutils_test.RemoveTestGivenValues{
38+
Items: []string{
39+
"../../component/component1",
40+
"../../component/component2",
41+
"../../component/component3",
42+
"../../component/do_not_delete",
43+
},
44+
RemoveArgs: []string{"../../component/component*"},
45+
},
46+
Expected: testutils_test.RemoveTestExpectedValues{
47+
Items: []string{
48+
"../../component/do_not_delete",
49+
},
50+
Deleted: []string{
51+
"../../component/component1",
52+
"../../component/component2",
53+
"../../component/component3",
54+
},
55+
},
56+
},
57+
{
58+
Description: "nothing found to remove",
59+
Given: testutils_test.RemoveTestGivenValues{
60+
Items: []string{
61+
"component/component1",
62+
"component/component2",
63+
"component/component3",
64+
},
65+
RemoveArgs: []string{"component/component4"},
66+
},
67+
Expected: testutils_test.RemoveTestExpectedValues{
68+
Items: []string{
69+
"component/component1",
70+
"component/component2",
71+
"component/component3",
72+
},
73+
},
74+
},
75+
{
76+
Description: "no arguments",
77+
Given: testutils_test.RemoveTestGivenValues{},
78+
Expected: testutils_test.RemoveTestExpectedValues{
79+
Err: errors.New("must specify a component file"),
80+
},
81+
},
82+
{
83+
Description: "remove with multiple pattern arguments",
84+
Given: testutils_test.RemoveTestGivenValues{
85+
Items: []string{
86+
"foo/component1",
87+
"bar/component2",
88+
"do_not_delete",
89+
"component3",
90+
},
91+
RemoveArgs: []string{
92+
"foo/*",
93+
"bar/*",
94+
"compo*",
95+
},
96+
},
97+
Expected: testutils_test.RemoveTestExpectedValues{
98+
Items: []string{
99+
"do_not_delete",
100+
},
101+
Deleted: []string{
102+
"foo/component1",
103+
"bar/component2",
104+
"component3",
105+
},
106+
},
107+
},
108+
}
109+
110+
testutils_test.ExecuteRemoveTestCases(t, testCases, "components", newCmdRemoveComponent)
111+
}

0 commit comments

Comments
 (0)