Skip to content

Commit fe46e47

Browse files
committed
chore: update deprecated polling methods in apiextensions-apiserver
Signed-off-by: Omer Aplatony <[email protected]>
1 parent feb3f92 commit fe46e47

File tree

11 files changed

+49
-45
lines changed

11 files changed

+49
-45
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/apiserver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package apiserver
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"net/http"
2223
"time"
@@ -258,14 +259,14 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
258259
// we don't want to report healthy until we can handle all CRDs that have already been registered. Waiting for the informer
259260
// to sync makes sure that the lister will be valid before we begin. There may still be races for CRDs added after startup,
260261
// but we won't go healthy until we can handle the ones already present.
261-
s.GenericAPIServer.AddPostStartHookOrDie("crd-informer-synced", func(context genericapiserver.PostStartHookContext) error {
262-
return wait.PollImmediateUntil(100*time.Millisecond, func() (bool, error) {
262+
s.GenericAPIServer.AddPostStartHookOrDie("crd-informer-synced", func(ctx genericapiserver.PostStartHookContext) error {
263+
return wait.PollUntilContextCancel(ctx.Context, 100*time.Millisecond, true, func(ctx context.Context) (bool, error) {
263264
if s.Informers.Apiextensions().V1().CustomResourceDefinitions().Informer().HasSynced() {
264265
close(hasCRDInformerSyncedSignal)
265266
return true, nil
266267
}
267268
return false, nil
268-
}, context.Done())
269+
})
269270
})
270271

271272
return s, nil

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_discovery_controller.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package apiserver
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"sort"
2223
"time"
@@ -297,7 +298,7 @@ func (c *DiscoveryController) Run(stopCh <-chan struct{}, synchedCh chan<- struc
297298
}
298299

299300
// initially sync all group versions to make sure we serve complete discovery
300-
if err := wait.PollImmediateUntil(time.Second, func() (bool, error) {
301+
if err := wait.PollUntilContextCancel(context.Background(), time.Second, true, func(ctx context.Context) (bool, error) {
301302
crds, err := c.crdLister.List(labels.Everything())
302303
if err != nil {
303304
utilruntime.HandleError(fmt.Errorf("failed to initially list CRDs: %v", err))
@@ -313,10 +314,11 @@ func (c *DiscoveryController) Run(stopCh <-chan struct{}, synchedCh chan<- struc
313314
}
314315
}
315316
return true, nil
316-
}, stopCh); err == wait.ErrWaitTimeout {
317-
utilruntime.HandleError(fmt.Errorf("timed out waiting for discovery endpoint to initialize"))
318-
return
319-
} else if err != nil {
317+
}); err != nil {
318+
if err == context.DeadlineExceeded {
319+
utilruntime.HandleError(fmt.Errorf("timed out waiting for initial discovery sync"))
320+
return
321+
}
320322
panic(fmt.Errorf("unexpected error: %v", err))
321323
}
322324
close(synchedCh)

staging/src/k8s.io/apiextensions-apiserver/pkg/controller/finalizer/crd_finalizer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (c *CRDFinalizer) deleteInstances(crd *apiextensionsv1.CustomResourceDefini
235235
// now we need to wait until all the resources are deleted. Start with a simple poll before we do anything fancy.
236236
// TODO not all servers are synchronized on caches. It is possible for a stale one to still be creating things.
237237
// Once we have a mechanism for servers to indicate their states, we should check that for concurrence.
238-
err = wait.PollImmediate(5*time.Second, 1*time.Minute, func() (bool, error) {
238+
err = wait.PollUntilContextTimeout(ctx, 1*time.Second, 1*time.Minute, true, func(ctx context.Context) (bool, error) {
239239
listObj, err := crClient.List(ctx, nil)
240240
if err != nil {
241241
return false, err

staging/src/k8s.io/apiextensions-apiserver/test/integration/apiapproval_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func TestAPIApproval(t *testing.T) {
7272
if err != nil {
7373
t.Fatal(err)
7474
}
75-
err = wait.PollImmediate(100*time.Millisecond, 30*time.Second, func() (bool, error) {
76-
approvedKubeAPI, err = apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), approvedKubeAPI.Name, metav1.GetOptions{})
75+
err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 30*time.Second, true, func(ctx context.Context) (bool, error) {
76+
approvedKubeAPI, err = apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, approvedKubeAPI.Name, metav1.GetOptions{})
7777
if err != nil {
7878
return false, err
7979
}

staging/src/k8s.io/apiextensions-apiserver/test/integration/conversion/conversion_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ func testWebhookConverter(t *testing.T, watchCache bool) {
219219
defer ctc.removeConversionWebhook(t)
220220

221221
// wait until new webhook is called the first time
222-
if err := wait.PollImmediate(time.Millisecond*100, wait.ForeverTestTimeout, func() (bool, error) {
223-
_, err := ctc.versionedClient(marker.GetNamespace(), "v1alpha1").Get(context.TODO(), marker.GetName(), metav1.GetOptions{})
222+
if err := wait.PollUntilContextTimeout(context.Background(), time.Millisecond*100, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
223+
_, err = ctc.versionedClient(marker.GetNamespace(), "v1alpha1").Get(ctx, marker.GetName(), metav1.GetOptions{})
224224
select {
225225
case <-upCh:
226226
return true, nil

staging/src/k8s.io/apiextensions-apiserver/test/integration/conversion/webhook.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package conversion
1818

1919
import (
20+
"context"
2021
"crypto/tls"
2122
"crypto/x509"
2223
"encoding/json"
@@ -63,8 +64,8 @@ func StartConversionWebhookServer(handler http.Handler) (func(), *apiextensionsv
6364
}
6465

6566
// StartTLS returns immediately, there is a small chance of a race to avoid.
66-
if err := wait.PollImmediate(time.Millisecond*100, wait.ForeverTestTimeout, func() (bool, error) {
67-
_, err := webhookServer.Client().Get(webhookServer.URL) // even a 404 is fine
67+
if err := wait.PollUntilContextTimeout(context.Background(), time.Millisecond*100, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
68+
_, err = webhookServer.Client().Get(webhookServer.URL) // even a 404 is fine
6869
return err == nil, nil
6970
}); err != nil {
7071
webhookServer.Close()

staging/src/k8s.io/apiextensions-apiserver/test/integration/defaulting_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ func testDefaulting(t *testing.T, watchCache bool) {
316316
addDefault("v1beta2", "c", "C")
317317

318318
t.Logf("wait until GET sees 'c' in both status and spec")
319-
if err := wait.PollImmediate(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
320-
obj, err := fooClient.Get(context.TODO(), foo.GetName(), metav1.GetOptions{})
319+
if err := wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
320+
obj, err := fooClient.Get(ctx, foo.GetName(), metav1.GetOptions{})
321321
if err != nil {
322322
return false, err
323323
}
@@ -333,7 +333,7 @@ func testDefaulting(t *testing.T, watchCache bool) {
333333
mustExist(foo.Object, [][]string{{"spec", "a"}, {"spec", "b"}, {"spec", "c"}, {"status", "a"}, {"status", "b"}, {"status", "c"}})
334334

335335
t.Logf("wait until GET sees 'c' in both status and spec of cached get")
336-
if err := wait.PollImmediate(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
336+
if err := wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
337337
obj, err := fooClient.Get(context.TODO(), foo.GetName(), metav1.GetOptions{ResourceVersion: "0"})
338338
if err != nil {
339339
return false, err
@@ -409,8 +409,8 @@ func testDefaulting(t *testing.T, watchCache bool) {
409409
t.Logf("Add 'c' default to the REST version, remove it from the storage version, and wait until GET no longer sees it in both status and spec")
410410
addDefault("v1beta1", "c", "C")
411411
removeDefault("v1beta2", "c")
412-
if err := wait.PollImmediate(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
413-
obj, err := fooClient.Get(context.TODO(), foo.GetName(), metav1.GetOptions{})
412+
if err := wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
413+
obj, err := fooClient.Get(ctx, foo.GetName(), metav1.GetOptions{})
414414
if err != nil {
415415
return false, err
416416
}
@@ -434,8 +434,8 @@ func testDefaulting(t *testing.T, watchCache bool) {
434434
removeDefault("v1beta1", "a")
435435
removeDefault("v1beta1", "b")
436436
removeDefault("v1beta1", "c")
437-
if err := wait.PollImmediate(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
438-
obj, err := fooClient.Get(context.TODO(), foo.GetName(), metav1.GetOptions{})
437+
if err := wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
438+
obj, err := fooClient.Get(ctx, foo.GetName(), metav1.GetOptions{})
439439
if err != nil {
440440
return false, err
441441
}

staging/src/k8s.io/apiextensions-apiserver/test/integration/fixtures/resources.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func existsInDiscoveryV1(crd *apiextensionsv1.CustomResourceDefinition, apiExten
347347
func waitForCRDReadyWatchUnsafe(crd *apiextensionsv1.CustomResourceDefinition, apiExtensionsClient clientset.Interface) (*apiextensionsv1.CustomResourceDefinition, error) {
348348
// wait until all resources appears in discovery
349349
for _, version := range servedV1Versions(crd) {
350-
err := wait.PollImmediate(500*time.Millisecond, 30*time.Second, func() (bool, error) {
350+
err := wait.PollUntilContextTimeout(context.Background(), 500*time.Second, 30*time.Second, true, func(ctx context.Context) (bool, error) {
351351
return existsInDiscoveryV1(crd, apiExtensionsClient, version)
352352
})
353353
if err != nil {
@@ -375,7 +375,7 @@ func waitForCRDReady(crd *apiextensionsv1.CustomResourceDefinition, apiExtension
375375
// For this test, we'll actually cycle, "list/watch/create/delete" until we get an RV from list that observes the create and not an error.
376376
// This way all the tests that are checking for watches don't have to worry about RV too old problems because crazy things *could* happen
377377
// before like the created RV could be too old to watch.
378-
err = wait.PollImmediate(500*time.Millisecond, 30*time.Second, func() (bool, error) {
378+
err = wait.PollUntilContextTimeout(context.Background(), 500*time.Millisecond, 30*time.Second, true, func(ctx context.Context) (bool, error) {
379379
return isWatchCachePrimed(v1CRD, dynamicClientSet)
380380
})
381381
if err != nil {
@@ -396,7 +396,7 @@ func CreateNewV1CustomResourceDefinitionWatchUnsafe(v1CRD *apiextensionsv1.Custo
396396

397397
// wait until all resources appears in discovery
398398
for _, version := range servedV1Versions(v1CRD) {
399-
err := wait.PollImmediate(500*time.Millisecond, 30*time.Second, func() (bool, error) {
399+
err := wait.PollUntilContextTimeout(context.Background(), 500*time.Second, 30*time.Second, true, func(ctx context.Context) (bool, error) {
400400
return existsInDiscoveryV1(v1CRD, apiExtensionsClient, version)
401401
})
402402
if err != nil {
@@ -424,7 +424,7 @@ func CreateNewV1CustomResourceDefinition(v1CRD *apiextensionsv1.CustomResourceDe
424424
// For this test, we'll actually cycle, "list/watch/create/delete" until we get an RV from list that observes the create and not an error.
425425
// This way all the tests that are checking for watches don't have to worry about RV too old problems because crazy things *could* happen
426426
// before like the created RV could be too old to watch.
427-
err = wait.PollImmediate(500*time.Millisecond, 30*time.Second, func() (bool, error) {
427+
err = wait.PollUntilContextTimeout(context.Background(), 500*time.Second, 30*time.Second, true, func(ctx context.Context) (bool, error) {
428428
return isWatchCachePrimed(v1CRD, dynamicClientSet)
429429
})
430430
if err != nil {
@@ -518,7 +518,7 @@ func DeleteV1CustomResourceDefinition(crd *apiextensionsv1.CustomResourceDefinit
518518
return err
519519
}
520520
for _, version := range servedV1Versions(crd) {
521-
err := wait.PollImmediate(500*time.Millisecond, 30*time.Second, func() (bool, error) {
521+
err := wait.PollUntilContextTimeout(context.Background(), 500*time.Second, 30*time.Second, true, func(ctx context.Context) (bool, error) {
522522
exists, err := existsInDiscoveryV1(crd, apiExtensionsClient, version)
523523
return !exists, err
524524
})
@@ -540,7 +540,7 @@ func DeleteV1CustomResourceDefinitions(deleteListOpts metav1.ListOptions, apiExt
540540
}
541541
for _, crd := range list.Items {
542542
for _, version := range servedV1Versions(&crd) {
543-
err := wait.PollImmediate(500*time.Millisecond, 30*time.Second, func() (bool, error) {
543+
err := wait.PollUntilContextTimeout(context.Background(), 500*time.Second, 30*time.Second, true, func(ctx context.Context) (bool, error) {
544544
exists, err := existsInDiscoveryV1(&crd, apiExtensionsClient, version)
545545
return !exists, err
546546
})

staging/src/k8s.io/apiextensions-apiserver/test/integration/listtype_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ func TestListTypes(t *testing.T) {
209209
}
210210

211211
t.Logf("Updating again with invalid values, eventually successfully due to ratcheting logic")
212-
err = wait.PollImmediate(time.Millisecond*100, wait.ForeverTestTimeout, func() (bool, error) {
213-
_, err = fooClient.Update(context.TODO(), modifiedInstance, metav1.UpdateOptions{})
212+
err = wait.PollUntilContextTimeout(context.Background(), time.Microsecond*100, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
213+
_, err = fooClient.Update(ctx, modifiedInstance, metav1.UpdateOptions{})
214214
if err == nil {
215215
return true, err
216216
}

staging/src/k8s.io/apiextensions-apiserver/test/integration/validation_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,8 @@ func TestCRValidationOnCRDUpdate(t *testing.T) {
777777
}
778778

779779
// CR is now accepted
780-
err = wait.Poll(500*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
781-
_, err := noxuResourceClient.Create(context.TODO(), instanceToCreate, metav1.CreateOptions{})
780+
err = wait.PollUntilContextTimeout(context.Background(), 500*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
781+
_, err = noxuResourceClient.Create(ctx, instanceToCreate, metav1.CreateOptions{})
782782
if _, isStatus := err.(*apierrors.StatusError); isStatus {
783783
if apierrors.IsInvalid(err) {
784784
return false, nil
@@ -925,8 +925,8 @@ spec:
925925
// wait for condition with violations
926926
t.Log("Waiting for NonStructuralSchema condition")
927927
var cond *apiextensionsv1.CustomResourceDefinitionCondition
928-
err = wait.PollImmediate(100*time.Millisecond, 5*time.Second, func() (bool, error) {
929-
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), name, metav1.GetOptions{})
928+
err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (done bool, err error) {
929+
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{})
930930
if err != nil {
931931
return false, err
932932
}
@@ -963,8 +963,8 @@ spec:
963963

964964
// wait for condition to go away
965965
t.Log("Wait for condition to disappear")
966-
err = wait.PollImmediate(100*time.Millisecond, 5*time.Second, func() (bool, error) {
967-
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), name, metav1.GetOptions{})
966+
err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (done bool, err error) {
967+
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{})
968968
if err != nil {
969969
return false, err
970970
}
@@ -1529,8 +1529,8 @@ properties:
15291529
if len(tst.expectedViolations) == 0 {
15301530
// wait for condition to not appear
15311531
var cond *apiextensionsv1.CustomResourceDefinitionCondition
1532-
err := wait.PollImmediate(100*time.Millisecond, 5*time.Second, func() (bool, error) {
1533-
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), betaCRD.Name, metav1.GetOptions{})
1532+
err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (done bool, err error) {
1533+
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, betaCRD.Name, metav1.GetOptions{})
15341534
if err != nil {
15351535
return false, err
15361536
}
@@ -1540,16 +1540,16 @@ properties:
15401540
}
15411541
return true, nil
15421542
})
1543-
if err != wait.ErrWaitTimeout {
1543+
if err != context.DeadlineExceeded {
15441544
t.Fatalf("expected no NonStructuralSchema condition, but got one: %v", cond)
15451545
}
15461546
return
15471547
}
15481548

15491549
// wait for condition to appear with the given violations
15501550
var cond *apiextensionsv1.CustomResourceDefinitionCondition
1551-
err = wait.PollImmediate(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
1552-
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), betaCRD.Name, metav1.GetOptions{})
1551+
err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
1552+
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, betaCRD.Name, metav1.GetOptions{})
15531553
if err != nil {
15541554
return false, err
15551555
}

0 commit comments

Comments
 (0)