-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
331 lines (282 loc) · 10.6 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"fmt"
knapv1 "github.com/bluebosh/knap/pkg/apis/knap/v1alpha1"
knapclientset "github.com/bluebosh/knap/pkg/client/clientset/versioned"
"github.com/golang/glog"
"github.com/labstack/echo"
"github.com/labstack/gommon/color"
tektoncdclientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
"html/template"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" // from https://github.com/kubernetes/client-go/issues/345
"k8s.io/client-go/tools/clientcmd"
"net/http"
"os"
"strconv"
)
var kubeconfig string
// TemplateRenderer is a custom html/template renderer for Echo framework
type TemplateRenderer struct {
templates *template.Template
}
// Render renders a template document
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
// Add global methods if data is a map
if viewContext, isMap := data.(map[string]interface{}); isMap {
viewContext["reverse"] = c.Echo().Reverse
}
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
kubeconfig = os.Getenv("KUBECONFIG")
if kubeconfig == "" {
glog.Fatalf("Cannot get kubeconfig from: %v", "KUBECONFIG")
}
e := echo.New()
renderer := &TemplateRenderer{
templates: template.Must(template.ParseGlob("views/*.html")),
}
e.Renderer = renderer
e.File("/img/knap.png", "img/knap.png")
e.File("/img/Running.png", "img/running.png")
e.File("/img/Pending.png", "img/pending.png")
e.File("/img/Deployed.png", "img/deployed.png")
e.File("/img/Fail.png", "img/fail.png")
e.File("/", "index.html")
e.File("/create", "views/create.html")
e.GET("/get", Get)
e.GET("/list", List)
e.GET("/templates", Templates)
e.GET("/spaces", Spaces)
e.GET("/services", Services)
e.GET("/createnew", CreateNew)
e.GET("/edit", Edit)
e.GET("/getedit", GetEdit)
e.GET("/delete", Delete)
e.GET("/logs", Logs)
e.Logger.Fatal(e.Start(":1323"))
}
func Get(c echo.Context) error {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
knapClient, err := knapclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building knap clientset: %v", err)
}
app, err := knapClient.KnapV1alpha1().Appengines("default").Get(c.Request().FormValue("name"), metav1.GetOptions{})
if err != nil {
glog.Fatalf("Error getting appengine: %v", c.Request().FormValue("name"))
}
fmt.Print("Get appengine", "appengine name", app.Name)
return c.Render(http.StatusOK, "get.html", app)
}
func Delete(c echo.Context) error {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
knapClient, err := knapclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building knap clientset: %v", err)
}
app, err := knapClient.KnapV1alpha1().Appengines("default").Get(c.Request().FormValue("name"), metav1.GetOptions{})
if err != nil {
glog.Fatalf("Error getting appengine: %v", c.Request().FormValue("name"))
}
fmt.Print("Get appengine", "appengine name", app.Spec.AppName)
err = knapClient.KnapV1alpha1().Appengines("default").Delete(app.Name, &metav1.DeleteOptions{})
if err != nil {
glog.Fatalf("Error deleting appengine: %v", c.Request().FormValue("name"))
}
fmt.Print("Delete appengine", "appengine name", app.Name)
return c.Render(http.StatusOK, "deleteDone.html", app.Spec.AppName)
}
func List(c echo.Context) error {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
knapClient, err := knapclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building knap clientset: %v", err)
}
appLst, err := knapClient.KnapV1alpha1().Appengines("default").List(metav1.ListOptions{})
color.Cyan("%-30s%-20s%-20s%-20s%-20s\n", "Engine Name", "Application Name", "Ready", "Instance", "Domain")
for i := 0; i < len(appLst.Items); i++ {
app := appLst.Items[i]
fmt.Printf("%-30s%-20s%-20s%-20s%-20s\n", app.Name, app.Spec.AppName, app.Status.Ready, fmt.Sprint(app.Status.Instance)+"/"+fmt.Sprint(app.Spec.Size), app.Status.Domain)
}
return c.Render(http.StatusOK, "list.html", appLst.Items)
}
func Templates(c echo.Context) error {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
tektoncdClient, err := tektoncdclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building tektoncd clientset: %v", err)
}
pipelines, err := tektoncdClient.TektonV1alpha1().Pipelines("default").List(metav1.ListOptions{})
color.Cyan("%-40s%-80s\n", "Template Name", "Template Flow")
for i := 0; i < len(pipelines.Items); i++ {
pipeline := pipelines.Items[i]
taskFlow := ""
for i := 0; i < len(pipeline.Spec.Tasks); i++ {
task := pipeline.Spec.Tasks[i]
if taskFlow == "" {
taskFlow = task.Name
} else {
taskFlow = taskFlow + " -> " + task.Name
}
}
fmt.Printf("%-40s%-80s\n", pipeline.Name, taskFlow)
}
return c.Render(http.StatusOK, "templates.html", pipelines.Items)
}
func Spaces(c echo.Context) error {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
knapClient, err := knapclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building knap clientset: %v", err)
}
appLst, err := knapClient.KnapV1alpha1().Appengines("default").List(metav1.ListOptions{})
color.Cyan("%-30s%-20s%-20s%-20s%-20s\n", "Engine Name", "Application Name", "Ready", "Instance", "Domain")
for i := 0; i < len(appLst.Items); i++ {
app := appLst.Items[i]
fmt.Printf("%-30s%-20s%-20s%-20s%-20s\n", app.Name, app.Spec.AppName, app.Status.Ready, fmt.Sprint(app.Status.Instance)+"/"+fmt.Sprint(app.Spec.Size), app.Status.Domain)
}
return c.Render(http.StatusOK, "spaces.html", appLst.Items)
}
func Services(c echo.Context) error {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
knapClient, err := knapclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building knap clientset: %v", err)
}
appLst, err := knapClient.KnapV1alpha1().Appengines("default").List(metav1.ListOptions{})
color.Cyan("%-30s%-20s%-20s%-20s%-20s\n", "Engine Name", "Application Name", "Ready", "Instance", "Domain")
for i := 0; i < len(appLst.Items); i++ {
app := appLst.Items[i]
fmt.Printf("%-30s%-20s%-20s%-20s%-20s\n", app.Name, app.Spec.AppName, app.Status.Ready, fmt.Sprint(app.Status.Instance)+"/"+fmt.Sprint(app.Spec.Size), app.Status.Domain)
}
return c.Render(http.StatusOK, "services.html", appLst.Items)
}
func CreateNew(c echo.Context) error {
r := c.Request()
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
knapClient, err := knapclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building knap clientset: %v", err)
}
size, err := strconv.ParseInt(r.FormValue("size"), 10, 32)
size32 := int32(size)
if err != nil {
//glog.Fatalf("Error creating application engine: %s", args[0])
fmt.Println("Error parsing size parameter", err)
}
app := &knapv1.Appengine{
ObjectMeta: metav1.ObjectMeta{
Name: r.FormValue("appName") + "-appengine",
Namespace: r.FormValue("namespace"),
},
Spec: knapv1.AppengineSpec{
AppName: r.FormValue("appName"),
GitRepo: r.FormValue("gitRepo"),
GitRevision: r.FormValue("gitRevision"),
// GitWatch: r.FormValue("gitWatch"),
Size: size32,
PipelineTemplate: r.FormValue("template"),
},
}
_, err = knapClient.KnapV1alpha1().Appengines("default").Create(app)
if err != nil {
//glog.Fatalf("Error creating application engine: %s", args[0])
fmt.Println("Error creating application engine", r.FormValue("appName"), err)
} else {
fmt.Println("Application engine", r.FormValue("appName"), "is created successfully")
}
return c.Render(http.StatusOK, "createDone.html", map[string]interface{}{
"name": r.FormValue("appName"),
})
}
func Edit(c echo.Context) error {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
knapClient, err := knapclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building knap clientset: %v", err)
}
app, err := knapClient.KnapV1alpha1().Appengines("default").Get(c.Request().FormValue("name"), metav1.GetOptions{})
if err != nil {
glog.Fatalf("Error getting appengine: %v", c.Request().FormValue("name"))
}
fmt.Print("Get appengine", "appengine name", app.Name)
return c.Render(http.StatusOK, "edit.html", app)
}
func GetEdit(c echo.Context) error {
r := c.Request()
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
knapClient, err := knapclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building knap clientset: %v", err)
}
app, err := knapClient.KnapV1alpha1().Appengines("default").Get(r.FormValue("appName")+"-appengine", metav1.GetOptions{})
if err != nil {
glog.Fatalf("Error getting appengine: %v", r.FormValue("appName")+"-appengine")
}
size, err := strconv.ParseInt(r.FormValue("size"), 10, 32)
size32 := int32(size)
if err != nil {
//glog.Fatalf("Error creating application engine: %s", args[0])
fmt.Println("Error parsing size parameter", err)
}
app.Spec.GitRevision = r.FormValue("gitRevision")
//app.Spec.GitWatch = r.FormValue("gitWatch")
app.Spec.Size = size32
app.Spec.PipelineTemplate = r.FormValue("template")
_, err = knapClient.KnapV1alpha1().Appengines("default").Update(app)
if err != nil {
//glog.Fatalf("Error creating application engine: %s", args[0])
fmt.Println("Error updating application engine", r.FormValue("appName"), err)
} else {
fmt.Println("Application engine", r.FormValue("appName"), "is updated successfully")
}
return c.Render(http.StatusOK, "editDone.html", map[string]interface{}{
"name": r.FormValue("appName"),
})
}
func Logs(c echo.Context) error {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
knapClient, err := knapclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building knap clientset: %v", err)
}
app, err := knapClient.KnapV1alpha1().Appengines("default").Get(c.Request().FormValue("name"), metav1.GetOptions{})
if err != nil {
glog.Fatalf("Error getting appengine: %v", c.Request().FormValue("name"))
}
fmt.Print("Get appengine", "appengine name", app.Name)
return c.Render(http.StatusOK, "logs.html", app)
}