-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.go
305 lines (260 loc) · 10.3 KB
/
server.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
package main
import (
"errors"
"fmt"
api "k8-api/api"
apply "k8-api/apply"
"k8-api/install"
"net/http"
"runtime"
"time"
"github.com/distribution/distribution/v3/uuid"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/sirupsen/logrus"
"github.com/unrolled/secure"
)
func timeoutMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
start := time.Now()
err := next(c)
duration := time.Since(start)
if err != nil {
return err
}
if duration > 6*time.Second {
return echo.NewHTTPError(http.StatusRequestTimeout, "Request timed out")
}
return nil
}
}
func retryMax(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
for i := 0; i < 5; i++ {
err := next(c)
if err == nil {
return nil
}
time.Sleep(1 * time.Second)
}
return errors.New("Tried Multiple times, but failed. Time to restart the server!")
}
}
func main() {
e := echo.New()
// Setting up Logging
log := logrus.New()
//making the logs in JSON format
log.SetReportCaller(true)
log.Formatter = &logrus.JSONFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", f.File, f.Line)
},
}
// Securing the API, customise as per your usage
// Add more options as per your need from Here: https://github.com/unrolled/secure#available-options
secureMiddleware := secure.New(secure.Options{
SSLRedirect: false,
// SSLHost : "localhost" Remove this if you are not using on localhost
})
// Middleware to secure the API
e.Use(echo.WrapMiddleware(secureMiddleware.Handler))
// Middleware to add UUID to each request, helps us to track the request in case of any error
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set("uuid", "kube-ez-"+uuid.Generate().String()[:8])
cc := c
return next(cc)
}
})
// Middleware to set the order of the log that is genererated
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: `{"level":"INFO","time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}",` +
`"host":"${host}","method":"${method}","uri":"${uri}","user_agent":"${user_agent}",` +
`"status":${status},"error":"${error}","latency":${latency},"latency_human":"${latency_human}"` +
`,"bytes_in":${bytes_in},"bytes_out":${bytes_out}}` + "\n",
CustomTimeFormat: "2006-01-02 15:04:05",
}))
// These two middlewares are used to handle the timeout and retry the request
e.Use(timeoutMiddleware, retryMax)
// Calling the Main fucntion that connects with the kubernetes cluster
api.Main()
//Middlewae to handle CORS
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
}))
// All the routes are described this point forward
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Yes! I am alive!\n")
})
e.GET("/pods", func(c echo.Context) error {
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get pods intitiated")
namespace := c.QueryParam("namespace")
containerDetails := c.QueryParam("containerDetails") == "True" || c.QueryParam("containerDetails") == "true"
return c.String(http.StatusOK, api.Pods(namespace, containerDetails, l))
})
e.GET("/namespace", func(c echo.Context) error {
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get Namespace intitiated")
return c.String(http.StatusOK, api.NameSpace(l))
})
e.GET("/deployments", func(c echo.Context) error {
namespace := c.QueryParam("namespace")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get Deployments intitiated")
return c.String(http.StatusOK, api.Deployments(namespace, l))
})
e.GET("/configmaps", func(c echo.Context) error {
namespace := c.QueryParam("namespace")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get Configmaps intitiated")
return c.String(http.StatusOK, api.Configmaps(namespace, l))
})
e.GET("/services", func(c echo.Context) error {
namespace := c.QueryParam("namespace")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get Services intitiated")
return c.String(http.StatusOK, api.Services(namespace, l))
})
e.GET("/events", func(c echo.Context) error {
namespace := c.QueryParam("namespace")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get Events intitiated")
return c.String(http.StatusOK, api.Events(namespace, l))
})
e.GET("/secrets", func(c echo.Context) error {
namespace := c.QueryParam("namespace")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get Secrets intitiated")
return c.String(http.StatusOK, api.Secrets(namespace, l))
})
e.GET("/replicationController", func(c echo.Context) error {
namespace := c.QueryParam("namespace")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get RepilicationControllers intitiated")
return c.String(http.StatusOK, api.ReplicationController(namespace, l))
})
e.GET("/daemonset", func(c echo.Context) error {
namespace := c.QueryParam("namespace")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get Daemaonsets intitiated")
return c.String(http.StatusOK, api.DaemonSet(namespace, l))
})
e.GET("/podLogs", func(c echo.Context) error {
namespace := c.QueryParam("namespace")
pod := c.QueryParam("pod")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get Pod's Logs intitiated")
return c.String(http.StatusOK, api.PodLogs(namespace, pod, l))
})
e.GET("/helmRepoUpdate", func(c echo.Context) error {
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Get Helm Repo updates intitiated")
return c.String(http.StatusOK, install.RepoUpdate(l))
})
e.POST("/helmRepoAdd", func(c echo.Context) error {
url := c.QueryParam("url")
repoName := c.QueryParam("repoName")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Adding Helm Repo intitiated")
return c.String(http.StatusOK, install.RepoAdd(repoName, url, l))
})
e.POST("/helmInstall", func(c echo.Context) error {
namespace := c.QueryParam("namespace")
chartName := c.QueryParam("chartName")
name := c.QueryParam("name")
repo := c.QueryParam("repo")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Adding Helm Install intitiated")
return c.String(http.StatusOK, install.InstallChart(namespace, chartName, name, repo, l))
})
e.POST("/createNamespace", func(c echo.Context) error {
namespace := c.FormValue("namespace")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Creating Namespace intitiated")
return c.String(http.StatusOK, api.CreateNamespace(namespace, l))
})
e.POST("/applyFile", func(c echo.Context) error {
filepath := c.FormValue("filepath")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Intiating File appliying")
return c.String(http.StatusOK, apply.Main(filepath, l))
})
e.DELETE("/deleteHelm", func(c echo.Context) error {
namespace := c.FormValue("namespace")
name := c.FormValue("name")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Delete Helm intitiated")
return c.String(http.StatusOK, install.DeleteChart(name, namespace, l))
})
e.DELETE("/deleteNamespace", func(c echo.Context) error {
namespace := c.FormValue("namespace")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Deleting Namespace intitiated")
return c.String(http.StatusOK, api.DeleteNamespace(namespace, l))
})
e.DELETE("/deleteDeployment", func(c echo.Context) error {
namespace := c.FormValue("namespace")
deployment := c.FormValue("deployment")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Delete Deployment intitiated")
return c.String(http.StatusOK, api.DeleteDeployment(namespace, deployment, l))
})
e.DELETE("/deleteService", func(c echo.Context) error {
namespace := c.FormValue("namespace")
service := c.FormValue("service")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Delete Service intitiated")
return c.String(http.StatusOK, api.DeleteService(namespace, service, l))
})
e.DELETE("/deleteConfigMap", func(c echo.Context) error {
namespace := c.FormValue("namespace")
configMap := c.FormValue("configMap")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Delete Configmap intitiated")
return c.String(http.StatusOK, api.DeleteConfigMap(namespace, configMap, l))
})
e.DELETE("/deleteSecret", func(c echo.Context) error {
namespace := c.FormValue("namespace")
secret := c.FormValue("secret")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Delete Secret intitiated")
return c.String(http.StatusOK, api.DeleteSecret(namespace, secret, l))
})
e.DELETE("/deleteReplicationController", func(c echo.Context) error {
namespace := c.FormValue("namespace")
replicationController := c.FormValue("replicationController")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Delete ReplicationControlller intitiated")
return c.String(http.StatusOK, api.DeleteReplicationController(namespace, replicationController, l))
})
e.DELETE("/deleteDaemonSet", func(c echo.Context) error {
namespace := c.FormValue("namespace")
daemonSet := c.FormValue("daemonSet")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Delete Daemonset intitiated")
return c.String(http.StatusOK, api.DeleteDaemonSet(namespace, daemonSet, l))
})
e.DELETE("/deletePod", func(c echo.Context) error {
namespace := c.FormValue("namespace")
pod := c.FormValue("pod")
return c.String(http.StatusOK, api.DeletePod(namespace, pod))
})
e.DELETE("/deleteEvent", func(c echo.Context) error {
namespace := c.FormValue("namespace")
event := c.FormValue("event")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Delete Event intitiated")
return c.String(http.StatusOK, api.DeleteEvent(namespace, event, l))
})
e.DELETE("/deleteAll", func(c echo.Context) error {
namespace := c.FormValue("namespace")
l := log.WithFields(logrus.Fields{"uuid": c.Get("uuid")})
l.Info("Delete All intitiated")
return c.String(http.StatusOK, api.DeleteAll(namespace, l))
})
// Run Server
e.Logger.Fatal(e.Start(":8000"))
}