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{ dst.Hosts = append(dst.Hosts, &localconfig.Host{
Name: host.Name, Name: host.Name,
ClusterName: ctx.Cluster.Name,
Labels: ctx.Labels,
Annotations: ctx.Annotations,
MACs: macs, MACs: macs,
IPs: ips, IPs: ips,

View File

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

View File

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

View File

@ -101,6 +101,23 @@ func wsClusterSetPassword(req *restful.Request, resp *restful.Response) {
} }
} }
func wsClusterToken(req *restful.Request, resp *restful.Response) {
cluster := wsReadCluster(req, resp)
if cluster == nil {
return
}
name := req.PathParameter("token-name")
token, err := secretData.Token(cluster.Name, name)
if err != nil {
wsError(resp, err)
return
}
resp.WriteEntity(token)
}
func wsClusterBootstrapPods(req *restful.Request, resp *restful.Response) { func wsClusterBootstrapPods(req *restful.Request, resp *restful.Response) {
cluster := wsReadCluster(req, resp) cluster := wsReadCluster(req, resp)
if cluster == nil { if cluster == nil {

View File

@ -27,7 +27,8 @@ func (ws *wsHost) register(rws *restful.WebService, alterRB func(*restful.RouteB
for _, rb := range []*restful.RouteBuilder{ for _, rb := range []*restful.RouteBuilder{
rws.GET(ws.prefix).To(ws.get). rws.GET(ws.prefix).To(ws.get).
Doc("Get the " + ws.hostDoc + "'s details"), Doc("Get the "+ws.hostDoc+"'s details").
Returns(200, "OK", localconfig.Host{}),
// raw configuration // raw configuration
b("config"). b("config").

View File

@ -48,6 +48,9 @@ func registerWS(rest *restful.Container) {
ws.Route(ws.PUT("/clusters/{cluster-name}/passwords/{password-name}").To(wsClusterSetPassword). ws.Route(ws.PUT("/clusters/{cluster-name}/passwords/{password-name}").To(wsClusterSetPassword).
Doc("Set cluster's password")) Doc("Set cluster's password"))
ws.Route(ws.GET("/clusters/{cluster-name}/tokens/{token-name}").To(wsClusterToken).
Doc("Get cluster's token"))
ws.Route(ws.GET("/hosts").To(wsListHosts). ws.Route(ws.GET("/hosts").To(wsListHosts).
Doc("List hosts")) Doc("List hosts"))

2
go.mod
View File

@ -21,7 +21,7 @@ require (
gopkg.in/src-d/go-git.v4 v4.13.1 gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v2 v2.2.4 gopkg.in/yaml.v2 v2.2.4
k8s.io/apimachinery v0.0.0-20191203211716-adc6f4cd9e7d k8s.io/apimachinery v0.0.0-20191203211716-adc6f4cd9e7d
novit.nc/direktil/pkg v0.0.0-20191009054056-6e432c2a06e6 novit.nc/direktil/pkg v0.0.0-20191211161950-96b0448b84c2
) )
go 1.13 go 1.13

4
go.sum
View File

@ -250,5 +250,9 @@ k8s.io/kube-openapi v0.0.0-20190816220812-743ec37842bf/go.mod h1:1TqjTSzOxsLGIKf
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a/go.mod h1:1TqjTSzOxsLGIKfj0lK8EeCP7K1iUG65v09OM0/WG5E= k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a/go.mod h1:1TqjTSzOxsLGIKfj0lK8EeCP7K1iUG65v09OM0/WG5E=
novit.nc/direktil/pkg v0.0.0-20191009054056-6e432c2a06e6 h1:zJFvtQXH8euAzEvbJRME7EhIy7hyyNRMIVYc9tNc/oo= novit.nc/direktil/pkg v0.0.0-20191009054056-6e432c2a06e6 h1:zJFvtQXH8euAzEvbJRME7EhIy7hyyNRMIVYc9tNc/oo=
novit.nc/direktil/pkg v0.0.0-20191009054056-6e432c2a06e6/go.mod h1:zwTVO6U0tXFEaga73megQIBK7yVIKZJVePaIh/UtdfU= novit.nc/direktil/pkg v0.0.0-20191009054056-6e432c2a06e6/go.mod h1:zwTVO6U0tXFEaga73megQIBK7yVIKZJVePaIh/UtdfU=
novit.nc/direktil/pkg v0.0.0-20191211135749-9b2e3dd362b1 h1:+5BJJAbOjMg5kmMsKq+xhTsgUXsZsEekyv7hnJ8Gy7I=
novit.nc/direktil/pkg v0.0.0-20191211135749-9b2e3dd362b1/go.mod h1:zwTVO6U0tXFEaga73megQIBK7yVIKZJVePaIh/UtdfU=
novit.nc/direktil/pkg v0.0.0-20191211161950-96b0448b84c2 h1:LN3K19gAJ1GamJXkzXAQmjbl8xCV7utqdxTTrM89MMc=
novit.nc/direktil/pkg v0.0.0-20191211161950-96b0448b84c2/go.mod h1:zwTVO6U0tXFEaga73megQIBK7yVIKZJVePaIh/UtdfU=
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI= sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=

View File

@ -210,7 +210,11 @@ func (t *Template) Execute(contextName, elementName string, wr io.Writer, data i
// Host represents a host served by this server. // Host represents a host served by this server.
type Host struct { type Host struct {
WithRev WithRev
Name string Name string
Labels map[string]string
Annotations map[string]string
MAC string MAC string
IP string IP string
IPs []string IPs []string
@ -222,7 +226,11 @@ type Host struct {
// Group represents a group of hosts and provides their configuration. // Group represents a group of hosts and provides their configuration.
type Group struct { type Group struct {
WithRev WithRev
Name string Name string
Labels map[string]string
Annotations map[string]string
Master bool Master bool
IPXE string IPXE string
Kernel string Kernel string
@ -239,7 +247,11 @@ type Vars map[string]interface{}
// Cluster represents a cluster of hosts, allowing for cluster-wide variables. // Cluster represents a cluster of hosts, allowing for cluster-wide variables.
type Cluster struct { type Cluster struct {
WithRev WithRev
Name string Name string
Labels map[string]string
Annotations map[string]string
Domain string Domain string
Addons string Addons string
BootstrapPods string `yaml:"bootstrap_pods"` BootstrapPods string `yaml:"bootstrap_pods"`