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 13, 2020
1 parent 086aa05 commit 34b1b50
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions api/k8s/v1beta4/k8s_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,69 @@ 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 {
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: req.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 {
if req.Region == "" {
defaultRegion, _ := s.client.GetDefaultRegion()
req.Region = defaultRegion
}

poolRequest := &GetPoolRequest{
PoolID: req.PoolID,
Region: req.Region,
}

_, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
pool, err := s.GetPool(poolRequest)
if err != nil {
return nil, false, err
}

return nil, pool.Status == PoolStatusReady, nil
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second),
})

return err
}

0 comments on commit 34b1b50

Please sign in to comment.