-
Notifications
You must be signed in to change notification settings - Fork 81
/
router.go
372 lines (317 loc) · 12 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package routers
import (
"context"
"fmt"
"net"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"golang.org/x/sync/errgroup"
"kubegems.io/kubegems/pkg/i18n"
"kubegems.io/kubegems/pkg/log"
msgbus "kubegems.io/kubegems/pkg/msgbus/client"
"kubegems.io/kubegems/pkg/service/aaa"
"kubegems.io/kubegems/pkg/service/aaa/audit"
"kubegems.io/kubegems/pkg/service/aaa/auth"
"kubegems.io/kubegems/pkg/service/aaa/authorization"
"kubegems.io/kubegems/pkg/service/apis"
"kubegems.io/kubegems/pkg/service/handlers"
alerthandler "kubegems.io/kubegems/pkg/service/handlers/alerts"
announcement "kubegems.io/kubegems/pkg/service/handlers/announcement"
applicationhandler "kubegems.io/kubegems/pkg/service/handlers/application"
approveHandler "kubegems.io/kubegems/pkg/service/handlers/approve"
appstorehandler "kubegems.io/kubegems/pkg/service/handlers/appstore"
auditloghandler "kubegems.io/kubegems/pkg/service/handlers/auditlog"
authsource "kubegems.io/kubegems/pkg/service/handlers/authsource"
"kubegems.io/kubegems/pkg/service/handlers/base"
clusterhandler "kubegems.io/kubegems/pkg/service/handlers/cluster"
environmenthandler "kubegems.io/kubegems/pkg/service/handlers/environment"
eventhandler "kubegems.io/kubegems/pkg/service/handlers/event"
loginhandler "kubegems.io/kubegems/pkg/service/handlers/login"
logoperatorhandler "kubegems.io/kubegems/pkg/service/handlers/logoperator"
logqueryhandler "kubegems.io/kubegems/pkg/service/handlers/logquery"
lokiloghandler "kubegems.io/kubegems/pkg/service/handlers/lokilog"
messagehandler "kubegems.io/kubegems/pkg/service/handlers/message"
microservice "kubegems.io/kubegems/pkg/service/handlers/microservice"
myinfohandler "kubegems.io/kubegems/pkg/service/handlers/myinfo"
noproxyhandler "kubegems.io/kubegems/pkg/service/handlers/noproxy"
"kubegems.io/kubegems/pkg/service/handlers/oauthserver"
"kubegems.io/kubegems/pkg/service/handlers/observability"
projecthandler "kubegems.io/kubegems/pkg/service/handlers/project"
proxyhandler "kubegems.io/kubegems/pkg/service/handlers/proxy"
registryhandler "kubegems.io/kubegems/pkg/service/handlers/registry"
sel "kubegems.io/kubegems/pkg/service/handlers/sels"
systemrolehandler "kubegems.io/kubegems/pkg/service/handlers/systemrole"
tenanthandler "kubegems.io/kubegems/pkg/service/handlers/tenant"
userhandler "kubegems.io/kubegems/pkg/service/handlers/users"
workloadreshandler "kubegems.io/kubegems/pkg/service/handlers/workloadres"
"kubegems.io/kubegems/pkg/service/models/cache"
"kubegems.io/kubegems/pkg/service/models/validate"
"kubegems.io/kubegems/pkg/service/options"
"kubegems.io/kubegems/pkg/utils/agents"
"kubegems.io/kubegems/pkg/utils/argo"
"kubegems.io/kubegems/pkg/utils/database"
"kubegems.io/kubegems/pkg/utils/git"
"kubegems.io/kubegems/pkg/utils/prometheus/exporter"
"kubegems.io/kubegems/pkg/utils/redis"
"kubegems.io/kubegems/pkg/utils/tracing"
"kubegems.io/kubegems/pkg/version"
)
func getClientIP(c *gin.Context) string {
forwardHeader := c.Request.Header.Get("x-forwarded-for")
if len(forwardHeader) > 0 {
firstAddress := strings.Split(forwardHeader, ",")[0]
if net.ParseIP(strings.TrimSpace(firstAddress)) != nil {
return firstAddress
}
}
return c.ClientIP()
}
func RealClientIPMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
_, port, _ := net.SplitHostPort(strings.TrimSpace(ctx.Request.RemoteAddr))
ip := getClientIP(ctx)
ctx.Request.RemoteAddr = fmt.Sprintf("%s:%s", ip, port)
ctx.Next()
}
}
type Router struct {
Opts *options.Options
Agents *agents.ClientSet
Database *database.Database
Redis *redis.Client
Argo *argo.Client
GitProvider *git.SimpleLocalProvider
auditInstance *audit.DefaultAuditInstance
gin *gin.Engine
}
func (r *Router) Run(ctx context.Context) error {
if err := r.Complete(ctx); err != nil {
return err
}
httpserver := &http.Server{
Addr: r.Opts.System.Listen,
Handler: r.gin,
BaseContext: func(l net.Listener) context.Context {
return ctx // 注入basecontext
},
}
// run
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
log.FromContextOrDiscard(ctx).Info("start listen", "addr", httpserver.Addr)
go func() {
<-ctx.Done()
httpserver.Close()
}()
return httpserver.ListenAndServe()
})
eg.Go(func() error {
return r.auditInstance.Consumer(ctx)
})
return eg.Wait()
}
// nolint: funlen
func (r *Router) Complete(ctx context.Context) error {
// validator
validate.InitValidator(r.Database.DB())
// oauth
// oauthtool := oauth.NewOauthTool(r.OnlineOptions.Oauth)
// user interface
userif := aaa.NewUserInfoHandler()
// cache
cache := &cache.ModelCache{DB: r.Database.DB(), Redis: r.Redis}
if err := cache.BuildCacheIfNotExist(); err != nil {
return err
}
// audit
r.auditInstance = audit.NewAuditMiddleware(r.Database.DB(), cache, userif)
// base handler
basehandler := base.NewHandler(
r.auditInstance,
&authorization.DefaultPermissionManager{Cache: cache, Userif: userif},
userif,
r.Agents,
r.Database,
r.Redis,
msgbus.NewMessageBusClient(r.Database, r.Opts.Msgbus),
cache,
)
// init gin
if !r.Opts.DebugMode {
gin.SetMode(gin.ReleaseMode)
}
r.gin = gin.New()
router := r.gin
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("/swagger/doc.json")))
dir, _ := os.Getwd()
router.StaticFS("/lokiExport", http.Dir(dir+"/lokiExport"))
// authMiddleware, err := auth.NewAuthMiddleware(r.Opts.JWT, r.Database, r.Redis, aaa.NewUserInfoHandler())
// if err != nil {
// return err
// }
globalMiddlewares := []func(*gin.Context){
i18n.SetLang,
// prometheus request metrics
exporter.GetRequestCollector().HandlerFunc(),
// logger
log.DefaultGinLoggerMideare(),
// panic recovery
gin.Recovery(),
// http tracing
tracing.GinMiddleware(),
// real ip tracking
RealClientIPMiddleware(),
}
for _, middleware := range globalMiddlewares {
router.Use(middleware)
}
router.GET("/healthz", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": "healthy"}) })
router.GET("/version", func(c *gin.Context) { c.JSON(http.StatusOK, version.Get()) })
router.GET("/v1/version", func(c *gin.Context) { handlers.OK(c, version.Get()) })
// inner go-restful router no auth required
if err := r.AddRestAPI(ctx, apis.Dependencies{
Opts: r.Opts,
Agents: r.Agents,
Database: r.Database,
Gitp: r.GitProvider,
Argo: r.Argo,
Redis: r.Redis,
}); err != nil {
log.Errorf("add new restful error: %v", err)
return err
}
// 登录和认证相关
oauth := loginhandler.OAuthHandler{
DB: r.Database.DB(),
AuthModule: *auth.NewAuthenticateModule(r.Database.DB()),
JWTOptions: r.Opts.JWT,
}
router.POST("/v1/login", oauth.LoginHandler)
router.GET("/v1/oauth/addr", oauth.GetOauthAddr)
router.GET("/v1/oauth/callback", oauth.GetOauthToken)
oauthserver := oauthserver.NewOauthServer(r.Opts.JWT, basehandler)
router.GET("/v1/oauth/validate", oauthserver.Validate)
authSourceHandler := authsource.AuthSourceHandler{BaseHandler: basehandler}
router.GET("/v1/system/authsource", authSourceHandler.ListAuthSourceSimple)
router.GET("/v1/system/authsource/predefined", authSourceHandler.GetAuthSourcePredifinedVar)
rg := router.Group("v1")
// 注册中间件
apiMidwares := []func(*gin.Context){
// authc
auth.NewAuthMiddleware(r.Opts.JWT, userif).FilterFunc,
// audit
r.auditInstance.Middleware(),
}
for _, mw := range apiMidwares {
rg.Use(mw)
}
oauthserver.RegistRouter(rg)
// 用户
userHandler := &userhandler.UserHandler{BaseHandler: basehandler}
userHandler.RegistRouter(rg)
// 系统角色
systemroleHandler := &systemrolehandler.SystemRoleHandler{BaseHandler: basehandler}
systemroleHandler.RegistRouter(rg)
// 集群
clusterHandler := &clusterhandler.ClusterHandler{
BaseHandler: basehandler,
}
clusterHandler.RegistRouter(rg)
// 审计
auditlogHandler := &auditloghandler.AuditLogHandler{BaseHandler: basehandler}
auditlogHandler.RegistRouter(rg)
// 租户
tenantHandler := &tenanthandler.TenantHandler{BaseHandler: basehandler}
tenantHandler.RegistRouter(rg)
// 项目
projectHandler := &projecthandler.ProjectHandler{BaseHandler: basehandler}
projectHandler.RegistRouter(rg)
// 消息
messageHandler := &messagehandler.MessageHandler{BaseHandler: basehandler}
messageHandler.RegistRouter(rg)
// 消息
approveHandler := &approveHandler.ApproveHandler{BaseHandler: basehandler}
approveHandler.RegistRouter(rg)
// 日志
lokilogHandler := &lokiloghandler.LogHandler{BaseHandler: basehandler}
lokilogHandler.RegistRouter(rg)
// 事件
eventHandler := &eventhandler.EventHandler{LogHandler: lokilogHandler}
eventHandler.RegistRouter(rg)
// 告警规则
alertRuleHandler := &alerthandler.AlertsHandler{
BaseHandler: basehandler,
}
alertRuleHandler.RegistRouter(rg)
// 环境
environmentHandler := &environmenthandler.EnvironmentHandler{BaseHandler: basehandler}
environmentHandler.RegistRouter(rg)
// 当前个人信息
myHandler := &myinfohandler.MyHandler{BaseHandler: basehandler}
myHandler.RegistRouter(rg)
// 应用商店
appstoreHandler := &appstorehandler.AppstoreHandler{BaseHandler: basehandler, AppStoreOpt: r.Opts.Appstore}
appstoreHandler.RegistRouter(rg)
// 镜像仓库
registryHandler := ®istryhandler.RegistryHandler{BaseHandler: basehandler}
registryHandler.RegistRouter(rg)
// 日志查询历史
logqueryhistoryHandler := &logqueryhandler.LogQueryHistoryHandler{BaseHandler: basehandler}
logqueryhistoryHandler.RegistRouter(rg)
// 日志查询快照
logquerysnapshotHandler := &logqueryhandler.LogQuerySnapshotHandler{BaseHandler: basehandler}
logquerysnapshotHandler.RegistRouter(rg)
// 非反向代理资源[HPA]
hpaHandler := &noproxyhandler.HpaHandler{BaseHandler: basehandler}
hpaHandler.RegistRouter(rg)
// 非反向代理资源[PVC]
pvcHandler := &noproxyhandler.PersistentVolumeClaimHandler{BaseHandler: basehandler}
pvcHandler.RegistRouter(rg)
// 非反向代理资源[卷快照]
volumeSnapshotHandler := &noproxyhandler.VolumeSnapshotHandler{BaseHandler: basehandler}
volumeSnapshotHandler.RegistRouter(rg)
// 资源统计相关
workloadHandler := &workloadreshandler.WorkloadHandler{BaseHandler: basehandler}
workloadHandler.RegistRouter(rg)
// sels
selHandler := &sel.SelsHandler{BaseHandler: basehandler}
selHandler.RegistRouter(rg)
// app handler
appHandler := applicationhandler.MustNewApplicationDeployHandler(r.Opts.Git, r.Argo, basehandler)
appHandler.RegistRouter(rg)
// authsource
authSourceHandler.RegistRouter(rg)
// microservice handler
// TODO: kiali在每个集群配置可能不相同,先写死,后面看要不要支持配置
microservicehandler := microservice.NewMicroServiceHandler(basehandler, r.Opts.Microservice)
microservicehandler.RegistRouter(rg)
// logoperator handler
logoperatorHandler := &logoperatorhandler.LogOperatorHandler{BaseHandler: basehandler}
logoperatorHandler.RegistRouter(rg)
// observability handler
(&observability.ObservabilityHandler{BaseHandler: basehandler, AppStoreOpt: r.Opts.Appstore}).RegistRouter(rg)
(&announcement.AnnouncementHandler{BaseHandler: basehandler}).RegistRouter(rg)
// workload 的反向代理
proxyHandler := proxyhandler.ProxyHandler{BaseHandler: basehandler}
rg.Any("/proxy/cluster/:cluster/*action", proxyHandler.Proxy)
router.Any("/v1/service-proxy/cluster/:cluster/namespace/:namespace/service/:service/port/:port/*action", proxyHandler.ProxyService)
registPlugins(rg, basehandler)
return nil
}