Skip to content

Commit

Permalink
feat(manifest-service): skip minor commits during cleanup (#1889)
Browse files Browse the repository at this point in the history
Ref: SRX-41DILA
Releases that do not change any manifests are considered "minor". These releases are skipped during cleanup process.
  • Loading branch information
AminSlk authored Aug 16, 2024
1 parent 77403eb commit 541eb3b
Show file tree
Hide file tree
Showing 7 changed files with 623 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/minor-commits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Minor Commits
Sometimes there are some commits that do not change any manifests in any environments at all. We consider these commits as "Minor".
Usually during the cleanup process kuberpult will keep some releases (20 by default, see `git.releaseVersionsLimit` in values.yaml) and remove all old releases other than that. During this process kuberpult will skip these "minor" releases, meaning that it will keep at least 20 releases, that are not minor.
2 changes: 2 additions & 0 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ type DBReleaseMetaData struct {
SourceMessage string
DisplayVersion string
UndeployVersion bool
IsMinor bool
}

type DBReleaseManifests struct {
Expand Down Expand Up @@ -697,6 +698,7 @@ func (h *DBHandler) processReleaseRows(ctx context.Context, err error, rows *sql
SourceMessage: "",
DisplayVersion: "",
UndeployVersion: false,
IsMinor: false,
}
err = json.Unmarshal(([]byte)(metadataStr), &metaData)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,7 @@ func (s *State) WriteAllReleases(ctx context.Context, transaction *sql.Tx, app s
SourceCommitId: repoRelease.SourceCommitId,
SourceMessage: repoRelease.SourceMessage,
DisplayVersion: repoRelease.DisplayVersion,
IsMinor: false,
},
Deleted: false,
}
Expand Down
63 changes: 63 additions & 0 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,10 @@ func (c *CreateApplicationVersion) Transform(
if len(prevRelease) > 0 {
v = prevRelease[0].EslVersion
}
isMinor, err := c.checkMinorFlags(ctx, transaction, state.DBHandler, version)
if err != nil {
return "", err
}
release := db.DBReleaseWithMetaData{
EslVersion: 0,
ReleaseNumber: version,
Expand All @@ -648,6 +652,7 @@ func (c *CreateApplicationVersion) Transform(
SourceMessage: c.SourceMessage,
DisplayVersion: c.DisplayVersion,
UndeployVersion: false,
IsMinor: isMinor,
},
Created: time.Now(),
Deleted: false,
Expand Down Expand Up @@ -733,6 +738,63 @@ func (c *CreateApplicationVersion) Transform(
return fmt.Sprintf("created version %d of %q", version, c.Application), nil
}

func (c *CreateApplicationVersion) checkMinorFlags(ctx context.Context, transaction *sql.Tx, dbHandler *db.DBHandler, version uint64) (bool, error) {
allReleases, err := dbHandler.DBSelectAllReleasesOfApp(ctx, transaction, c.Application)
if err != nil {
return false, err
}
if allReleases == nil {
return false, err
}
releaseVersions := allReleases.Metadata.Releases
sort.Slice(releaseVersions, func(i, j int) bool { return releaseVersions[i] > releaseVersions[j] })
nextVersion := int64(-1)
previousVersion := int64(-1)
for i := len(releaseVersions) - 1; i >= 0; i-- {
if releaseVersions[i] > int64(version) {
nextVersion = releaseVersions[i]
break
}
}
for i := 0; i < len(releaseVersions); i++ {
if releaseVersions[i] < int64(version) {
previousVersion = releaseVersions[i]
break
}
}
if nextVersion != -1 {
nextRelease, err := dbHandler.DBSelectReleaseByVersion(ctx, transaction, c.Application, uint64(nextVersion))
if err != nil {
return false, err
}
nextRelease.Metadata.IsMinor = compareManifests(ctx, c.Manifests, nextRelease.Manifests.Manifests)
err = dbHandler.DBInsertRelease(ctx, transaction, *nextRelease, nextRelease.EslVersion)
if err != nil {
return false, err
}
}
if previousVersion == -1 {
return false, nil
}
previousRelease, err := dbHandler.DBSelectReleaseByVersion(ctx, transaction, c.Application, uint64(previousVersion))
if err != nil {
return false, err
}
return compareManifests(ctx, c.Manifests, previousRelease.Manifests.Manifests), nil
}

func compareManifests(ctx context.Context, firstManifests map[string]string, secondManifests map[string]string) bool {
if len(firstManifests) != len(secondManifests) {
return false
}
for key, value1 := range firstManifests {
if value2, exists := secondManifests[key]; !exists || value1 != value2 {
return false
}
}
return true
}

func getGenerator(ctx context.Context) uuid.GenerateUUIDs {
gen, ok := ctx.Value(ctxMarkerGenerateUuidKey).(uuid.GenerateUUIDs)
if !ok || gen == nil {
Expand Down Expand Up @@ -1141,6 +1203,7 @@ func (c *CreateUndeployApplicationVersion) Transform(
SourceMessage: "",
DisplayVersion: "",
UndeployVersion: true,
IsMinor: false,
},
Created: time.Now(),
Deleted: false,
Expand Down
Loading

0 comments on commit 541eb3b

Please sign in to comment.