Skip to content

Commit ec1e7fe

Browse files
authored
Merge pull request kubernetes#128759 from omerap12/PollUntilContextCancel-apiextensions-apiserver
chore: update deprecated polling methods in apiextensions-apiserver
2 parents 5ba2b78 + 7e578bd commit ec1e7fe

File tree

11 files changed

+74
-63
lines changed

11 files changed

+74
-63
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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package apiserver
1818

1919
import (
20+
"context"
21+
"errors"
2022
"fmt"
2123
"sort"
2224
"time"
@@ -297,7 +299,7 @@ func (c *DiscoveryController) Run(stopCh <-chan struct{}, synchedCh chan<- struc
297299
}
298300

299301
// initially sync all group versions to make sure we serve complete discovery
300-
if err := wait.PollImmediateUntil(time.Second, func() (bool, error) {
302+
if err := wait.PollUntilContextCancel(context.Background(), time.Second, true, func(ctx context.Context) (bool, error) {
301303
crds, err := c.crdLister.List(labels.Everything())
302304
if err != nil {
303305
utilruntime.HandleError(fmt.Errorf("failed to initially list CRDs: %v", err))
@@ -313,10 +315,11 @@ func (c *DiscoveryController) Run(stopCh <-chan struct{}, synchedCh chan<- struc
313315
}
314316
}
315317
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 {
318+
}); err != nil {
319+
if errors.Is(err, context.DeadlineExceeded) {
320+
utilruntime.HandleError(fmt.Errorf("timed out waiting for initial discovery sync"))
321+
return
322+
}
320323
panic(fmt.Errorf("unexpected error: %v", err))
321324
}
322325
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, 5*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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ 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{})
77-
if err != nil {
78-
return false, err
75+
err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 30*time.Second, true, func(ctx context.Context) (bool, error) {
76+
approvedKubeAPI, getErr := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, approvedKubeAPI.Name, metav1.GetOptions{})
77+
if getErr != nil {
78+
return false, getErr
7979
}
8080
if approvedKubeAPIApproved := findCRDCondition(approvedKubeAPI, apiextensionsv1.KubernetesAPIApprovalPolicyConformant); approvedKubeAPIApproved == nil || approvedKubeAPIApproved.Status != apiextensionsv1.ConditionTrue {
8181
t.Log(approvedKubeAPIApproved)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ 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+
_, getErr := ctc.versionedClient(marker.GetNamespace(), "v1alpha1").Get(ctx, marker.GetName(), metav1.GetOptions{})
224224
select {
225225
case <-upCh:
226226
return true, nil
227227
default:
228-
t.Logf("Waiting for webhook to become effective, getting marker object: %v", err)
228+
t.Logf("Waiting for webhook to become effective, getting marker object: %v", getErr)
229229
return false, nil
230230
}
231231
}); err != nil {

staging/src/k8s.io/apiextensions-apiserver/test/integration/conversion/webhook.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 conversion
1818

1919
import (
20+
"context"
2021
"crypto/tls"
2122
"crypto/x509"
2223
"encoding/json"
@@ -63,9 +64,9 @@ 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
68-
return err == nil, nil
67+
if err := wait.PollUntilContextTimeout(context.Background(), time.Millisecond*100, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
68+
_, getErr := webhookServer.Client().Get(webhookServer.URL) // even a 404 is fine
69+
return getErr == nil, nil
6970
}); err != nil {
7071
webhookServer.Close()
7172
return nil, nil, err

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.Millisecond, 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.Millisecond, 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.Millisecond, 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.Millisecond, 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.Millisecond, 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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,16 @@ 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{})
214-
if err == nil {
215-
return true, err
212+
err = wait.PollUntilContextTimeout(context.Background(), time.Millisecond*100, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
213+
_, updateErr := fooClient.Update(ctx, modifiedInstance, metav1.UpdateOptions{})
214+
if updateErr == nil {
215+
return true, nil
216216
}
217-
if errors.IsInvalid(err) {
217+
if errors.IsInvalid(updateErr) {
218218
// wait until modifiedInstance becomes valid again
219219
return false, nil
220220
}
221-
return false, err
221+
return false, updateErr
222222
})
223223
if err != nil {
224224
t.Fatalf("unexpected error: %v", err)

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package integration
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"strings"
2324
"testing"
@@ -777,15 +778,16 @@ func TestCRValidationOnCRDUpdate(t *testing.T) {
777778
}
778779

779780
// 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{})
782-
if _, isStatus := err.(*apierrors.StatusError); isStatus {
783-
if apierrors.IsInvalid(err) {
781+
err = wait.PollUntilContextTimeout(context.Background(), 500*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
782+
_, createErr := noxuResourceClient.Create(ctx, instanceToCreate, metav1.CreateOptions{})
783+
var statusErr *apierrors.StatusError
784+
if errors.As(createErr, &statusErr) {
785+
if apierrors.IsInvalid(createErr) {
784786
return false, nil
785787
}
786788
}
787-
if err != nil {
788-
return false, err
789+
if createErr != nil {
790+
return false, createErr
789791
}
790792
return true, nil
791793
})
@@ -925,8 +927,8 @@ spec:
925927
// wait for condition with violations
926928
t.Log("Waiting for NonStructuralSchema condition")
927929
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{})
930+
err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (done bool, err error) {
931+
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{})
930932
if err != nil {
931933
return false, err
932934
}
@@ -963,8 +965,8 @@ spec:
963965

964966
// wait for condition to go away
965967
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{})
968+
err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (done bool, err error) {
969+
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{})
968970
if err != nil {
969971
return false, err
970972
}
@@ -1529,8 +1531,8 @@ properties:
15291531
if len(tst.expectedViolations) == 0 {
15301532
// wait for condition to not appear
15311533
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{})
1534+
err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (done bool, err error) {
1535+
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, betaCRD.Name, metav1.GetOptions{})
15341536
if err != nil {
15351537
return false, err
15361538
}
@@ -1540,16 +1542,16 @@ properties:
15401542
}
15411543
return true, nil
15421544
})
1543-
if err != wait.ErrWaitTimeout {
1545+
if !errors.Is(err, context.DeadlineExceeded) {
15441546
t.Fatalf("expected no NonStructuralSchema condition, but got one: %v", cond)
15451547
}
15461548
return
15471549
}
15481550

15491551
// wait for condition to appear with the given violations
15501552
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{})
1553+
err = wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
1554+
obj, err := apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, betaCRD.Name, metav1.GetOptions{})
15531555
if err != nil {
15541556
return false, err
15551557
}

0 commit comments

Comments
 (0)