Skip to content

Commit

Permalink
feat(k8s): add WaitForPoolNodesReady & WaitForClusterNodesReady helpe…
Browse files Browse the repository at this point in the history
…r methods
  • Loading branch information
debovema committed Feb 18, 2020
1 parent 086aa05 commit fc9f601
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions api/k8s/v1beta4/k8s_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/scaleway/scaleway-sdk-go/scw"
)

const (
waitForNodesReadyDefaultTimeout = time.Minute * 15
)

// WaitForClusterRequest is used by WaitForCluster method.
type WaitForClusterRequest struct {
ClusterID string
Expand Down Expand Up @@ -47,3 +51,79 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) {
}
return cluster.(*Cluster), nil
}

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

// WaitForClusterNodesReady waits for the nodes of a cluster to be ready
func (s *API) WaitForClusterNodesReady(req *WaitForClusterNodesReadyRequest) error {
timeout := waitForNodesReadyDefaultTimeout
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.WaitForPoolNodesReady(&WaitForPoolNodesReadyRequest{
PoolID: pool.ID,
Timeout: &timeout,
})

if err != nil {
return err
}
}

return nil
}

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

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

timeout := waitForNodesReadyDefaultTimeout
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
}

0 comments on commit fc9f601

Please sign in to comment.