improve debugging

This commit is contained in:
Mikaël Cluseau 2019-10-23 11:03:35 +11:00
parent fec03e0a7e
commit b5b8514c59
3 changed files with 31 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"path"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
@ -62,14 +63,16 @@ func renderClusterTemplates(cluster *clustersconfig.Cluster, setName string,
funcs := clusterFuncs(cluster) funcs := clusterFuncs(cluster)
log.Print("rendering cluster templates with ", clusterAsMap) log.Print("rendering cluster templates in ", setName, " with ", clusterAsMap)
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
contextName := "cluster:" + cluster.Name
for _, t := range templates { for _, t := range templates {
log.Print("- template: ", setName, ": ", t.Name) log.Print("- template: ", setName, ": ", t.Name)
fmt.Fprintf(buf, "---\n# %s: %s\n", setName, t.Name) fmt.Fprintf(buf, "---\n# %s: %s\n", setName, t.Name)
err := t.Execute(buf, clusterAsMap, funcs) err := t.Execute(contextName, path.Join(setName, t.Name), buf, clusterAsMap, funcs)
if err != nil { if err != nil {
log.Fatalf("cluster %q: %s: failed to render %q: %v", log.Fatalf("cluster %q: %s: failed to render %q: %v",
@ -112,7 +115,7 @@ func renderBootstrapPods(cluster *clustersconfig.Cluster) (pods []namePod) {
} }
// render bootstrap pods // render bootstrap pods
parts := bytes.Split(renderClusterTemplates(cluster, "bootstrap pods", bootstrapPods), []byte("\n---\n")) parts := bytes.Split(renderClusterTemplates(cluster, "bootstrap-pods", bootstrapPods), []byte("\n---\n"))
for _, part := range parts { for _, part := range parts {
buf := bytes.NewBuffer(part) buf := bytes.NewBuffer(part)
dec := yaml.NewDecoder(buf) dec := yaml.NewDecoder(buf)

View File

@ -103,18 +103,33 @@ func genericMerge(target, source interface{}) (result interface{}) {
return source return source
} }
func (ctx *renderContext) Name() string {
switch {
case ctx.Host != nil:
return "host:" + ctx.Host.Name
case ctx.Group != nil:
return "group:" + ctx.Group.Name
case ctx.Cluster != nil:
return "cluster:" + ctx.Cluster.Name
default:
return "unknown"
}
}
func (ctx *renderContext) Config() string { func (ctx *renderContext) Config() string {
if ctx.ConfigTemplate == nil { if ctx.ConfigTemplate == nil {
log.Fatalf("no such config: %q", ctx.Group.Config) log.Fatalf("no such config: %q", ctx.Group.Config)
} }
ctxName := ctx.Name()
ctxMap := ctx.asMap() ctxMap := ctx.asMap()
templateFuncs := ctx.templateFuncs(ctxMap) templateFuncs := ctx.templateFuncs(ctxMap)
render := func(what string, t *clustersconfig.Template) (s string, err error) { render := func(what string, t *clustersconfig.Template) (s string, err error) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err = t.Execute(buf, ctxMap, templateFuncs) err = t.Execute(ctxName, what, buf, ctxMap, templateFuncs)
if err != nil { if err != nil {
log.Printf("host %s: failed to render %s [%q]: %v", ctx.Host.Name, what, t.Name, err) log.Printf("host %s: failed to render %s [%q]: %v", ctx.Host.Name, what, t.Name, err)
return return
@ -137,7 +152,7 @@ func (ctx *renderContext) Config() string {
return "", fmt.Errorf("no static pods template named %q", name) return "", fmt.Errorf("no static pods template named %q", name)
} }
return render("static pods", t) return render("static-pods", t)
} }
extraFuncs["bootstrap_pods_files"] = func(dir string) (string, error) { extraFuncs["bootstrap_pods_files"] = func(dir string) (string, error) {
@ -165,7 +180,7 @@ func (ctx *renderContext) Config() string {
} }
buf := bytes.NewBuffer(make([]byte, 0, 4096)) buf := bytes.NewBuffer(make([]byte, 0, 4096))
if err := ctx.ConfigTemplate.Execute(buf, ctxMap, extraFuncs); err != nil { 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) log.Fatalf("failed to render config %q for host %q: %v", ctx.Group.Config, ctx.Host.Name, err)
} }
@ -180,7 +195,7 @@ func (ctx *renderContext) StaticPods() (ba []byte, err error) {
ctxMap := ctx.asMap() ctxMap := ctx.asMap()
buf := bytes.NewBuffer(make([]byte, 0, 4096)) buf := bytes.NewBuffer(make([]byte, 0, 4096))
if err = ctx.StaticPodsTemplate.Execute(buf, ctxMap, ctx.templateFuncs(ctxMap)); err != nil { if err = ctx.StaticPodsTemplate.Execute(ctx.Name(), "static-pods", buf, ctxMap, ctx.templateFuncs(ctxMap)); err != nil {
return return
} }
@ -204,7 +219,7 @@ func (ctx *renderContext) templateFuncs(ctxMap map[string]interface{}) map[strin
} }
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err = req.Execute(buf, ctxMap, nil) err = req.Execute(ctx.Name(), "req:"+name, buf, ctxMap, nil)
if err != nil { if err != nil {
return return
} }

View File

@ -9,7 +9,6 @@ import (
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"text/template" "text/template"
@ -18,7 +17,7 @@ import (
var ( var (
templateDetailsDir = flag.String("template-details-dir", templateDetailsDir = flag.String("template-details-dir",
filepath.Join(os.TempDir(), "dkl-dir2config", strconv.Itoa(os.Getpid())), filepath.Join(os.TempDir(), "dkl-dir2config"),
"write details of template execute in this dir") "write details of template execute in this dir")
templateID = 0 templateID = 0
@ -151,7 +150,7 @@ type Template struct {
parsedTemplate *template.Template parsedTemplate *template.Template
} }
func (t *Template) Execute(wr io.Writer, data interface{}, extraFuncs map[string]interface{}) error { func (t *Template) Execute(contextName, elementName string, wr io.Writer, data interface{}, extraFuncs map[string]interface{}) error {
if t.parsedTemplate == nil { if t.parsedTemplate == nil {
var templateFuncs = map[string]interface{}{ var templateFuncs = map[string]interface{}{
"indent": func(indent, s string) (indented string) { "indent": func(indent, s string) (indented string) {
@ -176,9 +175,10 @@ func (t *Template) Execute(wr io.Writer, data interface{}, extraFuncs map[string
if *templateDetailsDir != "" { if *templateDetailsDir != "" {
templateID++ templateID++
os.MkdirAll(*templateDetailsDir, 0700) base := filepath.Join(*templateDetailsDir, contextName, fmt.Sprintf("%s-%03d", elementName, templateID))
base := fmt.Sprintf("%s/%03d-", *templateDetailsDir, templateID) os.MkdirAll(base, 0700)
base += string(filepath.Separator)
log.Print("writing template details: ", base, "{in,data,out}") log.Print("writing template details: ", base, "{in,data,out}")
if err := ioutil.WriteFile(base+"in", []byte(t.Template), 0600); err != nil { if err := ioutil.WriteFile(base+"in", []byte(t.Template), 0600); err != nil {