-
Notifications
You must be signed in to change notification settings - Fork 23
/
configmap.go
91 lines (71 loc) · 2.3 KB
/
configmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package transformers
import (
"fmt"
"regexp"
"sort"
"github.com/ContainerSolutions/helm-convert/pkg/types"
"github.com/golang/glog"
ktypes "sigs.k8s.io/kustomize/pkg/types"
)
type configMapTransformer struct{}
var regexpMultiline = regexp.MustCompile("\n")
var _ Transformer = &configMapTransformer{}
// NewConfigMapTransformer constructs a configMapTransformer.
func NewConfigMapTransformer() Transformer {
return &configMapTransformer{}
}
// Transform retrieve configmap from manifests and store them as configMapGenerator in the kustomization.yaml
func (t *configMapTransformer) Transform(config *ktypes.Kustomization, resources *types.Resources) error {
for id, res := range resources.ResMap {
kind, err := res.GetFieldValue("kind")
if err != nil {
return err
}
if kind != "ConfigMap" {
continue
}
name, err := res.GetFieldValue("metadata.name")
if err != nil {
return err
}
obj := resources.ResMap[id].Map()
if _, found := obj["data"]; !found {
glog.V(8).Infof("Data field from configmap '%s' was not found", name)
continue
}
if obj["data"] == nil {
glog.V(8).Infof("Data field from configmap '%s' is empty", name)
continue
}
data := obj["data"].(map[string]interface{})
configMapArg := ktypes.ConfigMapArgs{
Name: name,
}
var fileSources []string
var literalSources []string
for key, value := range data {
// if multiline, store as external file otherwise literal
// TODO: detect if key/value file, ie: .env, .ini and set DataSources.EnvSource
if v, ok := value.(string); ok {
if regexpMultiline.MatchString(v) {
glog.V(8).Infof("Converting '%s' as external file from configmap '%s'", key, name)
filename := fmt.Sprintf("%s-%s", name, key)
fileSources = append(fileSources, filename)
resources.ConfigFiles[filename] = v
} else {
glog.V(8).Infof("Converting '%s' as literal value from configmap '%s'", key, name)
literalSources = append(literalSources, fmt.Sprintf("%s=\"%s\"", key, value))
}
}
}
sort.Strings(literalSources)
sort.Strings(fileSources)
configMapArg.DataSources = ktypes.DataSources{
LiteralSources: literalSources,
FileSources: fileSources,
}
config.ConfigMapGenerator = append(config.ConfigMapGenerator, configMapArg)
delete(resources.ResMap, res.Id())
}
return nil
}