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

123 lines
2.8 KiB
Go
Raw Normal View History

2019-10-09 05:58:28 +00:00
package main
import (
"bytes"
"fmt"
"log"
2019-10-23 00:03:35 +00:00
"path"
2019-10-09 05:58:28 +00:00
2022-04-28 01:33:19 +00:00
"novit.tech/direktil/local-server/pkg/clustersconfig"
2019-10-09 05:58:28 +00:00
)
2019-10-09 06:45:19 +00:00
func clusterFuncs(clusterSpec *clustersconfig.Cluster) map[string]interface{} {
cluster := clusterSpec.Name
return map[string]interface{}{
"password": func(name string) (s string) {
return fmt.Sprintf("{{ password %q %q }}", cluster, name)
},
"token": func(name string) (s string) {
return fmt.Sprintf("{{ token %q %q }}", cluster, name)
},
"ca_key": func(name string) (s string, err error) {
// TODO check CA exists
// ?ctx.clusterConfig.CA(name)
return fmt.Sprintf("{{ ca_key %q %q }}", cluster, name), nil
},
"ca_crt": func(name string) (s string, err error) {
// TODO check CA exists
return fmt.Sprintf("{{ ca_crt %q %q }}", cluster, name), nil
},
"ca_dir": func(name string) (s string, err error) {
return fmt.Sprintf("{{ ca_dir %q %q }}", cluster, name), nil
},
2019-10-24 03:06:18 +00:00
"hosts_by_cluster": func(cluster string) (hosts []interface{}) {
for _, host := range src.Hosts {
if host.Cluster == cluster {
hosts = append(hosts, asMap(host))
}
}
if len(hosts) == 0 {
log.Printf("WARNING: no hosts in cluster %q", cluster)
}
return
},
"hosts_by_group": func(group string) (hosts []interface{}) {
2019-10-09 06:45:19 +00:00
for _, host := range src.Hosts {
2023-05-15 13:54:16 +00:00
if host.Cluster == cluster && host.Group == group {
hosts = append(hosts, asMap(host))
2019-10-09 06:45:19 +00:00
}
}
if len(hosts) == 0 {
2019-10-24 03:06:18 +00:00
log.Printf("WARNING: no hosts in group %q", group)
2019-10-09 06:45:19 +00:00
}
return
},
}
}
2019-10-09 05:58:28 +00:00
func renderClusterTemplates(cluster *clustersconfig.Cluster, setName string,
templates []*clustersconfig.Template) []byte {
clusterAsMap := asMap(cluster)
clusterAsMap["kubernetes_svc_ip"] = cluster.KubernetesSvcIP().String()
clusterAsMap["dns_svc_ip"] = cluster.DNSSvcIP().String()
funcs := clusterFuncs(cluster)
2019-12-16 07:00:57 +00:00
log.Print("rendering cluster templates in ", setName)
2019-10-09 05:58:28 +00:00
buf := &bytes.Buffer{}
2019-10-23 00:03:35 +00:00
contextName := "cluster:" + cluster.Name
2019-10-09 05:58:28 +00:00
for _, t := range templates {
log.Print("- template: ", setName, ": ", t.Name)
2019-10-09 05:58:28 +00:00
fmt.Fprintf(buf, "---\n# %s: %s\n", setName, t.Name)
2019-10-23 00:03:35 +00:00
err := t.Execute(contextName, path.Join(setName, t.Name), buf, clusterAsMap, funcs)
2019-10-09 05:58:28 +00:00
if err != nil {
log.Fatalf("cluster %q: %s: failed to render %q: %v",
cluster.Name, setName, t.Name, err)
}
fmt.Fprintln(buf)
}
return buf.Bytes()
}
func renderAddons(cluster *clustersconfig.Cluster) string {
if len(cluster.Addons) == 0 {
return ""
}
2023-05-15 12:47:53 +00:00
buf := new(bytes.Buffer)
for _, addonSet := range cluster.Addons {
addons := src.Addons[addonSet]
if addons == nil {
log.Fatalf("cluster %q: no addons with name %q", cluster.Name, addonSet)
}
buf.Write(renderClusterTemplates(cluster, "addons", addons))
2019-10-09 05:58:28 +00:00
}
2023-05-15 12:47:53 +00:00
return buf.String()
2019-10-09 05:58:28 +00:00
}
type namePod struct {
Namespace string
Name string
Pod map[string]interface{}
}