|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package client provides utilities for controller-runtime client operations, |
| 18 | +// including cache consistency helpers. |
| 19 | +// |
| 20 | +// Copied from sigs.k8s.io/cluster-api/internal/util/client. |
| 21 | +// Remove when upstream CAPI exposes these APIs publicly. |
| 22 | +package client |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "fmt" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/pkg/errors" |
| 30 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 31 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 32 | + kerrors "k8s.io/apimachinery/pkg/util/errors" |
| 33 | + "k8s.io/apimachinery/pkg/util/resourceversion" |
| 34 | + "k8s.io/apimachinery/pkg/util/wait" |
| 35 | + "k8s.io/klog/v2" |
| 36 | + ctrl "sigs.k8s.io/controller-runtime" |
| 37 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 38 | + "sigs.k8s.io/controller-runtime/pkg/client/apiutil" |
| 39 | +) |
| 40 | + |
| 41 | +const ( |
| 42 | + waitBackoffDuration = 25 * time.Microsecond |
| 43 | + waitBackoffFactor = 1.2 |
| 44 | + waitBackoffSteps = 63 |
| 45 | +) |
| 46 | + |
| 47 | +// waitBackoff is the timeout used when waiting for the cache to become up-to-date. |
| 48 | +// This adds up to ~ 10 seconds max wait duration. |
| 49 | +var waitBackoff = wait.Backoff{ |
| 50 | + Duration: waitBackoffDuration, |
| 51 | + Cap: 2 * time.Second, |
| 52 | + Factor: waitBackoffFactor, |
| 53 | + Steps: waitBackoffSteps, |
| 54 | +} |
| 55 | + |
| 56 | +// WaitForCacheToBeUpToDate waits until the cache is up-to-date in the sense of that the cache contains |
| 57 | +// all passed in objects with at least the passed in resourceVersion. |
| 58 | +// This is done by retrieving objects from the cache via the client and then comparing resourceVersions. |
| 59 | +// Note: This func will update the passed in objects while polling. |
| 60 | +// Note: resourceVersion must be set on the passed in objects. |
| 61 | +// Note: The generic parameter enforces that all objects have the same type. |
| 62 | +func WaitForCacheToBeUpToDate[T client.Object](ctx context.Context, c client.Client, action string, objs ...T) error { |
| 63 | + return waitFor(ctx, c, action, checkIfObjectUpToDate, objs...) |
| 64 | +} |
| 65 | + |
| 66 | +// WaitForObjectsToBeAddedToTheCache waits until the cache is up-to-date in the sense of that the |
| 67 | +// passed in objects exist in the cache. |
| 68 | +// Note: This func will update the passed in objects while polling. |
| 69 | +// Note: The generic parameter enforces that all objects have the same type. |
| 70 | +func WaitForObjectsToBeAddedToTheCache[T client.Object](ctx context.Context, c client.Client, action string, objs ...T) error { |
| 71 | + return waitFor(ctx, c, action, checkIfObjectAdded, objs...) |
| 72 | +} |
| 73 | + |
| 74 | +// WaitForObjectsToBeDeletedFromTheCache waits until the cache is up-to-date in the sense of that the |
| 75 | +// passed in objects have been either removed from the cache or they have a deletionTimestamp set. |
| 76 | +// Note: This func will update the passed in objects while polling. |
| 77 | +// Note: The generic parameter enforces that all objects have the same type. |
| 78 | +func WaitForObjectsToBeDeletedFromTheCache[T client.Object](ctx context.Context, c client.Client, action string, objs ...T) error { |
| 79 | + return waitFor(ctx, c, action, checkIfObjectDeleted, objs...) |
| 80 | +} |
| 81 | + |
| 82 | +// checkIfObjectUpToDate checks if an object is up-to-date and returns an error if it is not. |
| 83 | +func checkIfObjectUpToDate(ctx context.Context, c client.Client, desiredObj desiredObject) (isErrorRetryable bool, err error) { |
| 84 | + if desiredObj.MinimumResourceVersion == "" { |
| 85 | + // Unexpected error occurred: resourceVersion not set on passed in object (not retryable). |
| 86 | + return false, errors.Errorf("%s: cannot compare with invalid resourceVersion: resourceVersion not set", |
| 87 | + klog.KObj(desiredObj.Object)) |
| 88 | + } |
| 89 | + |
| 90 | + if err := c.Get(ctx, desiredObj.Key, desiredObj.Object); err != nil { |
| 91 | + if apierrors.IsNotFound(err) { |
| 92 | + // Done, object was deleted in the meantime. |
| 93 | + return false, nil |
| 94 | + } |
| 95 | + // Unexpected error occurred (not retryable). |
| 96 | + return false, err |
| 97 | + } |
| 98 | + |
| 99 | + cmp, err := resourceversion.CompareResourceVersion(desiredObj.Object.GetResourceVersion(), desiredObj.MinimumResourceVersion) |
| 100 | + if err != nil { |
| 101 | + // Unexpected error occurred: invalid resourceVersion (not retryable). |
| 102 | + return false, errors.Wrapf(err, "%s: cannot compare with invalid resourceVersion: current: %s, expected to be >= %s", |
| 103 | + klog.KObj(desiredObj.Object), desiredObj.Object.GetResourceVersion(), desiredObj.MinimumResourceVersion) |
| 104 | + } |
| 105 | + |
| 106 | + if cmp < 0 { |
| 107 | + // resourceVersion < MinimumResourceVersion (retryable). |
| 108 | + return true, errors.Errorf("%s: resourceVersion not yet up-to-date: current: %s, expected to be >= %s", |
| 109 | + klog.KObj(desiredObj.Object), desiredObj.Object.GetResourceVersion(), desiredObj.MinimumResourceVersion) |
| 110 | + } |
| 111 | + |
| 112 | + // Done, resourceVersion is new enough. |
| 113 | + return false, nil |
| 114 | +} |
| 115 | + |
| 116 | +func checkIfObjectAdded(ctx context.Context, c client.Client, desiredObj desiredObject) (isErrorRetryable bool, err error) { |
| 117 | + if err := c.Get(ctx, desiredObj.Key, desiredObj.Object); err != nil { |
| 118 | + if apierrors.IsNotFound(err) { |
| 119 | + // Object is not yet in the cache (retryable). |
| 120 | + return true, err |
| 121 | + } |
| 122 | + // Unexpected error occurred (not retryable). |
| 123 | + return false, err |
| 124 | + } |
| 125 | + |
| 126 | + // Done, object exists in the cache. |
| 127 | + return false, nil |
| 128 | +} |
| 129 | + |
| 130 | +func checkIfObjectDeleted(ctx context.Context, c client.Client, desiredObj desiredObject) (isErrorRetryable bool, err error) { |
| 131 | + if err := c.Get(ctx, desiredObj.Key, desiredObj.Object); err != nil { |
| 132 | + if apierrors.IsNotFound(err) { |
| 133 | + // Done, object has been removed from the cache. |
| 134 | + return false, nil |
| 135 | + } |
| 136 | + // Unexpected error occurred (not retryable). |
| 137 | + return false, err |
| 138 | + } |
| 139 | + |
| 140 | + if !desiredObj.Object.GetDeletionTimestamp().IsZero() { |
| 141 | + // Done, object has deletionTimestamp set. |
| 142 | + return false, nil |
| 143 | + } |
| 144 | + |
| 145 | + // Object does not have deletionTimestamp set yet (retryable). |
| 146 | + return true, fmt.Errorf("%s still exists", klog.KObj(desiredObj.Object)) |
| 147 | +} |
| 148 | + |
| 149 | +type desiredObject struct { |
| 150 | + Object client.Object |
| 151 | + Key client.ObjectKey |
| 152 | + MinimumResourceVersion string |
| 153 | +} |
| 154 | + |
| 155 | +type checkFunc func(ctx context.Context, c client.Client, desiredObj desiredObject) (retryableErr bool, err error) |
| 156 | + |
| 157 | +func waitFor[T client.Object](ctx context.Context, c client.Client, action string, checkFunc checkFunc, objs ...T) error { |
| 158 | + // Done, if there are no objects. |
| 159 | + if len(objs) == 0 { |
| 160 | + return nil |
| 161 | + } |
| 162 | + |
| 163 | + var o any = objs[0] |
| 164 | + if _, ok := o.(*unstructured.Unstructured); ok { |
| 165 | + return errors.Errorf("failed to wait for up-to-date objects in the cache after %s: Unstructured is not supported", action) |
| 166 | + } |
| 167 | + |
| 168 | + // All objects have the same type, so we can just take the GVK of the first object. |
| 169 | + objGVK, err := apiutil.GVKForObject(objs[0], c.Scheme()) |
| 170 | + if err != nil { |
| 171 | + return errors.Wrapf(err, "failed to wait for up-to-date objects in the cache after %s", action) |
| 172 | + } |
| 173 | + |
| 174 | + log := ctrl.LoggerFrom(ctx) |
| 175 | + |
| 176 | + desiredObjects := make([]desiredObject, len(objs)) |
| 177 | + for i, obj := range objs { |
| 178 | + desiredObjects[i] = desiredObject{ |
| 179 | + Object: obj, |
| 180 | + Key: client.ObjectKeyFromObject(obj), |
| 181 | + MinimumResourceVersion: obj.GetResourceVersion(), |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + now := time.Now() |
| 186 | + |
| 187 | + var pollErrs []error |
| 188 | + |
| 189 | + err = wait.ExponentialBackoffWithContext(ctx, waitBackoff, func(ctx context.Context) (bool, error) { |
| 190 | + pollErrs = nil |
| 191 | + |
| 192 | + for _, desiredObj := range desiredObjects { |
| 193 | + if isErrorRetryable, err := checkFunc(ctx, c, desiredObj); err != nil { |
| 194 | + pollErrs = append(pollErrs, err) |
| 195 | + |
| 196 | + if !isErrorRetryable { |
| 197 | + // Stop polling, non-retryable error occurred. |
| 198 | + return true, nil |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + if len(pollErrs) > 0 { |
| 204 | + // Continue polling, only retryable errors occurred. |
| 205 | + return false, nil |
| 206 | + } |
| 207 | + |
| 208 | + // Stop polling, all objects are up-to-date. |
| 209 | + return true, nil |
| 210 | + }) |
| 211 | + |
| 212 | + waitDuration := time.Since(now) |
| 213 | + |
| 214 | + if err != nil || len(pollErrs) > 0 { |
| 215 | + waitDurationMetric.WithLabelValues(objGVK.Kind, "error").Observe(waitDuration.Seconds()) |
| 216 | + |
| 217 | + var errSuffix string |
| 218 | + |
| 219 | + if err != nil { |
| 220 | + if wait.Interrupted(err) { |
| 221 | + errSuffix = ": timed out" |
| 222 | + } else { |
| 223 | + errSuffix = ": " + err.Error() |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + err := errors.Errorf( |
| 228 | + "failed to wait for up-to-date %s objects in the cache after %s%s: %s", |
| 229 | + objGVK.Kind, action, errSuffix, kerrors.NewAggregate(pollErrs)) |
| 230 | + log.Error(err, "Failed to wait for cache to be up-to-date", "kind", objGVK.Kind, "waitDuration", waitDuration) |
| 231 | + |
| 232 | + return err |
| 233 | + } |
| 234 | + |
| 235 | + waitDurationMetric.WithLabelValues(objGVK.Kind, "success").Observe(waitDuration.Seconds()) |
| 236 | + |
| 237 | + // Log on a high log-level if it took a long time for the cache to be up-to-date. |
| 238 | + if waitDuration >= 1*time.Second { |
| 239 | + log.Info("Successfully waited for cache to be up-to-date (>=1s)", "kind", objGVK.Kind, "waitDuration", waitDuration) |
| 240 | + } else { |
| 241 | + log.V(10).Info("Successfully waited for cache to be up-to-date", "kind", objGVK.Kind, "waitDuration", waitDuration) |
| 242 | + } |
| 243 | + |
| 244 | + return nil |
| 245 | +} |
0 commit comments