Skip to content

Commit

Permalink
merge main go.mod
Browse files Browse the repository at this point in the history
Signed-off-by: blade <ddh2022lin@gmail.com>
  • Loading branch information
riven-blade committed Jan 11, 2025
2 parents 1df72ed + a7ef527 commit 25693e2
Show file tree
Hide file tree
Showing 29 changed files with 752 additions and 503 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/docker/go-connections v0.4.0
github.com/envoyproxy/go-control-plane v0.11.1-0.20230524094728-9239064ad72f
github.com/gin-gonic/gin v1.9.1
github.com/go-co-op/gocron v1.17.0
github.com/go-co-op/gocron/v2 v2.14.0
github.com/go-ldap/ldap/v3 v3.3.0
github.com/go-redsync/redsync/v4 v4.11.0
github.com/go-resty/resty/v2 v2.7.0
Expand Down Expand Up @@ -82,7 +82,7 @@ require (
github.com/shirou/gopsutil/v3 v3.22.8
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a
github.com/swaggo/gin-swagger v1.5.3
github.com/tidwall/gjson v1.14.3
Expand Down Expand Up @@ -234,6 +234,7 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect
Expand Down
99 changes: 99 additions & 0 deletions pkg/cli/upgradeassistant/cmd/migrate/321.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2024 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 migrate

import (
"context"
"fmt"
"time"

"github.com/koderover/zadig/v2/pkg/cli/upgradeassistant/internal/upgradepath"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
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"
"github.com/koderover/zadig/v2/pkg/shared/handler"
"github.com/koderover/zadig/v2/pkg/util"
"go.mongodb.org/mongo-driver/bson"
)

func init() {
upgradepath.RegisterHandler("3.2.0", "3.2.1", V320ToV321)
upgradepath.RegisterHandler("3.2.1", "3.2.0", V321ToV320)
}

func V320ToV321() error {
ctx := handler.NewBackgroupContext()

ctx.Logger.Infof("-------- start migrate release plan cronjob --------")
err := migrateReleasePlanCron(ctx)
if err != nil {
err = fmt.Errorf("failed to migrate release plan cronjob, error: %w", err)
ctx.Logger.Error(err)
return err
}

return nil
}

func V321ToV320() error {
return nil
}

func migrateReleasePlanCron(ctx *handler.Context) error {
// delete all release plan cronjob first
_, err := commonrepo.NewCronjobColl().DeleteMany(ctx,
bson.M{"type": "release_plan"},
)
if err != nil {
return fmt.Errorf("failed to delete release plan cronjobs, error: %w", err)
}

cursor, err := commonrepo.NewReleasePlanColl().ListByCursor()
if err != nil {
return fmt.Errorf("failed to list release plans, error: %w", err)
}
for cursor.Next(context.Background()) {
var releasePlan models.ReleasePlan
if err := cursor.Decode(&releasePlan); err != nil {
return err
}

if releasePlan.ScheduleExecuteTime != 0 && releasePlan.Status == config.StatusExecuting {
if time.Unix(releasePlan.ScheduleExecuteTime, 0).After(time.Now()) {
releasePlanCronName := util.GetReleasePlanCronName(releasePlan.ID.Hex(), releasePlan.Name, releasePlan.Index)
cronjob := &commonmodels.Cronjob{
Enabled: true,
Name: releasePlanCronName,
Type: "release_plan",
JobType: string(config.UnixstampSchedule),
UnixStamp: releasePlan.ScheduleExecuteTime,
ReleasePlanArgs: &commonmodels.ReleasePlanArgs{
ID: releasePlan.ID.Hex(),
Name: releasePlan.Name,
Index: releasePlan.Index,
},
}
if err := commonrepo.NewCronjobColl().Upsert(cronjob); err != nil {
return fmt.Errorf("failed to create new release plan schedule job, error: %w", err)
}
}
}
}

return nil
}
2 changes: 2 additions & 0 deletions pkg/microservice/aslan/config/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const (
TimingSchedule ScheduleType = "timing"
// GapSchedule 间隔循环
GapSchedule ScheduleType = "gap"
// UnixstampSchedule 时间戳
UnixstampSchedule ScheduleType = "unix_stamp"
)

type SlackNotifyType string
Expand Down
5 changes: 4 additions & 1 deletion pkg/microservice/aslan/core/build/handler/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ func OpenAPIGetBuildModule(c *gin.Context) {
return
}

serviceName := c.Query("serviceName")
serviceModule := c.Query("serviceModule")

// authorization checks
if !ctx.Resources.IsSystemAdmin {
if _, ok := ctx.Resources.ProjectAuthInfo[projectKey]; !ok {
Expand All @@ -230,5 +233,5 @@ func OpenAPIGetBuildModule(c *gin.Context) {
}
}

ctx.Resp, ctx.RespErr = buildservice.OpenAPIGetBuildModule(name, projectKey, ctx.Logger)
ctx.Resp, ctx.RespErr = buildservice.OpenAPIGetBuildModule(name, serviceName, serviceModule, projectKey, ctx.Logger)
}
45 changes: 33 additions & 12 deletions pkg/microservice/aslan/core/build/service/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func OpenAPIListBuildModules(projectName string, pageNum, pageSize int64, logger
return resp, nil
}

func OpenAPIGetBuildModule(name, projectName string, logger *zap.SugaredLogger) (*OpenAPIBuildDetailResp, error) {
func OpenAPIGetBuildModule(name, serviceName, serviceModule, projectName string, logger *zap.SugaredLogger) (*OpenAPIBuildDetailResp, error) {
opt := &commonrepo.BuildFindOption{
Name: name,
ProductName: projectName,
Expand Down Expand Up @@ -348,18 +348,39 @@ func OpenAPIGetBuildModule(name, projectName string, logger *zap.SugaredLogger)
}

resp.Repos = make([]*OpenAPIRepo, 0)
for _, rp := range build.Repos {
repo := &OpenAPIRepo{
RepoName: rp.RepoName,
Branch: rp.Branch,
Source: rp.Source,
RepoOwner: rp.RepoOwner,
RemoteName: rp.RemoteName,
CheckoutPath: rp.CheckoutPath,
Submodules: rp.SubModules,
Hidden: rp.Hidden,
if serviceName == "" || serviceModule == "" || build.TemplateID == "" {
for _, rp := range build.Repos {
repo := &OpenAPIRepo{
RepoName: rp.RepoName,
Branch: rp.Branch,
Source: rp.Source,
RepoOwner: rp.RepoOwner,
RemoteName: rp.RemoteName,
CheckoutPath: rp.CheckoutPath,
Submodules: rp.SubModules,
Hidden: rp.Hidden,
}
resp.Repos = append(resp.Repos, repo)
}
} else {
for _, svcBuild := range build.Targets {
if svcBuild.ServiceName == serviceName && svcBuild.ServiceModule == serviceModule {
for _, rp := range svcBuild.Repos {
repo := &OpenAPIRepo{
RepoName: rp.RepoName,
Branch: rp.Branch,
Source: rp.Source,
RepoOwner: rp.RepoOwner,
RemoteName: rp.RemoteName,
CheckoutPath: rp.CheckoutPath,
Submodules: rp.SubModules,
Hidden: rp.Hidden,
}
resp.Repos = append(resp.Repos, repo)
}
break
}
}
resp.Repos = append(resp.Repos, repo)
}

resp.TargetServices = make([]*commonmodels.ServiceWithModule, 0)
Expand Down
2 changes: 1 addition & 1 deletion pkg/microservice/aslan/core/code/client/gitee/gitee.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (c *Client) ListProjects(opt client.ListOpt) ([]*client.Project, error) {
for _, project := range projects {
res = append(res, &client.Project{
ID: project.ID,
Name: project.Name,
Name: project.Path,
DefaultBranch: project.DefaultBranch,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Cronjob struct {
Name string `bson:"name" json:"name"`
Type string `bson:"type" json:"type"`
Number uint64 `bson:"number" json:"number"`
UnixStamp int64 `bson:"unix_stamp" json:"unix_stamp"`
Frequency string `bson:"frequency" json:"frequency"`
Time string `bson:"time" json:"time"`
Cron string `bson:"cron" json:"cron"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ type Schedule struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Number uint64 `bson:"number" json:"number"`
Frequency string `bson:"frequency" json:"frequency"`
UnixStamp int64 `bson:"unix_stamp" json:"unix_stamp"`
Time string `bson:"time" json:"time"`
MaxFailures int `bson:"max_failures,omitempty" json:"max_failures,omitempty"`
TaskArgs *TaskArgs `bson:"task_args,omitempty" json:"task_args,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (c *ReleasePlanColl) EnsureIndex(ctx context.Context) error {
Keys: bson.M{"success_time": 1},
Options: options.Index().SetUnique(false),
},
{
Keys: bson.M{"update_time": 1},
Options: options.Index().SetUnique(false),
},
}

_, err := c.Indexes().CreateMany(ctx, mod)
Expand Down Expand Up @@ -124,14 +128,24 @@ func (c *ReleasePlanColl) DeleteByID(ctx context.Context, idString string) error
return err
}

type SortReleasePlanBy string

const (
SortReleasePlanByIndex SortReleasePlanBy = "index"
SortReleasePlanByUpdateTime SortReleasePlanBy = "update_time"
)

type ListReleasePlanOption struct {
PageNum int64
PageSize int64
Name string
Manager string
SuccessTimeStart int64
SuccessTimeEnd int64
UpdateTimeStart int64
UpdateTimeEnd int64
IsSort bool
SortBy SortReleasePlanBy
ExcludedFields []string
Status config.ReleasePlanStatus
}
Expand All @@ -147,8 +161,15 @@ func (c *ReleasePlanColl) ListByOptions(opt *ListReleasePlanOption) ([]*models.R
ctx := context.Background()
opts := options.Find()
if opt.IsSort {
opts.SetSort(bson.D{{"index", -1}})
if opt.SortBy == SortReleasePlanByIndex {
opts.SetSort(bson.D{{"index", -1}})
} else if opt.SortBy == SortReleasePlanByUpdateTime {
opts.SetSort(bson.D{{"update_time", -1}})
} else {
opts.SetSort(bson.D{{"index", -1}})
}
}

if opt.PageNum > 0 && opt.PageSize > 0 {
opts.SetSkip((opt.PageNum - 1) * opt.PageSize)
opts.SetLimit(opt.PageSize)
Expand All @@ -162,6 +183,9 @@ func (c *ReleasePlanColl) ListByOptions(opt *ListReleasePlanOption) ([]*models.R
if opt.SuccessTimeStart > 0 && opt.SuccessTimeEnd > 0 {
query["success_time"] = bson.M{"$gte": opt.SuccessTimeStart, "$lte": opt.SuccessTimeEnd}
}
if opt.UpdateTimeStart > 0 && opt.UpdateTimeEnd > 0 {
query["update_time"] = bson.M{"$gte": opt.UpdateTimeStart, "$lte": opt.UpdateTimeEnd}
}
if opt.Status != "" {
query["status"] = opt.Status
}
Expand Down Expand Up @@ -230,3 +254,8 @@ func (c *ReleasePlanColl) ListFinishedReleasePlan(startTime, endTime int64) ([]*

return resp, nil
}

func (c *ReleasePlanColl) ListByCursor() (*mongo.Cursor, error) {
query := bson.M{}
return c.Collection.Find(context.TODO(), query)
}
13 changes: 7 additions & 6 deletions pkg/microservice/aslan/core/common/service/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
)

type CronjobPayload struct {
Name string `json:"name"`
ProductName string `json:"product_name"`
Action string `json:"action"`
JobType string `json:"job_type"`
DeleteList []string `json:"delete_list,omitempty"`
JobList []*models.Schedule `json:"job_list,omitempty"`
Name string `json:"name"`
ProductName string `json:"product_name"`
Action string `json:"action"`
JobType string `json:"job_type"`
ScheduleType string `json:"schedule_type"`
DeleteList []string `json:"delete_list,omitempty"`
JobList []*models.Schedule `json:"job_list,omitempty"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ func (w *Service) getNotificationContent(notify *models.NotifyCtl, task *models.
}
}
}
if image != "" {
if image != "" && !strings.HasPrefix(image, "{{.") && !strings.Contains(image, "}}") {
jobTplcontent += fmt.Sprintf("{{if eq .WebHookType \"dingding\"}}##### {{end}}**镜像信息**:%s \n", image)
mailJobTplcontent += fmt.Sprintf("镜像信息:%s \n", image)
workflowNotifyJobTaskSpec.Image = image
Expand Down
3 changes: 3 additions & 0 deletions pkg/microservice/aslan/core/cron/handler/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type cronjobResp struct {
Name string `json:"name"`
Type string `json:"type"`
Number uint64 `json:"number"`
UnixStamp int64 `json:"unix_stamp"`
Frequency string `json:"frequency"`
Time string `json:"time"`
Cron string `json:"cron"`
Expand Down Expand Up @@ -92,6 +93,7 @@ func ListActiveCronjobFailsafe(c *gin.Context) {
Name: cronjob.Name,
Type: cronjob.Type,
Number: cronjob.Number,
UnixStamp: cronjob.UnixStamp,
Frequency: cronjob.Frequency,
Time: cronjob.Time,
Cron: cronjob.Cron,
Expand Down Expand Up @@ -124,6 +126,7 @@ func ListActiveCronjob(c *gin.Context) {
Name: cronjob.Name,
Type: cronjob.Type,
Number: cronjob.Number,
UnixStamp: cronjob.UnixStamp,
Frequency: cronjob.Frequency,
Time: cronjob.Time,
Cron: cronjob.Cron,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ func GetAffectedServices(productName, envName string, arg *K8sRendersetArg, log
func GeneEstimatedValues(productName, envName, serviceOrReleaseName, scene, format string, arg *EstimateValuesArg, isHelmChartDeploy bool, log *zap.SugaredLogger) (interface{}, error) {
var (
productSvc *commonmodels.ProductService
tmplSvc *commonmodels.Service
tmplSvc *commonmodels.Service
productInfo *commonmodels.Product
err error
)
Expand Down Expand Up @@ -3791,6 +3791,7 @@ func cronJobToSchedule(input *commonmodels.Cronjob) *commonmodels.Schedule {
return &commonmodels.Schedule{
ID: input.ID,
Number: input.Number,
UnixStamp: input.UnixStamp,
Frequency: input.Frequency,
Time: input.Time,
MaxFailures: input.MaxFailure,
Expand Down
1 change: 0 additions & 1 deletion pkg/microservice/aslan/core/log/handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func (*Router) Inject(router *gin.RouterGroup) {
sse := router.Group("sse")
{
sse.GET("/pods/:podName/containers/:containerName", GetContainerLogsSSE)
sse.GET("/production/pods/:podName/containers/:containerName", GetProductionEnvContainerLogsSSE)
sse.GET("/testing/:test_name/tasks/:task_id", GetTestingContainerLogsSSE)
sse.GET("/scanning/:id/task/:scan_id", GetScanningContainerLogsSSE)
sse.GET("/v4/workflow/:workflowName/:taskID/:jobName/:lines", GetWorkflowJobContainerLogsSSE)
Expand Down
Loading

0 comments on commit 25693e2

Please sign in to comment.