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"
"io"
"log"
"path"
yaml "gopkg.in/yaml.v2"
@ -62,14 +63,16 @@ func renderClusterTemplates(cluster *clustersconfig.Cluster, setName string,
funcs := clusterFuncs(cluster)
log.Print("rendering cluster templates with ", clusterAsMap)
log.Print("rendering cluster templates in ", setName, " with ", clusterAsMap)
buf := &bytes.Buffer{}
contextName := "cluster:" + cluster.Name
for _, t := range templates {
log.Print("- template: ", 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 {
log.Fatalf("cluster %q: %s: failed to render %q: %v",
@ -112,7 +115,7 @@ func renderBootstrapPods(cluster *clustersconfig.Cluster) (pods []namePod) {
}
// 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 {
buf := bytes.NewBuffer(part)
dec := yaml.NewDecoder(buf)

View File

@ -103,18 +103,33 @@ func genericMerge(target, source interface{}) (result interface{}) {
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 {
if ctx.ConfigTemplate == nil {
log.Fatalf("no such config: %q", ctx.Group.Config)
}
ctxName := ctx.Name()
ctxMap := ctx.asMap()
templateFuncs := ctx.templateFuncs(ctxMap)
render := func(what string, t *clustersconfig.Template) (s string, err error) {
buf := &bytes.Buffer{}
err = t.Execute(buf, ctxMap, templateFuncs)
err = t.Execute(ctxName, what, buf, ctxMap, templateFuncs)
if err != nil {
log.Printf("host %s: failed to render %s [%q]: %v", ctx.Host.Name, what, t.Name, err)
return
@ -137,7 +152,7 @@ func (ctx *renderContext) Config() string {
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) {
@ -165,7 +180,7 @@ func (ctx *renderContext) Config() string {
}
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)
}
@ -180,7 +195,7 @@ func (ctx *renderContext) StaticPods() (ba []byte, err error) {
ctxMap := ctx.asMap()
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
}
@ -204,7 +219,7 @@ func (ctx *renderContext) templateFuncs(ctxMap map[string]interface{}) map[strin
}
buf := &bytes.Buffer{}
err = req.Execute(buf, ctxMap, nil)
err = req.Execute(ctx.Name(), "req:"+name, buf, ctxMap, nil)
if err != nil {
return
}

View File

@ -9,7 +9,6 @@ import (
"net"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
@ -18,7 +17,7 @@ import (
var (
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")
templateID = 0
@ -151,7 +150,7 @@ type Template struct {
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 {
var templateFuncs = map[string]interface{}{
"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 != "" {
templateID++
os.MkdirAll(*templateDetailsDir, 0700)
base := fmt.Sprintf("%s/%03d-", *templateDetailsDir, templateID)
base := filepath.Join(*templateDetailsDir, contextName, fmt.Sprintf("%s-%03d", elementName, templateID))
os.MkdirAll(base, 0700)
base += string(filepath.Separator)
log.Print("writing template details: ", base, "{in,data,out}")
if err := ioutil.WriteFile(base+"in", []byte(t.Template), 0600); err != nil {