Repository navigation
fix(provider): confirm against the API server before skipping a status write #10409
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
Open
nobbs
wants to merge
6
commits into
envoyproxy:main
Choose a base branch
from
nobbs:fix/gatewayclass-generation-flake
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
62538f6
fix(provider): confirm against the API server before skipping a statu…
nobbs 44f7861
docs: name the release note after the PR
nobbs 2500865
fix(provider): keep the last-write record until a skipped status is c…
nobbs 03a4ecf
fix(provider): key last-write records by group and kind
nobbs 54a500d
fix(provider): expire last-write records of the status updater
nobbs 14a957f
refactor(provider): simplify the last-write records of the status upd…
nobbs 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
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,168 @@ | ||
| // Copyright Envoy Gateway Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // The full text of the Apache license is available in the LICENSE file at | ||
| // the root of the repo. | ||
|
|
||
| package kubernetes | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/stretchr/testify/require" | ||
| kerrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/interceptor" | ||
| gwapiv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
|
|
||
| egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" | ||
| "github.com/envoyproxy/gateway/internal/envoygateway" | ||
| ) | ||
|
|
||
| // laggingCache serves GatewayClass reads from a snapshot the test takes by hand, standing in for an | ||
| // informer cache that has not seen the latest writes yet. Until the first snapshot, reads go to the | ||
| // embedded client, which plays the API server and takes the writes. | ||
| type laggingCache struct { | ||
| client.Client | ||
| snapshot *gwapiv1.GatewayClass | ||
| } | ||
|
|
||
| func (c *laggingCache) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { | ||
| if gc, ok := obj.(*gwapiv1.GatewayClass); ok && c.snapshot != nil { | ||
| c.snapshot.DeepCopyInto(gc) | ||
| return nil | ||
| } | ||
| return c.Client.Get(ctx, key, obj, opts...) | ||
| } | ||
|
|
||
| // newGatewayClass sets a UID, which the API server would assign and the fake client does not. | ||
| func newGatewayClass(name string, generation int64) *gwapiv1.GatewayClass { | ||
| return &gwapiv1.GatewayClass{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: name, UID: types.UID(name), Generation: generation}, | ||
| Spec: gwapiv1.GatewayClassSpec{ControllerName: egv1a1.GatewayControllerName}, | ||
| } | ||
| } | ||
|
|
||
| func acceptedGatewayClassUpdate(key types.NamespacedName, observedGeneration int64) Update { | ||
| return Update{ | ||
| NamespacedName: key, | ||
| Resource: new(gwapiv1.GatewayClass), | ||
| Mutator: MutatorFunc(func(obj client.Object) client.Object { | ||
| gc := obj.(*gwapiv1.GatewayClass).DeepCopy() | ||
| gc.Status.Conditions = []metav1.Condition{{ | ||
| Type: string(gwapiv1.GatewayClassConditionStatusAccepted), | ||
| Status: metav1.ConditionTrue, | ||
| Reason: string(gwapiv1.GatewayClassReasonAccepted), | ||
| ObservedGeneration: observedGeneration, | ||
| }} | ||
| return gc | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| // gatewayClassStatusWriter writes the status of one GatewayClass through an UpdateHandler whose | ||
| // cache only catches up when the test says so. | ||
| type gatewayClassStatusWriter struct { | ||
| t *testing.T | ||
| key types.NamespacedName | ||
| api client.Client | ||
| cache *laggingCache | ||
| u *UpdateHandler | ||
| } | ||
|
|
||
| func newGatewayClassStatusWriter(t *testing.T, funcs *interceptor.Funcs) *gatewayClassStatusWriter { | ||
| key := types.NamespacedName{Name: "gc"} | ||
| api := fakeclient.NewClientBuilder(). | ||
| WithScheme(envoygateway.GetScheme()). | ||
| WithObjects(newGatewayClass(key.Name, 2)). | ||
| WithStatusSubresource(&gwapiv1.GatewayClass{}). | ||
| WithInterceptorFuncs(*funcs). | ||
| Build() | ||
| cache := &laggingCache{Client: api} | ||
| return &gatewayClassStatusWriter{t: t, key: key, api: api, cache: cache, u: NewUpdateHandler(logr.Discard(), cache, api)} | ||
| } | ||
|
|
||
| func (w *gatewayClassStatusWriter) write(observedGeneration int64) { | ||
| w.u.apply(acceptedGatewayClassUpdate(w.key, observedGeneration)) | ||
| } | ||
|
|
||
| // catchUp lets the cache see the current state of the GatewayClass. | ||
| func (w *gatewayClassStatusWriter) catchUp() { | ||
| w.t.Helper() | ||
| gc := &gwapiv1.GatewayClass{} | ||
| require.NoError(w.t, w.api.Get(w.t.Context(), w.key, gc)) | ||
| w.cache.snapshot = gc | ||
| } | ||
|
|
||
| func (w *gatewayClassStatusWriter) storedObservedGeneration() int64 { | ||
| w.t.Helper() | ||
| gc := &gwapiv1.GatewayClass{} | ||
| require.NoError(w.t, w.api.Get(w.t.Context(), w.key, gc)) | ||
| return gc.Status.Conditions[0].ObservedGeneration | ||
| } | ||
|
|
||
| // writeOutOfOrder writes generation 2, lets the cache catch up, and then writes a late update | ||
| // from generation 1 that the cache does not see. | ||
| func (w *gatewayClassStatusWriter) writeOutOfOrder() { | ||
| w.t.Helper() | ||
| w.write(2) | ||
| w.catchUp() | ||
| w.write(1) | ||
| require.Equal(w.t, int64(1), w.storedObservedGeneration()) | ||
| } | ||
|
|
||
| func TestStatusUpdateIsNotSkippedWhileCacheLagsBehindOwnWrite(t *testing.T) { | ||
| w := newGatewayClassStatusWriter(t, &interceptor.Funcs{}) | ||
| w.writeOutOfOrder() | ||
|
|
||
| // Compared with the cache, which still shows the first write, this status looks unchanged. | ||
| w.write(2) | ||
| require.Equal(t, int64(2), w.storedObservedGeneration()) | ||
| } | ||
|
|
||
| func TestStatusUpdateRetriedAfterConflictIsNotSkippedWhileCacheLags(t *testing.T) { | ||
| conflictOnce := false | ||
| w := newGatewayClassStatusWriter(t, &interceptor.Funcs{ | ||
| SubResourceUpdate: func(ctx context.Context, c client.Client, subResource string, obj client.Object, opts ...client.SubResourceUpdateOption) error { | ||
| if conflictOnce { | ||
| conflictOnce = false | ||
| return kerrors.NewConflict(schema.GroupResource{Resource: "gatewayclasses"}, obj.GetName(), fmt.Errorf("concurrent write")) | ||
| } | ||
| return c.SubResource(subResource).Update(ctx, obj, opts...) | ||
| }, | ||
| }) | ||
| w.writeOutOfOrder() | ||
|
|
||
| // The write that follows the check against the API server conflicts once. The retry still | ||
| // meets the stale cache. | ||
| conflictOnce = true | ||
| w.write(2) | ||
| require.Equal(t, int64(2), w.storedObservedGeneration()) | ||
| } | ||
|
|
||
| func TestStatusUpdateRecordsExpire(t *testing.T) { | ||
| builder := fakeclient.NewClientBuilder(). | ||
| WithScheme(envoygateway.GetScheme()). | ||
| WithStatusSubresource(&gwapiv1.GatewayClass{}) | ||
| for i := range 100 { | ||
| builder = builder.WithObjects(newGatewayClass(fmt.Sprintf("gc-%d", i), 1)) | ||
| } | ||
| api := builder.Build() | ||
| u := NewUpdateHandler(logr.Discard(), api, api) | ||
| for i := range 100 { | ||
| u.apply(acceptedGatewayClassUpdate(types.NamespacedName{Name: fmt.Sprintf("gc-%d", i)}, 1)) | ||
| } | ||
|
|
||
| u.expireLastWrites(time.Now()) | ||
| require.Len(t, u.lastWritten, 100) | ||
|
|
||
| u.expireLastWrites(time.Now().Add(lastWriteTTL)) | ||
| require.Empty(t, u.lastWritten) | ||
| } |
1 change: 1 addition & 0 deletions
1
release-notes/current/bug_fixes/10409-status-update-skipped-on-stale-cache.md
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 @@ | ||
| Fixed the status updater skipping a status write as unchanged when its cache had not yet seen its own previous write, which could leave a resource with an outdated status, for example a GatewayClass keeping the observedGeneration of its previous generation. |
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.
When the cache remains behind the last write past this TTL and the next sweep (one to two minutes),
expireLastWritesremoves the only signal that forces an API-server read. In the same S2 → stale-cache S1 → corrective S2 sequence covered by the new test, delaying the correction until after expiry makescacheMayLagfalse, so the updater treats cached S2 as current and leaves server-side S1 in place; the deduplicating status store may never emit S2 again. Cleanup therefore needs to distinguish records whose resource version has been observed by the cache, or otherwise retain a bounded guard that survives prolonged cache lag.Useful? React with 👍 / 👎.