feat: add remove configmap command

This commit is contained in:
yufei.li
2022-09-15 21:04:20 +08:00
parent ce18530656
commit 0d7c56dcf8
3 changed files with 165 additions and 0 deletions

View File

@@ -22,6 +22,9 @@ func NewCmdRemove(
kustomize edit remove resource {filepath} {filepath}
kustomize edit remove resource {pattern}
# Removes one or more configmap from the kustomization file
kustomize edit remove configmap {name1},{name2}
# Removes one or more patches from the kustomization file
kustomize edit remove patch --path {filepath} --group {target group name} --version {target version}
@@ -37,6 +40,7 @@ func NewCmdRemove(
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
newCmdRemoveConfigMap(fSys),
newCmdRemoveResource(fSys),
newCmdRemoveLabel(fSys, v.MakeLabelNameValidator()),
newCmdRemoveAnnotation(fSys, v.MakeAnnotationNameValidator()),

View File

@@ -0,0 +1,78 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package remove
import (
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v4/commands/internal/kustfile"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
type removeConfigMapOptions struct {
configMapNames []string
}
// newCmdRemoveResource remove the name of a file containing a resource to the kustomization file.
func newCmdRemoveConfigMap(fSys filesys.FileSystem) *cobra.Command {
var o removeConfigMapOptions
cmd := &cobra.Command{
Use: "configmap",
Short: "Removes specified configmap" +
konfig.DefaultKustomizationFileName(),
Example: `
remove configmap my-configmap
`,
RunE: func(cmd *cobra.Command, args []string) error {
err := o.Validate(args)
if err != nil {
return err
}
return o.RunRemoveConfigMap(fSys)
},
}
return cmd
}
// Validate validates removeConfigMap command.
func (o *removeConfigMapOptions) Validate(args []string) error {
if len(args) == 0 {
return errors.New("must specify a configmap name")
}
if len(args) > 1 {
return fmt.Errorf("too many arguments: %s; to provide multiple config map options, please separate options by comma", args)
}
o.configMapNames = strings.Split(args[0], ",")
return nil
}
// RunRemoveConfigMap runs ConfigMap command (do real work).
func (o *removeConfigMapOptions) RunRemoveConfigMap(fSys filesys.FileSystem) error {
mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil {
return err
}
m, err := mf.Read()
if err != nil {
return err
}
var newConfigMaps []types.ConfigMapArgs
for _, configMap := range m.ConfigMapGenerator {
if kustfile.StringInSlice(configMap.Name, o.configMapNames) {
continue
}
newConfigMaps = append(newConfigMaps, configMap)
}
m.ConfigMapGenerator = newConfigMaps
return mf.Write(m)
}

View File

@@ -0,0 +1,83 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package remove
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
testutils_test "sigs.k8s.io/kustomize/kustomize/v4/commands/internal/testutils"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
func TestRemoveConfigMap(t *testing.T) {
const configMapName = "example-configmap"
tests := map[string]struct {
input string
args []string
expectedErr string
}{
"happy path": {
input: fmt.Sprintf(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: %s
files:
- application.properties
`, configMapName),
args: []string{configMapName},
},
"multiple": {
input: fmt.Sprintf(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: %s-01
files:
- application.properties
- name: %s-02
files:
- application.properties
`, configMapName, configMapName),
args: []string{
fmt.Sprintf("%s-01,%s-02", configMapName, configMapName),
},
},
"miss": {
input: fmt.Sprintf(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: %s
files:
- application.properties
`, configMapName),
args: []string{"foo"},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, []byte(tc.input))
cmd := newCmdRemoveConfigMap(fSys)
err := cmd.RunE(cmd, tc.args)
if tc.expectedErr != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tc.expectedErr)
} else {
assert.NoError(t, err)
content, err := testutils_test.ReadTestKustomization(fSys)
assert.NoError(t, err)
for _, opt := range strings.Split(tc.args[0], ",") {
assert.NotContains(t, string(content), opt)
}
}
})
}
}