Skip to content

Commit 7671db8

Browse files
committed
Separate WaitForStatus as its own function in the CassManager to allow reusability
1 parent 8a1726e commit 7671db8

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

pkg/cassdcutil/manage.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import (
1111
"sigs.k8s.io/controller-runtime/pkg/client"
1212
)
1313

14+
const (
15+
defaultPollInterval = 1 * time.Second
16+
defaultTimeout = 10 * time.Minute
17+
)
18+
1419
type CassManager struct {
1520
client client.Client
1621
}
@@ -39,28 +44,22 @@ func (c *CassManager) ModifyStoppedState(ctx context.Context, name, namespace st
3944

4045
if wait {
4146
if stop {
42-
if err := waitutil.PollUntilContextTimeout(ctx, 10*time.Second, 10*time.Minute, true, func(context.Context) (bool, error) {
43-
return c.RefreshStatus(ctx, cassdc, cassdcapi.DatacenterStopped, corev1.ConditionTrue)
44-
}); err != nil {
47+
if err := c.WaitForStatus(ctx, cassdc, cassdcapi.DatacenterStopped, corev1.ConditionTrue, defaultPollInterval, defaultTimeout); err != nil {
4548
return err
4649
}
4750

48-
// And wait for it to finish..
49-
return waitutil.PollUntilContextTimeout(ctx, 10*time.Second, 10*time.Minute, true, func(context.Context) (bool, error) {
50-
return c.RefreshStatus(ctx, cassdc, cassdcapi.DatacenterReady, corev1.ConditionFalse)
51-
})
52-
}
51+
if err := c.WaitForStatus(ctx, cassdc, cassdcapi.DatacenterReady, corev1.ConditionFalse, defaultPollInterval, defaultTimeout); err != nil {
52+
return err
53+
}
54+
} else {
55+
if err := c.WaitForStatus(ctx, cassdc, cassdcapi.DatacenterStopped, corev1.ConditionFalse, defaultPollInterval, defaultTimeout); err != nil {
56+
return err
57+
}
5358

54-
if err := waitutil.PollUntilContextTimeout(ctx, 10*time.Second, 10*time.Minute, true, func(context.Context) (bool, error) {
55-
return c.RefreshStatus(ctx, cassdc, cassdcapi.DatacenterStopped, corev1.ConditionFalse)
56-
}); err != nil {
57-
return err
59+
if err := c.WaitForStatus(ctx, cassdc, cassdcapi.DatacenterReady, corev1.ConditionTrue, defaultPollInterval, defaultTimeout); err != nil {
60+
return err
61+
}
5862
}
59-
60-
// And wait for it to finish..
61-
return waitutil.PollUntilContextTimeout(ctx, 10*time.Second, 10*time.Minute, true, func(context.Context) (bool, error) {
62-
return c.RefreshStatus(ctx, cassdc, cassdcapi.DatacenterReady, corev1.ConditionTrue)
63-
})
6463
}
6564

6665
return nil
@@ -95,3 +94,20 @@ func (c *CassManager) RestartDc(ctx context.Context, name, namespace, rack strin
9594
}
9695
return nil
9796
}
97+
98+
func (c *CassManager) WaitForStatus(ctx context.Context, cassdc *cassdcapi.CassandraDatacenter, status cassdcapi.DatacenterConditionType, wanted corev1.ConditionStatus, interval, timeout time.Duration) error {
99+
if interval == 0 {
100+
interval = defaultPollInterval
101+
}
102+
103+
if timeout == 0 {
104+
timeout = defaultTimeout
105+
}
106+
107+
if err := waitutil.PollUntilContextTimeout(ctx, interval, timeout, true, func(context.Context) (bool, error) {
108+
return c.RefreshStatus(ctx, cassdc, status, wanted)
109+
}); err != nil {
110+
return err
111+
}
112+
return nil
113+
}

0 commit comments

Comments
 (0)