Skip to content

Commit 7b3f213

Browse files
committed
Adjust to new wait utils
1 parent f0b2b03 commit 7b3f213

File tree

19 files changed

+75
-38
lines changed

19 files changed

+75
-38
lines changed

cmd/clusterctl/client/client_test.go

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

1919
import (
20+
"context"
2021
"fmt"
2122
"testing"
2223
"time"
@@ -209,7 +210,7 @@ func newFakeCluster(kubeconfig cluster.Kubeconfig, configClient config.Client) *
209210
}
210211

211212
fake.fakeProxy = test.NewFakeProxy()
212-
pollImmediateWaiter := func(interval, timeout time.Duration, condition wait.ConditionFunc) error {
213+
pollImmediateWaiter := func(ctx context.Context, interval, timeout time.Duration, condition wait.ConditionWithContextFunc) error {
213214
return nil
214215
}
215216

cmd/clusterctl/client/cluster/cert_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ func (cm *certManagerClient) deleteObj(obj unstructured.Unstructured) error {
526526
// cert-manager API group.
527527
// If retry is true, the createObj call will be retried if it fails. Otherwise, the
528528
// 'create' operations will only be attempted once.
529-
func (cm *certManagerClient) waitForAPIReady(_ context.Context, retry bool) error {
529+
func (cm *certManagerClient) waitForAPIReady(ctx context.Context, retry bool) error {
530530
log := logf.Log
531531
// Waits for the cert-manager to be available.
532532
if retry {
@@ -544,7 +544,7 @@ func (cm *certManagerClient) waitForAPIReady(_ context.Context, retry bool) erro
544544
// Create the Kubernetes object.
545545
// This is wrapped with a retry as the cert-manager API may not be available
546546
// yet, so we need to keep retrying until it is.
547-
if err := cm.pollImmediateWaiter(waitCertManagerInterval, cm.getWaitTimeout(), func() (bool, error) {
547+
if err := cm.pollImmediateWaiter(ctx, waitCertManagerInterval, cm.getWaitTimeout(), func(ctx context.Context) (bool, error) {
548548
if err := cm.createObj(o); err != nil {
549549
// If retrying is disabled, return the error here.
550550
if !retry {

cmd/clusterctl/client/cluster/cert_manager_test.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 cluster
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"testing"
2223
"time"
@@ -165,7 +166,7 @@ func Test_getManifestObjs(t *testing.T) {
165166
}
166167

167168
func Test_GetTimeout(t *testing.T) {
168-
pollImmediateWaiter := func(interval, timeout time.Duration, condition wait.ConditionFunc) error {
169+
pollImmediateWaiter := func(ctx context.Context, interval, timeout time.Duration, condition wait.ConditionWithContextFunc) error {
169170
return nil
170171
}
171172

@@ -421,7 +422,7 @@ func Test_shouldUpgrade(t *testing.T) {
421422
g := NewWithT(t)
422423
proxy := test.NewFakeProxy()
423424
fakeConfigClient := newFakeConfig().WithCertManager("", tt.configVersion, "")
424-
pollImmediateWaiter := func(interval, timeout time.Duration, condition wait.ConditionFunc) error {
425+
pollImmediateWaiter := func(ctx context.Context, interval, timeout time.Duration, condition wait.ConditionWithContextFunc) error {
425426
return nil
426427
}
427428
cm := newCertManagerClient(fakeConfigClient, nil, proxy, pollImmediateWaiter)
@@ -706,7 +707,7 @@ func Test_certManagerClient_PlanUpgrade(t *testing.T) {
706707

707708
proxy := test.NewFakeProxy().WithObjs(tt.objs...)
708709
fakeConfigClient := newFakeConfig()
709-
pollImmediateWaiter := func(interval, timeout time.Duration, condition wait.ConditionFunc) error {
710+
pollImmediateWaiter := func(ctx context.Context, interval, timeout time.Duration, condition wait.ConditionWithContextFunc) error {
710711
return nil
711712
}
712713
cm := newCertManagerClient(fakeConfigClient, nil, proxy, pollImmediateWaiter)

cmd/clusterctl/client/cluster/client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type Client interface {
8989
}
9090

9191
// PollImmediateWaiter tries a condition func until it returns true, an error, or the timeout is reached.
92-
type PollImmediateWaiter func(interval, timeout time.Duration, condition wait.ConditionFunc) error
92+
type PollImmediateWaiter func(ctx context.Context, interval, timeout time.Duration, condition wait.ConditionWithContextFunc) error
9393

9494
// clusterClient implements Client.
9595
type clusterClient struct {
@@ -214,7 +214,9 @@ func newClusterClient(kubeconfig Kubeconfig, configClient config.Client, options
214214

215215
// if there is an injected PollImmediateWaiter, use it, otherwise use the default one
216216
if client.pollImmediateWaiter == nil {
217-
client.pollImmediateWaiter = wait.PollImmediate
217+
client.pollImmediateWaiter = func(ctx context.Context, interval, timeout time.Duration, condition wait.ConditionWithContextFunc) error {
218+
return wait.PollUntilContextTimeout(ctx, interval, timeout, true, condition)
219+
}
218220
}
219221

220222
return client

cmd/clusterctl/client/cluster/installer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func waitManagerDeploymentsReady(opts InstallOptions, installQueue []repository.
141141
for _, obj := range components.Objs() {
142142
if util.IsDeploymentWithManager(obj) {
143143
if err := waitDeploymentReady(obj, opts.WaitProviderTimeout, proxy); err != nil {
144-
return err
144+
return errors.Wrapf(err, "deployment %q is not ready after %s", obj.GetName(), opts.WaitProviderTimeout)
145145
}
146146
}
147147
}
@@ -150,7 +150,7 @@ func waitManagerDeploymentsReady(opts InstallOptions, installQueue []repository.
150150
}
151151

152152
func waitDeploymentReady(deployment unstructured.Unstructured, timeout time.Duration, proxy Proxy) error {
153-
return wait.Poll(100*time.Millisecond, timeout, func() (bool, error) {
153+
return wait.PollUntilContextTimeout(context.TODO(), 100*time.Millisecond, timeout, false, func(ctx context.Context) (bool, error) {
154154
c, err := proxy.NewClient()
155155
if err != nil {
156156
return false, err
@@ -160,7 +160,7 @@ func waitDeploymentReady(deployment unstructured.Unstructured, timeout time.Dura
160160
Name: deployment.GetName(),
161161
}
162162
dep := &appsv1.Deployment{}
163-
if err := c.Get(context.TODO(), key, dep); err != nil {
163+
if err := c.Get(ctx, key, dep); err != nil {
164164
return false, err
165165
}
166166
for _, c := range dep.Status.Conditions {

cmd/clusterctl/client/cluster/internal/dryrun/client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3131
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3232

33+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3334
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/scheme"
3435
)
3536

@@ -114,7 +115,7 @@ type ChangeSummary struct {
114115
// when the objects are not found in the internal object tracker. Typically the apiReader passed would be a reader client
115116
// to a real Kubernetes Cluster.
116117
func NewClient(apiReader client.Reader, objs []client.Object) *Client {
117-
fakeClient := fake.NewClientBuilder().WithObjects(objs...).WithScheme(localScheme).Build()
118+
fakeClient := fake.NewClientBuilder().WithObjects(objs...).WithStatusSubresource(&clusterv1.ClusterClass{}, &clusterv1.Cluster{}).WithScheme(localScheme).Build()
118119
return &Client{
119120
fakeClient: fakeClient,
120121
apiReader: apiReader,
@@ -308,6 +309,16 @@ func (c *Client) SubResource(subResource string) client.SubResourceClient {
308309
return c.fakeClient.SubResource(subResource)
309310
}
310311

312+
// GroupVersionKindFor returns the GroupVersionKind for the given object.
313+
func (c *Client) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
314+
return c.fakeClient.GroupVersionKindFor(obj)
315+
}
316+
317+
// IsObjectNamespaced returns true if the GroupVersionKind of the object is namespaced.
318+
func (c *Client) IsObjectNamespaced(obj runtime.Object) (bool, error) {
319+
return c.fakeClient.IsObjectNamespaced(obj)
320+
}
321+
311322
// Changes generates a summary of all the changes observed from the creation of the dry run client
312323
// to when this function is called.
313324
func (c *Client) Changes(ctx context.Context) (*ChangeSummary, error) {

cmd/clusterctl/client/cluster/inventory.go

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

1919
import (
20+
"context"
2021
"fmt"
2122
"time"
2223

@@ -197,7 +198,7 @@ func (p *inventoryClient) EnsureCustomResourceDefinitions() error {
197198
// If the object is a CRDs, waits for it being Established.
198199
if apiextensionsv1.SchemeGroupVersion.WithKind("CustomResourceDefinition").GroupKind() == o.GroupVersionKind().GroupKind() {
199200
crdKey := client.ObjectKeyFromObject(&o)
200-
if err := p.pollImmediateWaiter(waitInventoryCRDInterval, waitInventoryCRDTimeout, func() (bool, error) {
201+
if err := p.pollImmediateWaiter(ctx, waitInventoryCRDInterval, waitInventoryCRDTimeout, func(ctx context.Context) (bool, error) {
201202
c, err := p.proxy.NewClient()
202203
if err != nil {
203204
return false, err

cmd/clusterctl/client/cluster/inventory_test.go

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

1919
import (
20+
"context"
2021
"testing"
2122
"time"
2223

@@ -31,7 +32,7 @@ import (
3132
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
3233
)
3334

34-
func fakePollImmediateWaiter(_, _ time.Duration, _ wait.ConditionFunc) error {
35+
func fakePollImmediateWaiter(_ context.Context, _, _ time.Duration, _ wait.ConditionWithContextFunc) error {
3536
return nil
3637
}
3738

cmd/clusterctl/client/cluster/topology.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,11 @@ func (t *topologyClient) defaultAndValidateObjs(ctx context.Context, objs []*uns
567567
}
568568
}
569569
if oldObject != nil {
570-
if err := validator.ValidateUpdate(ctx, oldObject, object); err != nil {
570+
if _, err := validator.ValidateUpdate(ctx, oldObject, object); err != nil {
571571
return errors.Wrapf(err, "failed validation of %s %s/%s", obj.GroupVersionKind().String(), obj.GetNamespace(), obj.GetName())
572572
}
573573
} else {
574-
if err := validator.ValidateCreate(ctx, object); err != nil {
574+
if _, err := validator.ValidateCreate(ctx, object); err != nil {
575575
return errors.Wrapf(err, "failed validation of %s %s/%s", obj.GroupVersionKind().String(), obj.GetNamespace(), obj.GetName())
576576
}
577577
}

cmd/clusterctl/client/init.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ type InitOptions struct {
9292
func (c *clusterctlClient) Init(options InitOptions) ([]Components, error) {
9393
log := logf.Log
9494

95+
// Default WaitProviderTimeout as we cannot rely on defaulting in the CLI
96+
// when clusterctl is used as a library.
97+
if options.WaitProviderTimeout.Nanoseconds() == 0 {
98+
options.WaitProviderTimeout = time.Duration(5*60) * time.Second
99+
}
100+
95101
// gets access to the management cluster
96102
clusterClient, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig})
97103
if err != nil {

0 commit comments

Comments
 (0)