Skip to content

Commit f14988f

Browse files
committed
configMap factory refactor for #86
1 parent 2d0d09e commit f14988f

File tree

11 files changed

+252
-512
lines changed

11 files changed

+252
-512
lines changed

pkg/commands/data_config.go renamed to pkg/commands/cmapflagsandargs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"fmt"
2121
)
2222

23-
// dataConfig encapsulates the options for add configmap/Secret commands.
24-
type dataConfig struct {
23+
// cMapFlagsAndArgs encapsulates the options for add configmap commands.
24+
type cMapFlagsAndArgs struct {
2525
// Name of configMap/Secret (required)
2626
Name string
2727
// FileSources to derive the configMap/Secret from (optional)
@@ -34,7 +34,7 @@ type dataConfig struct {
3434
}
3535

3636
// Validate validates required fields are set to support structured generation.
37-
func (a *dataConfig) Validate(args []string) error {
37+
func (a *cMapFlagsAndArgs) Validate(args []string) error {
3838
if len(args) != 1 {
3939
return fmt.Errorf("name must be specified once")
4040
}

pkg/commands/data_config_test.go renamed to pkg/commands/cmapflagsandargs_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ import (
2121
)
2222

2323
func TestDataConfigValidation_NoName(t *testing.T) {
24-
config := dataConfig{}
24+
config := cMapFlagsAndArgs{}
2525

2626
if config.Validate([]string{}) == nil {
2727
t.Fatal("Validation should fail if no name is specified")
2828
}
2929
}
3030

3131
func TestDataConfigValidation_MoreThanOneName(t *testing.T) {
32-
config := dataConfig{}
32+
config := cMapFlagsAndArgs{}
3333

3434
if config.Validate([]string{"name", "othername"}) == nil {
3535
t.Fatal("Validation should fail if more than one name is specified")
@@ -39,33 +39,33 @@ func TestDataConfigValidation_MoreThanOneName(t *testing.T) {
3939
func TestDataConfigValidation_Flags(t *testing.T) {
4040
tests := []struct {
4141
name string
42-
config dataConfig
42+
config cMapFlagsAndArgs
4343
shouldFail bool
4444
}{
4545
{
4646
name: "env-file-source and literal are both set",
47-
config: dataConfig{
47+
config: cMapFlagsAndArgs{
4848
LiteralSources: []string{"one", "two"},
4949
EnvFileSource: "three",
5050
},
5151
shouldFail: true,
5252
},
5353
{
5454
name: "env-file-source and from-file are both set",
55-
config: dataConfig{
55+
config: cMapFlagsAndArgs{
5656
FileSources: []string{"one", "two"},
5757
EnvFileSource: "three",
5858
},
5959
shouldFail: true,
6060
},
6161
{
6262
name: "we don't have any option set",
63-
config: dataConfig{},
63+
config: cMapFlagsAndArgs{},
6464
shouldFail: true,
6565
},
6666
{
6767
name: "we have from-file and literal ",
68-
config: dataConfig{
68+
config: cMapFlagsAndArgs{
6969
LiteralSources: []string{"one", "two"},
7070
FileSources: []string{"three", "four"},
7171
},

pkg/commands/configmap.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
"github.com/kubernetes-sigs/kustomize/pkg/types"
2828
)
2929

30-
func newCmdAddConfigMap(fsys fs.FileSystem) *cobra.Command {
31-
var config dataConfig
30+
func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
31+
var flagsAndArgs cMapFlagsAndArgs
3232
cmd := &cobra.Command{
3333
Use: "configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1]",
3434
Short: "Adds a configmap to the kustomization file.",
@@ -44,75 +44,77 @@ func newCmdAddConfigMap(fsys fs.FileSystem) *cobra.Command {
4444
kustomize edit add configmap my-configmap --from-env-file=env/path.env
4545
`,
4646
RunE: func(_ *cobra.Command, args []string) error {
47-
err := config.Validate(args)
47+
err := flagsAndArgs.Validate(args)
4848
if err != nil {
4949
return err
5050
}
5151

52-
// Load in the kustomization file.
53-
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
52+
// Load the kustomization file.
53+
mf, err := newKustomizationFile(constants.KustomizationFileName, fSys)
5454
if err != nil {
5555
return err
5656
}
5757

58-
m, err := mf.read()
58+
kustomization, err := mf.read()
5959
if err != nil {
6060
return err
6161
}
6262

63-
// Add the config map to the kustomization file.
64-
err = addConfigMap(m, config)
63+
// Add the flagsAndArgs map to the kustomization file.
64+
err = addConfigMap(kustomization, flagsAndArgs, fSys)
6565
if err != nil {
6666
return err
6767
}
6868

6969
// Write out the kustomization file with added configmap.
70-
return mf.write(m)
70+
return mf.write(kustomization)
7171
},
7272
}
7373

7474
cmd.Flags().StringSliceVar(
75-
&config.FileSources,
75+
&flagsAndArgs.FileSources,
7676
"from-file",
7777
[]string{},
7878
"Key file can be specified using its file path, in which case file basename will be used as configmap "+
7979
"key, or optionally with a key and file path, in which case the given key will be used. Specifying a "+
8080
"directory will iterate each named file in the directory whose basename is a valid configmap key.")
8181
cmd.Flags().StringArrayVar(
82-
&config.LiteralSources,
82+
&flagsAndArgs.LiteralSources,
8383
"from-literal",
8484
[]string{},
8585
"Specify a key and literal value to insert in configmap (i.e. mykey=somevalue)")
8686
cmd.Flags().StringVar(
87-
&config.EnvFileSource,
87+
&flagsAndArgs.EnvFileSource,
8888
"from-env-file",
8989
"",
9090
"Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).")
9191

9292
return cmd
9393
}
9494

95-
// addConfigMap updates a configmap within a kustomization file, using the data in config.
95+
// addConfigMap adds a configmap to a kustomization file.
9696
// Note: error may leave kustomization file in an undefined state. Suggest passing a copy
9797
// of kustomization file.
98-
func addConfigMap(m *types.Kustomization, config dataConfig) error {
99-
cm := getOrCreateConfigMap(m, config.Name)
98+
func addConfigMap(k *types.Kustomization, flagsAndArgs cMapFlagsAndArgs, fSys fs.FileSystem) error {
99+
cmArgs := makeConfigMapArgs(k, flagsAndArgs.Name)
100100

101-
err := mergeData(&cm.DataSources, config)
101+
err := mergeFlagsIntoCmArgs(&cmArgs.DataSources, flagsAndArgs)
102102
if err != nil {
103103
return err
104104
}
105105

106+
factory := configmapandsecret.NewConfigMapFactory(cmArgs, fSys)
107+
106108
// Validate by trying to create corev1.configmap.
107-
_, _, err = configmapandsecret.MakeConfigmapAndGenerateName(*cm)
109+
_, _, err = factory.MakeUnstructAndGenerateName()
108110
if err != nil {
109111
return err
110112
}
111113

112114
return nil
113115
}
114116

115-
func getOrCreateConfigMap(m *types.Kustomization, name string) *types.ConfigMapArgs {
117+
func makeConfigMapArgs(m *types.Kustomization, name string) *types.ConfigMapArgs {
116118
for i, v := range m.ConfigMapGenerator {
117119
if name == v.Name {
118120
return &m.ConfigMapGenerator[i]
@@ -124,13 +126,12 @@ func getOrCreateConfigMap(m *types.Kustomization, name string) *types.ConfigMapA
124126
return &m.ConfigMapGenerator[len(m.ConfigMapGenerator)-1]
125127
}
126128

127-
func mergeData(src *types.DataSources, config dataConfig) error {
128-
src.LiteralSources = append(src.LiteralSources, config.LiteralSources...)
129-
src.FileSources = append(src.FileSources, config.FileSources...)
130-
if src.EnvSource != "" && src.EnvSource != config.EnvFileSource {
129+
func mergeFlagsIntoCmArgs(src *types.DataSources, flags cMapFlagsAndArgs) error {
130+
src.LiteralSources = append(src.LiteralSources, flags.LiteralSources...)
131+
src.FileSources = append(src.FileSources, flags.FileSources...)
132+
if src.EnvSource != "" && src.EnvSource != flags.EnvFileSource {
131133
return fmt.Errorf("updating existing env source '%s' not allowed", src.EnvSource)
132134
}
133-
src.EnvSource = config.EnvFileSource
134-
135+
src.EnvSource = flags.EnvFileSource
135136
return nil
136137
}

pkg/commands/configmap_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestNewAddConfigMapIsNotNil(t *testing.T) {
2929
}
3030
}
3131

32-
func TestGetOrCreateConfigMap(t *testing.T) {
32+
func TestMakeConfigMapArgs(t *testing.T) {
3333
cmName := "test-config-name"
3434

3535
kustomization := &types.Kustomization{
@@ -39,35 +39,35 @@ func TestGetOrCreateConfigMap(t *testing.T) {
3939
if len(kustomization.ConfigMapGenerator) != 0 {
4040
t.Fatal("Initial kustomization should not have any configmaps")
4141
}
42-
cm := getOrCreateConfigMap(kustomization, cmName)
42+
args := makeConfigMapArgs(kustomization, cmName)
4343

44-
if cm == nil {
45-
t.Fatalf("ConfigMap should always be non-nil")
44+
if args == nil {
45+
t.Fatalf("args should always be non-nil")
4646
}
4747

4848
if len(kustomization.ConfigMapGenerator) != 1 {
4949
t.Fatalf("Kustomization should have newly created configmap")
5050
}
5151

52-
if &kustomization.ConfigMapGenerator[len(kustomization.ConfigMapGenerator)-1] != cm {
53-
t.Fatalf("Pointer address for newly inserted configmap should be same")
52+
if &kustomization.ConfigMapGenerator[len(kustomization.ConfigMapGenerator)-1] != args {
53+
t.Fatalf("Pointer address for newly inserted configmap generator should be same")
5454
}
5555

56-
existingCM := getOrCreateConfigMap(kustomization, cmName)
56+
args2 := makeConfigMapArgs(kustomization, cmName)
5757

58-
if existingCM != cm {
59-
t.Fatalf("should have returned an existing cm with name: %v", cmName)
58+
if args2 != args {
59+
t.Fatalf("should have returned an existing args with name: %v", cmName)
6060
}
6161

6262
if len(kustomization.ConfigMapGenerator) != 1 {
6363
t.Fatalf("Should not insert configmap for an existing name: %v", cmName)
6464
}
6565
}
6666

67-
func TestMergeData_LiteralSources(t *testing.T) {
67+
func TestMergeFlagsIntoCmArgs_LiteralSources(t *testing.T) {
6868
ds := &types.DataSources{}
6969

70-
err := mergeData(ds, dataConfig{LiteralSources: []string{"k1=v1"}})
70+
err := mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{LiteralSources: []string{"k1=v1"}})
7171
if err != nil {
7272
t.Fatalf("Merge initial literal source should not return error")
7373
}
@@ -76,7 +76,7 @@ func TestMergeData_LiteralSources(t *testing.T) {
7676
t.Fatalf("Initial literal source should have been added")
7777
}
7878

79-
err = mergeData(ds, dataConfig{LiteralSources: []string{"k2=v2"}})
79+
err = mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{LiteralSources: []string{"k2=v2"}})
8080
if err != nil {
8181
t.Fatalf("Merge second literal source should not return error")
8282
}
@@ -86,10 +86,10 @@ func TestMergeData_LiteralSources(t *testing.T) {
8686
}
8787
}
8888

89-
func TestMergeData_FileSources(t *testing.T) {
89+
func TestMergeFlagsIntoCmArgs_FileSources(t *testing.T) {
9090
ds := &types.DataSources{}
9191

92-
err := mergeData(ds, dataConfig{FileSources: []string{"file1"}})
92+
err := mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{FileSources: []string{"file1"}})
9393
if err != nil {
9494
t.Fatalf("Merge initial file source should not return error")
9595
}
@@ -98,7 +98,7 @@ func TestMergeData_FileSources(t *testing.T) {
9898
t.Fatalf("Initial file source should have been added")
9999
}
100100

101-
err = mergeData(ds, dataConfig{FileSources: []string{"file2"}})
101+
err = mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{FileSources: []string{"file2"}})
102102
if err != nil {
103103
t.Fatalf("Merge second file source should not return error")
104104
}
@@ -108,12 +108,12 @@ func TestMergeData_FileSources(t *testing.T) {
108108
}
109109
}
110110

111-
func TestMergeData_EnvSource(t *testing.T) {
111+
func TestMergeFlagsIntoCmArgs_EnvSource(t *testing.T) {
112112
envFileName := "env1"
113113
envFileName2 := "env2"
114114
ds := &types.DataSources{}
115115

116-
err := mergeData(ds, dataConfig{EnvFileSource: envFileName})
116+
err := mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{EnvFileSource: envFileName})
117117
if err != nil {
118118
t.Fatalf("Merge initial env source should not return error")
119119
}
@@ -122,7 +122,7 @@ func TestMergeData_EnvSource(t *testing.T) {
122122
t.Fatalf("Initial env source filename should have been added")
123123
}
124124

125-
err = mergeData(ds, dataConfig{EnvFileSource: envFileName2})
125+
err = mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{EnvFileSource: envFileName2})
126126
if err == nil {
127127
t.Fatalf("Updating env source should return an error")
128128
}

pkg/configmapandsecret/configmap_secret.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)