Skip to content

Commit

Permalink
refactor: move utilities and rename files for add commands
Browse files Browse the repository at this point in the history
* Rename files add/configmap.go and add/secret.go to add/addconfigmap.go and
  add/addsecret.go.
* Move configmapSecretFlagsAndArgs.go to the internal/util folder so it can be
  reused by the set commands.
  • Loading branch information
stormqueen1990 committed Oct 18, 2023
1 parent 1830b00 commit 65866d4
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"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"
)

Expand All @@ -20,7 +21,7 @@ func newCmdAddConfigMap(
ldr ifc.KvLoader,
rf *resource.Factory,
) *cobra.Command {
var flags configmapSecretFlagsAndArgs
var flags util.ConfigMapSecretFlagsAndArgs
cmd := &cobra.Command{
Use: "configmap NAME [--namespace=namespace-name] [--behavior={create|merge|replace}] [--from-file=[key=]source] [--from-literal=key1=value1]",
Short: "Adds a configmap to the kustomization file",
Expand Down Expand Up @@ -48,44 +49,44 @@ func newCmdAddConfigMap(

cmd.Flags().StringSliceVar(
&flags.FileSources,
fromFileFlag,
util.FromFileFlag,
[]string{},
"Key file can be specified using its file path, in which case file basename will be used as configmap "+
"key, or optionally with a key and file path, in which case the given key will be used. Specifying a "+
"directory will iterate each named file in the directory whose basename is a valid configmap key.")
cmd.Flags().StringArrayVar(
&flags.LiteralSources,
fromLiteralFlag,
util.FromLiteralFlag,
[]string{},
"Specify a key and literal value to insert in configmap (i.e. mykey=somevalue)")
cmd.Flags().StringVar(
&flags.EnvFileSource,
fromEnvFileFlag,
util.FromEnvFileFlag,
"",
"Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).")
cmd.Flags().BoolVar(
&flags.DisableNameSuffixHash,
disableNameSuffixHashFlag,
util.DisableNameSuffixHashFlag,
false,
"Disable the name suffix for the configmap")
cmd.Flags().StringVar(
&flags.Behavior,
behaviorFlag,
util.BehaviorFlag,
"",
"Specify the behavior for config map generation, i.e whether to create a new configmap (the default), "+
"to merge with a previously defined one, or to replace an existing one. Merge and replace should be used only "+
" when overriding an existing configmap defined in a base")
cmd.Flags().StringVar(
&flags.Namespace,
namespaceFlag,
util.NamespaceFlag,
"",
"Specify the namespace of the ConfigMap")

return cmd
}

func runEditAddConfigMap(
flags configmapSecretFlagsAndArgs,
flags util.ConfigMapSecretFlagsAndArgs,
fSys filesys.FileSystem,
args []string,
ldr ifc.KvLoader,
Expand Down Expand Up @@ -133,11 +134,11 @@ func runEditAddConfigMap(
func addConfigMap(
ldr ifc.KvLoader,
k *types.Kustomization,
flags configmapSecretFlagsAndArgs,
flags util.ConfigMapSecretFlagsAndArgs,
rf *resource.Factory,
) error {
args := findOrMakeConfigMapArgs(k, flags.Name, flags.Namespace)
mergeFlagsIntoGeneratorArgs(&args.GeneratorArgs, flags)
util.MergeFlagsIntoGeneratorArgs(&args.GeneratorArgs, flags)
// Validate by trying to create corev1.configmap.
args.Options = types.MergeGlobalOptionsIntoLocal(
args.Options, k.GeneratorOptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

Expand Down Expand Up @@ -54,38 +55,38 @@ func TestMakeConfigMapArgs(t *testing.T) {
func TestMergeFlagsIntoConfigMapArgs_LiteralSources(t *testing.T) {
k := &types.Kustomization{}
args := findOrMakeConfigMapArgs(k, "foo", configMapNamespace)
mergeFlagsIntoGeneratorArgs(
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{LiteralSources: []string{"k1=v1"}})
mergeFlagsIntoGeneratorArgs(
util.ConfigMapSecretFlagsAndArgs{LiteralSources: []string{"k1=v1"}})
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{LiteralSources: []string{"k2=v2"}})
util.ConfigMapSecretFlagsAndArgs{LiteralSources: []string{"k2=v2"}})
assert.Equal(t, "k1=v1", k.ConfigMapGenerator[0].LiteralSources[0])
assert.Equal(t, "k2=v2", k.ConfigMapGenerator[0].LiteralSources[1])
}

func TestMergeFlagsIntoConfigMapArgs_FileSources(t *testing.T) {
k := &types.Kustomization{}
args := findOrMakeConfigMapArgs(k, "foo", configMapNamespace)
mergeFlagsIntoGeneratorArgs(
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{FileSources: []string{"file1"}})
mergeFlagsIntoGeneratorArgs(
util.ConfigMapSecretFlagsAndArgs{FileSources: []string{"file1"}})
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{FileSources: []string{"file2"}})
util.ConfigMapSecretFlagsAndArgs{FileSources: []string{"file2"}})
assert.Equal(t, "file1", k.ConfigMapGenerator[0].FileSources[0])
assert.Equal(t, "file2", k.ConfigMapGenerator[0].FileSources[1])
}

func TestMergeFlagsIntoConfigMapArgs_EnvSource(t *testing.T) {
k := &types.Kustomization{}
args := findOrMakeConfigMapArgs(k, "foo", configMapNamespace)
mergeFlagsIntoGeneratorArgs(
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{EnvFileSource: "env1"})
mergeFlagsIntoGeneratorArgs(
util.ConfigMapSecretFlagsAndArgs{EnvFileSource: "env1"})
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{EnvFileSource: "env2"})
util.ConfigMapSecretFlagsAndArgs{EnvFileSource: "env2"})
assert.Equal(t, "env1", k.ConfigMapGenerator[0].EnvSources[0])
assert.Equal(t, "env2", k.ConfigMapGenerator[0].EnvSources[1])
}
Expand All @@ -94,31 +95,31 @@ func TestMergeFlagsIntoConfigMapArgs_Behavior(t *testing.T) {
k := &types.Kustomization{}
args := findOrMakeConfigMapArgs(k, "foo", configMapNamespace)

createBehaviorFlags := configmapSecretFlagsAndArgs{
createBehaviorFlags := util.ConfigMapSecretFlagsAndArgs{
Behavior: "create",
EnvFileSource: "env1",
}
mergeFlagsIntoGeneratorArgs(
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
createBehaviorFlags)
require.Equal(t, configMapNamespace, k.ConfigMapGenerator[0].Namespace)
assert.Equal(t, "create", k.ConfigMapGenerator[0].Behavior)

mergeBehaviorFlags := configmapSecretFlagsAndArgs{
mergeBehaviorFlags := util.ConfigMapSecretFlagsAndArgs{
Behavior: "merge",
EnvFileSource: "env1",
}
mergeFlagsIntoGeneratorArgs(
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
mergeBehaviorFlags)
require.Equal(t, configMapNamespace, k.ConfigMapGenerator[0].Namespace)
assert.Equal(t, "merge", k.ConfigMapGenerator[0].Behavior)

replaceBehaviorFlags := configmapSecretFlagsAndArgs{
replaceBehaviorFlags := util.ConfigMapSecretFlagsAndArgs{
Behavior: "replace",
EnvFileSource: "env1",
}
mergeFlagsIntoGeneratorArgs(
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
replaceBehaviorFlags)
require.Equal(t, configMapNamespace, k.ConfigMapGenerator[0].Namespace)
Expand Down Expand Up @@ -162,8 +163,8 @@ func TestEditAddConfigMapWithLiteralSource(t *testing.T) {

args := []string{
tc.configMapName,
fmt.Sprintf(flagFormat, fromLiteralFlag, tc.literalSource),
fmt.Sprintf(flagFormat, namespaceFlag, tc.configMapNamespace),
fmt.Sprintf(util.FlagFormat, util.FromLiteralFlag, tc.literalSource),
fmt.Sprintf(util.FlagFormat, util.NamespaceFlag, tc.configMapNamespace),
}
cmd := newCmdAddConfigMap(fSys, ldr, pvd.GetResourceFactory())
cmd.SetArgs(args)
Expand Down Expand Up @@ -243,8 +244,8 @@ func TestEditAddConfigMapWithEnvSource(t *testing.T) {

args := []string{
tc.configMapName,
fmt.Sprintf(flagFormat, fromEnvFileFlag, tc.envSource),
fmt.Sprintf(flagFormat, namespaceFlag, tc.configMapNamespace),
fmt.Sprintf(util.FlagFormat, util.FromEnvFileFlag, tc.envSource),
fmt.Sprintf(util.FlagFormat, util.NamespaceFlag, tc.configMapNamespace),
}
cmd := newCmdAddConfigMap(fSys, ldr, pvd.GetResourceFactory())
cmd.SetArgs(args)
Expand Down Expand Up @@ -323,8 +324,8 @@ func TestEditAddConfigMapWithFileSource(t *testing.T) {

args := []string{
tc.configMapName,
fmt.Sprintf(flagFormat, fromFileFlag, tc.fileSource),
fmt.Sprintf(flagFormat, namespaceFlag, tc.configMapNamespace),
fmt.Sprintf(util.FlagFormat, util.FromFileFlag, tc.fileSource),
fmt.Sprintf(util.FlagFormat, util.NamespaceFlag, tc.configMapNamespace),
}
cmd := newCmdAddConfigMap(fSys, ldr, pvd.GetResourceFactory())
cmd.SetArgs(args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"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"
)

Expand All @@ -20,7 +21,7 @@ func newCmdAddSecret(
ldr ifc.KvLoader,
rf *resource.Factory,
) *cobra.Command {
var flags configmapSecretFlagsAndArgs
var flags util.ConfigMapSecretFlagsAndArgs
cmd := &cobra.Command{
Use: "secret NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--type=Opaque|kubernetes.io/tls]",
Short: "Adds a secret to the kustomization file.",
Expand All @@ -42,19 +43,19 @@ func newCmdAddSecret(

cmd.Flags().StringSliceVar(
&flags.FileSources,
fromFileFlag,
util.FromFileFlag,
[]string{},
"Key file can be specified using its file path, in which case file basename will be used as secret "+
"key, or optionally with a key and file path, in which case the given key will be used. Specifying a "+
"directory will iterate each named file in the directory whose basename is a valid secret key.")
cmd.Flags().StringArrayVar(
&flags.LiteralSources,
fromLiteralFlag,
util.FromLiteralFlag,
[]string{},
"Specify a key and literal value to insert in secret (i.e. mykey=somevalue)")
cmd.Flags().StringVar(
&flags.EnvFileSource,
fromEnvFileFlag,
util.FromEnvFileFlag,
"",
"Specify the path to a file to read lines of key=val pairs to create a secret (i.e. a Docker .env file).")
cmd.Flags().StringVar(
Expand All @@ -64,20 +65,20 @@ func newCmdAddSecret(
"Specify the secret type this can be 'Opaque' (default), or 'kubernetes.io/tls'")
cmd.Flags().StringVar(
&flags.Namespace,
namespaceFlag,
util.NamespaceFlag,
"",
"Specify the namespace of the secret")
cmd.Flags().BoolVar(
&flags.DisableNameSuffixHash,
disableNameSuffixHashFlag,
util.DisableNameSuffixHashFlag,
false,
"Disable the name suffix for the secret")

return cmd
}

func runEditAddSecret(
flags configmapSecretFlagsAndArgs,
flags util.ConfigMapSecretFlagsAndArgs,
fSys filesys.FileSystem,
args []string,
ldr ifc.KvLoader,
Expand Down Expand Up @@ -125,11 +126,11 @@ func runEditAddSecret(
func addSecret(
ldr ifc.KvLoader,
k *types.Kustomization,
flags configmapSecretFlagsAndArgs,
flags util.ConfigMapSecretFlagsAndArgs,
rf *resource.Factory,
) error {
args := findOrMakeSecretArgs(k, flags.Name, flags.Namespace, flags.Type)
mergeFlagsIntoGeneratorArgs(&args.GeneratorArgs, flags)
util.MergeFlagsIntoGeneratorArgs(&args.GeneratorArgs, flags)
// Validate by trying to create corev1.secret.
args.Options = types.MergeGlobalOptionsIntoLocal(
args.Options, k.GeneratorOptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

Expand Down Expand Up @@ -51,48 +52,48 @@ func TestMakeSecretArgs(t *testing.T) {
func TestMergeFlagsIntoSecretArgs_LiteralSources(t *testing.T) {
k := &types.Kustomization{}
args := findOrMakeSecretArgs(k, "foo", "bar", "forbidden")
mergeFlagsIntoGeneratorArgs(
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{LiteralSources: []string{"k1=v1"}})
mergeFlagsIntoGeneratorArgs(
util.ConfigMapSecretFlagsAndArgs{LiteralSources: []string{"k1=v1"}})
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{LiteralSources: []string{"k2=v2"}})
util.ConfigMapSecretFlagsAndArgs{LiteralSources: []string{"k2=v2"}})
assert.Equal(t, "k1=v1", k.SecretGenerator[0].LiteralSources[0])
assert.Equal(t, "k2=v2", k.SecretGenerator[0].LiteralSources[1])
}

func TestMergeFlagsIntoSecretArgs_FileSources(t *testing.T) {
k := &types.Kustomization{}
args := findOrMakeSecretArgs(k, "foo", "bar", "forbidden")
mergeFlagsIntoGeneratorArgs(
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{FileSources: []string{"file1"}})
mergeFlagsIntoGeneratorArgs(
util.ConfigMapSecretFlagsAndArgs{FileSources: []string{"file1"}})
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{FileSources: []string{"file2"}})
util.ConfigMapSecretFlagsAndArgs{FileSources: []string{"file2"}})
assert.Equal(t, "file1", k.SecretGenerator[0].FileSources[0])
assert.Equal(t, "file2", k.SecretGenerator[0].FileSources[1])
}

func TestMergeFlagsIntoSecretArgs_EnvSource(t *testing.T) {
k := &types.Kustomization{}
args := findOrMakeSecretArgs(k, "foo", "bar", "forbidden")
mergeFlagsIntoGeneratorArgs(
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{EnvFileSource: "env1"})
mergeFlagsIntoGeneratorArgs(
util.ConfigMapSecretFlagsAndArgs{EnvFileSource: "env1"})
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{EnvFileSource: "env2"})
util.ConfigMapSecretFlagsAndArgs{EnvFileSource: "env2"})
assert.Equal(t, "env1", k.SecretGenerator[0].EnvSources[0])
assert.Equal(t, "env2", k.SecretGenerator[0].EnvSources[1])
}

func TestMergeFlagsIntoSecretArgs_DisableNameSuffixHash(t *testing.T) {
k := &types.Kustomization{}
args := findOrMakeSecretArgs(k, "foo", "bar", "forbidden")
mergeFlagsIntoGeneratorArgs(
util.MergeFlagsIntoGeneratorArgs(
&args.GeneratorArgs,
configmapSecretFlagsAndArgs{DisableNameSuffixHash: true})
util.ConfigMapSecretFlagsAndArgs{DisableNameSuffixHash: true})
assert.True(t, k.SecretGenerator[0].Options.DisableNameSuffixHash)
}

Expand All @@ -112,7 +113,7 @@ func TestEditAddSecretWithLiteralSource(t *testing.T) {

args := []string{
secretName,
fmt.Sprintf(flagFormat, fromLiteralFlag, literalSource),
fmt.Sprintf(util.FlagFormat, util.FromLiteralFlag, literalSource),
}
cmd := newCmdAddSecret(fSys, ldr, pvd.GetResourceFactory())
cmd.SetArgs(args)
Expand Down Expand Up @@ -162,7 +163,7 @@ func TestEditAddSecretWithEnvSource(t *testing.T) {

args := []string{
secretName,
fmt.Sprintf(flagFormat, fromEnvFileFlag, envSource),
fmt.Sprintf(util.FlagFormat, util.FromEnvFileFlag, envSource),
}
cmd := newCmdAddSecret(fSys, ldr, pvd.GetResourceFactory())
cmd.SetArgs(args)
Expand Down Expand Up @@ -212,7 +213,7 @@ func TestEditAddSecretWithFileSource(t *testing.T) {

args := []string{
secretName,
fmt.Sprintf(flagFormat, fromFileFlag, fileSource),
fmt.Sprintf(util.FlagFormat, util.FromFileFlag, fileSource),
}
cmd := newCmdAddSecret(fSys, ldr, pvd.GetResourceFactory())
cmd.SetArgs(args)
Expand Down

0 comments on commit 65866d4

Please sign in to comment.