Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add remove configmap command #4796

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions kustomize/commands/edit/remove/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -37,6 +40,7 @@ func NewCmdRemove(
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
newCmdRemoveConfigMap(fSys),
newCmdRemoveResource(fSys),
newCmdRemoveLabel(fSys, v.MakeLabelNameValidator()),
newCmdRemoveAnnotation(fSys, v.MakeAnnotationNameValidator()),
Expand Down
96 changes: 96 additions & 0 deletions kustomize/commands/edit/remove/removeconfigmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package remove

import (
"errors"
"fmt"
"log"
"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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to change to v5

"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(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should say Removes specified configmap from kustomization.yaml

It currently reads as Removes specified configmapkustomization.yaml

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 fmt.Errorf("configmap cannot load from file system, got %w", err)
}

m, err := mf.Read()
if err != nil {
return fmt.Errorf("configmap cannot read from file, got %w", err)
}

foundConfigMaps := make(map[string]bool)
for _, removeName := range o.configMapNames {
foundConfigMaps[removeName] = false
}

newConfigMaps := make([]types.ConfigMapArgs, 0, len(m.ConfigMapGenerator))
for _, currentConfigMap := range m.ConfigMapGenerator {
if kustfile.StringInSlice(currentConfigMap.Name, o.configMapNames) {
foundConfigMaps[currentConfigMap.Name] = true
continue
}
newConfigMaps = append(newConfigMaps, currentConfigMap)
}

for name, found := range foundConfigMaps {
if !found {
log.Printf("configmap %s doesn't exist in kustomization file", name)
}
}

m.ConfigMapGenerator = newConfigMaps
err = mf.Write(m)
if err != nil {
return fmt.Errorf("configmap cannot write back to file, got %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion:

Suggested change
return fmt.Errorf("configmap cannot write back to file, got %w", err)
return fmt.Errorf("failed to write kustomization file: %w", err)

}
return nil
}
84 changes: 84 additions & 0 deletions kustomize/commands/edit/remove/removeconfigmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package remove //nolint:testpackage

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 configMapName01 = "example-configmap-01"
const configMapName02 = "example-configmap-02"

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
`, configMapName01),
args: []string{configMapName01},
},
"multiple": {
input: fmt.Sprintf(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: %s
files:
- application.properties
- name: %s
files:
- application.properties
`, configMapName01, configMapName02),
args: []string{
fmt.Sprintf("%s,%s", configMapName01, configMapName02),
},
},
"miss": {
input: fmt.Sprintf(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: %s
files:
- application.properties
`, configMapName01),
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)
}
}
})
}
}