-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5327 from stormqueen1990/feat/add-remove-configma…
…p-command feat: add remove configmap command
- Loading branch information
Showing
6 changed files
with
231 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2023 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/v5/commands/internal/kustfile" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
type removeConfigMapOptions struct { | ||
configMapNamesToRemove []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 the specified configmap(s) from " + | ||
konfig.DefaultKustomizationFileName(), | ||
Long: "", | ||
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 { | ||
switch { | ||
case len(args) == 0: | ||
return errors.New("at least one configmap name must be specified") | ||
case len(args) > 1: | ||
return fmt.Errorf("too many arguments: %s; to provide multiple configmaps to remove, please separate configmap names by commas", args) | ||
} | ||
|
||
o.configMapNamesToRemove = 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("could not read kustomization file: %w", err) | ||
} | ||
|
||
m, err := mf.Read() | ||
if err != nil { | ||
return fmt.Errorf("could not read kustomization file contents: %w", err) | ||
} | ||
|
||
foundConfigMaps := make(map[string]struct{}) | ||
|
||
newConfigMaps := make([]types.ConfigMapArgs, 0, len(m.ConfigMapGenerator)) | ||
for _, currentConfigMap := range m.ConfigMapGenerator { | ||
if kustfile.StringInSlice(currentConfigMap.Name, o.configMapNamesToRemove) { | ||
foundConfigMaps[currentConfigMap.Name] = struct{}{} | ||
continue | ||
} | ||
newConfigMaps = append(newConfigMaps, currentConfigMap) | ||
} | ||
|
||
if len(foundConfigMaps) == 0 { | ||
return fmt.Errorf("no specified configmap(s) were found in the %s file", | ||
konfig.DefaultKustomizationFileName()) | ||
} | ||
|
||
for _, name := range o.configMapNamesToRemove { | ||
if _, found := foundConfigMaps[name]; !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("failed to write kustomization file: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2023 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package remove //nolint:testpackage | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/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 | ||
expectedOutput 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}, | ||
expectedOutput: ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
`, | ||
}, | ||
"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), | ||
}, | ||
expectedOutput: ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
`, | ||
}, | ||
"miss": { | ||
input: fmt.Sprintf(` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
configMapGenerator: | ||
- name: %s | ||
files: | ||
- application.properties | ||
`, configMapName01), | ||
args: []string{"foo"}, | ||
expectedErr: "no specified configmap(s) were found", | ||
}, | ||
"no configmap name specified": { | ||
args: []string{}, | ||
expectedErr: "at least one configmap name must be specified", | ||
}, | ||
"too many configmap names specified": { | ||
args: []string{"test1", "test2"}, | ||
expectedErr: "too many arguments", | ||
}, | ||
"one existing and one non-existing": { | ||
input: fmt.Sprintf(` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
configMapGenerator: | ||
- name: %s | ||
files: | ||
- application.properties | ||
`, configMapName01), | ||
args: []string{fmt.Sprintf("%s,%s", configMapName01, "foo")}, | ||
expectedOutput: ` | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
`, | ||
}, | ||
} | ||
|
||
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 != "" { | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), tc.expectedErr) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
content, err := testutils_test.ReadTestKustomization(fSys) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expectedOutput, string(content)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters