local-server/cmd/dkl-dir2config/render-context.go

268 lines
6.0 KiB
Go
Raw Normal View History

2018-12-10 10:59:24 +00:00
package main
import (
"bytes"
"fmt"
"log"
2019-10-09 05:58:28 +00:00
"path"
2018-12-10 10:59:24 +00:00
yaml "gopkg.in/yaml.v2"
2018-12-10 21:23:20 +00:00
"novit.nc/direktil/local-server/pkg/clustersconfig"
2019-10-09 05:58:28 +00:00
"novit.nc/direktil/pkg/config"
2018-12-10 10:59:24 +00:00
)
type renderContext struct {
Host *clustersconfig.Host
Group *clustersconfig.Group
Cluster *clustersconfig.Cluster
Vars map[string]interface{}
ConfigTemplate *clustersconfig.Template
StaticPodsTemplate *clustersconfig.Template
clusterConfig *clustersconfig.Config
}
func newRenderContext(host *clustersconfig.Host, cfg *clustersconfig.Config) (ctx *renderContext, err error) {
cluster := cfg.Cluster(host.Cluster)
if cluster == nil {
err = fmt.Errorf("no cluster named %q", host.Cluster)
return
}
group := cfg.Group(host.Group)
if group == nil {
err = fmt.Errorf("no group named %q", host.Group)
return
}
vars := make(map[string]interface{})
for _, oVars := range []map[string]interface{}{
cluster.Vars,
group.Vars,
host.Vars,
} {
2019-04-19 16:07:22 +00:00
mapMerge(vars, oVars)
2018-12-10 10:59:24 +00:00
}
2019-04-19 16:07:22 +00:00
log.Print("vars: ", vars)
2018-12-10 10:59:24 +00:00
return &renderContext{
Host: host,
Group: group,
Cluster: cluster,
Vars: vars,
ConfigTemplate: cfg.ConfigTemplate(group.Config),
StaticPodsTemplate: cfg.StaticPodsTemplate(group.StaticPods),
clusterConfig: cfg,
}, nil
}
2019-04-19 16:07:22 +00:00
func mapMerge(target, source map[string]interface{}) {
for k, v := range source {
if tMap, targetIsMap := target[k].(map[string]interface{}); targetIsMap {
if sMap, sourceIsMap := v.(map[string]interface{}); sourceIsMap {
mapMerge(tMap, sMap)
continue
}
}
target[k] = v
}
}
2018-12-10 13:44:05 +00:00
func (ctx *renderContext) Config() string {
2018-12-10 10:59:24 +00:00
if ctx.ConfigTemplate == nil {
2018-12-10 13:44:05 +00:00
log.Fatalf("no such config: %q", ctx.Group.Config)
2018-12-10 10:59:24 +00:00
}
ctxMap := ctx.asMap()
2018-12-10 13:44:05 +00:00
templateFuncs := ctx.templateFuncs(ctxMap)
2018-12-10 10:59:24 +00:00
render := func(what string, t *clustersconfig.Template) (s string, err error) {
buf := &bytes.Buffer{}
err = t.Execute(buf, ctxMap, templateFuncs)
if err != nil {
log.Printf("host %s: failed to render %s [%q]: %v", ctx.Host.Name, what, t.Name, err)
return
}
s = buf.String()
return
}
2018-12-10 13:44:05 +00:00
extraFuncs := ctx.templateFuncs(ctxMap)
2018-12-10 10:59:24 +00:00
2019-02-28 08:27:09 +00:00
extraFuncs["static_pods"] = func() (string, error) {
name := ctx.Group.StaticPods
if len(name) == 0 {
return "", fmt.Errorf("group %q has no static pods defined", ctx.Group.Name)
}
2018-12-10 10:59:24 +00:00
t := ctx.clusterConfig.StaticPodsTemplate(name)
if t == nil {
return "", fmt.Errorf("no static pods template named %q", name)
}
return render("static pods", t)
}
2019-10-09 05:58:28 +00:00
extraFuncs["bootstrap_pods_files"] = func(dir string) (string, error) {
namePods := renderBootstrapPods(ctx.Cluster)
defs := make([]config.FileDef, 0)
for _, namePod := range namePods {
name := namePod.Namespace + "_" + namePod.Name
ba, err := yaml.Marshal(namePod.Pod)
if err != nil {
return "", fmt.Errorf("bootstrap pod %s: failed to render: %v", name, err)
}
defs = append(defs, config.FileDef{
Path: path.Join(dir, name+".yaml"),
Mode: 0640,
Content: string(ba),
})
}
ba, err := yaml.Marshal(defs)
return string(ba), err
}
2018-12-10 10:59:24 +00:00
buf := bytes.NewBuffer(make([]byte, 0, 4096))
2018-12-10 13:44:05 +00:00
if err := ctx.ConfigTemplate.Execute(buf, ctxMap, extraFuncs); err != nil {
log.Fatalf("failed to render config %q for host %q: %v", ctx.Group.Config, ctx.Host.Name, err)
2018-12-10 10:59:24 +00:00
}
2018-12-10 13:44:05 +00:00
return buf.String()
2018-12-10 10:59:24 +00:00
}
func (ctx *renderContext) StaticPods() (ba []byte, err error) {
if ctx.StaticPodsTemplate == nil {
2018-12-10 13:44:05 +00:00
log.Fatalf("no such static-pods: %q", ctx.Group.StaticPods)
2018-12-10 10:59:24 +00:00
}
ctxMap := ctx.asMap()
buf := bytes.NewBuffer(make([]byte, 0, 4096))
2018-12-10 13:44:05 +00:00
if err = ctx.StaticPodsTemplate.Execute(buf, ctxMap, ctx.templateFuncs(ctxMap)); err != nil {
2018-12-10 10:59:24 +00:00
return
}
ba = buf.Bytes()
return
}
2018-12-10 13:44:05 +00:00
func (ctx *renderContext) templateFuncs(ctxMap map[string]interface{}) map[string]interface{} {
2018-12-10 10:59:24 +00:00
cluster := ctx.Cluster.Name
2018-12-10 13:44:05 +00:00
getKeyCert := func(name, funcName string) (s string, err error) {
2018-12-10 10:59:24 +00:00
req := ctx.clusterConfig.CSR(name)
if req == nil {
2018-12-10 13:44:05 +00:00
err = fmt.Errorf("no certificate request named %q", name)
2018-12-10 10:59:24 +00:00
return
}
if req.CA == "" {
2018-12-10 13:44:05 +00:00
err = fmt.Errorf("CA not defined in req %q", name)
2018-12-10 10:59:24 +00:00
return
}
buf := &bytes.Buffer{}
err = req.Execute(buf, ctxMap, nil)
if err != nil {
return
}
2019-01-22 10:07:48 +00:00
key := name
2019-01-21 22:44:11 +00:00
if req.PerHost {
2019-01-22 10:07:48 +00:00
key += "/" + ctx.Host.Name
2019-01-21 22:44:11 +00:00
}
2019-01-22 10:07:48 +00:00
if funcName == "tls_dir" {
// needs the dir name
dir := "/etc/tls/" + name
s = fmt.Sprintf("{{ %s %q %q %q %q %q %q %q }}", funcName,
dir, cluster, req.CA, key, req.Profile, req.Label, buf.String())
} else {
s = fmt.Sprintf("{{ %s %q %q %q %q %q %q }}", funcName,
cluster, req.CA, key, req.Profile, req.Label, buf.String())
}
2018-12-10 13:44:05 +00:00
return
2018-12-10 10:59:24 +00:00
}
2019-10-09 06:45:19 +00:00
funcs := clusterFuncs(ctx.Cluster)
for k, v := range map[string]interface{}{
2018-12-10 13:44:05 +00:00
"tls_key": func(name string) (string, error) {
return getKeyCert(name, "tls_key")
2018-12-10 10:59:24 +00:00
},
"tls_crt": func(name string) (s string, err error) {
2018-12-10 13:44:05 +00:00
return getKeyCert(name, "tls_crt")
2018-12-10 10:59:24 +00:00
},
"tls_dir": func(name string) (s string, err error) {
2018-12-10 13:44:05 +00:00
return getKeyCert(name, "tls_dir")
2018-12-10 10:59:24 +00:00
},
"hosts_of_group": func() (hosts []interface{}) {
hosts = make([]interface{}, 0)
for _, host := range ctx.clusterConfig.Hosts {
if host.Group != ctx.Host.Group {
continue
}
hosts = append(hosts, asMap(host))
}
return hosts
},
"hosts_of_group_count": func() (count int) {
for _, host := range ctx.clusterConfig.Hosts {
2018-12-10 13:44:05 +00:00
if host.Group == ctx.Host.Group {
count++
2018-12-10 10:59:24 +00:00
}
}
return
},
2019-10-09 06:45:19 +00:00
} {
funcs[k] = v
2018-12-10 10:59:24 +00:00
}
2019-10-09 06:45:19 +00:00
return funcs
2018-12-10 10:59:24 +00:00
}
func (ctx *renderContext) asMap() map[string]interface{} {
result := asMap(ctx)
// also expand cluster:
cluster := result["cluster"].(map[interface{}]interface{})
cluster["kubernetes_svc_ip"] = ctx.Cluster.KubernetesSvcIP().String()
cluster["dns_svc_ip"] = ctx.Cluster.DNSSvcIP().String()
return result
}
func asMap(v interface{}) map[string]interface{} {
ba, err := yaml.Marshal(v)
if err != nil {
panic(err) // shouldn't happen
}
result := make(map[string]interface{})
if err := yaml.Unmarshal(ba, result); err != nil {
panic(err) // shouldn't happen
}
return result
}