Skip to content

Commit

Permalink
Merge pull request #5467 from stormqueen1990/feat/edit-set-secret
Browse files Browse the repository at this point in the history
feat: edit set secret
  • Loading branch information
k8s-ci-robot committed Feb 8, 2024
2 parents cf01ceb + 3bb9a6d commit b154361
Show file tree
Hide file tree
Showing 6 changed files with 591 additions and 132 deletions.
7 changes: 7 additions & 0 deletions kustomize/commands/edit/set/all.go
Expand Up @@ -27,12 +27,19 @@ func NewCmdSet(
# Sets the namesuffix field
kustomize edit set namesuffix <suffix-value>
# Edits a field in an existing configmap in the kustomization file
kustomize edit set configmap my-configmap --from-literal=key1=value1
# Edits a field in an existing secret in the kustomization file
kustomize edit set secret my-secret --from-literal=key1=value1
`,
Args: cobra.MinimumNArgs(1),
}

c.AddCommand(
newCmdSetConfigMap(fSys, ldr, rf),
newCmdSetSecret(fSys, ldr, rf),
newCmdSetNamePrefix(fSys),
newCmdSetNameSuffix(fSys),
newCmdSetNamespace(fSys, v),
Expand Down
27 changes: 18 additions & 9 deletions kustomize/commands/edit/set/setconfigmap.go
@@ -1,11 +1,14 @@
// Copyright 2023 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

//
//nolint:dupl
package set

import (
"fmt"

"sigs.k8s.io/kustomize/api/konfig"

"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"sigs.k8s.io/kustomize/api/ifc"
Expand All @@ -24,16 +27,22 @@ func newCmdSetConfigMap(
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
Short: fmt.Sprintf("Edits the value for an existing key for a ConfigMap in the %s file", konfig.DefaultKustomizationFileName()),
Long: fmt.Sprintf(`Edits the value for an existing key in an existing ConfigMap in the %[1]s file.
ConfigMap name, ConfigMap namespace, and key name must match an existing entry in the %[1]s file for this command to succeed.
When namespace is omitted, the default namespace is used. Conversely, when an entry without a specified namespace exists
in the %[1]s file, it can be updated by either omitting the namespace on the kustomize edit set configmap invocation or by
specifying --namespace=default.`, konfig.DefaultKustomizationFileName()),
Example: fmt.Sprintf(`
# Edits an existing ConfigMap in the %[1]s file, changing value of key1 to 2, and namespace is implicitly defined as "default"
kustomize edit set configmap my-configmap --from-literal=key1=2
# Edits an existing configmap in the kustomization file, changing namespace to 'new-namespace'
# Edits an existing ConfigMap in the %[1]s file, changing value of key1 to 2, and explicitly define namespace as "default"
kustomize edit set configmap my-configmap --from-literal=key1=2 --namespace default
# Edits an existing ConfigMap in the %[1]s file, changing namespace to "new-namespace"
kustomize edit set configmap my-configmap --namespace=current-namespace --new-namespace=new-namespace
`,
`, konfig.DefaultKustomizationFileName()),
RunE: func(_ *cobra.Command, args []string) error {
return runEditSetConfigMap(flags, fSys, args, ldr, rf)
},
Expand Down Expand Up @@ -144,7 +153,7 @@ func findConfigMapArgs(m *types.Kustomization, name, namespace string) (*types.C
})

if cmIndex == -1 {
return nil, fmt.Errorf("unable to find ConfigMap with name '%q'", name)
return nil, fmt.Errorf("unable to find ConfigMap with name %q", name)
}

return &m.ConfigMapGenerator[cmIndex], nil
Expand Down

0 comments on commit b154361

Please sign in to comment.