Skip to content

Commit 32c54f7

Browse files
committed
CFE-682 : Replace deprecated functions of apimachinery/wait pkg
1 parent b4e3338 commit 32c54f7

29 files changed

+778
-697
lines changed

pkg/operator/finalizer.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,34 +98,36 @@ func (c *Controller) finalizeResources(o *imageregistryv1.Config) error {
9898

9999
retryTime := 3 * time.Second
100100

101-
err = wait.PollInfinite(retryTime, func() (stop bool, err error) {
102-
_, err = c.listers.RegistryConfigs.Get(o.Name)
103-
if err == nil {
104-
return
105-
}
101+
err = wait.PollUntilContextCancel(context.Background(), retryTime, false,
102+
func(context.Context) (stop bool, err error) {
103+
_, err = c.listers.RegistryConfigs.Get(o.Name)
104+
if err == nil {
105+
return
106+
}
106107

107-
if !kerrors.IsNotFound(err) {
108-
for _, isRetryError := range errorFuncs {
109-
if isRetryError(err) {
110-
return false, nil
108+
if !kerrors.IsNotFound(err) {
109+
for _, isRetryError := range errorFuncs {
110+
if isRetryError(err) {
111+
return false, nil
112+
}
111113
}
112-
}
113114

114-
// If the error sends the Retry-After header, we respect it as an explicit confirmation we should retry.
115-
if delaySeconds, shouldRetry := kerrors.SuggestsClientDelay(err); shouldRetry {
116-
delayTime := time.Duration(delaySeconds) * time.Second
117-
if retryTime < delayTime {
118-
time.Sleep(delayTime - retryTime)
115+
// If the error sends the Retry-After header, we respect it as an explicit confirmation we should retry.
116+
if delaySeconds, shouldRetry := kerrors.SuggestsClientDelay(err); shouldRetry {
117+
delayTime := time.Duration(delaySeconds) * time.Second
118+
if retryTime < delayTime {
119+
time.Sleep(delayTime - retryTime)
120+
}
121+
return false, nil
119122
}
120-
return false, nil
121-
}
122123

123-
err = fmt.Errorf("failed to get %s: %s", utilObjectInfo(o), err)
124-
return
125-
}
124+
err = fmt.Errorf("failed to get %s: %s", utilObjectInfo(o), err)
125+
return
126+
}
126127

127-
return true, nil
128-
})
128+
return true, nil
129+
},
130+
)
129131

130132
if err != nil {
131133
return fmt.Errorf("unable to wait for %s deletion: %s", utilObjectInfo(o), err)

pkg/resource/deployment_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,20 @@ func testSecret(sData map[string][]byte) *corev1.Secret {
164164
}
165165

166166
func waitForUpdatedSecret(ctx context.Context, kubeClient kubeclient.Interface, expectedData map[string][]byte) error {
167-
err := wait.Poll(time.Second, time.Minute, func() (stop bool, err error) {
168-
sec, err := kubeClient.CoreV1().Secrets(defaults.ImageRegistryOperatorNamespace).Get(ctx, defaults.ImageRegistryPrivateConfiguration, metav1.GetOptions{})
169-
if err != nil {
170-
// Keep waiting
167+
err := wait.PollUntilContextTimeout(ctx, time.Second, time.Minute, false,
168+
func(context.Context) (stop bool, err error) {
169+
sec, err := kubeClient.CoreV1().Secrets(defaults.ImageRegistryOperatorNamespace).Get(ctx, defaults.ImageRegistryPrivateConfiguration, metav1.GetOptions{})
170+
if err != nil {
171+
// Keep waiting
172+
return false, nil
173+
}
174+
if reflect.DeepEqual(sec.Data, expectedData) {
175+
return true, nil
176+
}
177+
// Keep trying
171178
return false, nil
172-
}
173-
if reflect.DeepEqual(sec.Data, expectedData) {
174-
return true, nil
175-
}
176-
// Keep trying
177-
return false, nil
178-
})
179+
},
180+
)
179181
return err
180182
}
181183

pkg/resource/generator.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,18 @@ func (g *Generator) Remove(cr *imageregistryv1.Config) error {
298298

299299
var derr error
300300
var retriable bool
301-
err = wait.PollImmediate(1*time.Second, 5*time.Minute, func() (stop bool, err error) {
302-
if retriable, derr = driver.RemoveStorage(cr); derr != nil {
303-
if retriable {
304-
return false, nil
305-
} else {
306-
return true, derr
301+
err = wait.PollUntilContextTimeout(context.Background(), 1*time.Second, 5*time.Minute, true,
302+
func(context.Context) (stop bool, err error) {
303+
if retriable, derr = driver.RemoveStorage(cr); derr != nil {
304+
if retriable {
305+
return false, nil
306+
} else {
307+
return true, derr
308+
}
307309
}
308-
}
309-
return true, nil
310-
})
310+
return true, nil
311+
},
312+
)
311313
if err != nil {
312314
return fmt.Errorf("unable to remove storage: %s, %s", err, derr)
313315
}

test/e2e/aws_test.go

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func TestAWSUnableToCreateBucketOnStartup(t *testing.T) {
345345
}
346346

347347
// Create the image-registry-private-configuration-user secret using the invalid credentials
348-
if _, err := framework.CreateOrUpdateSecret(defaults.ImageRegistryPrivateConfigurationUser, defaults.ImageRegistryOperatorNamespace, fakeAWSCredsData); err != nil {
348+
if _, err := framework.CreateOrUpdateSecret(context.Background(), defaults.ImageRegistryPrivateConfigurationUser, defaults.ImageRegistryOperatorNamespace, fakeAWSCredsData); err != nil {
349349
t.Fatalf("unable to create secret %q: %#v", fmt.Sprintf("%s/%s", defaults.ImageRegistryOperatorNamespace, defaults.ImageRegistryPrivateConfigurationUser), err)
350350
}
351351

@@ -404,13 +404,15 @@ func TestAWSUpdateCredentials(t *testing.T) {
404404
framework.EnsureClusterOperatorStatusIsNormal(te)
405405

406406
// Create the image-registry-private-configuration-user secret using the invalid credentials
407-
err = wait.PollImmediate(1*time.Second, framework.AsyncOperationTimeout, func() (stop bool, err error) {
408-
if _, err := framework.CreateOrUpdateSecret(defaults.ImageRegistryPrivateConfigurationUser, defaults.ImageRegistryOperatorNamespace, fakeAWSCredsData); err != nil {
409-
t.Logf("unable to create secret: %s", err)
410-
return false, nil
411-
}
412-
return true, nil
413-
})
407+
err = wait.PollUntilContextTimeout(context.Background(), 1*time.Second, framework.AsyncOperationTimeout, true,
408+
func(ctx context.Context) (stop bool, err error) {
409+
if _, err := framework.CreateOrUpdateSecret(ctx, defaults.ImageRegistryPrivateConfigurationUser, defaults.ImageRegistryOperatorNamespace, fakeAWSCredsData); err != nil {
410+
t.Logf("unable to create secret: %s", err)
411+
return false, nil
412+
}
413+
return true, nil
414+
},
415+
)
414416
if err != nil {
415417
t.Fatal(err)
416418
}
@@ -580,42 +582,44 @@ func TestAWSChangeS3Encryption(t *testing.T) {
580582
}
581583

582584
found := false
583-
err = wait.Poll(1*time.Second, framework.AsyncOperationTimeout, func() (stop bool, err error) {
584-
// Check that the S3 bucket has the correct encryption configuration
585-
getBucketEncryptionResult, err = svc.GetBucketEncryption(&s3.GetBucketEncryptionInput{
586-
Bucket: aws.String(cr.Spec.Storage.S3.Bucket),
587-
})
588-
if aerr, ok := err.(awserr.Error); ok {
589-
t.Errorf("%#v, %#v", aerr.Code(), aerr.Error())
590-
}
591-
if err != nil {
592-
return true, err
593-
}
585+
err = wait.PollUntilContextTimeout(context.Background(), 1*time.Second, framework.AsyncOperationTimeout, false,
586+
func(context.Context) (stop bool, err error) {
587+
// Check that the S3 bucket has the correct encryption configuration
588+
getBucketEncryptionResult, err = svc.GetBucketEncryption(&s3.GetBucketEncryptionInput{
589+
Bucket: aws.String(cr.Spec.Storage.S3.Bucket),
590+
})
591+
if aerr, ok := err.(awserr.Error); ok {
592+
t.Errorf("%#v, %#v", aerr.Code(), aerr.Error())
593+
}
594+
if err != nil {
595+
return true, err
596+
}
594597

595-
wantedBucketEncryption = &s3.ServerSideEncryptionConfiguration{
596-
Rules: []*s3.ServerSideEncryptionRule{
597-
{
598-
ApplyServerSideEncryptionByDefault: &s3.ServerSideEncryptionByDefault{
599-
SSEAlgorithm: aws.String(s3.ServerSideEncryptionAwsKms),
600-
KMSMasterKeyID: aws.String("testKey"),
598+
wantedBucketEncryption = &s3.ServerSideEncryptionConfiguration{
599+
Rules: []*s3.ServerSideEncryptionRule{
600+
{
601+
ApplyServerSideEncryptionByDefault: &s3.ServerSideEncryptionByDefault{
602+
SSEAlgorithm: aws.String(s3.ServerSideEncryptionAwsKms),
603+
KMSMasterKeyID: aws.String("testKey"),
604+
},
605+
BucketKeyEnabled: aws.Bool(false),
601606
},
602-
BucketKeyEnabled: aws.Bool(false),
603607
},
604-
},
605-
}
608+
}
606609

607-
for _, wantedEncryptionRule := range wantedBucketEncryption.Rules {
608-
for _, gotRule := range getBucketEncryptionResult.ServerSideEncryptionConfiguration.Rules {
609-
if reflect.DeepEqual(wantedEncryptionRule, gotRule) {
610-
found = true
611-
break
612-
} else {
613-
return false, nil
610+
for _, wantedEncryptionRule := range wantedBucketEncryption.Rules {
611+
for _, gotRule := range getBucketEncryptionResult.ServerSideEncryptionConfiguration.Rules {
612+
if reflect.DeepEqual(wantedEncryptionRule, gotRule) {
613+
found = true
614+
break
615+
} else {
616+
return false, nil
617+
}
614618
}
615619
}
616-
}
617-
return true, nil
618-
})
620+
return true, nil
621+
},
622+
)
619623
if err != nil {
620624
t.Errorf("an error occurred checking for bucket encryption: %#v", err)
621625
}
@@ -732,21 +736,23 @@ func TestAWSFinalizerDeleteS3Bucket(t *testing.T) {
732736
}
733737
s3Client := s3.New(sess)
734738
exists := true
735-
err = wait.Poll(5*time.Second, framework.AsyncOperationTimeout, func() (stop bool, err error) {
736-
_, err = s3Client.HeadBucket(&s3.HeadBucketInput{
737-
Bucket: aws.String(cr.Status.Storage.S3.Bucket),
738-
})
739-
740-
if aerr, ok := err.(awserr.Error); ok {
741-
switch aerr.Code() {
742-
case s3.ErrCodeNoSuchBucket, "Forbidden", "NotFound":
743-
exists = false
744-
return true, nil
739+
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.AsyncOperationTimeout, false,
740+
func(context.Context) (stop bool, err error) {
741+
_, err = s3Client.HeadBucket(&s3.HeadBucketInput{
742+
Bucket: aws.String(cr.Status.Storage.S3.Bucket),
743+
})
744+
745+
if aerr, ok := err.(awserr.Error); ok {
746+
switch aerr.Code() {
747+
case s3.ErrCodeNoSuchBucket, "Forbidden", "NotFound":
748+
exists = false
749+
return true, nil
750+
}
745751
}
746-
}
747752

748-
return false, err
749-
})
753+
return false, err
754+
},
755+
)
750756
if err != nil {
751757
t.Errorf("an error occurred checking for s3 bucket existence: %#v", err)
752758
}

test/e2e/bootstrap_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package e2e
22

33
import (
4+
"context"
45
"strings"
56
"testing"
67

@@ -11,7 +12,7 @@ func TestBootstrapFailToUpdateSpec(t *testing.T) {
1112
te := framework.SetupAvailableImageRegistry(t, nil)
1213
defer framework.TeardownImageRegistry(te)
1314

14-
logs, err := framework.GetOperatorLogs(te.Client())
15+
logs, err := framework.GetOperatorLogs(context.Background(), te.Client())
1516
if err != nil {
1617
t.Fatalf("error reading operator logs: %s", err)
1718
}

test/e2e/configuration_test.go

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ func TestSecureRouteConfiguration(t *testing.T) {
452452
"tls.key": string(key),
453453
}
454454

455-
if _, err := framework.CreateOrUpdateSecret(tlsSecretName, defaults.ImageRegistryOperatorNamespace, tlsSecretData); err != nil {
455+
if _, err := framework.CreateOrUpdateSecret(context.Background(), tlsSecretName, defaults.ImageRegistryOperatorNamespace, tlsSecretData); err != nil {
456456
t.Fatalf("unable to create secret: %s", err)
457457
}
458458

@@ -476,25 +476,27 @@ func TestSecureRouteConfiguration(t *testing.T) {
476476
framework.EnsureClusterOperatorStatusIsNormal(te)
477477
framework.EnsureExternalRegistryHostnamesAreSet(te, []string{hostname})
478478

479-
err = wait.Poll(5*time.Second, 1*time.Minute, func() (done bool, err error) {
480-
route, err := te.Client().Routes(defaults.ImageRegistryOperatorNamespace).Get(
481-
context.Background(), routeName, metav1.GetOptions{},
482-
)
483-
if err != nil {
484-
t.Logf("unable to get route: %s", err)
485-
return false, nil
486-
}
487-
if route.Spec.TLS == nil {
488-
t.Fatal("route.Spec.TLS is nil, want a configuration")
489-
}
490-
if route.Spec.TLS.Certificate != string(cert) {
491-
t.Errorf("route tls certificate: got %q, want %q", route.Spec.TLS.Certificate, string(cert))
492-
}
493-
if route.Spec.TLS.Key != string(key) {
494-
t.Errorf("route tls key: got %q, want %q", route.Spec.TLS.Key, string(key))
495-
}
496-
return true, nil
497-
})
479+
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 1*time.Minute, false,
480+
func(ctx context.Context) (done bool, err error) {
481+
route, err := te.Client().Routes(defaults.ImageRegistryOperatorNamespace).Get(
482+
ctx, routeName, metav1.GetOptions{},
483+
)
484+
if err != nil {
485+
t.Logf("unable to get route: %s", err)
486+
return false, nil
487+
}
488+
if route.Spec.TLS == nil {
489+
t.Fatal("route.Spec.TLS is nil, want a configuration")
490+
}
491+
if route.Spec.TLS.Certificate != string(cert) {
492+
t.Errorf("route tls certificate: got %q, want %q", route.Spec.TLS.Certificate, string(cert))
493+
}
494+
if route.Spec.TLS.Key != string(key) {
495+
t.Errorf("route tls key: got %q, want %q", route.Spec.TLS.Key, string(key))
496+
}
497+
return true, nil
498+
},
499+
)
498500
if err != nil {
499501
t.Fatalf("failed to observe the route: %s", err)
500502
}
@@ -524,26 +526,28 @@ func TestVersionReporting(t *testing.T) {
524526

525527
framework.WaitUntilDeploymentIsRolledOut(te, framework.OperatorDeploymentNamespace, framework.OperatorDeploymentName)
526528

527-
err := wait.Poll(5*time.Second, 1*time.Minute, func() (bool, error) {
528-
clusterOperatorStatus, err := te.Client().ClusterOperators().Get(
529-
context.Background(), defaults.ImageRegistryClusterOperatorResourceName, metav1.GetOptions{},
530-
)
531-
if err != nil {
532-
t.Logf("Could not retrieve cluster operator status: %v", err)
533-
return false, nil
534-
}
535-
if len(clusterOperatorStatus.Status.Versions) == 0 {
536-
// We should always have *some* version information in the clusteroperator once we are available,
537-
// so we do not retry in this scenario.
538-
t.Fatalf("Cluster operator status has no version information: %v", clusterOperatorStatus)
539-
return true, err
540-
}
541-
if clusterOperatorStatus.Status.Versions[0].Name != "operator" || clusterOperatorStatus.Status.Versions[0].Version != "test-v2" {
542-
t.Logf("waiting for new version to be reported, saw: %v", clusterOperatorStatus.Status.Versions[0])
543-
return false, nil
544-
}
545-
return true, nil
546-
})
529+
err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 1*time.Minute, false,
530+
func(ctx context.Context) (bool, error) {
531+
clusterOperatorStatus, err := te.Client().ClusterOperators().Get(
532+
ctx, defaults.ImageRegistryClusterOperatorResourceName, metav1.GetOptions{},
533+
)
534+
if err != nil {
535+
t.Logf("Could not retrieve cluster operator status: %v", err)
536+
return false, nil
537+
}
538+
if len(clusterOperatorStatus.Status.Versions) == 0 {
539+
// We should always have *some* version information in the clusteroperator once we are available,
540+
// so we do not retry in this scenario.
541+
t.Fatalf("Cluster operator status has no version information: %v", clusterOperatorStatus)
542+
return true, err
543+
}
544+
if clusterOperatorStatus.Status.Versions[0].Name != "operator" || clusterOperatorStatus.Status.Versions[0].Version != "test-v2" {
545+
t.Logf("waiting for new version to be reported, saw: %v", clusterOperatorStatus.Status.Versions[0])
546+
return false, nil
547+
}
548+
return true, nil
549+
},
550+
)
547551
if err != nil {
548552
t.Fatalf("failed to observe updated version reported in clusteroperator status: %v", err)
549553
}

test/e2e/emptydir_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestBasicEmptyDir(t *testing.T) {
4141
t.Errorf("error: the deployment doesn't have available replicas")
4242
}
4343

44-
logs, err := framework.GetOperatorLogs(te.Client())
44+
logs, err := framework.GetOperatorLogs(context.Background(), te.Client())
4545
if err != nil {
4646
t.Fatal(err)
4747
}

0 commit comments

Comments
 (0)