Skip to content

Commit

Permalink
Merge pull request #792 from hongweiliu17/STONEINTG-973
Browse files Browse the repository at this point in the history
feat(STONEINTG-973): cleanup the unused/unreleased override snapshot
  • Loading branch information
hongweiliu17 authored Jul 3, 2024
2 parents 717975e + 407d7fa commit 2f08ad5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
8 changes: 6 additions & 2 deletions cmd/snapshotgc/snapshotgc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"

"github.com/go-logr/logr"
"github.com/konflux-ci/operator-toolkit/metadata"
releasev1alpha1 "github.com/konflux-ci/release-service/api/v1alpha1"
applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
zap2 "go.uber.org/zap"
Expand Down Expand Up @@ -135,7 +136,7 @@ func getSnapshotsForNSReleases(
return snapToData, nil
}

// Gets all namespace snapshots that aren't associated with a release/binding
// Gets all namespace snapshots that aren't associated with a release
func getUnassociatedNSSnapshots(
cl client.Client,
snapToData map[string]snapshotData,
Expand Down Expand Up @@ -188,8 +189,11 @@ func getSnapshotsForRemoval(
keptNonPrSnaps := 0

for _, snap := range snapshots {
snap := snap
label, found := snap.GetLabels()["pac.test.appstudio.openshift.io/event-type"]
if !found || label == "push" || label == "Push" {
isOverrideSnapshot := metadata.HasLabelWithValue(&snap, "test.appstudio.openshift.io/type", "override")
// override snapshot does not have the event-type label, but we still want to add it to the cleanup list
if !found || label == "push" || label == "Push" || isOverrideSnapshot {
if keptNonPrSnaps < nonPrSnapshotsToKeep {
logger.V(1).Info(
"Skipping non-PR candidate snapshot",
Expand Down
16 changes: 13 additions & 3 deletions cmd/snapshotgc/snapshotgc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,26 +376,36 @@ var _ = Describe("Test garbage collection for snapshots", func() {
CreationTimestamp: metav1.NewTime(currentTime.Add(time.Hour * 10)),
},
}
overrideSnap := &applicationapiv1alpha1.Snapshot{
ObjectMeta: metav1.ObjectMeta{
Name: "override-snapshot",
CreationTimestamp: metav1.NewTime(currentTime),
Labels: map[string]string{
"test.appstudio.openshift.io/type": "override",
},
},
}

cl := fake.NewClientBuilder().
WithScheme(scheme).
WithLists(
&applicationapiv1alpha1.SnapshotList{
Items: []applicationapiv1alpha1.Snapshot{
*newerPRSnap, *olderPRSnap, *anotherOldPRSnap,
*newerNonPRSnap, *olderNonPRSnap,
*newerNonPRSnap, *olderNonPRSnap, *overrideSnap,
},
}).Build()
candidates := []applicationapiv1alpha1.Snapshot{
*newerPRSnap, *olderPRSnap, *newerNonPRSnap, *olderNonPRSnap,
*anotherOldPRSnap,
*anotherOldPRSnap, *overrideSnap,
}
output := getSnapshotsForRemoval(cl, candidates, 1, 1, logger)

Expect(output).To(HaveLen(3))
Expect(output).To(HaveLen(4))
Expect(output[0].Name).To(Equal("older-non-pr-snapshot"))
Expect(output[1].Name).To(Equal("another-old-pr-snapshot"))
Expect(output[2].Name).To(Equal("older-pr-snapshot"))
Expect(output[3].Name).To(Equal("override-snapshot"))
})
})

Expand Down

0 comments on commit 2f08ad5

Please sign in to comment.