/
render.go
415 lines (364 loc) · 12.4 KB
/
render.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package template
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"
"github.com/Masterminds/sprig"
ctconfig "github.com/coreos/container-linux-config-transpiler/config"
cttypes "github.com/coreos/container-linux-config-transpiler/config/types"
igntypes "github.com/coreos/ignition/config/v2_2/types"
"github.com/ghodss/yaml"
"github.com/golang/glog"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
"github.com/openshift/machine-config-operator/pkg/controller/common"
"github.com/openshift/machine-config-operator/pkg/version"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// RenderConfig is wrapper around ControllerConfigSpec.
type RenderConfig struct {
*mcfgv1.ControllerConfigSpec
PullSecret string
}
const (
filesDir = "files"
unitsDir = "units"
// TODO: these constants are wrong, they should match what is reported by the infrastructure provider
platformAWS = "aws"
platformAzure = "azure"
platformBaremetal = "baremetal"
platformGCP = "gcp"
platformOpenstack = "openstack"
platformLibvirt = "libvirt"
platformNone = "none"
platformVSphere = "vsphere"
platformBase = "_base"
)
// generateTemplateMachineConfigs returns MachineConfig objects from the templateDir and a config object
// expected directory structure for correctly templating machine configs: <templatedir>/<role>/<name>/<platform>/<type>/<tmpl_file>
//
// All files from platform _base are always included, and may be overridden or
// supplemented by platform-specific templates.
//
// ex:
// templates/worker/00-worker/_base/units/kubelet.conf.tmpl
// /files/hostname.tmpl
// /aws/units/kubelet-dropin.conf.tmpl
// /01-worker-kubelet/_base/files/random.conf.tmpl
// /master/00-master/_base/units/kubelet.tmpl
// /files/hostname.tmpl
//
func generateTemplateMachineConfigs(config *RenderConfig, templateDir string) ([]*mcfgv1.MachineConfig, error) {
infos, err := ioutil.ReadDir(templateDir)
if err != nil {
return nil, fmt.Errorf("failed to read dir %q: %v", templateDir, err)
}
cfgs := []*mcfgv1.MachineConfig{}
for _, info := range infos {
if !info.IsDir() {
glog.Infof("ignoring non-directory path %q", info.Name())
continue
}
role := info.Name()
if role == "common" {
continue
}
roleConfigs, err := GenerateMachineConfigsForRole(config, role, templateDir)
if err != nil {
return nil, fmt.Errorf("failed to create MachineConfig for role %s: %v", role, err)
}
cfgs = append(cfgs, roleConfigs...)
}
// tag all machineconfigs with the controller version
for _, cfg := range cfgs {
if cfg.Annotations == nil {
cfg.Annotations = map[string]string{}
}
cfg.Annotations[common.GeneratedByControllerVersionAnnotationKey] = version.Hash
}
return cfgs, nil
}
// GenerateMachineConfigsForRole creates MachineConfigs for the role provided
func GenerateMachineConfigsForRole(config *RenderConfig, role, templateDir string) ([]*mcfgv1.MachineConfig, error) {
path := filepath.Join(templateDir, role)
infos, err := ioutil.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("failed to read dir %q: %v", path, err)
}
cfgs := []*mcfgv1.MachineConfig{}
for _, info := range infos {
if !info.IsDir() {
glog.Infof("ignoring non-directory path %q", info.Name())
continue
}
name := info.Name()
namePath := filepath.Join(path, name)
nameConfig, err := generateMachineConfigForName(config, role, name, templateDir, namePath)
if err != nil {
return nil, err
}
cfgs = append(cfgs, nameConfig)
}
return cfgs, nil
}
func platformFromControllerConfigSpec(ic *mcfgv1.ControllerConfigSpec) (string, error) {
switch ic.Platform {
case "":
return "", fmt.Errorf("cannot generate MachineConfigs with an empty platform field")
case platformBase:
return "", fmt.Errorf("platform _base unsupported")
case platformAWS, platformAzure, platformBaremetal, platformGCP, platformOpenstack, platformLibvirt, platformNone:
return ic.Platform, nil
default:
glog.Warningf("Warning: the controller config referenced an unsupported platform: %s", ic.Platform)
return platformNone, nil
}
}
func filterTemplates(toFilter map[string]string, path string, config *RenderConfig) error {
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
// empty templates signify don't create
if info.Size() == 0 {
delete(toFilter, info.Name())
return nil
}
filedata, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file %q: %v", path, err)
}
// Render the template file
renderedData, err := renderTemplate(*config, path, filedata)
if err != nil {
return err
}
toFilter[info.Name()] = string(renderedData)
return nil
}
return filepath.Walk(path, walkFn)
}
func generateMachineConfigForName(config *RenderConfig, role, name, templateDir, path string) (*mcfgv1.MachineConfig, error) {
platform, err := platformFromControllerConfigSpec(config.ControllerConfigSpec)
if err != nil {
return nil, err
}
platformDirs := []string{}
// Loop over templates/common which applies everywhere
for _, dir := range []string{platformBase, platform} {
basePath := filepath.Join(templateDir, "common", dir)
exists, err := existsDir(basePath)
if err != nil {
return nil, err
}
if !exists {
continue
}
platformDirs = append(platformDirs, basePath)
}
// And now over the target e.g. templates/master
for _, dir := range []string{platformBase, platform} {
platformPath := filepath.Join(path, dir)
exists, err := existsDir(platformPath)
if err != nil {
return nil, err
}
if !exists {
continue
}
platformDirs = append(platformDirs, platformPath)
}
files := map[string]string{}
units := map[string]string{}
// walk all role dirs, with later ones taking precedence
for _, platformDir := range platformDirs {
p := filepath.Join(platformDir, filesDir)
exists, err := existsDir(p)
if err != nil {
return nil, err
}
if exists {
if err := filterTemplates(files, p, config); err != nil {
return nil, err
}
}
p = filepath.Join(platformDir, unitsDir)
exists, err = existsDir(p)
if err != nil {
return nil, err
}
if exists {
if err := filterTemplates(units, p, config); err != nil {
return nil, err
}
}
}
// keySortVals returns a list of values, sorted by key
// we need the lists of files and units to have a stable ordering for the checksum
keySortVals := func(m map[string]string) []string {
ks := []string{}
for k := range m {
ks = append(ks, k)
}
sort.Strings(ks)
vs := []string{}
for _, k := range ks {
vs = append(vs, m[k])
}
return vs
}
ignCfg, err := transpileToIgn(keySortVals(files), keySortVals(units))
if err != nil {
return nil, fmt.Errorf("error transpiling ct config to Ignition config: %v", err)
}
mcfg := MachineConfigFromIgnConfig(role, name, ignCfg)
// And inject the osimageurl here
mcfg.Spec.OSImageURL = config.OSImageURL
return mcfg, nil
}
const (
machineConfigRoleLabelKey = "machineconfiguration.openshift.io/role"
)
// MachineConfigFromIgnConfig creates a MachineConfig with the provided Ignition config
func MachineConfigFromIgnConfig(role, name string, ignCfg *igntypes.Config) *mcfgv1.MachineConfig {
labels := map[string]string{
machineConfigRoleLabelKey: role,
}
return &mcfgv1.MachineConfig{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Name: name,
},
Spec: mcfgv1.MachineConfigSpec{
OSImageURL: "",
Config: *ignCfg,
},
}
}
func transpileToIgn(files, units []string) (*igntypes.Config, error) {
var ctCfg cttypes.Config
// Convert data to Ignition resources
for _, d := range files {
f := new(cttypes.File)
if err := yaml.Unmarshal([]byte(d), f); err != nil {
return nil, fmt.Errorf("failed to unmarshal file into struct: %v", err)
}
// Add the file to the config
ctCfg.Storage.Files = append(ctCfg.Storage.Files, *f)
}
for _, d := range units {
u := new(cttypes.SystemdUnit)
if err := yaml.Unmarshal([]byte(d), u); err != nil {
return nil, fmt.Errorf("failed to unmarshal systemd unit into struct: %v", err)
}
// Add the unit to the config
ctCfg.Systemd.Units = append(ctCfg.Systemd.Units, *u)
}
ignCfg, rep := ctconfig.Convert(ctCfg, "", nil)
if rep.IsFatal() {
return nil, fmt.Errorf("failed to convert config to Ignition config %s", rep)
}
return &ignCfg, nil
}
// renderTemplate renders a template file with values from a RenderConfig
// returns the rendered file data
func renderTemplate(config RenderConfig, path string, b []byte) ([]byte, error) {
funcs := sprig.TxtFuncMap()
funcs["skip"] = skipMissing
funcs["etcdServerCertDNSNames"] = etcdServerCertDNSNames
funcs["etcdPeerCertDNSNames"] = etcdPeerCertDNSNames
funcs["cloudProvider"] = cloudProvider
funcs["cloudConfigFlag"] = cloudConfigFlag
tmpl, err := template.New(path).Funcs(funcs).Parse(string(b))
if err != nil {
return nil, fmt.Errorf("failed to parse template %s: %v", path, err)
}
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, config); err != nil {
return nil, fmt.Errorf("failed to execute template: %v", err)
}
return buf.Bytes(), nil
}
var skipKeyValidate = regexp.MustCompile(`^[_a-z]\w*$`)
// Keys labelled with skip ie. {{skip "key"}}, don't need to be templated in now because at Ignition request they will be templated in with query params
func skipMissing(key string) (interface{}, error) {
if !skipKeyValidate.Match([]byte(key)) {
return nil, fmt.Errorf("invalid key for skipKey")
}
return fmt.Sprintf("{{.%s}}", key), nil
}
// Process the {{etcdPeerCertDNSNames}} and {{etcdServerCertDNSNames}}
func etcdServerCertDNSNames(cfg RenderConfig) (interface{}, error) {
var dnsNames = []string{
"localhost",
"etcd.kube-system.svc", // sign for the local etcd service name that cluster-network apiservers use to communicate
"etcd.kube-system.svc.cluster.local", // sign for the local etcd service name that cluster-network apiservers use to communicate
"etcd.openshift-etcd.svc", // sign for the local etcd service name that cluster-network apiservers use to communicate
"etcd.openshift-etcd.svc.cluster.local", // sign for the local etcd service name that cluster-network apiservers use to communicate
"${ETCD_WILDCARD_DNS_NAME}",
}
return strings.Join(dnsNames, ","), nil
}
func etcdPeerCertDNSNames(cfg RenderConfig) (interface{}, error) {
if cfg.EtcdDiscoveryDomain == "" {
return nil, fmt.Errorf("invalid configuration")
}
var dnsNames = []string{
"${ETCD_DNS_NAME}",
cfg.EtcdDiscoveryDomain, // https://github.com/etcd-io/etcd/blob/583763261f1c843e07c1bf7fea5fb4cfb684fe87/Documentation/op-guide/clustering.md#dns-discovery
}
return strings.Join(dnsNames, ","), nil
}
func cloudProvider(cfg RenderConfig) (interface{}, error) {
// FIXME Explicitly disable (remove) the cloud provider on OpenStack for now
// Don't forget to turn the test case back on as well
switch cfg.Platform {
case platformAWS, platformAzure, platformGCP, platformVSphere:
return cfg.Platform, nil
default:
return "", nil
}
}
// Process the {{cloudConfigFlag .}}
// If the CloudProviderConfig field is set and not empty, this
// returns the cloud conf flag for kubelet [1] pointing the kubelet to use
// /etc/kubernetes/cloud.conf for configuring the cloud provider for select platforms.
// By default, even if CloudProviderConfig fields is set, the kubelet will be configured to be
// used for select platforms only.
//
// [1]: https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/#options
func cloudConfigFlag(cfg RenderConfig) interface{} {
if cfg.CloudProviderConfig == "" {
return ""
}
flag := "--cloud-config=/etc/kubernetes/cloud.conf"
switch cfg.Platform {
case platformAzure, platformOpenstack:
return flag
default:
return ""
}
}
// existsDir returns true if path exists and is a directory, false if the path
// does not exist, and error if there is a runtime error or the path is not a directory
func existsDir(path string) (bool, error) {
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, fmt.Errorf("failed to open dir %q: %v", path, err)
}
if !info.IsDir() {
return false, fmt.Errorf("expected template directory, %q is not a directory", path)
}
return true, nil
}