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 7, 2020
1 parent a9e20f4 commit 749fb92
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions api/k8s/v1beta4/k8s_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,70 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) {
}
return cluster.(*Cluster), nil
}

// ListNodesInPool list all the nodes in a pool
//
// This method allows to list all the existing nodes for a specific pool.
func (s *API) ListNodesInPool(pool *Pool) (*ListNodesResponse, error) {
req := &ListNodesRequest{
Region: pool.Region,
ClusterID: pool.ClusterID,
PoolID: &pool.ID,
}

return s.ListNodes(req)
}

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

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

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

return res, true, nil
}

// WaitForPoolNodesRequest is used by WaitForPoolNodes method.
type WaitForPoolNodesRequest struct {
Pool *Pool
Timeout time.Duration
}

// WaitForPoolNodes waits for the nodes of a pool to be ready
func (s *API) WaitForPoolNodes(req *WaitForPoolNodesRequest) (*ListNodesResponse, error) {
r := &ListNodesRequest{
Region: req.Pool.Region,
ClusterID: req.Pool.ClusterID,
PoolID: &req.Pool.ID,
}

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

return n, ok, nil
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second),
})
if err != nil {
return nil, err
}

return nodes.(*ListNodesResponse), nil
}

0 comments on commit 749fb92

Please sign in to comment.