Skip to content

Commit

Permalink
vspherevm: ensure that the cache gets updated when patching .status.t…
Browse files Browse the repository at this point in the history
…askRef
  • Loading branch information
chrischdi committed Jan 30, 2024
1 parent 036fb05 commit 9c084ed
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions controllers/vspherevm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
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 All @@ -41,6 +42,7 @@ import (
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
ctrlbldr "sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"

Check failure on line 45 in controllers/vspherevm_controller.go

View workflow job for this annotation

GitHub Actions / lint

ST1019: package "sigs.k8s.io/controller-runtime/pkg/client" is being imported more than once (stylecheck)

Check failure on line 45 in controllers/vspherevm_controller.go

View workflow job for this annotation

GitHub Actions / lint

ST1019: package "sigs.k8s.io/controller-runtime/pkg/client" is being imported more than once (stylecheck)
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

Check failure on line 46 in controllers/vspherevm_controller.go

View workflow job for this annotation

GitHub Actions / lint

ST1019(related information): other import of "sigs.k8s.io/controller-runtime/pkg/client" (stylecheck)

Check failure on line 46 in controllers/vspherevm_controller.go

View workflow job for this annotation

GitHub Actions / lint

ST1019(related information): other import of "sigs.k8s.io/controller-runtime/pkg/client" (stylecheck)
"sigs.k8s.io/controller-runtime/pkg/controller"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -252,6 +254,7 @@ func (r vmReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.R
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 +272,25 @@ func (r vmReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.R
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).
if vmContext.VSphereVM.Status.TaskRef != originalTaskRef {
err = wait.PollUntilContextTimeout(ctx, 5*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (bool, error) {
key := client.ObjectKey{Namespace: vmContext.VSphereVM.GetNamespace(), Name: vmContext.VSphereVM.GetName()}
cachedMD := &infrav1.VSphereVM{}
if err := r.Client.Get(ctx, key, cachedMD); err != nil {
return false, err
}
return originalTaskRef != cachedMD.Status.TaskRef, nil

Check warning on line 286 in controllers/vspherevm_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspherevm_controller.go#L280-L286

Added lines #L280 - L286 were not covered by tests
})
if reterr != nil {
reterr = kerrors.NewAggregate([]error{reterr, err})
} else {
reterr = err
}

Check warning on line 292 in controllers/vspherevm_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspherevm_controller.go#L288-L292

Added lines #L288 - L292 were not covered by tests
}
}()

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

0 comments on commit 9c084ed

Please sign in to comment.