This repository has been archived by the owner on Aug 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (131 loc) · 3.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"github.com/go-macaron/binding"
"gopkg.in/macaron.v1"
"io/ioutil"
"os"
)
type KindCluster struct {
Name string `form:"name" binding:"Required"`
Version string `form:"version"` // TODO: Implement different kind cluster versions
}
func main() {
m := macaron.Classic()
err := ProxyStartup()
if err != nil {
panic(err)
}
m.Use(macaron.Renderer())
if os.Getenv("ROUTE_REGISTER") == "true" {
m.Use(MacaronRR(os.Getenv("DOMAIN")))
}
m.Get("/:id", GetKubeConfig)
m.Post("/new", binding.Bind(KindCluster{}), NewCluster)
m.Delete("/:id", DeleteCluster)
m.Get("/", ListClusters)
m.Get("/kubeconfig/:id", GetProxyKubeConfig)
m.Get("/kube/:id", GetKubeEndpoint)
// TODO: CRUD for routes
// m.Post("/routes/new", binding.Bind(Route{}), NewRoute)
// m.Delete("/routes/:id", DeleteRoute)
// m.Get("/routes", ListRoutes)
m.Run()
}
func GetProxyKubeConfig(ctx *macaron.Context) {
id := ctx.Params(":id")
port, err := Proxied.GetProxy(id)
if err != nil {
ctx.JSON(500, APIResult{Error: err.Error()})
return
}
ctx.PlainText(200, []byte(
`apiVersion: v1
clusters:
- cluster:
server: http://`+os.Getenv("HOST")+":"+port+`
name: kind
contexts:
- context:
cluster: kind
user: kubernetes-admin
name: kubernetes-admin@kind
current-context: kubernetes-admin@kind
kind: Config
preferences: {}`))
}
func GetKubeEndpoint(ctx *macaron.Context) {
id := ctx.Params(":id")
port, err := Proxied.GetProxy(id)
if err != nil {
ctx.JSON(500, APIResult{Error: err.Error()})
return
}
ctx.JSON(200, NewAPIResult(os.Getenv("HOST")+":"+port))
}
func KubePath(cluster string) (string, error) {
res, err := Kind("get", "kubeconfig-path", "--name", cluster)
if err != nil {
return "", err
}
return res, nil
}
func KubeConfig(id string) ([]byte, error) {
res, err := KubePath(id)
if err != nil {
return []byte{}, err
}
dat, err := ioutil.ReadFile(res)
if err != nil {
return []byte{}, err
}
return dat, nil
}
func GetKubeConfig(ctx *macaron.Context) {
id := ctx.Params(":id")
res, err := KubeConfig(id)
if err != nil {
ctx.JSON(500, APIResult{Error: err.Error(), Output: string(res)})
return
}
ctx.PlainText(200, res)
}
func NewCluster(ctx *macaron.Context, kc KindCluster) {
res, err := Kind("create", "cluster", "--name", kc.Name)
if err != nil {
ctx.JSON(500, APIResult{Error: err.Error(), Output: res})
return
}
p, err := GetFreePort()
if err != nil {
ctx.JSON(500, APIResult{Error: err.Error()})
return
}
res, err = KubePath(kc.Name)
if err != nil {
ctx.JSON(500, APIResult{Error: err.Error()})
return
}
err = KubeStartProxy(kc.Name, res, p)
if err != nil {
ctx.JSON(500, APIResult{Error: err.Error()})
return
}
ctx.JSON(200, NewAPIResult(res))
}
func DeleteCluster(ctx *macaron.Context) {
id := ctx.Params(":id")
res, err := Kind("delete", "cluster", "--name", id)
if err != nil {
ctx.JSON(500, APIResult{Error: err.Error(), Output: res})
return
}
err = Proxied.StopProxy(id)
if err != nil {
ctx.JSON(500, APIResult{Error: err.Error(), Output: res})
return
}
ctx.JSON(200, NewAPIResult(res))
}
func ListClusters(ctx *macaron.Context) {
ctx.JSON(200, NewAPIResult(""))
}