|
| 1 | +package k8s |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/scaleway/scaleway-sdk-go/internal/async" |
| 7 | + "github.com/scaleway/scaleway-sdk-go/internal/errors" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 9 | +) |
| 10 | + |
| 11 | +// WaitForClusterRequest is used by WaitForCluster method. |
| 12 | +type WaitForClusterRequest struct { |
| 13 | + ClusterID string |
| 14 | + Region scw.Region |
| 15 | + Status ClusterStatus |
| 16 | + Timeout time.Duration |
| 17 | +} |
| 18 | + |
| 19 | +// WaitForCluster waits for the cluster to be in a "terminal state" before returning. |
| 20 | +func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) { |
| 21 | + terminalStatus := map[ClusterStatus]struct{}{ |
| 22 | + ClusterStatusReady: {}, |
| 23 | + ClusterStatusError: {}, |
| 24 | + ClusterStatusWarning: {}, |
| 25 | + ClusterStatusLocked: {}, |
| 26 | + ClusterStatusDeleted: {}, |
| 27 | + } |
| 28 | + |
| 29 | + cluster, err := async.WaitSync(&async.WaitSyncConfig{ |
| 30 | + Get: func() (interface{}, error, bool) { |
| 31 | + cluster, err := s.GetCluster(&GetClusterRequest{ |
| 32 | + ClusterID: req.ClusterID, |
| 33 | + Region: req.Region, |
| 34 | + }) |
| 35 | + if err != nil { |
| 36 | + return nil, err, false |
| 37 | + } |
| 38 | + |
| 39 | + _, isTerminal := terminalStatus[cluster.Status] |
| 40 | + return cluster, nil, isTerminal |
| 41 | + }, |
| 42 | + Timeout: req.Timeout, |
| 43 | + IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second), |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + return nil, errors.Wrap(err, "waiting for cluster failed") |
| 47 | + } |
| 48 | + return cluster.(*Cluster), nil |
| 49 | +} |
0 commit comments