From 63c7850c6f565bbafe9c84221f5085784594824a Mon Sep 17 00:00:00 2001 From: Mathieu Debove Date: Tue, 28 Jan 2020 15:09:17 +0100 Subject: [PATCH] feat(k8s): add WaitForPoolNodesReady & WaitForClusterNodesReady helper methods --- api/k8s/v1beta4/k8s_helpers.go | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/api/k8s/v1beta4/k8s_helpers.go b/api/k8s/v1beta4/k8s_helpers.go index 66ae31d8..c1a9a5d5 100644 --- a/api/k8s/v1beta4/k8s_helpers.go +++ b/api/k8s/v1beta4/k8s_helpers.go @@ -47,3 +47,64 @@ 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 { + 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 +}