From 486e61755a37929d08e1f70b3f611d3c342912d5 Mon Sep 17 00:00:00 2001 From: Min Min Date: Fri, 29 Jul 2022 16:29:02 +0800 Subject: [PATCH 1/8] config merged into aslan Signed-off-by: Min Min --- Makefile | 2 +- cmd/config/main.go | 50 ------------------- pkg/config/config.go | 9 ---- .../core/common/service/template_product.go | 2 +- .../aslan/core/project/service/product.go | 2 +- pkg/microservice/aslan/server/rest/router.go | 16 ++++++ pkg/setting/types.go | 7 +-- pkg/shared/client/systemconfig/client.go | 2 +- 8 files changed, 21 insertions(+), 69 deletions(-) delete mode 100644 cmd/config/main.go diff --git a/Makefile b/Makefile index 0341b1bb..3f823cee 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ IMAGE_REPOSITORY = koderover.tencentcloudcr.com/koderover-public VERSION ?= $(shell date +'%Y%m%d%H%M%S') VERSION := $(VERSION) -TARGETS = aslan config cron hub-agent hub-server init jenkins-plugin packager-plugin picket podexec policy predator-plugin ua user warpdrive +TARGETS = aslan cron hub-agent hub-server init jenkins-plugin packager-plugin picket podexec policy predator-plugin ua user warpdrive REAPER_OS= focal xenial bionic ALL_IMAGES=$(TARGETS:=.image) diff --git a/cmd/config/main.go b/cmd/config/main.go deleted file mode 100644 index 3df10041..00000000 --- a/cmd/config/main.go +++ /dev/null @@ -1,50 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 main - -import ( - "context" - "flag" - "log" - "os/signal" - "syscall" - - "github.com/spf13/pflag" - "github.com/spf13/viper" - - "github.com/koderover/zadig/pkg/microservice/systemconfig/config" - "github.com/koderover/zadig/pkg/microservice/systemconfig/server" -) - -func main() { - - flag.String(config.FeatureFlag, "", "turn a set of features on or off") - - pflag.CommandLine.AddGoFlagSet(flag.CommandLine) - pflag.Parse() - viper.BindPFlags(pflag.CommandLine) - - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) - go func() { - <-ctx.Done() - stop() - }() - - if err := server.Serve(ctx); err != nil { - log.Fatal(err) - } -} diff --git a/pkg/config/config.go b/pkg/config/config.go index c6123172..8a782d4e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -142,15 +142,6 @@ func CollieServiceAddress() string { return GetServiceAddress(s.Name, s.Port) } -func ConfigServiceInfo() *setting.ServiceInfo { - return GetServiceByCode(setting.Config) -} - -func ConfigServiceAddress() string { - s := ConfigServiceInfo() - return GetServiceAddress(s.Name, s.Port) -} - func WarpDriveServiceInfo() *setting.ServiceInfo { return GetServiceByCode(setting.WarpDrive) } diff --git a/pkg/microservice/aslan/core/common/service/template_product.go b/pkg/microservice/aslan/core/common/service/template_product.go index 5546a651..163f8b12 100644 --- a/pkg/microservice/aslan/core/common/service/template_product.go +++ b/pkg/microservice/aslan/core/common/service/template_product.go @@ -52,7 +52,7 @@ func GetProductTemplate(productName string, log *zap.SugaredLogger) (*template.P } totalFreeStyles := make([]*collie.CiPipelineResource, 0) - cl := configclient.New(configbase.ConfigServiceAddress()) + cl := configclient.New(configbase.AslanServiceAddress()) if enable, err := cl.CheckFeature(setting.ModernWorkflowType); err == nil && enable { // CI场景onboarding流程处于第二步时,需要返回ci工作流id,用于前端跳转 collieAPIAddress := config.CollieAPIAddress() diff --git a/pkg/microservice/aslan/core/project/service/product.go b/pkg/microservice/aslan/core/project/service/product.go index 91558373..295f280c 100644 --- a/pkg/microservice/aslan/core/project/service/product.go +++ b/pkg/microservice/aslan/core/project/service/product.go @@ -434,7 +434,7 @@ func DeleteProductTemplate(userName, productName, requestID string, isDelete boo } // Delete freestyle workflow - cl := configclient.New(configbase.ConfigServiceAddress()) + cl := configclient.New(configbase.AslanServiceAddress()) if enable, err := cl.CheckFeature(setting.ModernWorkflowType); err == nil && enable { collieClient := collie.New(config.CollieAPIAddress()) if err = collieClient.DeleteCIPipelines(productName, log); err != nil { diff --git a/pkg/microservice/aslan/server/rest/router.go b/pkg/microservice/aslan/server/rest/router.go index 8b1d62c3..f68fdf43 100644 --- a/pkg/microservice/aslan/server/rest/router.go +++ b/pkg/microservice/aslan/server/rest/router.go @@ -39,6 +39,10 @@ import ( templatehandler "github.com/koderover/zadig/pkg/microservice/aslan/core/templatestore/handler" workflowhandler "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/handler" testinghandler "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/testing/handler" + connectorHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/connector/handler" + emailHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/email/handler" + featuresHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/handler" + jiraHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/jira/handler" // Note: have to load docs for swagger to work. See https://blog.csdn.net/weixin_43249914/article/details/103035711 _ "github.com/koderover/zadig/pkg/microservice/aslan/server/rest/doc" @@ -72,6 +76,7 @@ func (s *engine) injectRouterGroup(router *gin.RouterGroup) { router.GET("/api/kodespace/downloadUrl", commonhandler.GetToolDownloadURL) + // inject aslan related APIs for name, r := range map[string]injector{ "/api/project": new(projecthandler.Router), "/api/code": new(codehosthandler.Router), @@ -94,6 +99,17 @@ func (s *engine) injectRouterGroup(router *gin.RouterGroup) { r.Inject(router.Group(name)) } + // inject config related APIs + for _, r := range []injector{ + new(connectorHandler.Router), + new(emailHandler.Router), + new(jiraHandler.Router), + new(codehosthandler.Router), + new(featuresHandler.Router), + } { + r.Inject(router.Group("/api/v1")) + } + router.GET("/api/apidocs/*any", ginswagger.WrapHandler(swaggerfiles.Handler)) } diff --git a/pkg/setting/types.go b/pkg/setting/types.go index 779f3c11..52f9035b 100644 --- a/pkg/setting/types.go +++ b/pkg/setting/types.go @@ -103,8 +103,7 @@ const ( Minio // 11 OPA // 12 Policy // 13 - Config // 14 - User // 15 + User // 14 ) type ServiceInfo struct { @@ -161,10 +160,6 @@ var Services = map[int]*ServiceInfo{ Name: "policy", Port: 80, }, - Config: { - Name: "config", - Port: 80, - }, User: { Name: "user", Port: 80, diff --git a/pkg/shared/client/systemconfig/client.go b/pkg/shared/client/systemconfig/client.go index cc9052c5..b0176a99 100644 --- a/pkg/shared/client/systemconfig/client.go +++ b/pkg/shared/client/systemconfig/client.go @@ -28,7 +28,7 @@ type Client struct { } func New() *Client { - host := config.ConfigServiceAddress() + host := config.AslanServiceAddress() c := httpclient.New( httpclient.SetHostURL(host + "/api/v1"), ) -- Gitee From 6616545236cc32b443c5178f9a341aaf5cd97b02 Mon Sep 17 00:00:00 2001 From: Min Min Date: Fri, 29 Jul 2022 16:39:05 +0800 Subject: [PATCH 2/8] remnants of config service removed Signed-off-by: Min Min --- .../systemconfig/server/rest/router.go | 43 ----------- .../systemconfig/server/rest/server.go | 76 ------------------- .../systemconfig/server/server.go | 60 --------------- 3 files changed, 179 deletions(-) delete mode 100644 pkg/microservice/systemconfig/server/rest/router.go delete mode 100644 pkg/microservice/systemconfig/server/rest/server.go delete mode 100644 pkg/microservice/systemconfig/server/server.go diff --git a/pkg/microservice/systemconfig/server/rest/router.go b/pkg/microservice/systemconfig/server/rest/router.go deleted file mode 100644 index affff629..00000000 --- a/pkg/microservice/systemconfig/server/rest/router.go +++ /dev/null @@ -1,43 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 rest - -import ( - "github.com/gin-gonic/gin" - - codehosthandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/codehost/handler" - connectorHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/connector/handler" - emailHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/email/handler" - featuresHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/handler" - jiraHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/jira/handler" -) - -func (s *engine) injectRouterGroup(router *gin.RouterGroup) { - for _, r := range []injector{ - new(connectorHandler.Router), - new(emailHandler.Router), - new(jiraHandler.Router), - new(codehosthandler.Router), - new(featuresHandler.Router), - } { - r.Inject(router.Group("/api/v1")) - } -} - -type injector interface { - Inject(router *gin.RouterGroup) -} diff --git a/pkg/microservice/systemconfig/server/rest/server.go b/pkg/microservice/systemconfig/server/rest/server.go deleted file mode 100644 index 18843b0a..00000000 --- a/pkg/microservice/systemconfig/server/rest/server.go +++ /dev/null @@ -1,76 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 rest - -import ( - "net/http" - - "github.com/gin-gonic/gin" - - "github.com/koderover/zadig/pkg/config" - ginmiddleware "github.com/koderover/zadig/pkg/middleware/gin" - "github.com/koderover/zadig/pkg/tool/log" -) - -type engine struct { - *gin.Engine - - mode string -} - -func NewEngine() *engine { - s := &engine{mode: config.Mode()} - - gin.SetMode(s.mode) - - s.injectMiddlewares() - s.injectRouters() - - return s -} - -func (s *engine) injectMiddlewares() { - g := gin.New() - defer func() { - s.Engine = g - }() - - if s.mode == gin.TestMode { - return - } - g.Use(ginmiddleware.Response()) - g.Use(ginmiddleware.RequestID()) - g.Use(ginmiddleware.RequestLog(log.NewFileLogger(config.RequestLogFile()))) - g.Use(gin.Recovery()) -} - -func (s *engine) injectRouters() { - g := s.Engine - - g.NoRoute(func(c *gin.Context) { - c.String(http.StatusNotFound, "Invalid path: %s", c.Request.URL.Path) - }) - g.HandleMethodNotAllowed = true - g.NoMethod(func(c *gin.Context) { - c.String(http.StatusMethodNotAllowed, "Method not allowed: %s %s", c.Request.Method, c.Request.URL.Path) - }) - - apiRouters := g.Group("") - s.injectRouterGroup(apiRouters) - - s.Engine = g -} diff --git a/pkg/microservice/systemconfig/server/server.go b/pkg/microservice/systemconfig/server/server.go deleted file mode 100644 index cd8efc7a..00000000 --- a/pkg/microservice/systemconfig/server/server.go +++ /dev/null @@ -1,60 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 server - -import ( - "context" - "net/http" - "time" - - "github.com/koderover/zadig/pkg/microservice/systemconfig/core" - "github.com/koderover/zadig/pkg/microservice/systemconfig/server/rest" - "github.com/koderover/zadig/pkg/tool/log" -) - -func Serve(ctx context.Context) error { - core.Start(ctx) - defer core.Stop(ctx) - - log.Info("Start system config service") - - engine := rest.NewEngine() - server := &http.Server{Addr: ":80", Handler: engine} - - stopChan := make(chan struct{}) - go func() { - defer close(stopChan) - - <-ctx.Done() - - ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) - defer cancel() - - if err := server.Shutdown(ctx); err != nil { - log.Errorf("Failed to stop server, error: %s", err) - } - }() - - if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { - log.Errorf("Failed to start http server, error: %s", err) - return err - } - - <-stopChan - - return nil -} -- Gitee From 3b72a34e0314857f8dcfbf4b0ba4dd4e199c8a48 Mon Sep 17 00:00:00 2001 From: Min Min Date: Fri, 29 Jul 2022 17:31:02 +0800 Subject: [PATCH 3/8] config service initialization process merged into aslan Signed-off-by: Min Min --- pkg/microservice/aslan/config/config.go | 8 ++ pkg/microservice/aslan/core/service.go | 38 ++++++++ pkg/microservice/aslan/server/rest/router.go | 1 - pkg/microservice/systemconfig/core/service.go | 97 ------------------- pkg/setting/consts.go | 4 + 5 files changed, 50 insertions(+), 98 deletions(-) delete mode 100644 pkg/microservice/systemconfig/core/service.go diff --git a/pkg/microservice/aslan/config/config.go b/pkg/microservice/aslan/config/config.go index c2b14a77..a907da34 100644 --- a/pkg/microservice/aslan/config/config.go +++ b/pkg/microservice/aslan/config/config.go @@ -253,3 +253,11 @@ func ServiceAccountNameForUser(userID string) string { func DindImage() string { return viper.GetString(setting.DindImage) } + +func MysqlDexDB() string { + return viper.GetString(setting.ENVMysqlDexDB) +} + +func Features() string { + return viper.GetString(setting.FeatureFlag) +} diff --git a/pkg/microservice/aslan/core/service.go b/pkg/microservice/aslan/core/service.go index 37cd9584..49463190 100644 --- a/pkg/microservice/aslan/core/service.go +++ b/pkg/microservice/aslan/core/service.go @@ -23,10 +23,13 @@ import ( "time" "github.com/hashicorp/go-multierror" + "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/service" + gormtool "github.com/koderover/zadig/pkg/tool/gorm" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" commonconfig "github.com/koderover/zadig/pkg/config" + configbase "github.com/koderover/zadig/pkg/config" "github.com/koderover/zadig/pkg/microservice/aslan/config" modeMongodb "github.com/koderover/zadig/pkg/microservice/aslan/core/collaboration/repository/mongodb" commonrepo "github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/mongodb" @@ -41,6 +44,7 @@ import ( systemrepo "github.com/koderover/zadig/pkg/microservice/aslan/core/system/repository/mongodb" systemservice "github.com/koderover/zadig/pkg/microservice/aslan/core/system/service" workflowservice "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/service/workflow" + configmongodb "github.com/koderover/zadig/pkg/microservice/systemconfig/core/email/repository/mongodb" "github.com/koderover/zadig/pkg/setting" kubeclient "github.com/koderover/zadig/pkg/shared/kube/client" "github.com/koderover/zadig/pkg/tool/log" @@ -121,6 +125,9 @@ func Start(ctx context.Context) { initService() initDinD() + // old config service initialization, it didn't panic or stop if it fails, so I will just keep it that way. + InitializeConfigFeatureGates() + systemservice.SetProxyConfig() workflowservice.InitPipelineController() @@ -171,9 +178,19 @@ func initDinD() { } func initDatabase() { + err := gormtool.Open(configbase.MysqlUser(), + configbase.MysqlPassword(), + configbase.MysqlHost(), + config.MysqlDexDB(), + ) + if err != nil { + log.Panicf("Failed to open database %s", config.MysqlDexDB()) + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() + // mongodb initialization mongotool.Init(ctx, config.MongoURI()) if err := mongotool.Ping(ctx); err != nil { panic(fmt.Errorf("failed to connect to mongo, error: %s", err)) @@ -184,6 +201,7 @@ func initDatabase() { var wg sync.WaitGroup for _, r := range []indexer{ + // aslan related db index template.NewProductColl(), commonrepo.NewBasicImageColl(), commonrepo.NewBuildColl(), @@ -249,6 +267,9 @@ func initDatabase() { labelMongodb.NewLabelBindingColl(), modeMongodb.NewCollaborationModeColl(), modeMongodb.NewCollaborationInstanceColl(), + + // config related db index + configmongodb.NewEmailHostColl(), } { wg.Add(1) go func(r indexer) { @@ -275,3 +296,20 @@ type indexer interface { EnsureIndex(ctx context.Context) error GetCollectionName() string } + +// InitializeConfigFeatureGates initialize feature gates for the old config service module. +// Currently, the function of this part is unknown. But we will keep it just to make sure. +func InitializeConfigFeatureGates() error { + flagFG, err := service.FlagToFeatureGates(config.Features()) + if err != nil { + log.Errorf("FlagToFeatureGates err:%s", err) + return err + } + dbFG, err := service.DBToFeatureGates() + if err != nil { + log.Errorf("DBToFeatureGates err:%s", err) + return err + } + service.Features.MergeFeatureGates(flagFG, dbFG) + return nil +} diff --git a/pkg/microservice/aslan/server/rest/router.go b/pkg/microservice/aslan/server/rest/router.go index f68fdf43..1e01d4cb 100644 --- a/pkg/microservice/aslan/server/rest/router.go +++ b/pkg/microservice/aslan/server/rest/router.go @@ -43,7 +43,6 @@ import ( emailHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/email/handler" featuresHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/handler" jiraHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/jira/handler" - // Note: have to load docs for swagger to work. See https://blog.csdn.net/weixin_43249914/article/details/103035711 _ "github.com/koderover/zadig/pkg/microservice/aslan/server/rest/doc" ) diff --git a/pkg/microservice/systemconfig/core/service.go b/pkg/microservice/systemconfig/core/service.go deleted file mode 100644 index c5c485c7..00000000 --- a/pkg/microservice/systemconfig/core/service.go +++ /dev/null @@ -1,97 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 core - -import ( - "context" - "fmt" - "sync" - "time" - - configbase "github.com/koderover/zadig/pkg/config" - "github.com/koderover/zadig/pkg/microservice/systemconfig/config" - "github.com/koderover/zadig/pkg/microservice/systemconfig/core/email/repository/mongodb" - "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/service" - "github.com/koderover/zadig/pkg/setting" - gormtool "github.com/koderover/zadig/pkg/tool/gorm" - "github.com/koderover/zadig/pkg/tool/log" - mongotool "github.com/koderover/zadig/pkg/tool/mongo" -) - -func Start(_ context.Context) { - log.Init(&log.Config{ - Level: configbase.LogLevel(), - Filename: configbase.LogFile(), - SendToFile: configbase.SendLogToFile(), - Development: configbase.Mode() != setting.ReleaseMode, - }) - - initDatabase() - flagFG, err := service.FlagToFeatureGates(config.Features()) - if err != nil { - log.Errorf("FlagToFeatureGates err:%s", err) - } - dbFG, err := service.DBToFeatureGates() - if err != nil { - log.Errorf("DBToFeatureGates err:%s", err) - } - service.Features.MergeFeatureGates(flagFG, dbFG) -} - -func initDatabase() { - err := gormtool.Open(configbase.MysqlUser(), - configbase.MysqlPassword(), - configbase.MysqlHost(), - config.MysqlDexDB(), - ) - if err != nil { - log.Panicf("Failed to open database %s", config.MysqlDexDB()) - } - - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer cancel() - - mongotool.Init(ctx, config.MongoURI()) - if err := mongotool.Ping(ctx); err != nil { - panic(fmt.Errorf("failed to connect to mongo, error: %s", err)) - } - idxCtx, idxCancel := context.WithTimeout(ctx, 10*time.Minute) - defer idxCancel() - var wg sync.WaitGroup - for _, r := range []indexer{ - mongodb.NewEmailHostColl(), - } { - - wg.Add(1) - go func(r indexer) { - defer wg.Done() - if err := r.EnsureIndex(idxCtx); err != nil { - panic(fmt.Errorf("failed to create index for %s, error: %s", r.GetCollectionName(), err)) - } - }(r) - } - wg.Wait() -} - -type indexer interface { - EnsureIndex(ctx context.Context) error - GetCollectionName() string -} - -func Stop(_ context.Context) { - gormtool.Close() -} diff --git a/pkg/setting/consts.go b/pkg/setting/consts.go index 6c098839..d19bb057 100644 --- a/pkg/setting/consts.go +++ b/pkg/setting/consts.go @@ -122,6 +122,10 @@ const ( ENVTokenExpiresAt = "TOKEN_EXPIRES_AT" ENVUserPort = "USER_PORT" + // config + ENVMysqlDexDB = "MYSQL_DEX_DB" + FeatureFlag = "feature-gates" + // initconfig ENVAdminEmail = "ADMIN_EMAIL" ENVAdminPassword = "ADMIN_PASSWORD" -- Gitee From 3fea4f8d190652e4dfa63bc2e36c5c82226aac0d Mon Sep 17 00:00:00 2001 From: Min Min <37430175+jamsman94@users.noreply.github.com> Date: Wed, 3 Aug 2022 11:08:07 +0800 Subject: [PATCH 4/8] merge user into aslan (#1848) Signed-off-by: Min Min Co-authored-by: Min Min --- Makefile | 2 +- cmd/user/main.go | 38 ---------- pkg/cli/initconfig/cmd/healthz.go | 8 -- pkg/config/config.go | 9 --- pkg/microservice/aslan/config/config.go | 4 + .../service => aslan/core}/init/mysql.sql | 0 pkg/microservice/aslan/core/service.go | 43 +++++++++++ pkg/microservice/aslan/server/rest/router.go | 8 ++ pkg/microservice/user/core/handler/healthz.go | 30 -------- pkg/microservice/user/core/handler/router.go | 2 - pkg/microservice/user/core/service.go | 61 --------------- pkg/microservice/user/server/rest/router.go | 35 --------- pkg/microservice/user/server/rest/server.go | 76 ------------------- pkg/microservice/user/server/server.go | 60 --------------- pkg/setting/types.go | 5 -- pkg/shared/client/user/client.go | 2 +- 16 files changed, 57 insertions(+), 326 deletions(-) delete mode 100644 cmd/user/main.go rename pkg/microservice/{user/core/service => aslan/core}/init/mysql.sql (100%) delete mode 100644 pkg/microservice/user/core/handler/healthz.go delete mode 100644 pkg/microservice/user/server/rest/router.go delete mode 100644 pkg/microservice/user/server/rest/server.go delete mode 100644 pkg/microservice/user/server/server.go diff --git a/Makefile b/Makefile index 3f823cee..73ee7c7f 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ IMAGE_REPOSITORY = koderover.tencentcloudcr.com/koderover-public VERSION ?= $(shell date +'%Y%m%d%H%M%S') VERSION := $(VERSION) -TARGETS = aslan cron hub-agent hub-server init jenkins-plugin packager-plugin picket podexec policy predator-plugin ua user warpdrive +TARGETS = aslan cron hub-agent hub-server init jenkins-plugin packager-plugin picket podexec policy predator-plugin ua warpdrive REAPER_OS= focal xenial bionic ALL_IMAGES=$(TARGETS:=.image) diff --git a/cmd/user/main.go b/cmd/user/main.go deleted file mode 100644 index 2f8f7c69..00000000 --- a/cmd/user/main.go +++ /dev/null @@ -1,38 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 main - -import ( - "context" - "log" - "os/signal" - "syscall" - - "github.com/koderover/zadig/pkg/microservice/user/server" -) - -func main() { - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) - go func() { - <-ctx.Done() - stop() - }() - - if err := server.Serve(ctx); err != nil { - log.Fatal(err) - } -} diff --git a/pkg/cli/initconfig/cmd/healthz.go b/pkg/cli/initconfig/cmd/healthz.go index bdc4d036..8d0f7f70 100644 --- a/pkg/cli/initconfig/cmd/healthz.go +++ b/pkg/cli/initconfig/cmd/healthz.go @@ -22,13 +22,9 @@ import ( "github.com/koderover/zadig/pkg/config" "github.com/koderover/zadig/pkg/shared/client/aslan" "github.com/koderover/zadig/pkg/shared/client/policy" - "github.com/koderover/zadig/pkg/shared/client/user" ) func Healthz() error { - if err := checkUserServiceHealth(); err != nil { - return fmt.Errorf("checkUserServiceHealth error:%s", err) - } if err := checkPolicyServiceHealth(); err != nil { return fmt.Errorf("checkPolicyServiceHealth error:%s", err) } @@ -38,10 +34,6 @@ func Healthz() error { return nil } -func checkUserServiceHealth() error { - return user.New().Healthz() -} - func checkPolicyServiceHealth() error { return policy.NewDefault().Healthz() } diff --git a/pkg/config/config.go b/pkg/config/config.go index 8a782d4e..f0d6378f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -168,15 +168,6 @@ func PolicyServiceAddress() string { return GetServiceAddress(s.Name, s.Port) } -func UserServiceInfo() *setting.ServiceInfo { - return GetServiceByCode(setting.User) -} - -func UserServiceAddress() string { - s := UserServiceInfo() - return GetServiceAddress(s.Name, s.Port) -} - func GetServiceAddress(name string, port int32) string { return fmt.Sprintf("http://%s:%d", name, port) } diff --git a/pkg/microservice/aslan/config/config.go b/pkg/microservice/aslan/config/config.go index a907da34..855ed2a8 100644 --- a/pkg/microservice/aslan/config/config.go +++ b/pkg/microservice/aslan/config/config.go @@ -261,3 +261,7 @@ func MysqlDexDB() string { func Features() string { return viper.GetString(setting.FeatureFlag) } + +func MysqlUserDB() string { + return viper.GetString(setting.ENVMysqlUserDB) +} diff --git a/pkg/microservice/user/core/service/init/mysql.sql b/pkg/microservice/aslan/core/init/mysql.sql similarity index 100% rename from pkg/microservice/user/core/service/init/mysql.sql rename to pkg/microservice/aslan/core/init/mysql.sql diff --git a/pkg/microservice/aslan/core/service.go b/pkg/microservice/aslan/core/service.go index 49463190..5300aeae 100644 --- a/pkg/microservice/aslan/core/service.go +++ b/pkg/microservice/aslan/core/service.go @@ -18,10 +18,13 @@ package core import ( "context" + "database/sql" + _ "embed" "fmt" "sync" "time" + _ "github.com/go-sql-driver/mysql" "github.com/hashicorp/go-multierror" "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/service" gormtool "github.com/koderover/zadig/pkg/tool/gorm" @@ -45,6 +48,7 @@ import ( systemservice "github.com/koderover/zadig/pkg/microservice/aslan/core/system/service" workflowservice "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/service/workflow" configmongodb "github.com/koderover/zadig/pkg/microservice/systemconfig/core/email/repository/mongodb" + userCore "github.com/koderover/zadig/pkg/microservice/user/core" "github.com/koderover/zadig/pkg/setting" kubeclient "github.com/koderover/zadig/pkg/shared/kube/client" "github.com/koderover/zadig/pkg/tool/log" @@ -128,6 +132,10 @@ func Start(ctx context.Context) { // old config service initialization, it didn't panic or stop if it fails, so I will just keep it that way. InitializeConfigFeatureGates() + // old user service initialization process cannot be skipped since the DB variable is in that package + // the db initialization process has been moved to the initDatabase function. + userCore.Start(context.TODO()) + systemservice.SetProxyConfig() workflowservice.InitPipelineController() @@ -151,6 +159,7 @@ func Start(ctx context.Context) { func Stop(ctx context.Context) { mongotool.Close(ctx) + gormtool.Close() } func initService() { @@ -178,6 +187,8 @@ func initDinD() { } func initDatabase() { + InitializeUserDBAndTables() + err := gormtool.Open(configbase.MysqlUser(), configbase.MysqlPassword(), configbase.MysqlHost(), @@ -187,6 +198,15 @@ func initDatabase() { log.Panicf("Failed to open database %s", config.MysqlDexDB()) } + err = gormtool.Open(configbase.MysqlUser(), + configbase.MysqlPassword(), + configbase.MysqlHost(), + config.MysqlUserDB(), + ) + if err != nil { + log.Panicf("Failed to open database %s", config.MysqlUserDB()) + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() @@ -313,3 +333,26 @@ func InitializeConfigFeatureGates() error { service.Features.MergeFeatureGates(flagFG, dbFG) return nil } + +//go:embed init/mysql.sql +var mysql []byte + +func InitializeUserDBAndTables() { + if len(mysql) == 0 { + return + } + db, err := sql.Open("mysql", fmt.Sprintf( + "%s:%s@tcp(%s)/?charset=utf8&multiStatements=true", + configbase.MysqlUser(), configbase.MysqlPassword(), configbase.MysqlHost(), + )) + if err != nil { + log.Panic(err) + } + defer db.Close() + initSql := fmt.Sprintf(string(mysql), config.MysqlUserDB(), config.MysqlUserDB()) + _, err = db.Exec(initSql) + + if err != nil { + log.Panic(err) + } +} diff --git a/pkg/microservice/aslan/server/rest/router.go b/pkg/microservice/aslan/server/rest/router.go index 1e01d4cb..357b300a 100644 --- a/pkg/microservice/aslan/server/rest/router.go +++ b/pkg/microservice/aslan/server/rest/router.go @@ -43,6 +43,7 @@ import ( emailHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/email/handler" featuresHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/handler" jiraHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/jira/handler" + userHandler "github.com/koderover/zadig/pkg/microservice/user/core/handler" // Note: have to load docs for swagger to work. See https://blog.csdn.net/weixin_43249914/article/details/103035711 _ "github.com/koderover/zadig/pkg/microservice/aslan/server/rest/doc" ) @@ -109,6 +110,13 @@ func (s *engine) injectRouterGroup(router *gin.RouterGroup) { r.Inject(router.Group("/api/v1")) } + // inject user service APIs + for _, r := range []injector{ + new(userHandler.Router), + } { + r.Inject(router.Group("/api/v1")) + } + router.GET("/api/apidocs/*any", ginswagger.WrapHandler(swaggerfiles.Handler)) } diff --git a/pkg/microservice/user/core/handler/healthz.go b/pkg/microservice/user/core/handler/healthz.go deleted file mode 100644 index 74be7210..00000000 --- a/pkg/microservice/user/core/handler/healthz.go +++ /dev/null @@ -1,30 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 handler - -import ( - "github.com/gin-gonic/gin" - - "github.com/koderover/zadig/pkg/microservice/user/core" - internalhandler "github.com/koderover/zadig/pkg/shared/handler" -) - -func Healthz(c *gin.Context) { - ctx := internalhandler.NewContext(c) - defer func() { internalhandler.JSONResponse(c, ctx) }() - ctx.Err = core.Healthz() -} diff --git a/pkg/microservice/user/core/handler/router.go b/pkg/microservice/user/core/handler/router.go index f48cfbd8..3d25a07e 100644 --- a/pkg/microservice/user/core/handler/router.go +++ b/pkg/microservice/user/core/handler/router.go @@ -61,7 +61,5 @@ func (*Router) Inject(router *gin.RouterGroup) { router.GET("retrieve", user.Retrieve) router.POST("reset", user.Reset) - - router.GET("/healthz", Healthz) } } diff --git a/pkg/microservice/user/core/service.go b/pkg/microservice/user/core/service.go index 89296880..b8d7ab56 100644 --- a/pkg/microservice/user/core/service.go +++ b/pkg/microservice/user/core/service.go @@ -18,78 +18,17 @@ package core import ( "context" - "database/sql" _ "embed" - "fmt" _ "github.com/go-sql-driver/mysql" "gorm.io/gorm" - "github.com/koderover/zadig/pkg/config" config2 "github.com/koderover/zadig/pkg/microservice/user/config" - "github.com/koderover/zadig/pkg/setting" gormtool "github.com/koderover/zadig/pkg/tool/gorm" - "github.com/koderover/zadig/pkg/tool/log" ) var DB *gorm.DB -//go:embed service/init/mysql.sql -var mysql []byte - -func initDBAndTables() { - if len(mysql) == 0 { - return - } - db, err := sql.Open("mysql", fmt.Sprintf( - "%s:%s@tcp(%s)/?charset=utf8&multiStatements=true", - config.MysqlUser(), config.MysqlPassword(), config.MysqlHost(), - )) - if err != nil { - log.Panic(err) - } - defer db.Close() - initSql := fmt.Sprintf(string(mysql), config.MysqlUserDB(), config.MysqlUserDB()) - _, err = db.Exec(initSql) - - if err != nil { - log.Panic(err) - } -} - func Start(_ context.Context) { - log.Init(&log.Config{ - Level: config.LogLevel(), - Filename: config.LogFile(), - SendToFile: config.SendLogToFile(), - Development: config.Mode() != setting.ReleaseMode, - }) - - initDatabase() DB = gormtool.DB(config2.MysqlUserDB()) } - -func initDatabase() { - initDBAndTables() - err := gormtool.Open(config.MysqlUser(), - config.MysqlPassword(), - config.MysqlHost(), - config2.MysqlUserDB(), - ) - if err != nil { - log.Panicf("Failed to open database %s", config2.MysqlUserDB()) - } -} - -func Stop(_ context.Context) { - gormtool.Close() -} - -func Healthz() error { - sqlDB, err := DB.DB() - if err != nil { - log.Errorf("Healthz get db error:%s", err.Error()) - return err - } - return sqlDB.Ping() -} diff --git a/pkg/microservice/user/server/rest/router.go b/pkg/microservice/user/server/rest/router.go deleted file mode 100644 index 51139f3a..00000000 --- a/pkg/microservice/user/server/rest/router.go +++ /dev/null @@ -1,35 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 rest - -import ( - "github.com/gin-gonic/gin" - - "github.com/koderover/zadig/pkg/microservice/user/core/handler" -) - -func (s *engine) injectRouterGroup(router *gin.RouterGroup) { - for _, r := range []injector{ - new(handler.Router), - } { - r.Inject(router.Group("/api/v1")) - } -} - -type injector interface { - Inject(router *gin.RouterGroup) -} diff --git a/pkg/microservice/user/server/rest/server.go b/pkg/microservice/user/server/rest/server.go deleted file mode 100644 index 18843b0a..00000000 --- a/pkg/microservice/user/server/rest/server.go +++ /dev/null @@ -1,76 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 rest - -import ( - "net/http" - - "github.com/gin-gonic/gin" - - "github.com/koderover/zadig/pkg/config" - ginmiddleware "github.com/koderover/zadig/pkg/middleware/gin" - "github.com/koderover/zadig/pkg/tool/log" -) - -type engine struct { - *gin.Engine - - mode string -} - -func NewEngine() *engine { - s := &engine{mode: config.Mode()} - - gin.SetMode(s.mode) - - s.injectMiddlewares() - s.injectRouters() - - return s -} - -func (s *engine) injectMiddlewares() { - g := gin.New() - defer func() { - s.Engine = g - }() - - if s.mode == gin.TestMode { - return - } - g.Use(ginmiddleware.Response()) - g.Use(ginmiddleware.RequestID()) - g.Use(ginmiddleware.RequestLog(log.NewFileLogger(config.RequestLogFile()))) - g.Use(gin.Recovery()) -} - -func (s *engine) injectRouters() { - g := s.Engine - - g.NoRoute(func(c *gin.Context) { - c.String(http.StatusNotFound, "Invalid path: %s", c.Request.URL.Path) - }) - g.HandleMethodNotAllowed = true - g.NoMethod(func(c *gin.Context) { - c.String(http.StatusMethodNotAllowed, "Method not allowed: %s %s", c.Request.Method, c.Request.URL.Path) - }) - - apiRouters := g.Group("") - s.injectRouterGroup(apiRouters) - - s.Engine = g -} diff --git a/pkg/microservice/user/server/server.go b/pkg/microservice/user/server/server.go deleted file mode 100644 index aa0f55e5..00000000 --- a/pkg/microservice/user/server/server.go +++ /dev/null @@ -1,60 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 server - -import ( - "context" - "net/http" - "time" - - "github.com/koderover/zadig/pkg/microservice/user/core" - "github.com/koderover/zadig/pkg/microservice/user/server/rest" - "github.com/koderover/zadig/pkg/tool/log" -) - -func Serve(ctx context.Context) error { - core.Start(ctx) - defer core.Stop(ctx) - - log.Info("Start user system service") - - engine := rest.NewEngine() - server := &http.Server{Addr: ":80", Handler: engine} - - stopChan := make(chan struct{}) - go func() { - defer close(stopChan) - - <-ctx.Done() - - ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) - defer cancel() - - if err := server.Shutdown(ctx); err != nil { - log.Errorf("Failed to stop server, error: %s", err) - } - }() - - if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { - log.Errorf("Failed to start http server, error: %s", err) - return err - } - - <-stopChan - - return nil -} diff --git a/pkg/setting/types.go b/pkg/setting/types.go index 52f9035b..efec37cd 100644 --- a/pkg/setting/types.go +++ b/pkg/setting/types.go @@ -103,7 +103,6 @@ const ( Minio // 11 OPA // 12 Policy // 13 - User // 14 ) type ServiceInfo struct { @@ -160,8 +159,4 @@ var Services = map[int]*ServiceInfo{ Name: "policy", Port: 80, }, - User: { - Name: "user", - Port: 80, - }, } diff --git a/pkg/shared/client/user/client.go b/pkg/shared/client/user/client.go index 5e6d8ac1..4f43777e 100644 --- a/pkg/shared/client/user/client.go +++ b/pkg/shared/client/user/client.go @@ -28,7 +28,7 @@ type Client struct { } func New() *Client { - host := config.UserServiceAddress() + host := config.AslanServiceAddress() c := httpclient.New( httpclient.SetHostURL(host + "/api/v1"), -- Gitee From 4abb4a517f652e8ec70ea1b553c47a35ad9352c0 Mon Sep 17 00:00:00 2001 From: Min Min <37430175+jamsman94@users.noreply.github.com> Date: Wed, 3 Aug 2022 16:56:51 +0800 Subject: [PATCH 5/8] merge podexec into aslan (#1850) Signed-off-by: Min Min Co-authored-by: Min Min --- Makefile | 2 +- cmd/podexec/main.go | 38 --------- pkg/microservice/aslan/server/rest/router.go | 7 ++ pkg/microservice/aslan/server/server.go | 8 +- pkg/microservice/picket/client/user/client.go | 2 +- .../podexec/core/service/pod_server_ws.go | 42 +++++----- pkg/microservice/podexec/server/server.go | 77 ------------------- pkg/setting/types.go | 15 ++-- 8 files changed, 39 insertions(+), 152 deletions(-) delete mode 100644 cmd/podexec/main.go delete mode 100644 pkg/microservice/podexec/server/server.go diff --git a/Makefile b/Makefile index 73ee7c7f..774aa68a 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ IMAGE_REPOSITORY = koderover.tencentcloudcr.com/koderover-public VERSION ?= $(shell date +'%Y%m%d%H%M%S') VERSION := $(VERSION) -TARGETS = aslan cron hub-agent hub-server init jenkins-plugin packager-plugin picket podexec policy predator-plugin ua warpdrive +TARGETS = aslan cron hub-agent hub-server init jenkins-plugin packager-plugin picket policy predator-plugin ua warpdrive REAPER_OS= focal xenial bionic ALL_IMAGES=$(TARGETS:=.image) diff --git a/cmd/podexec/main.go b/cmd/podexec/main.go deleted file mode 100644 index 12e2923c..00000000 --- a/cmd/podexec/main.go +++ /dev/null @@ -1,38 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 main - -import ( - "context" - "log" - "os/signal" - "syscall" - - "github.com/koderover/zadig/pkg/microservice/podexec/server" -) - -func main() { - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) - go func() { - <-ctx.Done() - stop() - }() - - if err := server.Serve(ctx); err != nil { - log.Fatal(err) - } -} diff --git a/pkg/microservice/aslan/server/rest/router.go b/pkg/microservice/aslan/server/rest/router.go index 357b300a..d2003a6d 100644 --- a/pkg/microservice/aslan/server/rest/router.go +++ b/pkg/microservice/aslan/server/rest/router.go @@ -39,6 +39,7 @@ import ( templatehandler "github.com/koderover/zadig/pkg/microservice/aslan/core/templatestore/handler" workflowhandler "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/handler" testinghandler "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/testing/handler" + podexecservice "github.com/koderover/zadig/pkg/microservice/podexec/core/service" connectorHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/connector/handler" emailHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/email/handler" featuresHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/handler" @@ -117,6 +118,12 @@ func (s *engine) injectRouterGroup(router *gin.RouterGroup) { r.Inject(router.Group("/api/v1")) } + // inject podexec service API(s) + podexec := router.Group("/api/podexec") + { + podexec.GET("/:productName/:namespace/:podName/:containerName/podExec/:envName", podexecservice.ServeWs) + } + router.GET("/api/apidocs/*any", ginswagger.WrapHandler(swaggerfiles.Handler)) } diff --git a/pkg/microservice/aslan/server/server.go b/pkg/microservice/aslan/server/server.go index 6a16dfc1..4cc73026 100644 --- a/pkg/microservice/aslan/server/server.go +++ b/pkg/microservice/aslan/server/server.go @@ -41,7 +41,13 @@ func Serve(ctx context.Context) error { log.Infof("App Aslan Started at %s", time.Now()) engine := rest.NewEngine() - server := &http.Server{Addr: ":25000", Handler: engine} + server := &http.Server{ + Addr: ":25000", + WriteTimeout: time.Second * 15, + ReadTimeout: time.Second * 15, + IdleTimeout: time.Second * 5 * 60, + Handler: engine, + } stopChan := make(chan struct{}) go func() { diff --git a/pkg/microservice/picket/client/user/client.go b/pkg/microservice/picket/client/user/client.go index 5e6d8ac1..4f43777e 100644 --- a/pkg/microservice/picket/client/user/client.go +++ b/pkg/microservice/picket/client/user/client.go @@ -28,7 +28,7 @@ type Client struct { } func New() *Client { - host := config.UserServiceAddress() + host := config.AslanServiceAddress() c := httpclient.New( httpclient.SetHostURL(host + "/api/v1"), diff --git a/pkg/microservice/podexec/core/service/pod_server_ws.go b/pkg/microservice/podexec/core/service/pod_server_ws.go index 36511c38..5fc17f3e 100644 --- a/pkg/microservice/podexec/core/service/pod_server_ws.go +++ b/pkg/microservice/podexec/core/service/pod_server_ws.go @@ -17,37 +17,33 @@ limitations under the License. package service import ( - "encoding/json" "fmt" - "net/http" - - "github.com/gorilla/mux" + "github.com/gin-gonic/gin" + internalhandler "github.com/koderover/zadig/pkg/shared/handler" + e "github.com/koderover/zadig/pkg/tool/errors" "github.com/koderover/zadig/pkg/tool/log" ) -func ServeWs(w http.ResponseWriter, r *http.Request) { - // 获取路径中的参数 - pathParams := mux.Vars(r) - namespace := pathParams["namespace"] - podName := pathParams["podName"] - containerName := pathParams["containerName"] - // 获取query中的参数 - queryList := r.URL.Query() - clusterID := queryList.Get("clusterId") +func ServeWs(c *gin.Context) { + ctx := internalhandler.NewContext(c) + defer func() { internalhandler.JSONResponse(c, ctx) }() + + namespace := c.Param("namespace") + podName := c.Param("podName") + containerName := c.Param("containerName") + clusterID := c.Query("clusterId") if namespace == "" || podName == "" || containerName == "" { - w.WriteHeader(http.StatusBadRequest) - _ = json.NewEncoder(w).Encode(&EndpointResponse{ResultCode: http.StatusBadRequest, ErrorMsg: "namespace,podName,containerName can't be empty,please check!"}) + ctx.Err = e.ErrInvalidParam.AddDesc("namespace,podName,containerName can't be empty,please check!") return } log.Infof("exec containerName: %s, pod: %s, namespace: %s", containerName, podName, namespace) - pty, err := NewTerminalSession(w, r, nil) + pty, err := NewTerminalSession(c.Writer, c.Request, nil) if err != nil { log.Errorf("get pty failed: %v", err) - w.WriteHeader(http.StatusInternalServerError) - _ = json.NewEncoder(w).Encode(&EndpointResponse{ResultCode: http.StatusInternalServerError, ErrorMsg: fmt.Sprintf("get pty failed: %v", err)}) + ctx.Err = e.ErrInternalError.AddDesc(fmt.Sprintf("get pty failed: %v", err)) return } defer func() { @@ -62,8 +58,7 @@ func ServeWs(w http.ResponseWriter, r *http.Request) { _, _ = pty.Write([]byte(msg)) pty.Done() - w.WriteHeader(http.StatusInternalServerError) - _ = json.NewEncoder(w).Encode(&EndpointResponse{ResultCode: http.StatusInternalServerError, ErrorMsg: fmt.Sprintf("get kubecli err :%v", err)}) + ctx.Err = e.ErrInternalError.AddDesc(fmt.Sprintf("get kubecli err :%v", err)) return } @@ -74,8 +69,7 @@ func ServeWs(w http.ResponseWriter, r *http.Request) { _, _ = pty.Write([]byte(msg)) pty.Done() - w.WriteHeader(http.StatusBadRequest) - _ = json.NewEncoder(w).Encode(&EndpointResponse{ResultCode: http.StatusBadRequest, ErrorMsg: fmt.Sprintf("Validate pod error! err: %v", err)}) + ctx.Err = e.ErrInternalError.AddDesc(fmt.Sprintf("Validate pod error! err: %v", err)) return } @@ -86,7 +80,7 @@ func ServeWs(w http.ResponseWriter, r *http.Request) { _, _ = pty.Write([]byte(msg)) pty.Done() - w.WriteHeader(http.StatusInternalServerError) - _ = json.NewEncoder(w).Encode(&EndpointResponse{ResultCode: http.StatusInternalServerError, ErrorMsg: fmt.Sprintf("Exec to pod error! err: %v", err)}) + ctx.Err = e.ErrInternalError.AddDesc(fmt.Sprintf("Exec to pod error! err: %v", err)) + return } } diff --git a/pkg/microservice/podexec/server/server.go b/pkg/microservice/podexec/server/server.go deleted file mode 100644 index c326fe3b..00000000 --- a/pkg/microservice/podexec/server/server.go +++ /dev/null @@ -1,77 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 server - -import ( - "context" - "encoding/json" - "net/http" - "time" - - "github.com/gorilla/mux" - - commonconfig "github.com/koderover/zadig/pkg/config" - "github.com/koderover/zadig/pkg/microservice/podexec/core/service" - "github.com/koderover/zadig/pkg/setting" - "github.com/koderover/zadig/pkg/tool/log" -) - -func Serve(ctx context.Context) error { - log.Init(&log.Config{ - Level: commonconfig.LogLevel(), - Development: commonconfig.Mode() != setting.ReleaseMode, - }) - - log.Infof("App podexec Started at %s", time.Now()) - - router := mux.NewRouter() - router.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) { - _ = json.NewEncoder(w).Encode(map[string]string{"message": "success"}) - }) - router.HandleFunc("/api/{productName}/{namespace}/{podName}/{containerName}/podExec/{envName}", service.ServeWs) - - server := &http.Server{ - Addr: "0.0.0.0:27000", - WriteTimeout: time.Second * 15, - ReadTimeout: time.Second * 15, - IdleTimeout: time.Second * 5 * 60, - Handler: router, - } - - stopChan := make(chan struct{}) - go func() { - defer close(stopChan) - - <-ctx.Done() - - ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) - defer cancel() - - if err := server.Shutdown(ctx); err != nil { - log.Errorf("Failed to stop server, error: %s", err) - } - }() - - if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { - log.Errorf("Failed to start http server, error: %s", err) - return err - } - - <-stopChan - - return nil -} diff --git a/pkg/setting/types.go b/pkg/setting/types.go index efec37cd..e90b99f3 100644 --- a/pkg/setting/types.go +++ b/pkg/setting/types.go @@ -97,12 +97,11 @@ const ( Collie // 4 Cron // 5 HubServer // 6 - PodExec // 7 - SonarQube // 9 - WarpDrive // 10 - Minio // 11 - OPA // 12 - Policy // 13 + SonarQube // 7 + WarpDrive // 8 + Minio // 9 + OPA // 10 + Policy // 11 ) type ServiceInfo struct { @@ -135,10 +134,6 @@ var Services = map[int]*ServiceInfo{ Name: "hub-server", Port: 26000, }, - PodExec: { - Name: "podexec", - Port: 27000, - }, SonarQube: { Name: "sonarqube", Port: 80, -- Gitee From 93a92260ce435c960feea1380b31fac4402236a0 Mon Sep 17 00:00:00 2001 From: Min Min <37430175+jamsman94@users.noreply.github.com> Date: Thu, 4 Aug 2022 15:34:49 +0800 Subject: [PATCH 6/8] merge policy into aslan (#1853) Signed-off-by: Min Min Co-authored-by: Min Min --- Makefile | 2 +- cmd/policy/main.go | 38 ------ .../core/policy/service/migration.go} | 76 ++---------- pkg/microservice/aslan/core/service.go | 31 +++-- pkg/microservice/aslan/server/rest/router.go | 11 +- .../policy/core/handler/healthz.go | 30 ----- .../policy/core/handler/router.go | 4 - pkg/microservice/policy/core/service.go | 114 ------------------ .../core/service/bundle/rate_limiting.go | 4 +- 9 files changed, 47 insertions(+), 263 deletions(-) delete mode 100644 cmd/policy/main.go rename pkg/microservice/{policy/server/server.go => aslan/core/policy/service/migration.go} (74%) delete mode 100644 pkg/microservice/policy/core/handler/healthz.go delete mode 100644 pkg/microservice/policy/core/service.go diff --git a/Makefile b/Makefile index 774aa68a..61828dc4 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ IMAGE_REPOSITORY = koderover.tencentcloudcr.com/koderover-public VERSION ?= $(shell date +'%Y%m%d%H%M%S') VERSION := $(VERSION) -TARGETS = aslan cron hub-agent hub-server init jenkins-plugin packager-plugin picket policy predator-plugin ua warpdrive +TARGETS = aslan cron hub-agent hub-server init jenkins-plugin packager-plugin picket predator-plugin ua warpdrive REAPER_OS= focal xenial bionic ALL_IMAGES=$(TARGETS:=.image) diff --git a/cmd/policy/main.go b/cmd/policy/main.go deleted file mode 100644 index f42d45db..00000000 --- a/cmd/policy/main.go +++ /dev/null @@ -1,38 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 main - -import ( - "context" - "log" - "os/signal" - "syscall" - - "github.com/koderover/zadig/pkg/microservice/policy/server" -) - -func main() { - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) - go func() { - <-ctx.Done() - stop() - }() - - if err := server.Serve(ctx); err != nil { - log.Fatal(err) - } -} diff --git a/pkg/microservice/policy/server/server.go b/pkg/microservice/aslan/core/policy/service/migration.go similarity index 74% rename from pkg/microservice/policy/server/server.go rename to pkg/microservice/aslan/core/policy/service/migration.go index e3a671f3..0788887f 100644 --- a/pkg/microservice/policy/server/server.go +++ b/pkg/microservice/aslan/core/policy/service/migration.go @@ -1,76 +1,27 @@ -/* -Copyright 2021 The KodeRover 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 server +package service import ( - "context" - "net/http" "time" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/informers" - "k8s.io/client-go/tools/cache" - "sigs.k8s.io/yaml" - "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/cache" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/yaml" commonconfig "github.com/koderover/zadig/pkg/config" - "github.com/koderover/zadig/pkg/microservice/policy/core" - "github.com/koderover/zadig/pkg/microservice/policy/core/service" + policyservice "github.com/koderover/zadig/pkg/microservice/policy/core/service" "github.com/koderover/zadig/pkg/microservice/policy/core/yamlconfig" - "github.com/koderover/zadig/pkg/microservice/policy/server/rest" "github.com/koderover/zadig/pkg/setting" kubeclient "github.com/koderover/zadig/pkg/shared/kube/client" - toolClient "github.com/koderover/zadig/pkg/tool/kube/client" "github.com/koderover/zadig/pkg/tool/kube/getter" "github.com/koderover/zadig/pkg/tool/kube/updater" "github.com/koderover/zadig/pkg/tool/log" ) -func Serve(ctx context.Context) error { - core.Start(ctx) - defer core.Stop(ctx) - - log.Info("Start policy service") - - engine := rest.NewEngine() - server := &http.Server{Addr: ":80", Handler: engine} - - stopChan := make(chan struct{}) - go func() { - defer close(stopChan) - - <-ctx.Done() - - ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) - defer cancel() - - if err := server.Shutdown(ctx); err != nil { - log.Errorf("Failed to stop server, error: %s", err) - } - }() - go func() { - if err := toolClient.Start(ctx); err != nil { - panic(err) - } - }() - +func MigratePolicyData() error { if err := migratePolicyMeta(); err != nil { log.Errorf("fail to migrate policyMeta, err:%s", err) return err @@ -81,19 +32,12 @@ func Serve(ctx context.Context) error { return err } - if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { - log.Errorf("Failed to start http server, error: %s", err) - return err - } - - <-stopChan - return nil } type PresetRoleConfigYaml struct { - PresetRoles []*service.Role `json:"preset_roles"` - Description string `json:"description"` + PresetRoles []*policyservice.Role `json:"preset_roles"` + Description string `json:"description"` } func migrateRole() error { @@ -117,7 +61,7 @@ func migrateRole() error { } for _, role := range config.PresetRoles { - if err := service.UpdateOrCreateRole(role.Namespace, role, nil); err != nil { + if err := policyservice.UpdateOrCreateRole(role.Namespace, role, nil); err != nil { log.Errorf("UpdateOrCreateRole err:%s", err) return err } @@ -202,7 +146,7 @@ func migratePolicyMeta() error { } for _, v := range yamlconfig.DefaultPolicyMetas() { - if err := service.CreateOrUpdatePolicyRegistration(v, nil); err != nil { + if err := policyservice.CreateOrUpdatePolicyRegistration(v, nil); err != nil { log.DPanic(err) } } @@ -246,7 +190,7 @@ func NewClusterInformerFactory(clusterId string, cls *kubernetes.Clientset) (inf if b, ok := configMap.Data["meta.yaml"]; ok { log.Infof("start to update the meta configMap") for _, v := range yamlconfig.PolicyMetasFromBytes([]byte(b)) { - if err := service.CreateOrUpdatePolicyRegistration(v, nil); err != nil { + if err := policyservice.CreateOrUpdatePolicyRegistration(v, nil); err != nil { log.Errorf("fail to CreateOrUpdatePolicyRegistration,err:%s", err) continue } diff --git a/pkg/microservice/aslan/core/service.go b/pkg/microservice/aslan/core/service.go index 5300aeae..e5a46656 100644 --- a/pkg/microservice/aslan/core/service.go +++ b/pkg/microservice/aslan/core/service.go @@ -26,11 +26,6 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/hashicorp/go-multierror" - "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/service" - gormtool "github.com/koderover/zadig/pkg/tool/gorm" - apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - commonconfig "github.com/koderover/zadig/pkg/config" configbase "github.com/koderover/zadig/pkg/config" "github.com/koderover/zadig/pkg/microservice/aslan/config" @@ -44,21 +39,29 @@ import ( environmentservice "github.com/koderover/zadig/pkg/microservice/aslan/core/environment/service" labelMongodb "github.com/koderover/zadig/pkg/microservice/aslan/core/label/repository/mongodb" multiclusterservice "github.com/koderover/zadig/pkg/microservice/aslan/core/multicluster/service" + policyservice "github.com/koderover/zadig/pkg/microservice/aslan/core/policy/service" systemrepo "github.com/koderover/zadig/pkg/microservice/aslan/core/system/repository/mongodb" systemservice "github.com/koderover/zadig/pkg/microservice/aslan/core/system/service" workflowservice "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/service/workflow" + policydb "github.com/koderover/zadig/pkg/microservice/policy/core/repository/mongodb" + policybundle "github.com/koderover/zadig/pkg/microservice/policy/core/service/bundle" configmongodb "github.com/koderover/zadig/pkg/microservice/systemconfig/core/email/repository/mongodb" + configservice "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/service" userCore "github.com/koderover/zadig/pkg/microservice/user/core" "github.com/koderover/zadig/pkg/setting" kubeclient "github.com/koderover/zadig/pkg/shared/kube/client" + gormtool "github.com/koderover/zadig/pkg/tool/gorm" "github.com/koderover/zadig/pkg/tool/log" mongotool "github.com/koderover/zadig/pkg/tool/mongo" "github.com/koderover/zadig/pkg/tool/rsa" "github.com/koderover/zadig/pkg/types" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const ( webhookController = iota + bundleController ) type policyGetter interface { @@ -72,9 +75,11 @@ type Controller interface { func StartControllers(stopCh <-chan struct{}) { controllerWorkers := map[int]int{ webhookController: 1, + bundleController: 1, } controllers := map[int]Controller{ webhookController: webhook.NewWebhookController(), + bundleController: policybundle.NewBundleController(), } var wg sync.WaitGroup @@ -155,6 +160,10 @@ func Start(ctx context.Context) { go multiclusterservice.ClusterApplyUpgradeAgent() initRsaKey() + + // policy initialization process + policybundle.GenerateOPABundle() + policyservice.MigratePolicyData() } func Stop(ctx context.Context) { @@ -187,6 +196,7 @@ func initDinD() { } func initDatabase() { + // old user service initialization InitializeUserDBAndTables() err := gormtool.Open(configbase.MysqlUser(), @@ -290,6 +300,11 @@ func initDatabase() { // config related db index configmongodb.NewEmailHostColl(), + + // policy related db index + policydb.NewRoleColl(), + policydb.NewRoleBindingColl(), + policydb.NewPolicyMetaColl(), } { wg.Add(1) go func(r indexer) { @@ -320,17 +335,17 @@ type indexer interface { // InitializeConfigFeatureGates initialize feature gates for the old config service module. // Currently, the function of this part is unknown. But we will keep it just to make sure. func InitializeConfigFeatureGates() error { - flagFG, err := service.FlagToFeatureGates(config.Features()) + flagFG, err := configservice.FlagToFeatureGates(config.Features()) if err != nil { log.Errorf("FlagToFeatureGates err:%s", err) return err } - dbFG, err := service.DBToFeatureGates() + dbFG, err := configservice.DBToFeatureGates() if err != nil { log.Errorf("DBToFeatureGates err:%s", err) return err } - service.Features.MergeFeatureGates(flagFG, dbFG) + configservice.Features.MergeFeatureGates(flagFG, dbFG) return nil } diff --git a/pkg/microservice/aslan/server/rest/router.go b/pkg/microservice/aslan/server/rest/router.go index d2003a6d..4ac273f8 100644 --- a/pkg/microservice/aslan/server/rest/router.go +++ b/pkg/microservice/aslan/server/rest/router.go @@ -40,6 +40,8 @@ import ( workflowhandler "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/handler" testinghandler "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/testing/handler" podexecservice "github.com/koderover/zadig/pkg/microservice/podexec/core/service" + policyhandler "github.com/koderover/zadig/pkg/microservice/policy/core/handler" + configcodehostHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/codehost/handler" connectorHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/connector/handler" emailHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/email/handler" featuresHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/features/handler" @@ -105,7 +107,7 @@ func (s *engine) injectRouterGroup(router *gin.RouterGroup) { new(connectorHandler.Router), new(emailHandler.Router), new(jiraHandler.Router), - new(codehosthandler.Router), + new(configcodehostHandler.Router), new(featuresHandler.Router), } { r.Inject(router.Group("/api/v1")) @@ -124,6 +126,13 @@ func (s *engine) injectRouterGroup(router *gin.RouterGroup) { podexec.GET("/:productName/:namespace/:podName/:containerName/podExec/:envName", podexecservice.ServeWs) } + // inject policy APIs + for _, r := range []injector{ + new(policyhandler.Router), + } { + r.Inject(router.Group("/api/v1")) + } + router.GET("/api/apidocs/*any", ginswagger.WrapHandler(swaggerfiles.Handler)) } diff --git a/pkg/microservice/policy/core/handler/healthz.go b/pkg/microservice/policy/core/handler/healthz.go deleted file mode 100644 index 860a2e17..00000000 --- a/pkg/microservice/policy/core/handler/healthz.go +++ /dev/null @@ -1,30 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 handler - -import ( - "github.com/gin-gonic/gin" - - "github.com/koderover/zadig/pkg/microservice/policy/core" - internalhandler "github.com/koderover/zadig/pkg/shared/handler" -) - -func Healthz(c *gin.Context) { - ctx := internalhandler.NewContext(c) - defer func() { internalhandler.JSONResponse(c, ctx) }() - ctx.Err = core.Healthz(c.Request.Context()) -} diff --git a/pkg/microservice/policy/core/handler/router.go b/pkg/microservice/policy/core/handler/router.go index 96a9efb4..f9a0df89 100644 --- a/pkg/microservice/policy/core/handler/router.go +++ b/pkg/microservice/policy/core/handler/router.go @@ -108,10 +108,6 @@ func (*Router) Inject(router *gin.RouterGroup) { policyDefinitions.GET("", GetPolicyRegistrationDefinitions) } - policySvrHealthz := router.Group("healthz") - { - policySvrHealthz.GET("", Healthz) - } policyUserPermission := router.Group("permission") { policyUserPermission.GET("project/:name", GetUserRulesByProject) diff --git a/pkg/microservice/policy/core/service.go b/pkg/microservice/policy/core/service.go deleted file mode 100644 index ff16117c..00000000 --- a/pkg/microservice/policy/core/service.go +++ /dev/null @@ -1,114 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 core - -import ( - "context" - "fmt" - "sync" - "time" - - "github.com/koderover/zadig/pkg/config" - "github.com/koderover/zadig/pkg/microservice/policy/core/repository/mongodb" - "github.com/koderover/zadig/pkg/microservice/policy/core/service/bundle" - "github.com/koderover/zadig/pkg/setting" - "github.com/koderover/zadig/pkg/tool/log" - mongotool "github.com/koderover/zadig/pkg/tool/mongo" -) - -const ( - bundleController = iota -) - -type Controller interface { - Run(stopCh <-chan struct{}) -} - -func StartControllers(stopCh <-chan struct{}) { - controllers := map[int]Controller{ - bundleController: bundle.NewBundleController(), - } - - var wg sync.WaitGroup - for _, c := range controllers { - wg.Add(1) - go func(c Controller) { - defer wg.Done() - c.Run(stopCh) - }(c) - } - - wg.Wait() -} - -func Start(ctx context.Context) { - log.Init(&log.Config{ - Level: config.LogLevel(), - Filename: config.LogFile(), - SendToFile: config.SendLogToFile(), - Development: config.Mode() != setting.ReleaseMode, - }) - - initDatabase(ctx) - - go StartControllers(ctx.Done()) - - bundle.GenerateOPABundle() -} - -func Stop(ctx context.Context) { - _ = mongotool.Close(ctx) -} - -func initDatabase(ctx context.Context) { - ctx, cancel := context.WithTimeout(ctx, 30*time.Second) - defer cancel() - - mongotool.Init(ctx, config.MongoURI()) - if err := mongotool.Ping(ctx); err != nil { - panic(fmt.Errorf("failed to connect to mongo, error: %s", err)) - } - - idxCtx, idxCancel := context.WithTimeout(ctx, 10*time.Minute) - defer idxCancel() - - var wg sync.WaitGroup - for _, r := range []indexer{ - mongodb.NewRoleColl(), - mongodb.NewRoleBindingColl(), - mongodb.NewPolicyMetaColl(), - } { - wg.Add(1) - go func(r indexer) { - defer wg.Done() - if err := r.EnsureIndex(idxCtx); err != nil { - panic(fmt.Errorf("failed to create index for %s, error: %s", r.GetCollectionName(), err)) - } - }(r) - } - - wg.Wait() -} - -type indexer interface { - EnsureIndex(ctx context.Context) error - GetCollectionName() string -} - -func Healthz(ctx context.Context) error { - return mongotool.Ping(ctx) -} diff --git a/pkg/microservice/policy/core/service/bundle/rate_limiting.go b/pkg/microservice/policy/core/service/bundle/rate_limiting.go index 1b28ce7e..8150ffe3 100644 --- a/pkg/microservice/policy/core/service/bundle/rate_limiting.go +++ b/pkg/microservice/policy/core/service/bundle/rate_limiting.go @@ -43,7 +43,9 @@ type controller struct { } // Run starts the controller and blocks until receiving signal from stopCh. -func (c *controller) Run(stopCh <-chan struct{}) { +// Since the merge of aslan and policy, the worker parameter has to be added. +// It won't affect how the old logic works. +func (c *controller) Run(workers int, stopCh <-chan struct{}) { c.logger.Info("Starting bundle controller") defer c.logger.Info("Shutting down bundle controller") -- Gitee From 8714aaa5e042343c03b62de256600b7581ec7743 Mon Sep 17 00:00:00 2001 From: Min Min <37430175+jamsman94@users.noreply.github.com> Date: Thu, 4 Aug 2022 18:29:41 +0800 Subject: [PATCH 7/8] merge picket into aslan (#1854) Signed-off-by: Min Min Co-authored-by: Min Min --- Makefile | 2 +- cmd/picket/main.go | 38 ---------- pkg/cli/initconfig/cmd/healthz.go | 3 - pkg/microservice/aslan/server/rest/router.go | 17 +++++ pkg/microservice/picket/server/rest/router.go | 45 ----------- pkg/microservice/picket/server/rest/server.go | 76 ------------------- pkg/microservice/picket/server/server.go | 65 ---------------- pkg/shared/client/policy/client.go | 2 +- 8 files changed, 19 insertions(+), 229 deletions(-) delete mode 100644 cmd/picket/main.go delete mode 100644 pkg/microservice/picket/server/rest/router.go delete mode 100644 pkg/microservice/picket/server/rest/server.go delete mode 100644 pkg/microservice/picket/server/server.go diff --git a/Makefile b/Makefile index 61828dc4..6bca3ec1 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ IMAGE_REPOSITORY = koderover.tencentcloudcr.com/koderover-public VERSION ?= $(shell date +'%Y%m%d%H%M%S') VERSION := $(VERSION) -TARGETS = aslan cron hub-agent hub-server init jenkins-plugin packager-plugin picket predator-plugin ua warpdrive +TARGETS = aslan cron hub-agent hub-server init jenkins-plugin packager-plugin predator-plugin ua warpdrive REAPER_OS= focal xenial bionic ALL_IMAGES=$(TARGETS:=.image) diff --git a/cmd/picket/main.go b/cmd/picket/main.go deleted file mode 100644 index d26e3474..00000000 --- a/cmd/picket/main.go +++ /dev/null @@ -1,38 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 main - -import ( - "context" - "log" - "os/signal" - "syscall" - - "github.com/koderover/zadig/pkg/microservice/picket/server" -) - -func main() { - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) - go func() { - <-ctx.Done() - stop() - }() - - if err := server.Serve(ctx); err != nil { - log.Fatal(err) - } -} diff --git a/pkg/cli/initconfig/cmd/healthz.go b/pkg/cli/initconfig/cmd/healthz.go index 8d0f7f70..ad4138b3 100644 --- a/pkg/cli/initconfig/cmd/healthz.go +++ b/pkg/cli/initconfig/cmd/healthz.go @@ -25,9 +25,6 @@ import ( ) func Healthz() error { - if err := checkPolicyServiceHealth(); err != nil { - return fmt.Errorf("checkPolicyServiceHealth error:%s", err) - } if err := checkAslanServiceHealth(); err != nil { return fmt.Errorf("checkPolicyServiceHealth error:%s", err) } diff --git a/pkg/microservice/aslan/server/rest/router.go b/pkg/microservice/aslan/server/rest/router.go index 4ac273f8..a80f26e1 100644 --- a/pkg/microservice/aslan/server/rest/router.go +++ b/pkg/microservice/aslan/server/rest/router.go @@ -39,6 +39,9 @@ import ( templatehandler "github.com/koderover/zadig/pkg/microservice/aslan/core/templatestore/handler" workflowhandler "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/handler" testinghandler "github.com/koderover/zadig/pkg/microservice/aslan/core/workflow/testing/handler" + evaluationhandler "github.com/koderover/zadig/pkg/microservice/picket/core/evaluation/handler" + filterhandler "github.com/koderover/zadig/pkg/microservice/picket/core/filter/handler" + publichandler "github.com/koderover/zadig/pkg/microservice/picket/core/public/handler" podexecservice "github.com/koderover/zadig/pkg/microservice/podexec/core/service" policyhandler "github.com/koderover/zadig/pkg/microservice/policy/core/handler" configcodehostHandler "github.com/koderover/zadig/pkg/microservice/systemconfig/core/codehost/handler" @@ -133,6 +136,20 @@ func (s *engine) injectRouterGroup(router *gin.RouterGroup) { r.Inject(router.Group("/api/v1")) } + // inject picket APIs + for _, r := range []injector{ + new(evaluationhandler.Router), + new(filterhandler.Router), + } { + r.Inject(router.Group("/api/v1/picket")) + } + + for _, r := range []injector{ + new(publichandler.Router), + } { + r.Inject(router.Group("/public-api/v1")) + } + router.GET("/api/apidocs/*any", ginswagger.WrapHandler(swaggerfiles.Handler)) } diff --git a/pkg/microservice/picket/server/rest/router.go b/pkg/microservice/picket/server/rest/router.go deleted file mode 100644 index ba02b80e..00000000 --- a/pkg/microservice/picket/server/rest/router.go +++ /dev/null @@ -1,45 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 rest - -import ( - "github.com/gin-gonic/gin" - - evaluationhandler "github.com/koderover/zadig/pkg/microservice/picket/core/evaluation/handler" - filterhandler "github.com/koderover/zadig/pkg/microservice/picket/core/filter/handler" - publichandler "github.com/koderover/zadig/pkg/microservice/picket/core/public/handler" -) - -func (s *engine) injectRouterGroup(router *gin.RouterGroup) { - for _, r := range []injector{ - new(evaluationhandler.Router), - new(filterhandler.Router), - } { - r.Inject(router.Group("/api/v1")) - } - - for _, r := range []injector{ - new(publichandler.Router), - } { - r.Inject(router.Group("/public-api/v1")) - } - -} - -type injector interface { - Inject(router *gin.RouterGroup) -} diff --git a/pkg/microservice/picket/server/rest/server.go b/pkg/microservice/picket/server/rest/server.go deleted file mode 100644 index 18843b0a..00000000 --- a/pkg/microservice/picket/server/rest/server.go +++ /dev/null @@ -1,76 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 rest - -import ( - "net/http" - - "github.com/gin-gonic/gin" - - "github.com/koderover/zadig/pkg/config" - ginmiddleware "github.com/koderover/zadig/pkg/middleware/gin" - "github.com/koderover/zadig/pkg/tool/log" -) - -type engine struct { - *gin.Engine - - mode string -} - -func NewEngine() *engine { - s := &engine{mode: config.Mode()} - - gin.SetMode(s.mode) - - s.injectMiddlewares() - s.injectRouters() - - return s -} - -func (s *engine) injectMiddlewares() { - g := gin.New() - defer func() { - s.Engine = g - }() - - if s.mode == gin.TestMode { - return - } - g.Use(ginmiddleware.Response()) - g.Use(ginmiddleware.RequestID()) - g.Use(ginmiddleware.RequestLog(log.NewFileLogger(config.RequestLogFile()))) - g.Use(gin.Recovery()) -} - -func (s *engine) injectRouters() { - g := s.Engine - - g.NoRoute(func(c *gin.Context) { - c.String(http.StatusNotFound, "Invalid path: %s", c.Request.URL.Path) - }) - g.HandleMethodNotAllowed = true - g.NoMethod(func(c *gin.Context) { - c.String(http.StatusMethodNotAllowed, "Method not allowed: %s %s", c.Request.Method, c.Request.URL.Path) - }) - - apiRouters := g.Group("") - s.injectRouterGroup(apiRouters) - - s.Engine = g -} diff --git a/pkg/microservice/picket/server/server.go b/pkg/microservice/picket/server/server.go deleted file mode 100644 index f6535e53..00000000 --- a/pkg/microservice/picket/server/server.go +++ /dev/null @@ -1,65 +0,0 @@ -/* -Copyright 2021 The KodeRover 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 server - -import ( - "context" - "net/http" - "time" - - "github.com/koderover/zadig/pkg/config" - "github.com/koderover/zadig/pkg/microservice/picket/server/rest" - "github.com/koderover/zadig/pkg/setting" - "github.com/koderover/zadig/pkg/tool/log" -) - -func Serve(ctx context.Context) error { - log.Init(&log.Config{ - Level: config.LogLevel(), - Filename: config.LogFile(), - SendToFile: config.SendLogToFile(), - Development: config.Mode() != setting.ReleaseMode, - }) - - log.Info("Start adaptor service") - - engine := rest.NewEngine() - server := &http.Server{Addr: ":80", Handler: engine} - - stopChan := make(chan struct{}) - go func() { - defer close(stopChan) - - <-ctx.Done() - - ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) - defer cancel() - - if err := server.Shutdown(ctx); err != nil { - log.Errorf("Failed to stop server, error: %s", err) - } - }() - - if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { - log.Errorf("Failed to start http server, error: %s", err) - return err - } - - <-stopChan - - return nil -} diff --git a/pkg/shared/client/policy/client.go b/pkg/shared/client/policy/client.go index 34f60e29..b9cb0527 100644 --- a/pkg/shared/client/policy/client.go +++ b/pkg/shared/client/policy/client.go @@ -28,7 +28,7 @@ type Client struct { } func NewDefault() *Client { - host := config.PolicyServiceAddress() + host := config.AslanServiceAddress() c := httpclient.New( httpclient.SetHostURL(host + "/api/v1"), -- Gitee From 6b8010a62443cf3d765ebec86f6bdc12517ef224 Mon Sep 17 00:00:00 2001 From: lilianzhu <9208447+lilianzhu@user.noreply.gitee.com> Date: Sat, 25 Jan 2025 01:34:22 +0000 Subject: [PATCH 8/8] update action.yml. Signed-off-by: lilianzhu <9208447+lilianzhu@user.noreply.gitee.com> --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 16e71a8d..f7701a11 100644 --- a/action.yml +++ b/action.yml @@ -3,7 +3,7 @@ description: 'Automatically label new pull requests based on the paths of files author: 'GitHub' inputs: repo-token: - description: 'The GITHUB_TOKEN secret' + description: 'The GITHUB_TOKEN secret123' configuration-path: description: 'The path for the label configurations' default: '.github/labeler.yml' -- Gitee