From 84ef0970608537abb29bd44a8cae2d3a7483e7d8 Mon Sep 17 00:00:00 2001 From: haiqing chen <2538302280@qq.com> Date: Mon, 23 Dec 2024 14:31:48 +0800 Subject: [PATCH] modified for recommend --- .../apps/dop/dicehub/release/db/release.go | 6 +-- internal/apps/dop/dicehub/release/meta.go | 5 ++- internal/apps/dop/dicehub/release/provider.go | 12 ++++-- .../dop/dicehub/release/release.service.go | 38 +++++++++---------- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/internal/apps/dop/dicehub/release/db/release.go b/internal/apps/dop/dicehub/release/db/release.go index 5d601528d0e..88a3b54e849 100644 --- a/internal/apps/dop/dicehub/release/db/release.go +++ b/internal/apps/dop/dicehub/release/db/release.go @@ -290,7 +290,7 @@ func (client *ReleaseConfigDB) GetReleasesByBranch(projectID, appID int64, gitBr // GetGroupRelease list release group by project_id and application_id func (client *ReleaseConfigDB) GetGroupRelease() ([]Release, error) { var releases []Release - if err := client.Select([]string{"project_id", "application_id"}).Group("project_id, application_id"). + if err := client.Group("project_id, application_id"). Find(&releases).Error; err != nil { return nil, err } @@ -300,7 +300,7 @@ func (client *ReleaseConfigDB) GetGroupRelease() ([]Release, error) { // ListExpireReleaseWithVersion list release that has not been referenced before a given point in time and version is not empty func (client *ReleaseConfigDB) ListExpireReleaseWithVersion(projectID int64, applicationID int64, before time.Time) ([]Release, error) { var releases []Release - if err := client.Select([]string{"release_id", "cluster_name", "version"}).Where("reference <= ?", 0).Where("updated_at < ?", before).Where("version != ''"). + if err := client.Where("reference <= ?", 0).Where("updated_at < ?", before).Where("version != ''"). Where("project_id = ?", projectID).Where("application_id = ?", applicationID).Where("is_project_release = 0 And is_formal=0"). Order("updated_at ASC").Find(&releases).Error; err != nil { return nil, err @@ -311,7 +311,7 @@ func (client *ReleaseConfigDB) ListExpireReleaseWithVersion(projectID int64, app // ListReleaseByAppAndProject list release by application_id and project_id, version is not empty func (client *ReleaseConfigDB) ListReleaseByAppAndProject(projectID int64, appID int64) ([]Release, error) { var releases []Release - if err := client.Select([]string{"release_id", "cluster_name", "version"}).Where("application_id = ? AND project_id =? AND version != ''", appID, projectID).Order("updated_at ASC").Find(&releases).Error; err != nil { + if err := client.Where("application_id = ? AND project_id =? AND version != ''", appID, projectID).Order("updated_at ASC").Find(&releases).Error; err != nil { return nil, err } return releases, nil diff --git a/internal/apps/dop/dicehub/release/meta.go b/internal/apps/dop/dicehub/release/meta.go index 0e89dad58d3..5d2de9989c4 100644 --- a/internal/apps/dop/dicehub/release/meta.go +++ b/internal/apps/dop/dicehub/release/meta.go @@ -15,7 +15,10 @@ package release type releaseConfig struct { - MaxTimeReserved string + MaxTimeReserved string + MaxReleaseLimit int32 // Versioned release limit + MinReleaseLimit int32 + MaxTimeVersionedRelease string // Versioned release limit } // ResourceType Release type diff --git a/internal/apps/dop/dicehub/release/provider.go b/internal/apps/dop/dicehub/release/provider.go index 38b9ba16af1..3ad49455bb7 100644 --- a/internal/apps/dop/dicehub/release/provider.go +++ b/internal/apps/dop/dicehub/release/provider.go @@ -53,8 +53,11 @@ import ( ) type config struct { - MaxTimeReserved string `file:"max_time_reserved" env:"RELEASE_MAX_TIME_RESERVED"` - GCSwitch bool `file:"gc_switch" env:"RELEASE_GC_SWITCH"` + MaxTimeReserved string `file:"max_time_reserved" env:"RELEASE_MAX_TIME_RESERVED"` + GCSwitch bool `file:"gc_switch" env:"RELEASE_GC_SWITCH"` + MinReleaseLimit int32 `file:"min_release_limit" env:"RELEASE_MIN_RELEASE_LIMIT" default:"10"` // 10 + MaxReleaseLimit int32 `file:"max_release_limit" env:"RELEASE_MAX_RELEASE_LIMIT" default:"30"` // 30 + MaxTimeVersionedRelease string `file:"max_time_versioned_release" env:"RELEASE_MAX_TIME_VERSIONED_RELEASE" default:"168"` // 7 days = 168 h } // +provider @@ -87,7 +90,10 @@ func (p *provider) Init(ctx servicehub.Context) error { bdl: p.bdl, Etcd: p.Etcd, Config: &releaseConfig{ - MaxTimeReserved: p.Cfg.MaxTimeReserved, + MaxTimeReserved: p.Cfg.MaxTimeReserved, + MaxReleaseLimit: p.Cfg.MaxReleaseLimit, + MinReleaseLimit: p.Cfg.MinReleaseLimit, + MaxTimeVersionedRelease: p.Cfg.MaxTimeVersionedRelease, }, ReleaseRule: release_rule.New(release_rule.WithDBClient(&dbclient.DBClient{ DBEngine: &dbengine.DBEngine{DB: p.DB}, diff --git a/internal/apps/dop/dicehub/release/release.service.go b/internal/apps/dop/dicehub/release/release.service.go index a5eb3b8accb..c6470b07ab9 100644 --- a/internal/apps/dop/dicehub/release/release.service.go +++ b/internal/apps/dop/dicehub/release/release.service.go @@ -1056,8 +1056,7 @@ func (s *ReleaseService) GetLatestReleasesByProjectAndVersion(projectID int64, v } func (s *ReleaseService) FindExpiredReleaseBefore(now time.Time) ([]db.Release, error) { - // 7 days before - d, err := time.ParseDuration(strutil.Concat("-", "168", "h")) + d, err := time.ParseDuration(strutil.Concat("-", s.Config.MaxTimeVersionedRelease, "h")) before := now.Add(d) releasesWithIDs, err := s.db.GetGroupRelease() if err != nil { @@ -1065,8 +1064,7 @@ func (s *ReleaseService) FindExpiredReleaseBefore(now time.Time) ([]db.Release, } // Save all releases that need to be deleted deleteReleases := make([]db.Release, 0) - for i := range releasesWithIDs { - releaseWithID := releasesWithIDs[i] + for _, releaseWithID := range releasesWithIDs { // All release of current application releases, err := s.db.ListReleaseByAppAndProject(releaseWithID.ProjectID, releaseWithID.ApplicationID) if err != nil { @@ -1079,28 +1077,31 @@ func (s *ReleaseService) FindExpiredReleaseBefore(now time.Time) ([]db.Release, return nil, err } - releaseCount := len(releases) - expiredCount := len(expiredReleases) + releaseCount := int32(len(releases)) + expiredCount := int32(len(expiredReleases)) - if releaseCount <= 10 { - // If the quantity of release is less than 10, there is no need to delete + minReleaseLimit := s.Config.MinReleaseLimit + maxReleaseLimit := s.Config.MaxReleaseLimit + + if releaseCount <= minReleaseLimit { + // If the quantity of release is less than minReleaseLimit, there is no need to delete continue - } else if releaseCount < 30 { - // delete releases utils the quantity of release is 10 - if releaseCount-expiredCount < 10 { - ableToDeleteCount := releaseCount - 10 + } else if releaseCount < maxReleaseLimit { + // delete releases utils the quantity of release is minReleaseLimit + if releaseCount-expiredCount < minReleaseLimit { + ableToDeleteCount := releaseCount - minReleaseLimit deleteReleases = append(deleteReleases, expiredReleases[:ableToDeleteCount]...) } else { deleteReleases = append(deleteReleases, expiredReleases...) } } else { - // delete releases utils the quantity of release is 10-30 - if releaseCount-expiredCount < 10 { - deleteReleases = append(deleteReleases, expiredReleases[:releaseCount-10]...) + // delete releases utils the quantity of release is minReleaseLimit~maxReleaseLimit + if releaseCount-expiredCount < minReleaseLimit { + deleteReleases = append(deleteReleases, expiredReleases[:releaseCount-minReleaseLimit]...) } else { deleteReleases = append(deleteReleases, expiredReleases...) - if releaseCount-expiredCount > 30 { - deleteReleases = append(deleteReleases, releases[:releaseCount-expiredCount-30]...) + if releaseCount-expiredCount > maxReleaseLimit { + deleteReleases = append(deleteReleases, releases[:releaseCount-expiredCount-maxReleaseLimit]...) } } } @@ -1142,8 +1143,7 @@ func (s *ReleaseService) RemoveDeprecatedsReleases(now time.Time) error { deleteRelease := append(versionRelease, versionNoneRelease...) - for i := range deleteRelease { - release := deleteRelease[i] + for _, release := range deleteRelease { images, err := s.imageDB.GetImagesByRelease(release.ReleaseID) if err != nil { logrus.Warnf(err.Error())