Skip to content

feat(k8s): add WaitForPool & WaitForClusterPools helper methods #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions api/k8s/v1beta4/k8s_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

const (
waitForClusterDefaultTimeout = time.Minute * 15
waitForPoolDefaultTimeout = time.Minute * 15
)

// WaitForClusterRequest is used by WaitForCluster method.
Expand Down Expand Up @@ -55,3 +56,79 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) {
}
return cluster.(*Cluster), nil
}

// WaitForClusterPoolsRequest is used by WaitForClusterPools method.
type WaitForClusterPoolsRequest struct {
ClusterID string
Region scw.Region
Timeout *time.Duration
}

// WaitForClusterPools waits for the pools of a cluster to be ready
func (s *API) WaitForClusterPools(req *WaitForClusterPoolsRequest) error {
timeout := waitForPoolDefaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}

pools, err := s.ListPools(&ListPoolsRequest{
ClusterID: req.ClusterID,
Region: req.Region,
})
if err != nil {
return err
}

for _, pool := range pools.Pools {
err = s.WaitForPool(&WaitForPoolRequest{
PoolID: pool.ID,
Timeout: &timeout,
})

if err != nil {
return err
}
}

return nil
}

// WaitForPoolRequest is used by WaitForPool method.
type WaitForPoolRequest struct {
PoolID string
Region scw.Region
Timeout *time.Duration
}

// WaitForPool waits for a pool to be ready
func (s *API) WaitForPool(req *WaitForPoolRequest) error {
terminalStatus := map[PoolStatus]struct{}{
PoolStatusReady: {},
PoolStatusWarning: {},
}

timeout := waitForPoolDefaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}

_, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
res, err := s.GetPool(&GetPoolRequest{
PoolID: req.PoolID,
Region: req.Region,
})

if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[res.Status]

return nil, isTerminal, nil
},
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second),
})

return err
}