Skip to content

Commit

Permalink
modified for recommend
Browse files Browse the repository at this point in the history
  • Loading branch information
haiqingchq committed Dec 23, 2024
1 parent b4dd3ec commit 84ef097
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
6 changes: 3 additions & 3 deletions internal/apps/dop/dicehub/release/db/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Check warning on line 296 in internal/apps/dop/dicehub/release/db/release.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/dicehub/release/db/release.go#L295-L296

Added lines #L295 - L296 were not covered by tests
Expand All @@ -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
Expand All @@ -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
}

Check warning on line 316 in internal/apps/dop/dicehub/release/db/release.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/dicehub/release/db/release.go#L315-L316

Added lines #L315 - L316 were not covered by tests
return releases, nil
Expand Down
5 changes: 4 additions & 1 deletion internal/apps/dop/dicehub/release/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions internal/apps/dop/dicehub/release/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,

Check warning on line 96 in internal/apps/dop/dicehub/release/provider.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/dicehub/release/provider.go#L93-L96

Added lines #L93 - L96 were not covered by tests
},
ReleaseRule: release_rule.New(release_rule.WithDBClient(&dbclient.DBClient{
DBEngine: &dbengine.DBEngine{DB: p.DB},
Expand Down
38 changes: 19 additions & 19 deletions internal/apps/dop/dicehub/release/release.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,17 +1056,15 @@ 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 {
return nil, err
}

Check warning on line 1064 in internal/apps/dop/dicehub/release/release.service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/dicehub/release/release.service.go#L1063-L1064

Added lines #L1063 - L1064 were not covered by tests
// 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 {
Expand All @@ -1079,28 +1077,31 @@ func (s *ReleaseService) FindExpiredReleaseBefore(now time.Time) ([]db.Release,
return nil, err
}

Check warning on line 1078 in internal/apps/dop/dicehub/release/release.service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/dicehub/release/release.service.go#L1077-L1078

Added lines #L1077 - L1078 were not covered by tests

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]...)
}

Check warning on line 1105 in internal/apps/dop/dicehub/release/release.service.go

View check run for this annotation

Codecov / codecov/patch

internal/apps/dop/dicehub/release/release.service.go#L1089-L1105

Added lines #L1089 - L1105 were not covered by tests
}
}
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 84ef097

Please sign in to comment.