Skip to content

Commit

Permalink
api: limit maximum number of snapshots in history
Browse files Browse the repository at this point in the history
This ensures that on repetitive failures, the number of snapshots does
not grow indefinitely due to there not being any in a superseded or
deployed state.

Signed-off-by: Hidde Beydals <hidde@hhh.computer>
  • Loading branch information
hiddeco committed Dec 1, 2023
1 parent 7f9160c commit bc7fb25
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion api/v2beta2/helmrelease_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const (
HelmReleaseFinalizer = "finalizers.fluxcd.io"
)

const (
// defaultMaxHistory is the default number of Helm release versions to keep.
defaultMaxHistory = 5
)

// Kustomize Helm PostRenderer specification.
type Kustomize struct {
// Strategic merge and JSON patches, defined as inline YAML objects,
Expand Down Expand Up @@ -1200,7 +1205,7 @@ func (in HelmRelease) GetTimeout() metav1.Duration {
// GetMaxHistory returns the configured MaxHistory, or the default of 5.
func (in HelmRelease) GetMaxHistory() int {
if in.Spec.MaxHistory == nil {
return 5
return defaultMaxHistory
}
return *in.Spec.MaxHistory
}
Expand Down
11 changes: 10 additions & 1 deletion api/v2beta2/snapshot_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ func (in Snapshots) Previous(ignoreTests bool) *Snapshot {
}

// Truncate removes all Snapshots up to the Previous deployed Snapshot.
// If there is no previous-deployed Snapshot, no Snapshots are removed.
// If there is no previous-deployed Snapshot, the most recent 5 Snapshots are
// retained.
func (in *Snapshots) Truncate(ignoreTests bool) {
if in.Len() < 2 {
return
}

in.SortByVersion()
for i := range (*in)[1:] {
s := (*in)[i+1]
Expand All @@ -96,6 +98,13 @@ func (in *Snapshots) Truncate(ignoreTests bool) {
}
}
}

if in.Len() > defaultMaxHistory {
// If none of the Snapshots are deployed or superseded, and there
// are more than the defaultMaxHistory, truncate to the most recent
// Snapshots.
*in = (*in)[:defaultMaxHistory]
}
}

// Snapshot captures a point-in-time copy of the status information for a Helm release,
Expand Down
18 changes: 18 additions & 0 deletions api/v2beta2/snapshot_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,24 @@ func TestSnapshots_Truncate(t *testing.T) {
}},
},
},
{
name: "retains most recent snapshots when all have failed",
in: Snapshots{
{Version: 6, Status: "deployed"},
{Version: 5, Status: "failed"},
{Version: 4, Status: "failed"},
{Version: 3, Status: "failed"},
{Version: 2, Status: "failed"},
{Version: 1, Status: "failed"},
},
want: Snapshots{
{Version: 6, Status: "deployed"},
{Version: 5, Status: "failed"},
{Version: 4, Status: "failed"},
{Version: 3, Status: "failed"},
{Version: 2, Status: "failed"},
},
},
{
name: "without previous snapshot",
in: Snapshots{
Expand Down

0 comments on commit bc7fb25

Please sign in to comment.