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

337 lines
7.4 KiB
Go
Raw Normal View History

2018-12-10 10:59:24 +00:00
package main
import (
"bytes"
"fmt"
"io"
2018-12-10 10:59:24 +00:00
"log"
2019-10-09 05:58:28 +00:00
"path"
2019-10-19 04:08:41 +00:00
"reflect"
"strings"
2018-12-10 10:59:24 +00:00
yaml "gopkg.in/yaml.v2"
2022-04-28 01:33:19 +00:00
"novit.tech/direktil/pkg/config"
"novit.tech/direktil/local-server/pkg/clustersconfig"
2018-12-10 10:59:24 +00:00
)
type renderContext struct {
2019-12-16 07:00:57 +00:00
Labels map[string]string
Annotations map[string]string
2022-04-28 01:33:19 +00:00
Host *clustersconfig.Host
Cluster *clustersconfig.Cluster
Vars map[string]any
2022-04-28 01:33:19 +00:00
BootstrapConfigTemplate *clustersconfig.Template
ConfigTemplate *clustersconfig.Template
StaticPodsTemplate *clustersconfig.Template
2018-12-10 10:59:24 +00:00
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
}
vars := make(map[string]any)
2018-12-10 10:59:24 +00:00
for _, oVars := range []map[string]any{
2018-12-10 10:59:24 +00:00
cluster.Vars,
host.Vars,
} {
2019-04-19 16:07:22 +00:00
mapMerge(vars, oVars)
2018-12-10 10:59:24 +00:00
}
return &renderContext{
Labels: mergeLabels(cluster.Labels, host.Labels),
Annotations: mergeLabels(cluster.Annotations, host.Annotations),
2019-12-16 07:00:57 +00:00
2022-04-28 01:33:19 +00:00
Host: host,
Cluster: cluster,
Vars: vars,
BootstrapConfigTemplate: cfg.ConfigTemplate(host.BootstrapConfig),
ConfigTemplate: cfg.ConfigTemplate(host.Config),
2018-12-10 10:59:24 +00:00
clusterConfig: cfg,
}, nil
}
2019-12-16 07:00:57 +00:00
func mergeLabels(sources ...map[string]string) map[string]string {
ret := map[string]string{}
for _, src := range sources {
for k, v := range src {
ret[k] = v
}
}
return ret
}
2019-04-19 16:07:22 +00:00
func mapMerge(target, source map[string]interface{}) {
for k, v := range source {
2019-10-19 04:08:41 +00:00
target[k] = genericMerge(target[k], v)
}
}
func genericMerge(target, source interface{}) (result interface{}) {
srcV := reflect.ValueOf(source)
tgtV := reflect.ValueOf(target)
if srcV.Kind() == reflect.Map && tgtV.Kind() == reflect.Map {
// XXX maybe more specific later
result = map[interface{}]interface{}{}
resultV := reflect.ValueOf(result)
tgtIt := tgtV.MapRange()
for tgtIt.Next() {
sv := srcV.MapIndex(tgtIt.Key())
if sv.Kind() == 0 {
resultV.SetMapIndex(tgtIt.Key(), tgtIt.Value())
2019-04-19 16:07:22 +00:00
continue
}
2019-10-19 04:08:41 +00:00
merged := genericMerge(tgtIt.Value().Interface(), sv.Interface())
resultV.SetMapIndex(tgtIt.Key(), reflect.ValueOf(merged))
}
srcIt := srcV.MapRange()
for srcIt.Next() {
if resultV.MapIndex(srcIt.Key()).Kind() != 0 {
continue // already done
}
resultV.SetMapIndex(srcIt.Key(), srcIt.Value())
2019-04-19 16:07:22 +00:00
}
2019-10-19 04:08:41 +00:00
return
2019-04-19 16:07:22 +00:00
}
2019-10-19 04:08:41 +00:00
return source
2019-04-19 16:07:22 +00:00
}
2019-10-23 00:03:35 +00:00
func (ctx *renderContext) Name() string {
switch {
case ctx.Host != nil:
return "host:" + ctx.Host.Name
case ctx.Cluster != nil:
return "cluster:" + ctx.Cluster.Name
default:
return "unknown"
}
}
2022-04-28 01:33:19 +00:00
func (ctx *renderContext) BootstrapConfig() string {
if ctx.BootstrapConfigTemplate == nil {
log.Fatalf("no such (bootstrap) config: %q", ctx.Host.BootstrapConfig)
2022-04-28 01:33:19 +00:00
}
return ctx.renderConfig(ctx.BootstrapConfigTemplate)
}
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 {
log.Fatalf("no such config: %q", ctx.Host.Config)
2018-12-10 10:59:24 +00:00
}
2022-04-28 01:33:19 +00:00
return ctx.renderConfig(ctx.ConfigTemplate)
}
2018-12-10 10:59:24 +00:00
2022-04-28 01:33:19 +00:00
func (ctx *renderContext) renderConfig(configTemplate *clustersconfig.Template) string {
buf := new(strings.Builder)
ctx.renderConfigTo(buf, configTemplate)
return buf.String()
}
func (ctx *renderContext) renderConfigTo(buf io.Writer, configTemplate *clustersconfig.Template) {
2019-10-23 00:03:35 +00:00
ctxName := ctx.Name()
2018-12-10 10:59:24 +00:00
ctxMap := ctx.asMap()
2018-12-10 13:44:05 +00:00
extraFuncs := ctx.templateFuncs(ctxMap)
2018-12-10 10:59:24 +00:00
2023-05-15 14:22:04 +00:00
extraFuncs["static_pods_files"] = func(dir string) (string, error) {
namePods := ctx.renderStaticPods()
2019-10-09 05:58:28 +00:00
defs := make([]config.FileDef, 0)
for _, namePod := range namePods {
name := namePod.Namespace + "_" + namePod.Name
ba, err := yaml.Marshal(namePod.Pod)
if err != nil {
2023-05-15 14:54:29 +00:00
return "", fmt.Errorf("static pod %s: failed to render: %v", name, err)
2019-10-09 05:58:28 +00:00
}
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
}
2024-04-15 13:32:43 +00:00
extraFuncs["host_ip"] = func() string {
if ctx.Host.Template {
return "{{ host_ip }}"
}
return ctx.Host.IP
}
extraFuncs["host_name"] = func() string {
if ctx.Host.Template {
return "{{ host_name }}"
}
return ctx.Host.Name
}
2019-12-16 07:00:57 +00:00
extraFuncs["machine_id"] = func() string {
2024-04-15 13:32:43 +00:00
return "{{ machine_id }}"
2019-12-16 07:00:57 +00:00
}
2023-11-25 15:21:47 +00:00
extraFuncs["version"] = func() string { return Version }
2022-04-28 01:33:19 +00:00
if err := configTemplate.Execute(ctxName, "config", buf, ctxMap, extraFuncs); err != nil {
log.Fatalf("failed to render config %q for host %q: %v", ctx.Host.Config, ctx.Host.Name, err)
2018-12-10 10:59:24 +00:00
}
}
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{}
2019-10-23 00:03:35 +00:00
err = req.Execute(ctx.Name(), "req:"+name, buf, ctxMap, nil)
2018-12-10 10:59:24 +00:00
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{}{
2024-01-06 17:20:03 +00:00
"default": func(value, defaultValue any) any {
switch v := value.(type) {
case string:
if v != "" {
return v
}
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, float32, float64:
if v != 0 {
return v
}
default:
if v != nil {
return v
}
}
return defaultValue
},
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
},
2019-12-03 10:03:20 +00:00
"ssh_host_keys": func(dir string) (s string) {
2023-02-13 14:57:30 +00:00
return fmt.Sprintf("{{ ssh_host_keys %q %q \"\"}}",
dir, cluster)
},
"host_download_token": func() (s string) {
return "{{ host_download_token }}"
2019-12-03 10:03:20 +00:00
},
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
}