-
Notifications
You must be signed in to change notification settings - Fork 32
/
template.go
240 lines (205 loc) · 5.15 KB
/
template.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2016 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"text/template"
"log"
"os"
)
type TemplateProcessor struct {
configMaps map[string]*ConfigMap
namespace string
secrets map[string]*Secret
templates map[string]*ConfigMap
noop bool
}
func NewTemplateProcessor(namespace string) *TemplateProcessor {
return &TemplateProcessor{
namespace: namespace,
configMaps: make(map[string]*ConfigMap),
secrets: make(map[string]*Secret),
templates: make(map[string]*ConfigMap),
}
}
func (tp *TemplateProcessor) setNoop(noop bool) {
tp.noop = noop
}
func (tp *TemplateProcessor) configmap(name, key string) (string, error) {
// Check if the config map has already been fetched for this
// namespace. If not, retrieve the config map and cache it for
// future use.
_, ok := tp.configMaps[name]
if !ok {
cm, err := getConfigMap(tp.namespace, name)
if err != nil {
return "", err
}
tp.configMaps[name] = cm
}
v, ok := tp.configMaps[name].Data[key]
if !ok {
return "", errors.New("missing key " + key)
}
return v, nil
}
func (tp *TemplateProcessor) secret(name, key string) (string, error) {
_, ok := tp.secrets[name]
if !ok {
s, err := getSecret(tp.namespace, name)
if err != nil {
return "", err
}
tp.secrets[name] = s
}
v, ok := tp.secrets[name].Data[key]
if !ok {
return "", errors.New("missing key " + key)
}
d, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return "", err
}
return string(d), nil
}
func process(namespaces, configmaps []string, noop bool) {
if len(namespaces) == 0 {
ns, err := getNamespaces()
if err != nil {
log.Println(err)
return
}
for _, n := range ns.Items {
namespaces = append(namespaces, n.Metadata.Name)
}
}
for _, n := range namespaces {
tp := NewTemplateProcessor(n)
tp.setNoop(noop)
tp.sync(configmaps)
}
}
func (tp *TemplateProcessor) sync(configmaps []string) {
var cms []*ConfigMap
if len(configmaps) == 0 {
cmList, err := getConfigMaps(tp.namespace)
if err != nil {
log.Println(err)
return
}
for _, c := range cmList.Items {
c := c
cms = append(cms, &c)
}
}
for _, c := range configmaps {
cm, err := getConfigMap(tp.namespace, c)
if err != nil {
log.Println(err)
continue
}
cms = append(cms, cm)
}
for _, c := range cms {
if err := tp.processConfigMapTemplate(c); err != nil {
log.Println(err)
continue
}
}
}
func (tp *TemplateProcessor) processConfigMapTemplate(configmap *ConfigMap) error {
ts, ok := configmap.Data["template"]
if !ok {
return errors.New("missing template key")
}
t := template.New(configmap.Metadata.Name)
t.Funcs(template.FuncMap{
"configmap": tp.configmap,
"secret": tp.secret,
})
t, err := t.Parse(ts)
if err != nil {
return fmt.Errorf("error parsing template: %v", err)
}
var value bytes.Buffer
err = t.Execute(&value, nil)
if err != nil {
return fmt.Errorf("error executing template: %v", err)
}
annotations := configmap.Metadata.Annotations
name := annotations["konfd.io/name"]
key := annotations["konfd.io/key"]
kind := annotations["konfd.io/kind"]
switch kind {
case "configmap":
return tp.processConfigMap(tp.namespace, name, key, value.String())
case "secret":
return tp.processSecret(tp.namespace, name, key, value.String())
}
return nil
}
func (tp *TemplateProcessor) processConfigMap(namespace, name, key, value string) error {
cm := newConfigMap(namespace, name, key, value)
ccm, err := getConfigMap(namespace, name)
if err == ErrNotExist {
if tp.noop {
return printObject(cm)
}
return createConfigMap(cm)
}
if err != nil {
return err
}
if ccm.Data[key] != cm.Data[key] {
log.Printf("%s configmap out of sync; syncing...", name)
ccm.Data[key] = cm.Data[key]
}
if tp.noop {
return printObject(ccm)
}
return updateConfigMap(ccm)
}
func (tp *TemplateProcessor) processSecret(namespace, name, key, value string) error {
s := newSecret(namespace, name, key, value)
currentSecret, err := getSecret(namespace, name)
if err == ErrNotExist {
if tp.noop {
return printObject(s)
}
return createSecret(s)
}
if err != nil {
return err
}
if currentSecret.Data[key] != s.Data[key] {
log.Printf("%s secret out of sync; syncing...", name)
currentSecret.Data[key] = s.Data[key]
}
if tp.noop {
return printObject(currentSecret)
}
return updateSecret(currentSecret)
}
func printObject(v interface{}) error {
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
err := encoder.Encode(&v)
if err != nil {
return fmt.Errorf("error encoding object: %v", err)
}
return nil
}