2023-01-16 17:17:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
"novit.tech/direktil/local-server/pkg/clustersconfig"
|
|
|
|
)
|
|
|
|
|
2023-05-15 14:22:04 +00:00
|
|
|
func (ctx *renderContext) renderStaticPods() (pods []namePod) {
|
|
|
|
if ctx.Host.StaticPods == "" {
|
2023-01-16 17:17:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-15 14:22:04 +00:00
|
|
|
staticPods, ok := src.StaticPods[ctx.Host.StaticPods]
|
2023-01-16 17:17:11 +00:00
|
|
|
if !ok {
|
2023-05-15 14:22:04 +00:00
|
|
|
log.Fatalf("no static pods template named %q", ctx.Host.StaticPods)
|
2023-01-16 17:17:11 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 14:22:04 +00:00
|
|
|
// render static pods
|
|
|
|
parts := bytes.Split(ctx.renderHostTemplates("static-pods", staticPods), []byte("\n---\n"))
|
2023-01-16 17:17:11 +00:00
|
|
|
for _, part := range parts {
|
|
|
|
buf := bytes.NewBuffer(part)
|
|
|
|
dec := yaml.NewDecoder(buf)
|
|
|
|
|
|
|
|
for n := 0; ; n++ {
|
|
|
|
str := buf.String()
|
|
|
|
|
|
|
|
podMap := map[string]interface{}{}
|
|
|
|
err := dec.Decode(podMap)
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2023-05-15 14:22:04 +00:00
|
|
|
log.Fatalf("static pod %d: failed to parse: %v\n%s", n, err, str)
|
2023-01-16 17:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(podMap) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if podMap["metadata"] == nil {
|
2023-05-15 14:22:04 +00:00
|
|
|
log.Fatalf("static pod %d: no metadata\n%s", n, buf.String())
|
2023-01-16 17:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
md := podMap["metadata"].(map[interface{}]interface{})
|
|
|
|
|
|
|
|
namespace := md["namespace"].(string)
|
|
|
|
name := md["name"].(string)
|
|
|
|
|
|
|
|
pods = append(pods, namePod{namespace, name, podMap})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *renderContext) renderHostTemplates(setName string,
|
|
|
|
templates []*clustersconfig.Template) []byte {
|
|
|
|
|
|
|
|
log.Print("rendering host templates in ", setName)
|
|
|
|
|
2024-04-15 13:32:43 +00:00
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, 16<<10))
|
2023-01-16 17:17:11 +00:00
|
|
|
|
|
|
|
for _, t := range templates {
|
|
|
|
log.Print("- template: ", setName, ": ", t.Name)
|
|
|
|
fmt.Fprintf(buf, "---\n# %s: %s\n", setName, t.Name)
|
|
|
|
|
|
|
|
ctx.renderConfigTo(buf, t)
|
|
|
|
fmt.Fprintln(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|