Skip to content
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

refactor: move reusable bits in preparation for new 'edit set' commands #5409

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 h1:hb9wdF1z5waM+dSIICn1l0DkLVDT3hqhhQsDNUmHPRE=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6 h1:QE6XYQK6naiK1EPAe1g/ILLxN5RBoH5xkJk3CqlMI/Y=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b h1:+qEpEAPhDZ1o0x3tHzZTQDArnOixOzGD9HUJfcg0mb4=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
Expand All @@ -210,6 +211,7 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -221,8 +223,6 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
Expand Down
21 changes: 11 additions & 10 deletions kustomize/commands/edit/add/configmap.go
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
49 changes: 25 additions & 24 deletions kustomize/commands/edit/add/configmap_test.go
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
19 changes: 10 additions & 9 deletions kustomize/commands/edit/add/secret.go
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
Loading