-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new command 'edit set configmap'
* Add a new command 'edit set configmap' to allow editing the values of an already-existing configmap in a kustomization file. * Add tests to validate the new feature.
- Loading branch information
1 parent
96ad106
commit c87b1aa
Showing
11 changed files
with
421 additions
and
45 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
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,147 @@ | ||
// Copyright 2023 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package set | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/exp/slices" | ||
"sigs.k8s.io/kustomize/api/ifc" | ||
"sigs.k8s.io/kustomize/api/resource" | ||
"sigs.k8s.io/kustomize/api/types" | ||
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" | ||
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util" | ||
"sigs.k8s.io/kustomize/kyaml/filesys" | ||
) | ||
|
||
func newCmdSetConfigMap( | ||
fSys filesys.FileSystem, | ||
ldr ifc.KvLoader, | ||
rf *resource.Factory, | ||
) *cobra.Command { | ||
var flags util.ConfigMapSecretFlagsAndArgs | ||
cmd := &cobra.Command{ | ||
Use: "configmap NAME [--from-literal=key1=value1]", | ||
Short: "Edits the value for an existing key for a configmap in the kustomization file", | ||
Long: `Edits the value for an existing key in an existing configmap in the kustomization file. | ||
Both configmap name and key name must exist for this command to succeed.`, | ||
Example: ` | ||
# Edits an existing configmap in the kustomization file, changing value of key1 to 2 | ||
kustomize edit set configmap my-configmap --from-literal=key1=2 | ||
# Edits an existing configmap in the kustomization file, changing namespace to 'new-test-ns' | ||
kustomize edit set configmap my-configmap --namespace=current-test-ns --new-namespace=new-test-ns | ||
`, | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
return runEditSetConfigMap(flags, fSys, args, ldr, rf) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringArrayVar( | ||
&flags.LiteralSources, | ||
util.FromLiteralFlag, | ||
[]string{}, | ||
"Specify an existing key and a new value to update a ConfigMap (i.e. mykey=newvalue)") | ||
cmd.Flags().StringVar( | ||
&flags.Namespace, | ||
"namespace", | ||
"", | ||
"Current namespace of the target ConfigMap") | ||
cmd.Flags().StringVar( | ||
&flags.NewNamespace, | ||
"new-namespace", | ||
"", | ||
"New namespace value for the target ConfigMap") | ||
|
||
return cmd | ||
} | ||
|
||
func runEditSetConfigMap( | ||
flags util.ConfigMapSecretFlagsAndArgs, | ||
fSys filesys.FileSystem, | ||
args []string, | ||
ldr ifc.KvLoader, | ||
rf *resource.Factory, | ||
) error { | ||
err := flags.ExpandFileSource(fSys) | ||
if err != nil { | ||
return fmt.Errorf("failed to expand file source: %w", err) | ||
} | ||
|
||
err = flags.ValidateSet(args) | ||
if err != nil { | ||
return fmt.Errorf("failed to validate flags: %w", err) | ||
} | ||
|
||
// Load the kustomization file. | ||
mf, err := kustfile.NewKustomizationFile(fSys) | ||
if err != nil { | ||
return fmt.Errorf("failed to load kustomization file: %w", err) | ||
} | ||
|
||
kustomization, err := mf.Read() | ||
if err != nil { | ||
return fmt.Errorf("failed to read kustomization file: %w", err) | ||
} | ||
|
||
// Updates the existing ConfigMap | ||
err = setConfigMap(ldr, kustomization, flags, rf) | ||
if err != nil { | ||
return fmt.Errorf("failed to create configmap: %w", err) | ||
} | ||
|
||
// Write out the kustomization file with added configmap. | ||
err = mf.Write(kustomization) | ||
if err != nil { | ||
return fmt.Errorf("failed to write kustomization file: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func setConfigMap( | ||
ldr ifc.KvLoader, | ||
k *types.Kustomization, | ||
flags util.ConfigMapSecretFlagsAndArgs, | ||
rf *resource.Factory, | ||
) error { | ||
args, err := findConfigMapArgs(k, flags.Name, flags.Namespace) | ||
if err != nil { | ||
return fmt.Errorf("could not set new ConfigMap value: %w", err) | ||
} | ||
|
||
if len(flags.LiteralSources) > 0 { | ||
err := util.UpdateLiteralSources(&args.GeneratorArgs, flags) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// update namespace to new one | ||
if flags.NewNamespace != "" { | ||
args.Namespace = flags.NewNamespace | ||
} | ||
|
||
// Validate by trying to create corev1.configmap. | ||
args.Options = types.MergeGlobalOptionsIntoLocal( | ||
args.Options, k.GeneratorOptions) | ||
|
||
_, err = rf.MakeConfigMap(ldr, args) | ||
return err | ||
} | ||
|
||
// findConfigMapArgs finds the generator arguments corresponding to the specified | ||
// ConfigMap name. ConfigMap must exist for this command to be successful. | ||
func findConfigMapArgs(m *types.Kustomization, name, namespace string) (*types.ConfigMapArgs, error) { | ||
cmIndex := slices.IndexFunc(m.ConfigMapGenerator, func(cmArgs types.ConfigMapArgs) bool { | ||
return name == cmArgs.Name && namespace == cmArgs.Namespace | ||
}) | ||
|
||
if cmIndex == -1 { | ||
return nil, fmt.Errorf("unable to find ConfigMap with name '%s'", name) | ||
} | ||
|
||
return &m.ConfigMapGenerator[cmIndex], nil | ||
} |
Oops, something went wrong.