Repository navigation
⚠️ feat: add csv-based healthcheck retry backoff #3941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
grokspawn
wants to merge
1
commit into
operator-framework:master
from
grokspawn:csv-healthcheck-backoff
+207
−8
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package olm | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
| "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/client-go/util/workqueue" | ||
| ) | ||
|
|
||
| const ( | ||
| healthCheckBackoffBaseDelay = 10 * time.Second | ||
| healthCheckBackoffMaxDelay = 5 * time.Minute | ||
| ) | ||
|
|
||
| // csvHealthCheckBackoff tracks repeated transient Deployment readiness failures | ||
| // independently for each CSV. | ||
| type csvHealthCheckBackoff struct { | ||
| rateLimiter workqueue.TypedRateLimiter[types.NamespacedName] | ||
| } | ||
|
|
||
| func newCSVHealthCheckBackoff() *csvHealthCheckBackoff { | ||
| return &csvHealthCheckBackoff{ | ||
| rateLimiter: workqueue.NewTypedItemExponentialFailureRateLimiter[types.NamespacedName]( | ||
| healthCheckBackoffBaseDelay, | ||
| healthCheckBackoffMaxDelay, | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| func (b *csvHealthCheckBackoff) next(key types.NamespacedName) time.Duration { | ||
| return b.rateLimiter.When(key) | ||
| } | ||
|
|
||
| func (b *csvHealthCheckBackoff) forget(key types.NamespacedName) { | ||
| b.rateLimiter.Forget(key) | ||
| } | ||
|
|
||
| func (a *Operator) requeueAfterStrategyError(csv *v1alpha1.ClusterServiceVersion, strategyErr error) error { | ||
| key := types.NamespacedName{Namespace: csv.GetNamespace(), Name: csv.GetName()} | ||
| if install.ReasonForError(strategyErr) == install.StrategyErrReasonWaiting { | ||
| return a.csvQueueSet.RequeueAfter(csv.GetNamespace(), csv.GetName(), a.healthCheckBackoff.next(key)) | ||
| } | ||
|
|
||
| a.healthCheckBackoff.forget(key) | ||
| return a.csvQueueSet.Requeue(csv.GetNamespace(), csv.GetName()) | ||
| } |
140 changes: 140 additions & 0 deletions
140
pkg/controller/operators/olm/health_check_backoff_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package olm | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
| "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install" | ||
| "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer" | ||
| "github.com/stretchr/testify/require" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/client-go/tools/record" | ||
| "k8s.io/client-go/util/workqueue" | ||
| ) | ||
|
|
||
| type delayedQueueItem struct { | ||
| key types.NamespacedName | ||
| delay time.Duration | ||
| } | ||
|
|
||
| type recordingCSVQueue struct { | ||
| workqueue.TypedRateLimitingInterface[types.NamespacedName] | ||
| added []types.NamespacedName | ||
| delayed []delayedQueueItem | ||
| } | ||
|
|
||
| func (q *recordingCSVQueue) Add(key types.NamespacedName) { | ||
| q.added = append(q.added, key) | ||
| } | ||
|
|
||
| func (q *recordingCSVQueue) AddAfter(key types.NamespacedName, delay time.Duration) { | ||
| q.delayed = append(q.delayed, delayedQueueItem{key: key, delay: delay}) | ||
| } | ||
|
|
||
| func TestCSVHealthCheckBackoffIsExponentialAndCapped(t *testing.T) { | ||
| backoff := newCSVHealthCheckBackoff() | ||
| key := types.NamespacedName{Namespace: "operators", Name: "sample"} | ||
| otherKey := types.NamespacedName{Namespace: "operators", Name: "other"} | ||
| want := []time.Duration{ | ||
| 10 * time.Second, | ||
| 20 * time.Second, | ||
| 40 * time.Second, | ||
| 80 * time.Second, | ||
| 160 * time.Second, | ||
| 5 * time.Minute, | ||
| 5 * time.Minute, | ||
| } | ||
|
|
||
| for _, expected := range want { | ||
| require.Equal(t, expected, backoff.next(key)) | ||
| } | ||
| require.Equal(t, 10*time.Second, backoff.next(otherKey), "backoff should be isolated per CSV") | ||
|
|
||
| backoff.forget(key) | ||
| require.Equal(t, 10*time.Second, backoff.next(key), "forget should reset the retry sequence") | ||
| } | ||
|
|
||
| func TestRequeueAfterStrategyErrorDelaysOnlyDeploymentWaits(t *testing.T) { | ||
| queue := &recordingCSVQueue{} | ||
| queueSet := queueinformer.NewEmptyResourceQueueSet() | ||
| queueSet.Set("operators", queue) | ||
| op := &Operator{ | ||
| csvQueueSet: queueSet, | ||
| healthCheckBackoff: newCSVHealthCheckBackoff(), | ||
| } | ||
| csv := &v1alpha1.ClusterServiceVersion{ObjectMeta: metav1.ObjectMeta{ | ||
| Namespace: "operators", | ||
| Name: "sample", | ||
| }} | ||
| waiting := install.StrategyError{Reason: install.StrategyErrReasonWaiting, Message: "deployment is not ready"} | ||
|
|
||
| require.NoError(t, op.requeueAfterStrategyError(csv, waiting)) | ||
| require.Empty(t, queue.added, "health waits must not use immediate queue.Add") | ||
| require.Equal(t, []delayedQueueItem{{ | ||
| key: types.NamespacedName{Namespace: "operators", Name: "sample"}, | ||
| delay: 10 * time.Second, | ||
| }}, queue.delayed) | ||
|
|
||
| require.NoError(t, op.requeueAfterStrategyError(csv, waiting)) | ||
| require.Equal(t, 20*time.Second, queue.delayed[1].delay) | ||
|
|
||
| other := install.StrategyError{Reason: install.StrategyErrDeploymentUpdated, Message: "deployment spec changed"} | ||
| require.NoError(t, op.requeueAfterStrategyError(csv, other)) | ||
| require.Equal(t, []types.NamespacedName{{Namespace: "operators", Name: "sample"}}, queue.added) | ||
|
|
||
| require.NoError(t, op.requeueAfterStrategyError(csv, waiting)) | ||
| require.Equal(t, 10*time.Second, queue.delayed[2].delay, "non-waiting errors should reset health backoff") | ||
| } | ||
|
|
||
| func TestUpdateInstallStatusResetsHealthBackoffWhenDeploymentIsHealthy(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| op, err := NewFakeOperator(ctx, withNamespaces("health-backoff-test")) | ||
| require.NoError(t, err) | ||
| op.recorder = &record.FakeRecorder{Events: make(chan string, 10)} | ||
|
|
||
| csv := &v1alpha1.ClusterServiceVersion{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "sample", Namespace: "health-backoff-test"}, | ||
| Status: v1alpha1.ClusterServiceVersionStatus{ | ||
| Phase: v1alpha1.CSVPhaseFailed, | ||
| Reason: v1alpha1.CSVReasonComponentUnhealthy, | ||
| Message: "deployment is not ready", | ||
| }, | ||
| } | ||
| key := types.NamespacedName{Namespace: csv.GetNamespace(), Name: csv.GetName()} | ||
| require.Equal(t, 10*time.Second, op.healthCheckBackoff.next(key)) | ||
| require.Equal(t, 20*time.Second, op.healthCheckBackoff.next(key)) | ||
|
|
||
| err = op.updateInstallStatus( | ||
| csv, | ||
| NewTestInstaller(nil, nil), | ||
| &v1alpha1.StrategyDetailsDeployment{}, | ||
| v1alpha1.CSVPhaseFailed, | ||
| v1alpha1.CSVReasonComponentUnhealthy, | ||
| ) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 10*time.Second, op.healthCheckBackoff.next(key), "healthy checks should reset the retry sequence") | ||
| } | ||
|
|
||
| func TestCSVDeletionForgetsHealthBackoff(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| op, err := NewFakeOperator(ctx, withNamespaces("health-backoff-test")) | ||
| require.NoError(t, err) | ||
| csv := &v1alpha1.ClusterServiceVersion{ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "sample", | ||
| Namespace: "health-backoff-test", | ||
| }} | ||
| key := types.NamespacedName{Namespace: csv.GetNamespace(), Name: csv.GetName()} | ||
| op.healthCheckBackoff.next(key) | ||
| require.Equal(t, 20*time.Second, op.healthCheckBackoff.next(key)) | ||
|
|
||
| op.handleClusterServiceVersionDeletion(csv) | ||
|
|
||
| require.Equal(t, 10*time.Second, op.healthCheckBackoff.next(key), "deleted CSVs should not retain retry state") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: operator-framework/operator-lifecycle-manager
Length of output: 8152
🏁 Script executed:
Repository: operator-framework/operator-lifecycle-manager
Length of output: 13651
🏁 Script executed:
Repository: operator-framework/operator-lifecycle-manager
Length of output: 20040
🏁 Script executed:
Repository: operator-framework/operator-lifecycle-manager
Length of output: 7621
Reset health backoff only after all install checks pass.
CheckInstalledonly validates the deployment strategy. It can return no error while an APIService or webhook is still unavailable. The current reset then clears the CSV backoff before those checks run. A later waiting error can therefore restart at 10 seconds instead of using the next per-CSV delay.Suggested fix
now := a.now() key := types.NamespacedName{Namespace: csv.GetNamespace(), Name: csv.GetName()} - if strategyErr == nil { - a.healthCheckBackoff.forget(key) - } if strategyErr != nil { a.logger.WithError(strategyErr).Debug("operator not installed") } @@ if strategyInstalled && apiServicesInstalled && webhooksInstalled { + a.healthCheckBackoff.forget(key) // if there's no error, we're successfully running csv.SetPhaseWithEventIfChanged(v1alpha1.CSVPhaseSucceeded, v1alpha1.CSVReasonInstallSuccessful, "install strategy completed with no errors", now, a.recorder) return nil }🤖 Prompt for AI Agents