From 0753a8e75be48fcdc914349fafc17e440d4a9ac6 Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Fri, 15 Dec 2023 23:59:08 -0800 Subject: [PATCH] Fix linter warnings (#569) Signed-off-by: Tamal Saha --- .../v1/mutatingwebhookconfiguration.go | 2 +- .../v1/validatingwebhookconfiguration.go | 2 +- admissionregistration/v1/xray.go | 4 ++-- .../v1beta1/mutatingwebhookconfiguration.go | 2 +- .../v1beta1/validatingwebhookconfiguration.go | 2 +- admissionregistration/v1beta1/xray.go | 4 ++-- apiextensions/kubernetes.go | 2 +- apiextensions/v1/crd.go | 2 +- apiextensions/v1beta1/crd.go | 2 +- apiregistration/v1/apiservice.go | 2 +- apiregistration/v1beta1/apiservice.go | 2 +- apps/v1/daemonset.go | 4 ++-- apps/v1/deployment.go | 4 ++-- apps/v1/replicaset.go | 4 ++-- apps/v1/statefulset.go | 4 ++-- batch/v1/cronjob.go | 2 +- batch/v1/job.go | 4 ++-- batch/v1beta1/cronjob.go | 2 +- certificates/v1beta1/csr.go | 2 +- client/sa-token.go | 2 +- core/v1/configmap.go | 2 +- core/v1/events.go | 2 +- core/v1/node.go | 8 ++++---- core/v1/pod.go | 10 +++++----- core/v1/pv.go | 2 +- core/v1/pvc.go | 2 +- core/v1/rc.go | 4 ++-- core/v1/sa-token.go | 2 +- core/v1/secret.go | 2 +- core/v1/service.go | 4 ++-- core/v1/serviceaccount.go | 4 ++-- dynamic/kubernetes.go | 4 ++-- dynamic/unstructured.go | 4 ++-- extensions/v1beta1/daemonset.go | 4 ++-- extensions/v1beta1/deployment.go | 4 ++-- extensions/v1beta1/ingress.go | 2 +- extensions/v1beta1/replicaset.go | 4 ++-- networking/v1/ingress.go | 2 +- networking/v1beta1/ingress.go | 2 +- policy/v1/pdb.go | 2 +- policy/v1beta1/pdb.go | 2 +- rbac/v1/clusterrole.go | 2 +- rbac/v1/clusterrolebinding.go | 2 +- rbac/v1/role.go | 4 ++-- rbac/v1/rolebinding.go | 4 ++-- storage/v1/storageclass.go | 2 +- tools/exec/lib.go | 14 +++++++++----- tools/wait/wait.go | 18 +++++++++--------- 48 files changed, 88 insertions(+), 84 deletions(-) diff --git a/admissionregistration/v1/mutatingwebhookconfiguration.go b/admissionregistration/v1/mutatingwebhookconfiguration.go index 62561ea66..bb0459696 100644 --- a/admissionregistration/v1/mutatingwebhookconfiguration.go +++ b/admissionregistration/v1/mutatingwebhookconfiguration.go @@ -91,7 +91,7 @@ func PatchMutatingWebhookConfigurationObject(ctx context.Context, c kubernetes.I func TryUpdateMutatingWebhookConfiguration(ctx context.Context, c kubernetes.Interface, name string, transform func(*reg.MutatingWebhookConfiguration) *reg.MutatingWebhookConfiguration, opts metav1.UpdateOptions) (result *reg.MutatingWebhookConfiguration, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.AdmissionregistrationV1().MutatingWebhookConfigurations().Get(ctx, name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/admissionregistration/v1/validatingwebhookconfiguration.go b/admissionregistration/v1/validatingwebhookconfiguration.go index 214f584d3..7724d7416 100644 --- a/admissionregistration/v1/validatingwebhookconfiguration.go +++ b/admissionregistration/v1/validatingwebhookconfiguration.go @@ -91,7 +91,7 @@ func PatchValidatingWebhookConfigurationObject(ctx context.Context, c kubernetes func TryUpdateValidatingWebhookConfiguration(ctx context.Context, c kubernetes.Interface, name string, transform func(*reg.ValidatingWebhookConfiguration) *reg.ValidatingWebhookConfiguration, opts metav1.UpdateOptions) (result *reg.ValidatingWebhookConfiguration, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.AdmissionregistrationV1().ValidatingWebhookConfigurations().Get(ctx, name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/admissionregistration/v1/xray.go b/admissionregistration/v1/xray.go index dabaa8f63..3a43af148 100644 --- a/admissionregistration/v1/xray.go +++ b/admissionregistration/v1/xray.go @@ -117,7 +117,7 @@ func (d ValidatingWebhookXray) IsActive(ctx context.Context) error { attempt := 0 var failures []string - return wait.PollImmediateUntil(kutil.RetryInterval, func() (bool, error) { + return wait.PollUntilContextCancel(wait.ContextForChannel(d.stopCh), kutil.RetryInterval, true, func(ctx context.Context) (bool, error) { apisvc, err := apireg.ApiregistrationV1().APIServices().Get(ctx, d.apisvc, metav1.GetOptions{}) if err != nil { return false, retry(err) @@ -166,7 +166,7 @@ func (d ValidatingWebhookXray) IsActive(ctx context.Context) error { } } return false, nil - }, d.stopCh) + }) } func (d ValidatingWebhookXray) updateAPIService(ctx context.Context, apireg apireg_cs.Interface, apisvc *apiregistration.APIService, err error) error { diff --git a/admissionregistration/v1beta1/mutatingwebhookconfiguration.go b/admissionregistration/v1beta1/mutatingwebhookconfiguration.go index da50cd226..8f9944698 100644 --- a/admissionregistration/v1beta1/mutatingwebhookconfiguration.go +++ b/admissionregistration/v1beta1/mutatingwebhookconfiguration.go @@ -91,7 +91,7 @@ func PatchMutatingWebhookConfigurationObject(ctx context.Context, c kubernetes.I func TryUpdateMutatingWebhookConfiguration(ctx context.Context, c kubernetes.Interface, name string, transform func(*reg.MutatingWebhookConfiguration) *reg.MutatingWebhookConfiguration, opts metav1.UpdateOptions) (result *reg.MutatingWebhookConfiguration, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().Get(ctx, name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/admissionregistration/v1beta1/validatingwebhookconfiguration.go b/admissionregistration/v1beta1/validatingwebhookconfiguration.go index b1ec6fdfd..0337f4135 100644 --- a/admissionregistration/v1beta1/validatingwebhookconfiguration.go +++ b/admissionregistration/v1beta1/validatingwebhookconfiguration.go @@ -91,7 +91,7 @@ func PatchValidatingWebhookConfigurationObject(ctx context.Context, c kubernetes func TryUpdateValidatingWebhookConfiguration(ctx context.Context, c kubernetes.Interface, name string, transform func(*reg.ValidatingWebhookConfiguration) *reg.ValidatingWebhookConfiguration, opts metav1.UpdateOptions) (result *reg.ValidatingWebhookConfiguration, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations().Get(ctx, name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/admissionregistration/v1beta1/xray.go b/admissionregistration/v1beta1/xray.go index 10de9a4ae..092edafca 100644 --- a/admissionregistration/v1beta1/xray.go +++ b/admissionregistration/v1beta1/xray.go @@ -117,7 +117,7 @@ func (d ValidatingWebhookXray) IsActive(ctx context.Context) error { attempt := 0 var failures []string - return wait.PollImmediateUntil(kutil.RetryInterval, func() (bool, error) { + return wait.PollUntilContextCancel(wait.ContextForChannel(d.stopCh), kutil.RetryInterval, true, func(ctx context.Context) (bool, error) { apisvc, err := apireg.ApiregistrationV1beta1().APIServices().Get(ctx, d.apisvc, metav1.GetOptions{}) if err != nil { return false, retry(err) @@ -166,7 +166,7 @@ func (d ValidatingWebhookXray) IsActive(ctx context.Context) error { } } return false, nil - }, d.stopCh) + }) } func (d ValidatingWebhookXray) updateAPIService(ctx context.Context, apireg apireg_cs.Interface, apisvc *apiregistration.APIService, err error) error { diff --git a/apiextensions/kubernetes.go b/apiextensions/kubernetes.go index a628ca661..cab174972 100644 --- a/apiextensions/kubernetes.go +++ b/apiextensions/kubernetes.go @@ -66,7 +66,7 @@ func RegisterCRDs(client crd_cs.Interface, crds []*CustomResourceDefinition) err } func WaitForCRDReady(client crd_cs.Interface, crds []*CustomResourceDefinition) error { - err := wait.Poll(3*time.Second, 5*time.Minute, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.Background(), 3*time.Second, 5*time.Minute, false, func(ctx context.Context) (bool, error) { for _, crd := range crds { var gvr schema.GroupVersionResource if crd.V1 != nil { diff --git a/apiextensions/v1/crd.go b/apiextensions/v1/crd.go index 3adf9a40c..d49713a2c 100644 --- a/apiextensions/v1/crd.go +++ b/apiextensions/v1/crd.go @@ -70,7 +70,7 @@ func TryUpdateCustomResourceDefinition( opts metav1.UpdateOptions, ) (result *api.CustomResourceDefinition, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/apiextensions/v1beta1/crd.go b/apiextensions/v1beta1/crd.go index 2c5e9f0cb..1c62dc563 100644 --- a/apiextensions/v1beta1/crd.go +++ b/apiextensions/v1beta1/crd.go @@ -70,7 +70,7 @@ func TryUpdateCustomResourceDefinition( opts metav1.UpdateOptions, ) (result *api.CustomResourceDefinition, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.ApiextensionsV1beta1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/apiregistration/v1/apiservice.go b/apiregistration/v1/apiservice.go index e4c069a3b..ad05955e6 100644 --- a/apiregistration/v1/apiservice.go +++ b/apiregistration/v1/apiservice.go @@ -83,7 +83,7 @@ func PatchAPIServiceObject(ctx context.Context, c apireg_cs.Interface, cur, mod func TryUpdateAPIService(ctx context.Context, c apireg_cs.Interface, name string, transform func(*reg.APIService) *reg.APIService, opts metav1.UpdateOptions) (result *reg.APIService, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.ApiregistrationV1().APIServices().Get(ctx, name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/apiregistration/v1beta1/apiservice.go b/apiregistration/v1beta1/apiservice.go index 9f326f4cb..90f269a03 100644 --- a/apiregistration/v1beta1/apiservice.go +++ b/apiregistration/v1beta1/apiservice.go @@ -83,7 +83,7 @@ func PatchAPIServiceObject(ctx context.Context, c apireg_cs.Interface, cur, mod func TryUpdateAPIService(ctx context.Context, c apireg_cs.Interface, name string, transform func(*reg.APIService) *reg.APIService, opts metav1.UpdateOptions) (result *reg.APIService, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.ApiregistrationV1beta1().APIServices().Get(ctx, name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/apps/v1/daemonset.go b/apps/v1/daemonset.go index 5283d1d21..a0f66579b 100644 --- a/apps/v1/daemonset.go +++ b/apps/v1/daemonset.go @@ -81,7 +81,7 @@ func PatchDaemonSetObject(ctx context.Context, c kubernetes.Interface, cur, mod func TryUpdateDaemonSet(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*apps.DaemonSet) *apps.DaemonSet, opts metav1.UpdateOptions) (result *apps.DaemonSet, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.AppsV1().DaemonSets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -101,7 +101,7 @@ func TryUpdateDaemonSet(ctx context.Context, c kubernetes.Interface, meta metav1 } func WaitUntilDaemonSetReady(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { // It takes some time to populate .status field of the DaemonSet after it is being created. // If this function is called just after creating a DaemonSet, the Get methond returns an obj with .status field is defaulted to their default values. // At this time, "obj.Status.DesiredNumberScheduled" and "obj.Status.NumberReady" both are defaulted to 0 and "obj.Status.DesiredNumberScheduled == obj.Status.NumberReady" diff --git a/apps/v1/deployment.go b/apps/v1/deployment.go index ad460bdc5..941bc4f24 100644 --- a/apps/v1/deployment.go +++ b/apps/v1/deployment.go @@ -85,7 +85,7 @@ func PatchDeploymentObject(ctx context.Context, c kubernetes.Interface, cur, mod func TryUpdateDeployment(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*apps.Deployment) *apps.Deployment, opts metav1.UpdateOptions) (result *apps.Deployment, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.AppsV1().Deployments(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -122,7 +122,7 @@ func DeploymentsAreReady(items []*apps.Deployment) (bool, string) { } func WaitUntilDeploymentReady(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { if obj, err := c.AppsV1().Deployments(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}); err == nil { return IsDeploymentReady(obj), nil } diff --git a/apps/v1/replicaset.go b/apps/v1/replicaset.go index d02bbc356..ddad60adc 100644 --- a/apps/v1/replicaset.go +++ b/apps/v1/replicaset.go @@ -82,7 +82,7 @@ func PatchReplicaSetObject(ctx context.Context, c kubernetes.Interface, cur, mod func TryUpdateReplicaSet(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*apps.ReplicaSet) *apps.ReplicaSet, opts metav1.UpdateOptions) (result *apps.ReplicaSet, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.AppsV1().ReplicaSets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -102,7 +102,7 @@ func TryUpdateReplicaSet(ctx context.Context, c kubernetes.Interface, meta metav } func WaitUntilReplicaSetReady(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { if obj, err := c.AppsV1().ReplicaSets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}); err == nil { return pointer.Int32(obj.Spec.Replicas) == obj.Status.ReadyReplicas, nil } diff --git a/apps/v1/statefulset.go b/apps/v1/statefulset.go index ba908a44f..8fd145f81 100644 --- a/apps/v1/statefulset.go +++ b/apps/v1/statefulset.go @@ -85,7 +85,7 @@ func PatchStatefulSetObject(ctx context.Context, c kubernetes.Interface, cur, mo func TryUpdateStatefulSet(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*apps.StatefulSet) *apps.StatefulSet, opts metav1.UpdateOptions) (result *apps.StatefulSet, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.AppsV1().StatefulSets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -122,7 +122,7 @@ func StatefulSetsAreReady(items []*apps.StatefulSet) (bool, string) { } func WaitUntilStatefulSetReady(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { if obj, err := c.AppsV1().StatefulSets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}); err == nil { return IsStatefulSetReady(obj), nil } diff --git a/batch/v1/cronjob.go b/batch/v1/cronjob.go index 4777ecbc6..3ba282288 100644 --- a/batch/v1/cronjob.go +++ b/batch/v1/cronjob.go @@ -81,7 +81,7 @@ func PatchCronJobObject(ctx context.Context, c kubernetes.Interface, cur, mod *b func TryUpdateCronJob(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*batch.CronJob) *batch.CronJob, opts metav1.UpdateOptions) (result *batch.CronJob, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.BatchV1().CronJobs(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/batch/v1/job.go b/batch/v1/job.go index 2a67741ad..4b36ef10a 100644 --- a/batch/v1/job.go +++ b/batch/v1/job.go @@ -82,7 +82,7 @@ func PatchJobObject(ctx context.Context, c kubernetes.Interface, cur, mod *batch func TryUpdateJob(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*batch.Job) *batch.Job, opts metav1.UpdateOptions) (result *batch.Job, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.BatchV1().Jobs(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -102,7 +102,7 @@ func TryUpdateJob(ctx context.Context, c kubernetes.Interface, meta metav1.Objec } func WaitUntilJobCompletion(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollInfinite(kutil.RetryInterval, func() (bool, error) { + return wait.PollUntilContextCancel(ctx, kutil.RetryInterval, true, func(ctx context.Context) (bool, error) { job, err := c.BatchV1().Jobs(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if err != nil { if kerr.IsNotFound(err) { diff --git a/batch/v1beta1/cronjob.go b/batch/v1beta1/cronjob.go index a9ceae0f2..b38df8fd4 100644 --- a/batch/v1beta1/cronjob.go +++ b/batch/v1beta1/cronjob.go @@ -81,7 +81,7 @@ func PatchCronJobObject(ctx context.Context, c kubernetes.Interface, cur, mod *b func TryUpdateCronJob(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*batch.CronJob) *batch.CronJob, opts metav1.UpdateOptions) (result *batch.CronJob, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.BatchV1beta1().CronJobs(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/certificates/v1beta1/csr.go b/certificates/v1beta1/csr.go index 8bc04be92..231031a22 100644 --- a/certificates/v1beta1/csr.go +++ b/certificates/v1beta1/csr.go @@ -81,7 +81,7 @@ func PatchCSRObject(ctx context.Context, c kubernetes.Interface, cur, mod *certi func TryUpdateCSR(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*certificates.CertificateSigningRequest) *certificates.CertificateSigningRequest, opts metav1.UpdateOptions) (result *certificates.CertificateSigningRequest, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CertificatesV1beta1().CertificateSigningRequests().Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/client/sa-token.go b/client/sa-token.go index a19177367..866a86f38 100644 --- a/client/sa-token.go +++ b/client/sa-token.go @@ -62,7 +62,7 @@ const ( ) func tryGetServiceAccountTokenSecret(kc client.Client, sa client.ObjectKey) (secret *core.Secret, err error) { - err = wait.PollImmediate(kutil.RetryInterval, RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(context.Background(), kutil.RetryInterval, RetryTimeout, true, func(ctx context.Context) (bool, error) { var e2 error secret, e2 = getServiceAccountTokenSecret(kc, sa) if e2 == nil { diff --git a/core/v1/configmap.go b/core/v1/configmap.go index b114462e7..a354b82a8 100644 --- a/core/v1/configmap.go +++ b/core/v1/configmap.go @@ -81,7 +81,7 @@ func PatchConfigMapObject(ctx context.Context, c kubernetes.Interface, cur, mod func TryUpdateConfigMap(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*core.ConfigMap) *core.ConfigMap, opts metav1.UpdateOptions) (result *core.ConfigMap, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CoreV1().ConfigMaps(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/core/v1/events.go b/core/v1/events.go index 1fe9e2348..31e3ea40e 100644 --- a/core/v1/events.go +++ b/core/v1/events.go @@ -81,7 +81,7 @@ func PatchEventObject(ctx context.Context, c kubernetes.Interface, cur, mod *cor func TryUpdateEvent(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*core.Event) *core.Event, opts metav1.UpdateOptions) (result *core.Event, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CoreV1().Events(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/core/v1/node.go b/core/v1/node.go index 4fdb54615..7282a20d4 100644 --- a/core/v1/node.go +++ b/core/v1/node.go @@ -89,7 +89,7 @@ func PatchNodeObject(ctx context.Context, c kubernetes.Interface, cur, mod *core func TryUpdateNode(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*core.Node) *core.Node, opts metav1.UpdateOptions) (result *core.Node, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CoreV1().Nodes().Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -204,7 +204,7 @@ func DetectTopology(ctx context.Context, mc metadata.Interface) (*Topology, erro var topology Topology topology.TotalNodes = 0 - mapRegion := make(map[string]sets.String) + mapRegion := make(map[string]sets.Set[string]) instances := make(map[string]int) first := true @@ -259,7 +259,7 @@ func DetectTopology(ctx context.Context, mc metadata.Interface) (*Topology, erro region, _ := meta_util.GetStringValueForKeys(labels, topology.LabelRegion) zone, _ := meta_util.GetStringValueForKeys(labels, topology.LabelZone) if _, ok := mapRegion[region]; !ok { - mapRegion[region] = sets.NewString() + mapRegion[region] = sets.Set[string]{} } mapRegion[region].Insert(zone) @@ -278,7 +278,7 @@ func DetectTopology(ctx context.Context, mc metadata.Interface) (*Topology, erro regions := make(map[string][]string) for k, v := range mapRegion { - regions[k] = v.List() + regions[k] = sets.List(v) } topology.Regions = regions topology.InstanceTypes = instances diff --git a/core/v1/pod.go b/core/v1/pod.go index 3decd3093..df8089284 100644 --- a/core/v1/pod.go +++ b/core/v1/pod.go @@ -81,7 +81,7 @@ func PatchPodObject(ctx context.Context, c kubernetes.Interface, cur, mod *core. func TryUpdatePod(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*core.Pod) *core.Pod, opts metav1.UpdateOptions) (result *core.Pod, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CoreV1().Pods(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -144,7 +144,7 @@ func RestartPods(ctx context.Context, c kubernetes.Interface, namespace string, } func WaitUntilPodRunning(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { if pod, err := c.CoreV1().Pods(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}); err == nil { runningAndReady, _ := PodRunningAndReady(*pod) return runningAndReady, nil @@ -159,7 +159,7 @@ func WaitUntilPodRunningBySelector(ctx context.Context, c kubernetes.Interface, return err } - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { podList, err := c.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ LabelSelector: r.String(), }) @@ -187,7 +187,7 @@ func WaitUntilPodDeletedBySelector(ctx context.Context, c kubernetes.Interface, return err } - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { podList, err := c.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ LabelSelector: sel.String(), }) @@ -200,7 +200,7 @@ func WaitUntilPodDeletedBySelector(ctx context.Context, c kubernetes.Interface, // WaitUntillPodTerminatedByLabel waits until all pods with the label are terminated. Timeout is 5 minutes. func WaitUntillPodTerminatedByLabel(ctx context.Context, c kubernetes.Interface, namespace string, label string) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.GCTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.GCTimeout, true, func(ctx context.Context) (bool, error) { podList, err := c.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: label}) if err != nil { return false, nil diff --git a/core/v1/pv.go b/core/v1/pv.go index 1640a6ac3..102e9684e 100644 --- a/core/v1/pv.go +++ b/core/v1/pv.go @@ -81,7 +81,7 @@ func PatchPVObject(ctx context.Context, c kubernetes.Interface, cur, mod *core.P func TryUpdatePV(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*core.PersistentVolume) *core.PersistentVolume, opts metav1.UpdateOptions) (result *core.PersistentVolume, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CoreV1().PersistentVolumes().Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/core/v1/pvc.go b/core/v1/pvc.go index 8506715db..1bd228d80 100644 --- a/core/v1/pvc.go +++ b/core/v1/pvc.go @@ -81,7 +81,7 @@ func PatchPVCObject(ctx context.Context, c kubernetes.Interface, cur, mod *core. func TryUpdatePVC(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*core.PersistentVolumeClaim) *core.PersistentVolumeClaim, opts metav1.UpdateOptions) (result *core.PersistentVolumeClaim, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CoreV1().PersistentVolumeClaims(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/core/v1/rc.go b/core/v1/rc.go index e9aa32ed9..6f7fa20b3 100644 --- a/core/v1/rc.go +++ b/core/v1/rc.go @@ -82,7 +82,7 @@ func PatchRCObject(ctx context.Context, c kubernetes.Interface, cur, mod *core.R func TryUpdateRC(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*core.ReplicationController) *core.ReplicationController, opts metav1.UpdateOptions) (result *core.ReplicationController, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CoreV1().ReplicationControllers(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -102,7 +102,7 @@ func TryUpdateRC(ctx context.Context, c kubernetes.Interface, meta metav1.Object } func WaitUntilRCReady(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { if obj, err := c.CoreV1().ReplicationControllers(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}); err == nil { return pointer.Int32(obj.Spec.Replicas) == obj.Status.ReadyReplicas, nil } diff --git a/core/v1/sa-token.go b/core/v1/sa-token.go index bb1f1ca4f..034c365c6 100644 --- a/core/v1/sa-token.go +++ b/core/v1/sa-token.go @@ -61,7 +61,7 @@ const ( ) func tryGetServiceAccountTokenSecret(kc kubernetes.Interface, sa types.NamespacedName) (secret *core.Secret, err error) { - err = wait.PollImmediate(kutil.RetryInterval, retryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(context.Background(), kutil.RetryInterval, retryTimeout, true, func(ctx context.Context) (bool, error) { var e2 error secret, e2 = getServiceAccountTokenSecret(kc, sa) if e2 == nil { diff --git a/core/v1/secret.go b/core/v1/secret.go index c5e96663e..058a5c36d 100644 --- a/core/v1/secret.go +++ b/core/v1/secret.go @@ -105,7 +105,7 @@ func PatchSecretObject(ctx context.Context, c kubernetes.Interface, cur, mod *co func TryUpdateSecret(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*core.Secret) *core.Secret, opts metav1.UpdateOptions) (result *core.Secret, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CoreV1().Secrets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/core/v1/service.go b/core/v1/service.go index db3eb295a..88e639960 100644 --- a/core/v1/service.go +++ b/core/v1/service.go @@ -81,7 +81,7 @@ func PatchServiceObject(ctx context.Context, c kubernetes.Interface, cur, mod *c func TryUpdateService(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*core.Service) *core.Service, opts metav1.UpdateOptions) (result *core.Service, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CoreV1().Services(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -141,7 +141,7 @@ func WaitUntilServiceDeletedBySelector(ctx context.Context, c kubernetes.Interfa return err } - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { svcList, err := c.CoreV1().Services(namespace).List(ctx, metav1.ListOptions{ LabelSelector: sel.String(), }) diff --git a/core/v1/serviceaccount.go b/core/v1/serviceaccount.go index fdb00c9e2..3d08a625f 100644 --- a/core/v1/serviceaccount.go +++ b/core/v1/serviceaccount.go @@ -81,7 +81,7 @@ func PatchServiceAccountObject(ctx context.Context, c kubernetes.Interface, cur, func TryUpdateServiceAccount(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*core.ServiceAccount) *core.ServiceAccount, opts metav1.UpdateOptions) (result *core.ServiceAccount, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.CoreV1().ServiceAccounts(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -101,7 +101,7 @@ func TryUpdateServiceAccount(ctx context.Context, c kubernetes.Interface, meta m } func WaitUntillServiceAccountDeleted(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.GCTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.GCTimeout, true, func(ctx context.Context) (bool, error) { _, err := c.CoreV1().ServiceAccounts(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if err != nil && kerr.IsNotFound(err) { return true, nil diff --git a/dynamic/kubernetes.go b/dynamic/kubernetes.go index c58d6a04b..584cc112b 100644 --- a/dynamic/kubernetes.go +++ b/dynamic/kubernetes.go @@ -52,7 +52,7 @@ func WaitUntilDeleted(ri dynamic.ResourceInterface, stopCh <-chan struct{}, name return err } // delete operation was successful, now wait for obj to be removed(eg: objects with finalizers) - return wait.PollImmediateUntil(kutil.RetryInterval, func() (bool, error) { + return wait.PollUntilContextCancel(wait.ContextForChannel(stopCh), kutil.RetryInterval, true, func(ctx context.Context) (bool, error) { _, e2 := ri.Get(context.TODO(), name, metav1.GetOptions{}, subresources...) if kerr.IsNotFound(e2) { return true, nil @@ -60,7 +60,7 @@ func WaitUntilDeleted(ri dynamic.ResourceInterface, stopCh <-chan struct{}, name return false, e2 } return false, nil - }, stopCh) + }) } func UntilHasLabel(config *rest.Config, gvk schema.GroupVersionKind, namespace, name string, key string, value *string, timeout time.Duration) (out string, err error) { diff --git a/dynamic/unstructured.go b/dynamic/unstructured.go index f7a723caf..2c2ce2198 100644 --- a/dynamic/unstructured.go +++ b/dynamic/unstructured.go @@ -131,7 +131,7 @@ func TryUpdate( } attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := ri.Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -167,7 +167,7 @@ func UpdateStatus( attempt := 0 cur := in.DeepCopy() - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ var e2 error result, e2 = ri.UpdateStatus(ctx, transform(cur), opts) diff --git a/extensions/v1beta1/daemonset.go b/extensions/v1beta1/daemonset.go index 6d2c0d76d..2e268dff9 100644 --- a/extensions/v1beta1/daemonset.go +++ b/extensions/v1beta1/daemonset.go @@ -81,7 +81,7 @@ func PatchDaemonSetObject(ctx context.Context, c kubernetes.Interface, cur, mod func TryUpdateDaemonSet(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*extensions.DaemonSet) *extensions.DaemonSet, opts metav1.UpdateOptions) (result *extensions.DaemonSet, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.ExtensionsV1beta1().DaemonSets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -101,7 +101,7 @@ func TryUpdateDaemonSet(ctx context.Context, c kubernetes.Interface, meta metav1 } func WaitUntilDaemonSetReady(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { if obj, err := c.ExtensionsV1beta1().DaemonSets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}); err == nil { return obj.Status.DesiredNumberScheduled == obj.Status.NumberReady, nil } diff --git a/extensions/v1beta1/deployment.go b/extensions/v1beta1/deployment.go index 08e74fd06..bbfe21ef1 100644 --- a/extensions/v1beta1/deployment.go +++ b/extensions/v1beta1/deployment.go @@ -82,7 +82,7 @@ func PatchDeploymentObject(ctx context.Context, c kubernetes.Interface, cur, mod func TryUpdateDeployment(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*extensions.Deployment) *extensions.Deployment, opts metav1.UpdateOptions) (result *extensions.Deployment, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.ExtensionsV1beta1().Deployments(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -102,7 +102,7 @@ func TryUpdateDeployment(ctx context.Context, c kubernetes.Interface, meta metav } func WaitUntilDeploymentReady(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { if obj, err := c.ExtensionsV1beta1().Deployments(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}); err == nil { return pointer.Int32(obj.Spec.Replicas) == obj.Status.ReadyReplicas, nil } diff --git a/extensions/v1beta1/ingress.go b/extensions/v1beta1/ingress.go index ccbfa399e..106121098 100644 --- a/extensions/v1beta1/ingress.go +++ b/extensions/v1beta1/ingress.go @@ -81,7 +81,7 @@ func PatchIngressObject(ctx context.Context, c kubernetes.Interface, cur, mod *e func TryUpdateIngress(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*extensions.Ingress) *extensions.Ingress, opts metav1.UpdateOptions) (result *extensions.Ingress, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.ExtensionsV1beta1().Ingresses(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/extensions/v1beta1/replicaset.go b/extensions/v1beta1/replicaset.go index f0f5de857..5d54b0c75 100644 --- a/extensions/v1beta1/replicaset.go +++ b/extensions/v1beta1/replicaset.go @@ -82,7 +82,7 @@ func PatchReplicaSetObject(ctx context.Context, c kubernetes.Interface, cur, mod func TryUpdateReplicaSet(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*extensions.ReplicaSet) *extensions.ReplicaSet, opts metav1.UpdateOptions) (result *extensions.ReplicaSet, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.ExtensionsV1beta1().ReplicaSets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -102,7 +102,7 @@ func TryUpdateReplicaSet(ctx context.Context, c kubernetes.Interface, meta metav } func WaitUntilReplicaSetReady(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.ReadinessTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.ReadinessTimeout, true, func(ctx context.Context) (bool, error) { if obj, err := c.ExtensionsV1beta1().ReplicaSets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}); err == nil { return pointer.Int32(obj.Spec.Replicas) == obj.Status.ReadyReplicas, nil } diff --git a/networking/v1/ingress.go b/networking/v1/ingress.go index 5d1630645..16ef10d83 100644 --- a/networking/v1/ingress.go +++ b/networking/v1/ingress.go @@ -81,7 +81,7 @@ func PatchIngressObject(ctx context.Context, c kubernetes.Interface, cur, mod *n func TryUpdateIngress(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*networking.Ingress) *networking.Ingress, opts metav1.UpdateOptions) (result *networking.Ingress, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.NetworkingV1().Ingresses(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/networking/v1beta1/ingress.go b/networking/v1beta1/ingress.go index b568e393c..f82f4431c 100644 --- a/networking/v1beta1/ingress.go +++ b/networking/v1beta1/ingress.go @@ -81,7 +81,7 @@ func PatchIngressObject(ctx context.Context, c kubernetes.Interface, cur, mod *n func TryUpdateIngress(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*networking.Ingress) *networking.Ingress, opts metav1.UpdateOptions) (result *networking.Ingress, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.NetworkingV1beta1().Ingresses(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/policy/v1/pdb.go b/policy/v1/pdb.go index d2d29051b..98e748e7c 100644 --- a/policy/v1/pdb.go +++ b/policy/v1/pdb.go @@ -81,7 +81,7 @@ func PatchPodDisruptionBudgetObject(ctx context.Context, c kubernetes.Interface, func TryUpdatePodDisruptionBudget(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*policy.PodDisruptionBudget) *policy.PodDisruptionBudget, opts metav1.UpdateOptions) (result *policy.PodDisruptionBudget, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.PolicyV1().PodDisruptionBudgets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/policy/v1beta1/pdb.go b/policy/v1beta1/pdb.go index 782deecad..d7356484f 100644 --- a/policy/v1beta1/pdb.go +++ b/policy/v1beta1/pdb.go @@ -81,7 +81,7 @@ func PatchPodDisruptionBudgetObject(ctx context.Context, c kubernetes.Interface, func TryUpdatePodDisruptionBudget(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*policy.PodDisruptionBudget) *policy.PodDisruptionBudget, opts metav1.UpdateOptions) (result *policy.PodDisruptionBudget, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.PolicyV1beta1().PodDisruptionBudgets(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/rbac/v1/clusterrole.go b/rbac/v1/clusterrole.go index 6b0c7f867..2897e2141 100644 --- a/rbac/v1/clusterrole.go +++ b/rbac/v1/clusterrole.go @@ -81,7 +81,7 @@ func PatchClusterRoleObject(ctx context.Context, c kubernetes.Interface, cur, mo func TryUpdateClusterRole(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*rbac.ClusterRole) *rbac.ClusterRole, opts metav1.UpdateOptions) (result *rbac.ClusterRole, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.RbacV1().ClusterRoles().Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/rbac/v1/clusterrolebinding.go b/rbac/v1/clusterrolebinding.go index a2752abab..daafe4f5e 100644 --- a/rbac/v1/clusterrolebinding.go +++ b/rbac/v1/clusterrolebinding.go @@ -81,7 +81,7 @@ func PatchClusterRoleBindingObject(ctx context.Context, c kubernetes.Interface, func TryUpdateClusterRoleBinding(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*rbac.ClusterRoleBinding) *rbac.ClusterRoleBinding, opts metav1.UpdateOptions) (result *rbac.ClusterRoleBinding, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.RbacV1().ClusterRoleBindings().Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/rbac/v1/role.go b/rbac/v1/role.go index d23bccf73..1f54dc69a 100644 --- a/rbac/v1/role.go +++ b/rbac/v1/role.go @@ -81,7 +81,7 @@ func PatchRoleObject(ctx context.Context, c kubernetes.Interface, cur, mod *rbac func TryUpdateRole(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*rbac.Role) *rbac.Role, opts metav1.UpdateOptions) (result *rbac.Role, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.RbacV1().Roles(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -101,7 +101,7 @@ func TryUpdateRole(ctx context.Context, c kubernetes.Interface, meta metav1.Obje } func WaitUntillRoleDeleted(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.GCTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.GCTimeout, true, func(ctx context.Context) (bool, error) { _, err := c.RbacV1().Roles(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if err != nil && kerr.IsNotFound(err) { return true, nil diff --git a/rbac/v1/rolebinding.go b/rbac/v1/rolebinding.go index 0991c01a6..e1aa2b834 100644 --- a/rbac/v1/rolebinding.go +++ b/rbac/v1/rolebinding.go @@ -81,7 +81,7 @@ func PatchRoleBindingObject(ctx context.Context, c kubernetes.Interface, cur, mo func TryUpdateRoleBinding(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*rbac.RoleBinding) *rbac.RoleBinding, opts metav1.UpdateOptions) (result *rbac.RoleBinding, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.RbacV1().RoleBindings(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { @@ -101,7 +101,7 @@ func TryUpdateRoleBinding(ctx context.Context, c kubernetes.Interface, meta meta } func WaitUntillRoleBindingDeleted(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta) error { - return wait.PollImmediate(kutil.RetryInterval, kutil.GCTimeout, func() (bool, error) { + return wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.GCTimeout, true, func(ctx context.Context) (bool, error) { _, err := c.RbacV1().RoleBindings(meta.Namespace).Get(ctx, meta.Name, metav1.GetOptions{}) if err != nil && kerr.IsNotFound(err) { return true, nil diff --git a/storage/v1/storageclass.go b/storage/v1/storageclass.go index 6ab5559cb..e0f2c2ea7 100644 --- a/storage/v1/storageclass.go +++ b/storage/v1/storageclass.go @@ -81,7 +81,7 @@ func PatchStorageClassObject(ctx context.Context, c kubernetes.Interface, cur, m func TryUpdateStorageClass(ctx context.Context, c kubernetes.Interface, meta metav1.ObjectMeta, transform func(*storage.StorageClass) *storage.StorageClass, opts metav1.UpdateOptions) (result *storage.StorageClass, err error) { attempt := 0 - err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(ctx, kutil.RetryInterval, kutil.RetryTimeout, true, func(ctx context.Context) (bool, error) { attempt++ cur, e2 := c.StorageV1().StorageClasses().Get(ctx, meta.Name, metav1.GetOptions{}) if kerr.IsNotFound(e2) { diff --git a/tools/exec/lib.go b/tools/exec/lib.go index c4a44ff75..c6d021f0b 100644 --- a/tools/exec/lib.go +++ b/tools/exec/lib.go @@ -73,26 +73,30 @@ func TTY(enable bool) func(*Options) { } func Exec(config *rest.Config, pod types.NamespacedName, options ...func(*Options)) (string, error) { + ctx := context.Background() + kc, err := kubernetes.NewForConfig(config) if err != nil { return "", err } - p, err := kc.CoreV1().Pods(pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{}) + p, err := kc.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{}) if err != nil { return "", err } - return execIntoPod(config, kc, p, options...) + return execIntoPod(ctx, config, kc, p, options...) } func ExecIntoPod(config *rest.Config, pod *core.Pod, options ...func(*Options)) (string, error) { + ctx := context.Background() + kc, err := kubernetes.NewForConfig(config) if err != nil { return "", err } - return execIntoPod(config, kc, pod, options...) + return execIntoPod(ctx, config, kc, pod, options...) } -func execIntoPod(config *rest.Config, kc kubernetes.Interface, pod *core.Pod, options ...func(*Options)) (string, error) { +func execIntoPod(ctx context.Context, config *rest.Config, kc kubernetes.Interface, pod *core.Pod, options ...func(*Options)) (string, error) { var ( execOut bytes.Buffer execErr bytes.Buffer @@ -141,7 +145,7 @@ func execIntoPod(config *rest.Config, kc kubernetes.Interface, pod *core.Pod, op return "", fmt.Errorf("failed to init executor: %v", err) } - err = exec.Stream(opts.StreamOptions) + err = exec.StreamWithContext(ctx, opts.StreamOptions) if err != nil { return "", fmt.Errorf("could not execute: %v", err) } diff --git a/tools/wait/wait.go b/tools/wait/wait.go index 9e566a3e9..01d7691c6 100644 --- a/tools/wait/wait.go +++ b/tools/wait/wait.go @@ -124,7 +124,7 @@ func (o *WaitOptions) RunWait() error { func (o *WaitOptions) WaitUntilAvailable(forCondition string) error { if strings.HasPrefix(forCondition, "condition=") { // Wait for the resources to be available - return wait.PollImmediate(10*time.Second, o.Timeout, func() (bool, error) { + return wait.PollUntilContextTimeout(context.Background(), 10*time.Second, o.Timeout, true, func(ctx context.Context) (bool, error) { visitCount := 0 err := o.ResourceFinder.Do().Visit(func(info *resource.Info, err error) error { if err != nil { @@ -185,7 +185,7 @@ func IsDeleted(info *resource.Info, o *WaitOptions) (runtime.Object, bool, error } timeout := time.Until(endTime) - errWaitTimeoutWithName := extendErrWaitTimeout(wait.ErrWaitTimeout, info) + errWaitTimeoutWithName := extendErrWaitTimeout(info) if timeout < 0 { // we're out of time return gottenObj, false, errWaitTimeoutWithName @@ -197,9 +197,9 @@ func IsDeleted(info *resource.Info, o *WaitOptions) (runtime.Object, bool, error switch { case err == nil: return watchEvent.Object, true, nil - case err == watchtools.ErrWatchClosed: + case errors.Is(err, watchtools.ErrWatchClosed): continue - case err == wait.ErrWaitTimeout: + case wait.Interrupted(err): if watchEvent != nil { return watchEvent.Object, false, errWaitTimeoutWithName } @@ -280,7 +280,7 @@ func (w ConditionalWait) IsConditionMet(info *resource.Info, o *WaitOptions) (ru } timeout := time.Until(endTime) - errWaitTimeoutWithName := extendErrWaitTimeout(wait.ErrWaitTimeout, info) + errWaitTimeoutWithName := extendErrWaitTimeout(info) if timeout < 0 { // we're out of time return gottenObj, false, errWaitTimeoutWithName @@ -292,9 +292,9 @@ func (w ConditionalWait) IsConditionMet(info *resource.Info, o *WaitOptions) (ru switch { case err == nil: return watchEvent.Object, true, nil - case err == watchtools.ErrWatchClosed: + case errors.Is(err, watchtools.ErrWatchClosed): continue - case err == wait.ErrWaitTimeout: + case wait.Interrupted(err): if watchEvent != nil { return watchEvent.Object, false, errWaitTimeoutWithName } @@ -345,6 +345,6 @@ func (w ConditionalWait) isConditionMet(event watch.Event) (bool, error) { return w.checkCondition(obj) } -func extendErrWaitTimeout(err error, info *resource.Info) error { - return fmt.Errorf("%s on %s/%s", err.Error(), info.Mapping.Resource.Resource, info.Name) +func extendErrWaitTimeout(info *resource.Info) error { + return wait.ErrorInterrupted(fmt.Errorf("timed out waiting for the condition on %s/%s", info.Mapping.Resource.Resource, info.Name)) }