Skip to content
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

[release-1.9] 🐛 vspherevm: ensure that the cache gets updated when patching .status.taskRef #2680

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions controllers/vspherevm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apitypes "k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
Expand Down Expand Up @@ -252,6 +253,7 @@
defer func() {
log.V(4).Info("VSphereVM.Status.TaskRef OnExit", "taskRef", vmContext.VSphereVM.Status.TaskRef)
}()
originalTaskRef := vmContext.VSphereVM.Status.TaskRef

// Always issue a patch when exiting this function so changes to the
// resource are patched back to the API server.
Expand All @@ -269,6 +271,23 @@
if err := vmContext.Patch(ctx); err != nil {
reterr = kerrors.NewAggregate([]error{reterr, err})
}

// Wait until VSphereVM is updated in the cache if the `.Status.TaskRef` field changes.
// Note: We have to do this because otherwise using a cached client in current state could
// return a stale state of a VSphereVM we just patched (because the cache might be stale).
// This can lead to duplicate tasks being triggered (e.g. VM deletion) and make the controller
// wait for longer then required.
if vmContext.VSphereVM.Status.TaskRef != originalTaskRef {
err = wait.PollUntilContextTimeout(ctx, 5*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (bool, error) {
key := ctrlclient.ObjectKey{Namespace: vmContext.VSphereVM.GetNamespace(), Name: vmContext.VSphereVM.GetName()}
cachedVSphereVM := &infrav1.VSphereVM{}
if err := r.Client.Get(ctx, key, cachedVSphereVM); err != nil {
return false, err
}
return originalTaskRef != cachedVSphereVM.Status.TaskRef, nil

Check warning on line 287 in controllers/vspherevm_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspherevm_controller.go#L281-L287

Added lines #L281 - L287 were not covered by tests
})
reterr = kerrors.NewAggregate([]error{reterr, err})

Check warning on line 289 in controllers/vspherevm_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspherevm_controller.go#L289

Added line #L289 was not covered by tests
}
}()

if vsphereVM.ObjectMeta.DeletionTimestamp.IsZero() {
Expand Down
Loading