local-server/cmd/dkl-local-server/ws-clusters.go

209 lines
3.9 KiB
Go
Raw Normal View History

2019-02-04 02:56:43 +00:00
package main
import (
"log"
2021-11-14 14:28:40 +00:00
"sort"
2019-02-04 02:56:43 +00:00
restful "github.com/emicklei/go-restful"
2021-11-14 14:28:40 +00:00
2022-04-28 01:33:19 +00:00
"novit.tech/direktil/pkg/localconfig"
2019-02-04 02:56:43 +00:00
)
func wsListClusters(req *restful.Request, resp *restful.Response) {
cfg := wsReadConfig(resp)
if cfg == nil {
return
}
clusterNames := make([]string, len(cfg.Clusters))
for i, cluster := range cfg.Clusters {
clusterNames[i] = cluster.Name
}
resp.WriteEntity(clusterNames)
}
func wsReadCluster(req *restful.Request, resp *restful.Response) (cluster *localconfig.Cluster) {
clusterName := req.PathParameter("cluster-name")
cfg := wsReadConfig(resp)
if cfg == nil {
return
}
cluster = cfg.Cluster(clusterName)
if cluster == nil {
wsNotFound(req, resp)
return
}
return
}
func wsCluster(req *restful.Request, resp *restful.Response) {
cluster := wsReadCluster(req, resp)
if cluster == nil {
return
}
resp.WriteEntity(cluster)
}
func wsClusterAddons(req *restful.Request, resp *restful.Response) {
cluster := wsReadCluster(req, resp)
if cluster == nil {
return
}
if len(cluster.Addons) == 0 {
log.Printf("cluster %q has no addons defined", cluster.Name)
wsNotFound(req, resp)
return
}
wsRender(resp, cluster.Addons, cluster)
2019-02-04 02:56:43 +00:00
}
2019-04-13 09:36:58 +00:00
func wsClusterPasswords(req *restful.Request, resp *restful.Response) {
cluster := wsReadCluster(req, resp)
if cluster == nil {
return
}
resp.WriteEntity(secretData.Passwords(cluster.Name))
}
2019-04-13 09:36:58 +00:00
func wsClusterPassword(req *restful.Request, resp *restful.Response) {
cluster := wsReadCluster(req, resp)
if cluster == nil {
return
}
name := req.PathParameter("password-name")
resp.WriteEntity(secretData.Password(cluster.Name, name))
}
2019-04-13 09:36:58 +00:00
func wsClusterSetPassword(req *restful.Request, resp *restful.Response) {
cluster := wsReadCluster(req, resp)
if cluster == nil {
return
}
name := req.PathParameter("password-name")
var password string
if err := req.ReadEntity(&password); err != nil {
wsError(resp, err) // FIXME this is a BadRequest
return
}
secretData.SetPassword(cluster.Name, name, password)
if err := secretData.Save(); err != nil {
wsError(resp, err)
return
2019-04-13 09:36:58 +00:00
}
}
2019-10-09 05:58:28 +00:00
2019-12-16 07:00:57 +00:00
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)
}
2019-10-09 05:58:28 +00:00
func wsClusterBootstrapPods(req *restful.Request, resp *restful.Response) {
cluster := wsReadCluster(req, resp)
if cluster == nil {
return
}
if len(cluster.BootstrapPods) == 0 {
log.Printf("cluster %q has no bootstrap pods defined", cluster.Name)
wsNotFound(req, resp)
return
}
wsRender(resp, cluster.BootstrapPods, cluster)
2019-10-09 05:58:28 +00:00
}
2020-04-22 15:36:04 +00:00
2021-11-14 14:28:40 +00:00
func wsClusterCAs(req *restful.Request, resp *restful.Response) {
cs := secretData.clusters[req.PathParameter("cluster-name")]
if cs == nil {
wsNotFound(req, resp)
return
}
keys := make([]string, 0, len(cs.CAs))
for k := range cs.CAs {
keys = append(keys, k)
}
sort.Strings(keys)
resp.WriteJson(keys, restful.MIME_JSON)
}
2020-04-22 15:36:04 +00:00
func wsClusterCACert(req *restful.Request, resp *restful.Response) {
2021-11-14 14:28:40 +00:00
cs := secretData.clusters[req.PathParameter("cluster-name")]
if cs == nil {
wsNotFound(req, resp)
2020-04-22 15:36:04 +00:00
return
}
2021-11-14 14:28:40 +00:00
ca := cs.CAs[req.PathParameter("ca-name")]
if ca == nil {
wsNotFound(req, resp)
2020-04-22 15:36:04 +00:00
return
}
resp.Write(ca.Cert)
}
func wsClusterSignedCert(req *restful.Request, resp *restful.Response) {
2021-11-14 14:28:40 +00:00
cs := secretData.clusters[req.PathParameter("cluster-name")]
if cs == nil {
wsNotFound(req, resp)
2020-04-22 15:36:04 +00:00
return
}
2021-11-14 14:28:40 +00:00
ca := cs.CAs[req.PathParameter("ca-name")]
if ca == nil {
wsNotFound(req, resp)
return
}
name := req.QueryParameter("name")
if name == "" {
keys := make([]string, 0, len(ca.Signed))
for k := range ca.Signed {
keys = append(keys, k)
}
sort.Strings(keys)
resp.WriteJson(keys, restful.MIME_JSON)
2020-04-22 15:36:04 +00:00
return
}
2021-11-14 14:28:40 +00:00
kc := ca.Signed[name]
2020-04-22 15:36:04 +00:00
if kc == nil {
wsNotFound(req, resp)
return
}
resp.Write(kc.Cert)
}