Skip to content

Commit 4569a09

Browse files
authored
Merge pull request #200 from monopole/deleteDuplicativeCode
Delete duplicative code.
2 parents 77e1872 + 25d3ad7 commit 4569a09

File tree

6 files changed

+20
-223
lines changed

6 files changed

+20
-223
lines changed

pkg/commands/configmap.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/kubernetes-sigs/kustomize/pkg/configmapandsecret"
2525
"github.com/kubernetes-sigs/kustomize/pkg/constants"
2626
"github.com/kubernetes-sigs/kustomize/pkg/fs"
27+
"github.com/kubernetes-sigs/kustomize/pkg/loader"
2728
"github.com/kubernetes-sigs/kustomize/pkg/types"
2829
)
2930

@@ -59,10 +60,12 @@ func newCmdAddConfigMap(fSys fs.FileSystem) *cobra.Command {
5960
if err != nil {
6061
return err
6162
}
63+
6264
// Add the flagsAndArgs map to the kustomization file.
6365
err = addConfigMap(
6466
kustomization, flagsAndArgs,
65-
configmapandsecret.NewConfigMapFactory(fSys, nil))
67+
configmapandsecret.NewConfigMapFactory(
68+
fSys, loader.NewLoader(loader.NewFileLoader(fSys))))
6669
if err != nil {
6770
return err
6871
}

pkg/configmapandsecret/addfromenvfile.go

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

pkg/configmapandsecret/configmapfactory.go

Lines changed: 12 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package configmapandsecret
2020
import (
2121
"encoding/json"
2222
"fmt"
23-
"io/ioutil"
2423
"path"
2524
"strings"
2625

@@ -51,7 +50,7 @@ func NewConfigMapFactory(
5150
// MakeUnstructAndGenerateName returns an configmap and the name appended with a hash.
5251
func (f *ConfigMapFactory) MakeUnstructAndGenerateName(
5352
args *types.ConfigMapArgs) (*unstructured.Unstructured, string, error) {
54-
cm, err := f.MakeConfigMap1(args)
53+
cm, err := f.MakeConfigMap(args)
5554
if err != nil {
5655
return nil, "", err
5756
}
@@ -84,31 +83,8 @@ func (f *ConfigMapFactory) makeFreshConfigMap(
8483
return cm
8584
}
8685

87-
// MakeConfigMap1 returns a new ConfigMap, or nil and an error.
88-
func (f *ConfigMapFactory) MakeConfigMap1(
89-
args *types.ConfigMapArgs) (*corev1.ConfigMap, error) {
90-
cm := f.makeFreshConfigMap(args)
91-
if args.EnvSource != "" {
92-
if err := f.handleConfigMapFromEnvFileSource(cm, args); err != nil {
93-
return nil, err
94-
}
95-
}
96-
if args.FileSources != nil {
97-
if err := f.handleConfigMapFromFileSources(cm, args); err != nil {
98-
return nil, err
99-
}
100-
}
101-
if args.LiteralSources != nil {
102-
if err := f.handleConfigMapFromLiteralSources(cm, args.LiteralSources); err != nil {
103-
return nil, err
104-
}
105-
}
106-
return cm, nil
107-
}
108-
109-
// MakeConfigMap2 returns a new ConfigMap, or nil and an error.
110-
// TODO: Get rid of the nearly duplicated code in MakeConfigMap1 vs MakeConfigMap2
111-
func (f *ConfigMapFactory) MakeConfigMap2(
86+
// MakeConfigMap returns a new ConfigMap, or nil and an error.
87+
func (f *ConfigMapFactory) MakeConfigMap(
11288
args *types.ConfigMapArgs) (*corev1.ConfigMap, error) {
11389
var all []kvPair
11490
var err error
@@ -137,7 +113,7 @@ func (f *ConfigMapFactory) MakeConfigMap2(
137113
all = append(all, pairs...)
138114

139115
for _, kv := range all {
140-
err = AddKv(cm, kv.key, kv.value)
116+
err = addKvToConfigMap(cm, kv.key, kv.value)
141117
if err != nil {
142118
return nil, err
143119
}
@@ -148,7 +124,7 @@ func (f *ConfigMapFactory) MakeConfigMap2(
148124
func keyValuesFromLiteralSources(sources []string) ([]kvPair, error) {
149125
var kvs []kvPair
150126
for _, s := range sources {
151-
k, v, err := ParseLiteralSource(s)
127+
k, v, err := parseLiteralSource(s)
152128
if err != nil {
153129
return nil, err
154130
}
@@ -157,27 +133,10 @@ func keyValuesFromLiteralSources(sources []string) ([]kvPair, error) {
157133
return kvs, nil
158134
}
159135

160-
// handleConfigMapFromLiteralSources adds the specified literal source
161-
// information into the provided configMap.
162-
func (f *ConfigMapFactory) handleConfigMapFromLiteralSources(
163-
configMap *v1.ConfigMap, sources []string) error {
164-
for _, s := range sources {
165-
k, v, err := ParseLiteralSource(s)
166-
if err != nil {
167-
return err
168-
}
169-
err = AddKv(configMap, k, v)
170-
if err != nil {
171-
return err
172-
}
173-
}
174-
return nil
175-
}
176-
177136
func keyValuesFromFileSources(ldr loader.Loader, sources []string) ([]kvPair, error) {
178137
var kvs []kvPair
179138
for _, s := range sources {
180-
k, fPath, err := ParseFileSource(s)
139+
k, fPath, err := parseFileSource(s)
181140
if err != nil {
182141
return nil, err
183142
}
@@ -190,45 +149,6 @@ func keyValuesFromFileSources(ldr loader.Loader, sources []string) ([]kvPair, er
190149
return kvs, nil
191150
}
192151

193-
// handleConfigMapFromFileSources adds the specified file source information
194-
// into the provided configMap
195-
func (f *ConfigMapFactory) handleConfigMapFromFileSources(
196-
configMap *v1.ConfigMap, args *types.ConfigMapArgs) error {
197-
for _, fileSource := range args.FileSources {
198-
keyName, filePath, err := ParseFileSource(fileSource)
199-
if err != nil {
200-
return err
201-
}
202-
if !f.fSys.Exists(filePath) {
203-
return fmt.Errorf("unable to read configmap source file %s", filePath)
204-
}
205-
if f.fSys.IsDir(filePath) {
206-
if strings.Contains(fileSource, "=") {
207-
return fmt.Errorf("cannot give a key name for a directory path")
208-
}
209-
fileList, err := ioutil.ReadDir(filePath)
210-
if err != nil {
211-
return fmt.Errorf("error listing files in %s: %v", filePath, err)
212-
}
213-
for _, item := range fileList {
214-
itemPath := path.Join(filePath, item.Name())
215-
if item.Mode().IsRegular() {
216-
keyName = item.Name()
217-
err = addKeyFromFileToConfigMap(configMap, keyName, itemPath)
218-
if err != nil {
219-
return err
220-
}
221-
}
222-
}
223-
} else {
224-
if err := addKeyFromFileToConfigMap(configMap, keyName, filePath); err != nil {
225-
return err
226-
}
227-
}
228-
}
229-
return nil
230-
}
231-
232152
func keyValuesFromEnvFile(l loader.Loader, path string) ([]kvPair, error) {
233153
if path == "" {
234154
return nil, nil
@@ -240,34 +160,9 @@ func keyValuesFromEnvFile(l loader.Loader, path string) ([]kvPair, error) {
240160
return keyValuesFromLines(content)
241161
}
242162

243-
// HandleConfigMapFromEnvFileSource adds the specified env file source information
244-
// into the provided configMap
245-
func (f *ConfigMapFactory) handleConfigMapFromEnvFileSource(
246-
configMap *v1.ConfigMap, args *types.ConfigMapArgs) error {
247-
if !f.fSys.Exists(args.EnvSource) {
248-
return fmt.Errorf("unable to read configmap env file %s", args.EnvSource)
249-
}
250-
if f.fSys.IsDir(args.EnvSource) {
251-
return fmt.Errorf("env config file %s cannot be a directory", args.EnvSource)
252-
}
253-
return addFromEnvFile(args.EnvSource, func(key, value string) error {
254-
return AddKv(configMap, key, value)
255-
})
256-
}
257-
258-
// addKeyFromFileToConfigMap adds a key with the given name to a ConfigMap, populating
259-
// the value with the content of the given file path, or returns an error.
260-
func addKeyFromFileToConfigMap(configMap *v1.ConfigMap, keyName, filePath string) error {
261-
data, err := ioutil.ReadFile(filePath)
262-
if err != nil {
263-
return err
264-
}
265-
return AddKv(configMap, keyName, string(data))
266-
}
267-
268-
// AddKv adds the given key and data to the given config map.
163+
// addKvToConfigMap adds the given key and data to the given config map.
269164
// Error if key invalid, or already exists.
270-
func AddKv(configMap *v1.ConfigMap, keyName, data string) error {
165+
func addKvToConfigMap(configMap *v1.ConfigMap, keyName, data string) error {
271166
// Note, the rules for ConfigMap keys are the exact same as the ones for SecretKeys.
272167
if errs := validation.IsConfigMapKey(keyName); len(errs) != 0 {
273168
return fmt.Errorf("%q is not a valid key name for a ConfigMap: %s", keyName, strings.Join(errs, ";"))
@@ -279,15 +174,15 @@ func AddKv(configMap *v1.ConfigMap, keyName, data string) error {
279174
return nil
280175
}
281176

282-
// ParseFileSource parses the source given.
177+
// parseFileSource parses the source given.
283178
//
284179
// Acceptable formats include:
285180
// 1. source-path: the basename will become the key name
286181
// 2. source-name=source-path: the source-name will become the key name and
287182
// source-path is the path to the key file.
288183
//
289184
// Key names cannot include '='.
290-
func ParseFileSource(source string) (keyName, filePath string, err error) {
185+
func parseFileSource(source string) (keyName, filePath string, err error) {
291186
numSeparators := strings.Count(source, "=")
292187
switch {
293188
case numSeparators == 0:
@@ -304,10 +199,10 @@ func ParseFileSource(source string) (keyName, filePath string, err error) {
304199
}
305200
}
306201

307-
// ParseLiteralSource parses the source key=val pair into its component pieces.
202+
// parseLiteralSource parses the source key=val pair into its component pieces.
308203
// This functionality is distinguished from strings.SplitN(source, "=", 2) since
309204
// it returns an error in the case of empty keys, values, or a missing equals sign.
310-
func ParseLiteralSource(source string) (keyName, value string, err error) {
205+
func parseLiteralSource(source string) (keyName, value string, err error) {
311206
// leading equal is invalid
312207
if strings.Index(source, "=") == 0 {
313208
return "", "", fmt.Errorf("invalid literal source %v, expected key=value", source)

pkg/configmapandsecret/configmapfactory_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestConstructConfigMap(t *testing.T) {
139139
f := NewConfigMapFactory(fSys,
140140
loader.NewLoader(loader.NewFileLoader(fSys)))
141141
for _, tc := range testCases {
142-
cm, err := f.MakeConfigMap1(&tc.input)
142+
cm, err := f.MakeConfigMap(&tc.input)
143143
if err != nil {
144144
t.Fatalf("unexpected error: %v", err)
145145
}

pkg/configmapandsecret/kv.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type kvPair struct {
3434
value string
3535
}
3636

37+
var utf8bom = []byte{0xEF, 0xBB, 0xBF}
38+
3739
// keyValuesFromLines parses given content in to a list of key-value pairs.
3840
func keyValuesFromLines(content []byte) ([]kvPair, error) {
3941
var kvs []kvPair

pkg/resmap/configmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewResMapFromConfigMapArgs(
3232
if cmArgs.Behavior == "" {
3333
cmArgs.Behavior = "create"
3434
}
35-
cm, err := f.MakeConfigMap2(&cmArgs)
35+
cm, err := f.MakeConfigMap(&cmArgs)
3636
if err != nil {
3737
return nil, err
3838
}

0 commit comments

Comments
 (0)