sanity commit

This commit is contained in:
Mikaël Cluseau
2019-12-16 08:00:57 +01:00
parent 8ce4e97922
commit 55c72aefa8
9 changed files with 74 additions and 8 deletions

View File

@ -77,6 +77,12 @@ func main() {
dst.Hosts = append(dst.Hosts, &localconfig.Host{
Name: host.Name,
ClusterName: ctx.Cluster.Name,
Labels: ctx.Labels,
Annotations: ctx.Annotations,
MACs: macs,
IPs: ips,

View File

@ -77,7 +77,7 @@ func renderClusterTemplates(cluster *clustersconfig.Cluster, setName string,
funcs := clusterFuncs(cluster)
log.Print("rendering cluster templates in ", setName, " with ", clusterAsMap)
log.Print("rendering cluster templates in ", setName)
buf := &bytes.Buffer{}

View File

@ -2,6 +2,8 @@ package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"log"
"path"
@ -14,6 +16,9 @@ import (
)
type renderContext struct {
Labels map[string]string
Annotations map[string]string
Host *clustersconfig.Host
Group *clustersconfig.Group
Cluster *clustersconfig.Cluster
@ -47,9 +52,10 @@ func newRenderContext(host *clustersconfig.Host, cfg *clustersconfig.Config) (ct
mapMerge(vars, oVars)
}
log.Print("vars: ", vars)
return &renderContext{
Labels: mergeLabels(cluster.Labels, group.Labels, host.Labels),
Annotations: mergeLabels(cluster.Annotations, group.Annotations, host.Annotations),
Host: host,
Group: group,
Cluster: cluster,
@ -61,6 +67,18 @@ func newRenderContext(host *clustersconfig.Host, cfg *clustersconfig.Config) (ct
}, nil
}
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
}
func mapMerge(target, source map[string]interface{}) {
for k, v := range source {
target[k] = genericMerge(target[k], v)
@ -179,6 +197,11 @@ func (ctx *renderContext) Config() string {
return string(ba), err
}
extraFuncs["machine_id"] = func() string {
ba := sha1.Sum([]byte(ctx.Cluster.Name + "/" + ctx.Host.Name)) // TODO: check semantics of machine-id
return hex.EncodeToString(ba[:])
}
buf := bytes.NewBuffer(make([]byte, 0, 4096))
if err := ctx.ConfigTemplate.Execute(ctxName, "config", buf, ctxMap, extraFuncs); err != nil {
log.Fatalf("failed to render config %q for host %q: %v", ctx.Group.Config, ctx.Host.Name, err)