diff --git a/Makefile b/Makefile index 0341b1bb175b8c5b712bab8781c7c7138af70b78..6bca3ec12d9e36c6650672f029f18ed8d4c4a6c6 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 predator-plugin ua warpdrive REAPER_OS= focal xenial bionic ALL_IMAGES=$(TARGETS:=.image) diff --git a/action.yml b/action.yml index 16e71a8d1dd33264bc7bdfbaea98da014868c339..f7701a117c317bd60af57faae4d999d3b2bfc72c 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' diff --git a/cmd/config/main.go b/cmd/config/main.go deleted file mode 100644 index 3df10041fcaf3373bf0151575fc74641422e7e57..0000000000000000000000000000000000000000 --- 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/cmd/picket/main.go b/cmd/picket/main.go deleted file mode 100644 index d26e34742eee11076ab5f06f22ed8aa823f58b34..0000000000000000000000000000000000000000 --- 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/cmd/podexec/main.go b/cmd/podexec/main.go deleted file mode 100644 index 12e2923ccbec3016295f160feaccc81b04ef9c33..0000000000000000000000000000000000000000 --- 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/cmd/policy/main.go b/cmd/policy/main.go deleted file mode 100644 index f42d45db153431014ff624248b623617e1e073cf..0000000000000000000000000000000000000000 --- 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/cmd/user/main.go b/cmd/user/main.go deleted file mode 100644 index 2f8f7c699280caa206d914658a629e04c980188d..0000000000000000000000000000000000000000 --- 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 bdc4d036e6c8fb56a34088aac8f1025a8dbf58b3..ad4138b3a57f7c7895afedff9d2451a9efa19297 100644 --- a/pkg/cli/initconfig/cmd/healthz.go +++ b/pkg/cli/initconfig/cmd/healthz.go @@ -22,26 +22,15 @@ 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) - } if err := checkAslanServiceHealth(); err != nil { return fmt.Errorf("checkPolicyServiceHealth error:%s", err) } 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 c6123172ebf144b41457fef3a81b06da4f04d8bc..f0d6378f54f3bbbb2ca43f1250e507250896abee 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) } @@ -177,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 c2b14a77822e6c5ff5231562063f662f3c9e1966..855ed2a8e54c6d2b1651bfedbebccda9fdbf2f86 100644 --- a/pkg/microservice/aslan/config/config.go +++ b/pkg/microservice/aslan/config/config.go @@ -253,3 +253,15 @@ 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) +} + +func MysqlUserDB() string { + return viper.GetString(setting.ENVMysqlUserDB) +} diff --git a/pkg/microservice/aslan/core/common/service/template_product.go b/pkg/microservice/aslan/core/common/service/template_product.go index 5546a6510a3bb0e2fabb8abfee1e465cf672e4a5..163f8b120644d1dc154b6214cd70bc53e40a35bc 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/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/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 e3a671f3d7026e7b78ccf292f9e67d4c2d9f4b5d..0788887f46ab869a69da447279f5bfb0cf087cf8 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/project/service/product.go b/pkg/microservice/aslan/core/project/service/product.go index 91558373b861c6fdc286bf0d50020745e319f38e..295f280c7d1e718af41c597c5c9582e58c0da101 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/core/service.go b/pkg/microservice/aslan/core/service.go index 37cd95841928ec265ae16f9d339b6a021101d33d..e5a46656010c41193e00c4a8dacde2ef2187b3f2 100644 --- a/pkg/microservice/aslan/core/service.go +++ b/pkg/microservice/aslan/core/service.go @@ -18,15 +18,16 @@ package core import ( "context" + "database/sql" + _ "embed" "fmt" "sync" "time" + _ "github.com/go-sql-driver/mysql" "github.com/hashicorp/go-multierror" - 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" @@ -38,19 +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 { @@ -64,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 @@ -121,6 +134,13 @@ 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() + + // 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() @@ -140,10 +160,15 @@ func Start(ctx context.Context) { go multiclusterservice.ClusterApplyUpgradeAgent() initRsaKey() + + // policy initialization process + policybundle.GenerateOPABundle() + policyservice.MigratePolicyData() } func Stop(ctx context.Context) { mongotool.Close(ctx) + gormtool.Close() } func initService() { @@ -171,9 +196,31 @@ func initDinD() { } func initDatabase() { + // old user service initialization + InitializeUserDBAndTables() + + err := gormtool.Open(configbase.MysqlUser(), + configbase.MysqlPassword(), + configbase.MysqlHost(), + config.MysqlDexDB(), + ) + if err != nil { + 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() + // 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 +231,7 @@ func initDatabase() { var wg sync.WaitGroup for _, r := range []indexer{ + // aslan related db index template.NewProductColl(), commonrepo.NewBasicImageColl(), commonrepo.NewBuildColl(), @@ -249,6 +297,14 @@ func initDatabase() { labelMongodb.NewLabelBindingColl(), modeMongodb.NewCollaborationModeColl(), modeMongodb.NewCollaborationInstanceColl(), + + // config related db index + configmongodb.NewEmailHostColl(), + + // policy related db index + policydb.NewRoleColl(), + policydb.NewRoleBindingColl(), + policydb.NewPolicyMetaColl(), } { wg.Add(1) go func(r indexer) { @@ -275,3 +331,43 @@ 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 := configservice.FlagToFeatureGates(config.Features()) + if err != nil { + log.Errorf("FlagToFeatureGates err:%s", err) + return err + } + dbFG, err := configservice.DBToFeatureGates() + if err != nil { + log.Errorf("DBToFeatureGates err:%s", err) + return err + } + configservice.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 8b1d62c3ebea5ac62ff211a81040a33dacf0ce0e..a80f26e189e2225736d65e196919d20bcfc2c8cd 100644 --- a/pkg/microservice/aslan/server/rest/router.go +++ b/pkg/microservice/aslan/server/rest/router.go @@ -39,7 +39,17 @@ 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" + 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" + 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" ) @@ -72,6 +82,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 +105,51 @@ 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(configcodehostHandler.Router), + new(featuresHandler.Router), + } { + r.Inject(router.Group("/api/v1")) + } + + // inject user service APIs + for _, r := range []injector{ + new(userHandler.Router), + } { + 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) + } + + // inject policy APIs + for _, r := range []injector{ + new(policyhandler.Router), + } { + 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/aslan/server/server.go b/pkg/microservice/aslan/server/server.go index 6a16dfc16216475e50edb232552fc9e56f8be8a7..4cc730265a00fd2407d742351b0738e56f635e6c 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 5e6d8ac15e89bad3c8d9917c9d714db7b45001be..4f43777ea0ff8e85dff1698c577b0819b9796a18 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/picket/server/rest/router.go b/pkg/microservice/picket/server/rest/router.go deleted file mode 100644 index ba02b80ebc35067d01babe4f13a73d399f466361..0000000000000000000000000000000000000000 --- 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 18843b0a5ead8f194d217a281527d5152e939795..0000000000000000000000000000000000000000 --- 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 f6535e530f7b4b3d9f40baf63762a298e696aa29..0000000000000000000000000000000000000000 --- 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/microservice/podexec/core/service/pod_server_ws.go b/pkg/microservice/podexec/core/service/pod_server_ws.go index 36511c3822847521e6dc6c1795e565eb6f0eb6b6..5fc17f3e4a016a89eb33c5a4b8bad3be999c4cb8 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 c326fe3b8e1f8182b760de645798917742ade431..0000000000000000000000000000000000000000 --- 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/microservice/policy/core/handler/healthz.go b/pkg/microservice/policy/core/handler/healthz.go deleted file mode 100644 index 860a2e170cfc45912e09857142a7c6ef97f449d3..0000000000000000000000000000000000000000 --- 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 96a9efb4d70a896e922ea217a21c54ad83501cf5..f9a0df892d9f5f45d8d17443c27eb40f63f9aa9d 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 ff16117c662b04292f3d837889b413e2fc12b633..0000000000000000000000000000000000000000 --- 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 1b28ce7eb2bbbfdd592fb3b18b089ccaaccde9cf..8150ffe348fab1d75481c417c9201e8955f665e4 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") diff --git a/pkg/microservice/systemconfig/core/service.go b/pkg/microservice/systemconfig/core/service.go deleted file mode 100644 index c5c485c712d40c30c35c3b73feba8280ebd6747f..0000000000000000000000000000000000000000 --- 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/microservice/systemconfig/server/rest/router.go b/pkg/microservice/systemconfig/server/rest/router.go deleted file mode 100644 index affff6292b900277fbdee88c2c0e65ab5cb129b6..0000000000000000000000000000000000000000 --- 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 18843b0a5ead8f194d217a281527d5152e939795..0000000000000000000000000000000000000000 --- 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 cd8efc7a548dcf0ab1648b15cec61c42a89dd0cc..0000000000000000000000000000000000000000 --- 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 -} diff --git a/pkg/microservice/user/core/handler/healthz.go b/pkg/microservice/user/core/handler/healthz.go deleted file mode 100644 index 74be72103a19978ecc845776404e071277ec1bb6..0000000000000000000000000000000000000000 --- 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 f48cfbd886f7b91a661dd7d4a782badfcb6a6dd6..3d25a07e6b0f5ae8b6603ed0b01b87dc151dad43 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 89296880488d910ccf32bdc450899c5dfd1e2f5c..b8d7ab56dc925c358b6e4dfc689384d9de8ed59e 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 51139f3a6bddf883dc16c6882df6b72498b8e540..0000000000000000000000000000000000000000 --- 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 18843b0a5ead8f194d217a281527d5152e939795..0000000000000000000000000000000000000000 --- 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 aa0f55e5dc51bc614d6fe57f1bf620e9918011b4..0000000000000000000000000000000000000000 --- 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/consts.go b/pkg/setting/consts.go index f3eca36922b223c709b7ad8b9155c017c2ef8bf5..a79463b2c5050ff295b4b61407567564e743c7bd 100644 --- a/pkg/setting/consts.go +++ b/pkg/setting/consts.go @@ -119,6 +119,10 @@ const ( ENVTokenExpiresAt = "TOKEN_EXPIRES_AT" ENVUserPort = "USER_PORT" + // config + ENVMysqlDexDB = "MYSQL_DEX_DB" + FeatureFlag = "feature-gates" + // initconfig ENVAdminEmail = "ADMIN_EMAIL" ENVAdminPassword = "ADMIN_PASSWORD" diff --git a/pkg/setting/types.go b/pkg/setting/types.go index 779f3c11d5b7c011c9a96a3c93ec0db51d800a47..e90b99f3b733b73dd20d3e2bc464ab18da9cc0fa 100644 --- a/pkg/setting/types.go +++ b/pkg/setting/types.go @@ -97,14 +97,11 @@ const ( Collie // 4 Cron // 5 HubServer // 6 - PodExec // 7 - SonarQube // 9 - WarpDrive // 10 - Minio // 11 - OPA // 12 - Policy // 13 - Config // 14 - User // 15 + SonarQube // 7 + WarpDrive // 8 + Minio // 9 + OPA // 10 + Policy // 11 ) type ServiceInfo struct { @@ -137,10 +134,6 @@ var Services = map[int]*ServiceInfo{ Name: "hub-server", Port: 26000, }, - PodExec: { - Name: "podexec", - Port: 27000, - }, SonarQube: { Name: "sonarqube", Port: 80, @@ -161,12 +154,4 @@ 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/policy/client.go b/pkg/shared/client/policy/client.go index 34f60e2925dc7969c082f0c2c70fd4f23094b2d2..b9cb0527059e39f525686643d790013d463d6116 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"), diff --git a/pkg/shared/client/systemconfig/client.go b/pkg/shared/client/systemconfig/client.go index cc9052c52bd4843ed7fc010cbc520bda346e39aa..b0176a994a36f1fe3e5c05b549c0074a88a97e33 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"), ) diff --git a/pkg/shared/client/user/client.go b/pkg/shared/client/user/client.go index 5e6d8ac15e89bad3c8d9917c9d714db7b45001be..4f43777ea0ff8e85dff1698c577b0819b9796a18 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"),