Skip to content

Commit

Permalink
fix helm environment has duplicated service data in some case
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <zhaoyu@koderover.com>
  • Loading branch information
PetrusZ committed Oct 16, 2024
1 parent d4b0432 commit af61576
Show file tree
Hide file tree
Showing 23 changed files with 570 additions and 266 deletions.
2 changes: 1 addition & 1 deletion debug/debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dlv --headless --log --listen :9009 --api-version 2 --accept-multiclient --wd=$(
DLV_PID=$!

# 捕获Ctrl-C信号并终止Delve进程
trap "kill -9 $DLV_PID" SIGINT
trap "pkill -9 dlv $SERVICE_NAME"

# 等待Delve进程结束
wait $DLV_PID
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ require (
github.com/stretchr/testify v1.9.0
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a
github.com/swaggo/gin-swagger v1.5.3
github.com/swaggo/swag v1.16.3
github.com/tidwall/gjson v1.14.3
github.com/xanzy/go-gitlab v0.73.1
go.mongodb.org/mongo-driver v1.10.2
Expand Down Expand Up @@ -271,6 +270,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/swaggo/swag v1.16.3 // indirect
github.com/tdewolff/parse/v2 v2.7.11 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
Expand Down
35 changes: 35 additions & 0 deletions pkg/microservice/aslan/core/common/repository/models/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
templatemodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models/template"
commontypes "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/types"
"github.com/koderover/zadig/v2/pkg/setting"
"github.com/koderover/zadig/v2/pkg/tool/log"
)

type Product struct {
Expand Down Expand Up @@ -344,6 +345,40 @@ func (p *Product) GetChartServiceMap() map[string]*ProductService {
return ret
}

func (p *Product) LintServices() {
svcMap := make(map[string]*ProductService)
chartSvcMap := make(map[string]*ProductService)

shouldKeepService := func(svcMap map[string]*ProductService, svc *ProductService, key string) bool {
if prevSvc, ok := svcMap[key]; ok {
if prevSvc.Revision < svc.Revision {
svcMap[key] = svc
return true
}
log.Warnf("service %s has older revision %d, drop it", key, svc.Revision)
return false
}
svcMap[key] = svc
return true
}

for i, group := range p.Services {
newGroup := []*ProductService{}
for _, svc := range group {
if svc.FromZadig() {
if shouldKeepService(svcMap, svc, svc.ServiceName) {
newGroup = append(newGroup, svc)
}
} else {
if shouldKeepService(chartSvcMap, svc, svc.ReleaseName) {
newGroup = append(newGroup, svc)
}
}
}
p.Services[i] = newGroup
}
}

func (p *Product) GetProductSvcNames() []string {
ret := make([]string, 0)
for _, group := range p.Services {
Expand Down
79 changes: 77 additions & 2 deletions pkg/microservice/aslan/core/common/repository/mongodb/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (c *ProductColl) Find(opt *ProductFindOptions) (*models.Product, error) {
if err != nil && mongo.ErrNoDocuments == err && opt.IgnoreNotFoundErr {
return nil, nil
}
res.LintServices()
return res, err
}

Expand Down Expand Up @@ -442,9 +443,9 @@ func (c *ProductColl) Create(args *models.Product) error {
return err
}

// UpdateGroup TODO UpdateGroup needs to be optimized
// @todo UpdateGroup needs to be optimized
// Service info may be override when updating multiple services in same group at the sametime
func (c *ProductColl) UpdateGroup(envName, productName string, groupIndex int, group []*models.ProductService) error {
func (c *ProductColl) UpdateServicesGroup(productName, envName string, groupIndex int, group []*models.ProductService) error {
serviceGroup := fmt.Sprintf("services.%d", groupIndex)
query := bson.M{
"env_name": envName,
Expand All @@ -460,6 +461,80 @@ func (c *ProductColl) UpdateGroup(envName, productName string, groupIndex int, g
return err
}

// @todo UpdateServices needs to be optimized
// Service info may be override when updating multiple services at the sametime
func (c *ProductColl) UpdateAllServices(productName, envName string, services [][]*models.ProductService) error {
query := bson.M{
"env_name": envName,
"product_name": productName,
}
change := bson.M{
"update_time": time.Now().Unix(),
"services": services,
}

_, err := c.UpdateOne(mongotool.SessionContext(context.TODO(), c.Session), query, bson.M{"$set": change})

return err
}

// Note: Only use for update a service
// UpdateOneService updates a specific service in a group by its index
func (c *ProductColl) UpdateOneService(productName, envName string, groupIndex, serviceIndex int, service *models.ProductService) error {
servicePath := fmt.Sprintf("services.%d.%d", groupIndex, serviceIndex)
query := bson.M{
"env_name": envName,
"product_name": productName,
fmt.Sprintf("%s.service_name", servicePath): service.ServiceName, // ensure service_name equals
servicePath: bson.M{"$exists": true}, // ensure the service exists
}
change := bson.M{
"$set": bson.M{
servicePath: service,
"update_time": time.Now().Unix(),
},
}

result, err := c.UpdateOne(mongotool.SessionContext(context.TODO(), c.Session), query, change)
if err != nil {
return err
}

if result.MatchedCount == 0 {
return fmt.Errorf("no matching record found to update at %s", servicePath)
}

return nil
}

// Note: Only use for add a service
// AddOneService adds a specific service in a group by its index if it does not already exist
func (c *ProductColl) AddOneService(productName, envName string, groupIndex, serviceIndex int, service *models.ProductService) error {
servicePath := fmt.Sprintf("services.%d.%d", groupIndex, serviceIndex)
query := bson.M{
"env_name": envName,
"product_name": productName,
servicePath: bson.M{"$exists": false}, // ensure the service does not exist
}
change := bson.M{
"$set": bson.M{
servicePath: service,
"update_time": time.Now().Unix(),
},
}

result, err := c.UpdateOne(mongotool.SessionContext(context.TODO(), c.Session), query, change)
if err != nil {
return err
}

if result.MatchedCount == 0 {
return fmt.Errorf("service already exists at %s", servicePath)
}

return nil
}

func (c *ProductColl) UpdateDeployStrategyAndGlobalVariable(envName, productName string, deployStrategy map[string]string, globalVariables []*types.GlobalVariableKV) error {
query := bson.M{
"env_name": envName,
Expand Down
3 changes: 2 additions & 1 deletion pkg/microservice/aslan/core/common/service/dbinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ import (
"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
commonmodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
commonrepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb"
commonutil "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/util"
"github.com/koderover/zadig/v2/pkg/tool/crypto"
)

func ListDBInstances(encryptedKey string, log *zap.SugaredLogger) ([]*commonmodels.DBInstance, error) {
aesKey, err := GetAesKeyFromEncryptedKey(encryptedKey, log)
aesKey, err := commonutil.GetAesKeyFromEncryptedKey(encryptedKey, log)
if err != nil {
log.Errorf("ListDBInstances GetAesKeyFromEncryptedKey err:%v", err)
return nil, err
Expand Down
96 changes: 0 additions & 96 deletions pkg/microservice/aslan/core/common/service/helm.go

This file was deleted.

Loading

0 comments on commit af61576

Please sign in to comment.