Skip to content

Commit

Permalink
Separate WaitForStatus as its own function in the CassManager to allo…
Browse files Browse the repository at this point in the history
…w reusability
  • Loading branch information
burmanm committed Jan 10, 2025
1 parent 8a1726e commit 7671db8
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions pkg/cassdcutil/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
defaultPollInterval = 1 * time.Second
defaultTimeout = 10 * time.Minute
)

type CassManager struct {
client client.Client
}
Expand Down Expand Up @@ -39,28 +44,22 @@ func (c *CassManager) ModifyStoppedState(ctx context.Context, name, namespace st

if wait {
if stop {
if err := waitutil.PollUntilContextTimeout(ctx, 10*time.Second, 10*time.Minute, true, func(context.Context) (bool, error) {
return c.RefreshStatus(ctx, cassdc, cassdcapi.DatacenterStopped, corev1.ConditionTrue)
}); err != nil {
if err := c.WaitForStatus(ctx, cassdc, cassdcapi.DatacenterStopped, corev1.ConditionTrue, defaultPollInterval, defaultTimeout); err != nil {
return err
}

// And wait for it to finish..
return waitutil.PollUntilContextTimeout(ctx, 10*time.Second, 10*time.Minute, true, func(context.Context) (bool, error) {
return c.RefreshStatus(ctx, cassdc, cassdcapi.DatacenterReady, corev1.ConditionFalse)
})
}
if err := c.WaitForStatus(ctx, cassdc, cassdcapi.DatacenterReady, corev1.ConditionFalse, defaultPollInterval, defaultTimeout); err != nil {
return err
}
} else {
if err := c.WaitForStatus(ctx, cassdc, cassdcapi.DatacenterStopped, corev1.ConditionFalse, defaultPollInterval, defaultTimeout); err != nil {
return err
}

if err := waitutil.PollUntilContextTimeout(ctx, 10*time.Second, 10*time.Minute, true, func(context.Context) (bool, error) {
return c.RefreshStatus(ctx, cassdc, cassdcapi.DatacenterStopped, corev1.ConditionFalse)
}); err != nil {
return err
if err := c.WaitForStatus(ctx, cassdc, cassdcapi.DatacenterReady, corev1.ConditionTrue, defaultPollInterval, defaultTimeout); err != nil {
return err
}
}

// And wait for it to finish..
return waitutil.PollUntilContextTimeout(ctx, 10*time.Second, 10*time.Minute, true, func(context.Context) (bool, error) {
return c.RefreshStatus(ctx, cassdc, cassdcapi.DatacenterReady, corev1.ConditionTrue)
})
}

return nil
Expand Down Expand Up @@ -95,3 +94,20 @@ func (c *CassManager) RestartDc(ctx context.Context, name, namespace, rack strin
}
return nil
}

func (c *CassManager) WaitForStatus(ctx context.Context, cassdc *cassdcapi.CassandraDatacenter, status cassdcapi.DatacenterConditionType, wanted corev1.ConditionStatus, interval, timeout time.Duration) error {
if interval == 0 {
interval = defaultPollInterval
}

if timeout == 0 {
timeout = defaultTimeout
}

if err := waitutil.PollUntilContextTimeout(ctx, interval, timeout, true, func(context.Context) (bool, error) {
return c.RefreshStatus(ctx, cassdc, status, wanted)
}); err != nil {
return err
}
return nil
}

0 comments on commit 7671db8

Please sign in to comment.