-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: edit set configmap #5391
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
Merged
k8s-ci-robot
merged 7 commits into
kubernetes-sigs:master
from
stormqueen1990:feat/edit-set-configmap
Nov 17, 2023
Merged
feat: edit set configmap #5391
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a6fc28c
feat: add new command 'edit set configmap'
stormqueen1990 95563a9
fix: add tests, minor refactoring to use constants
stormqueen1990 f95ff57
fix: change format to print resource name
stormqueen1990 cae4b3e
fix: add changes from code review
stormqueen1990 f44ae56
fix: rollback sort for edit add/set configmap
stormqueen1990 65f3391
chore: rename test package and unexport functions
stormqueen1990 de2dcaf
feat: handle empty and default namespace as equal
stormqueen1990 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,151 @@ | ||
// 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] [--namespace=namespace-name] [--new-namespace=new-namespace-name]", | ||
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-namespace' | ||
kustomize edit set configmap my-configmap --namespace=current-namespace --new-namespace=new-namespace | ||
`, | ||
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, | ||
util.NamespaceFlag, | ||
"", | ||
"Current namespace of the target ConfigMap") | ||
cmd.Flags().StringVar( | ||
&flags.NewNamespace, | ||
util.NewNamespaceFlag, | ||
"", | ||
"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 fmt.Errorf("failed to update literal sources: %w", 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) | ||
if err != nil { | ||
return fmt.Errorf("failed to validate ConfigMap structure: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// 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 && util.NamespaceEqual(namespace, cmArgs.Namespace) | ||
}) | ||
|
||
if cmIndex == -1 { | ||
return nil, fmt.Errorf("unable to find ConfigMap with name '%q'", name) | ||
} | ||
|
||
return &m.ConfigMapGenerator[cmIndex], nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.