diff --git a/api/v2beta2/helmrelease_types.go b/api/v2beta2/helmrelease_types.go index 56cf9709d..eaea9c01c 100644 --- a/api/v2beta2/helmrelease_types.go +++ b/api/v2beta2/helmrelease_types.go @@ -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, @@ -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 } diff --git a/api/v2beta2/snapshot_types.go b/api/v2beta2/snapshot_types.go index 2258acc58..587667665 100644 --- a/api/v2beta2/snapshot_types.go +++ b/api/v2beta2/snapshot_types.go @@ -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] @@ -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, diff --git a/api/v2beta2/snapshot_types_test.go b/api/v2beta2/snapshot_types_test.go index 8447868b9..32911877f 100644 --- a/api/v2beta2/snapshot_types_test.go +++ b/api/v2beta2/snapshot_types_test.go @@ -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{