Skip to content

Commit

Permalink
feat(k8s): add WaitForPoolNodes helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
debovema committed Feb 13, 2020
1 parent b653c34 commit 4932a2d
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions api/k8s/v1beta4/k8s_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,91 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) {
}
return cluster.(*Cluster), nil
}

func (s *API) getNodesWhenReady(req *ListNodesRequest, expectedCount uint32) (bool, error) {
res, err := s.ListNodes(req, scw.WithAllPages())
if err != nil {
return false, err
}

// first check that we have the right nodes count
if res.TotalCount != expectedCount {
return false, nil
}

// then check that each node has Ready status
for _, node := range res.Nodes {
if node.Status != NodeStatusReady {
return false, nil
}
}

return true, 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
Timeout time.Duration
}

// WaitForPoolNodesReady waits for the nodes of a pool to be ready
func (s *API) WaitForPoolNodesReady(req *WaitForPoolNodesReadyRequest) error {
pool, err := s.GetPool(&GetPoolRequest{
PoolID: req.PoolID,
})
if err != nil {
return err
}

r := &ListNodesRequest{
ClusterID: pool.ClusterID,
PoolID: &req.PoolID,
}

_, err = async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
ok, err := s.getNodesWhenReady(r, pool.Size)
if err != nil {
return nil, ok, err
}

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

return err
}

0 comments on commit 4932a2d

Please sign in to comment.