Skip to content

Commit 8316521

Browse files
Sh4d1jerome-quere
authored andcommitted
feat: add k8s WaitForCluster method (#202)
1 parent 7c99a5a commit 8316521

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

api/k8s/v1beta3/k8s_helpers.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)