-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.go
151 lines (132 loc) · 5.26 KB
/
router.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
package router
import (
"net/http"
"git.containerum.net/ch/kube-api/pkg/kubeerrors"
"git.containerum.net/ch/kube-api/pkg/kubernetes"
h "git.containerum.net/ch/kube-api/pkg/router/handlers"
m "git.containerum.net/ch/kube-api/pkg/router/midlleware"
"git.containerum.net/ch/kube-api/static"
"github.com/containerum/cherry/adaptors/cherrylog"
"github.com/containerum/cherry/adaptors/gonic"
"github.com/gin-contrib/cors"
"github.com/sirupsen/logrus"
"time"
"github.com/containerum/kube-client/pkg/model"
"github.com/containerum/utils/httputil"
"github.com/gin-gonic/contrib/ginrus"
"github.com/gin-gonic/gin"
)
func CreateRouter(kube *kubernetes.Kube, status *model.ServiceStatus, enableCORS bool) http.Handler {
e := gin.New()
e.GET("/status", httputil.ServiceStatus(status))
initMiddlewares(e, kube)
initRoutes(e, status, enableCORS)
return e
}
func initMiddlewares(e gin.IRouter, kube *kubernetes.Kube) {
/* System */
e.Use(ginrus.Ginrus(logrus.WithField("component", "gin"), time.RFC3339, true))
e.Use(gonic.Recovery(kubeerrors.ErrInternalError, cherrylog.NewLogrusAdapter(logrus.WithField("component", "gin"))))
/* Custom */
e.Use(httputil.SaveHeaders)
e.Use(httputil.PrepareContext)
e.Use(m.RequiredUserHeaders())
e.Use(m.RegisterKubeClient(kube))
}
func initRoutes(e gin.IRouter, status *model.ServiceStatus, enableCORS bool) {
if enableCORS {
cfg := cors.DefaultConfig()
cfg.AllowAllOrigins = true
cfg.AddAllowMethods(http.MethodDelete)
cfg.AddAllowHeaders(httputil.UserRoleXHeader, httputil.UserIDXHeader, httputil.UserNamespacesXHeader)
e.Use(cors.New(cfg))
}
e.Group("/static").
StaticFS("/", static.HTTP)
e.GET("/ingresses", h.GetSelectedIngresses)
e.GET("/configmaps", h.GetSelectedConfigMaps)
e.GET("/storage", h.GetStorageList)
namespace := e.Group("/namespaces")
{
namespace.GET("", h.GetNamespaceList)
namespace.GET("/:namespace", m.ReadAccess, h.GetNamespace)
namespace.POST("", h.CreateNamespace)
namespace.PUT("/:namespace", h.UpdateNamespace)
namespace.DELETE("/:namespace", h.DeleteNamespace)
namespace.DELETE("", h.DeleteUserNamespaces)
solutions := namespace.Group("/:namespace/solutions")
{
solutions.GET("/:solution/deployments", m.ReadAccess, h.GetDeploymentSolutionList)
solutions.GET("/:solution/services", m.ReadAccess, h.GetServiceSolutionList)
solutions.DELETE("/:solution/deployments", m.WriteAccess, h.DeleteDeploymentsSolution)
solutions.DELETE("/:solution/services", m.WriteAccess, h.DeleteServicesSolution)
}
service := namespace.Group("/:namespace/services")
{
service.GET("", m.ReadAccess, h.GetServiceList)
service.GET("/:service", m.ReadAccess, h.GetService)
service.POST("", h.CreateService)
service.PUT("/:service", h.UpdateService)
service.DELETE("/:service", h.DeleteService)
}
deployment := namespace.Group("/:namespace/deployments")
{
deployment.GET("", m.ReadAccess, h.GetDeploymentList)
deployment.GET("/:deployment", m.ReadAccess, h.GetDeployment)
deployment.GET("/:deployment/pods", m.ReadAccess, h.GetDeploymentPodList)
deployment.POST("", h.CreateDeployment)
deployment.PUT("/:deployment", h.UpdateDeployment)
deployment.PUT("/:deployment/replicas", h.UpdateDeploymentReplicas)
deployment.PUT("/:deployment/image", h.UpdateDeploymentImage)
deployment.DELETE("/:deployment", h.DeleteDeployment)
}
secret := namespace.Group("/:namespace/secrets")
{
secret.GET("", m.ReadAccess, h.GetSecretList)
secret.GET("/:secret", m.ReadAccess, h.GetSecret)
secret.POST("/tls", m.WriteAccess, h.CreateTLSSecret)
secret.POST("/docker", m.WriteAccess, h.CreateDockerSecret)
secret.PUT("/:secret", m.WriteAccess, h.UpdateSecret)
secret.DELETE("/:secret", m.DeleteAccess, h.DeleteSecret)
}
ingress := namespace.Group("/:namespace/ingresses")
{
ingress.GET("", m.ReadAccess, h.GetIngressList)
ingress.GET("/:ingress", m.ReadAccess, h.GetIngress)
ingress.POST("", h.CreateIngress)
ingress.PUT("/:ingress", h.UpdateIngress)
ingress.DELETE("/:ingress", h.DeleteIngress)
}
endpoint := namespace.Group("/:namespace/endpoints", httputil.RequireAdminRole(kubeerrors.ErrAdminRequired))
{
endpoint.GET("", h.GetEndpointList)
endpoint.GET("/:endpoint", h.GetEndpoint)
endpoint.POST("", h.CreateEndpoint)
endpoint.PUT("/:endpoint", h.UpdateEndpoint)
endpoint.DELETE("/:endpoint", h.DeleteEndpoint)
}
configmap := namespace.Group("/:namespace/configmaps")
{
configmap.GET("", m.ReadAccess, h.GetConfigMapList)
configmap.GET("/:configmap", m.ReadAccess, h.GetConfigMap)
configmap.POST("", m.WriteAccess, h.CreateConfigMap)
configmap.PUT("/:configmap", m.WriteAccess, h.UpdateConfigMap)
configmap.DELETE("/:configmap", m.DeleteAccess, h.DeleteConfigMap)
}
volume := namespace.Group("/:namespace/volumes")
{
volume.GET("", m.ReadAccess, h.GetVolumeList)
volume.GET("/:volume", m.ReadAccess, h.GetVolume)
volume.POST("", m.WriteAccess, h.CreateVolume)
volume.PUT("/:volume", m.WriteAccess, h.UpdateVolume)
volume.DELETE("/:volume", m.DeleteAccess, h.DeleteVolume)
}
pod := namespace.Group("/:namespace/pods")
{
pod.GET("", m.ReadAccess, h.GetPodList)
pod.GET("/:pod", m.ReadAccess, h.GetPod)
pod.GET("/:pod/log", m.ReadAccess, h.GetPodLogs)
pod.DELETE("/:pod", m.DeleteAccess, h.DeletePod)
}
}
}