Skip to content

feat: add k8s WaitForCluster method #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions api/k8s/v1beta3/k8s_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package k8s

import (
"time"

"github.com./scaleway/scaleway-sdk-go/internal/async"
"github.com./scaleway/scaleway-sdk-go/internal/errors"
"github.com./scaleway/scaleway-sdk-go/scw"
)

// WaitForClusterRequest is used by WaitForCluster method.
type WaitForClusterRequest struct {
ClusterID string
Region scw.Region
Status ClusterStatus
Timeout time.Duration
}

// WaitForCluster waits for the cluster to be in a "terminal state" before returning.
func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) {
terminalStatus := map[ClusterStatus]struct{}{
ClusterStatusReady: {},
ClusterStatusError: {},
ClusterStatusWarning: {},
ClusterStatusLocked: {},
ClusterStatusDeleted: {},
}

cluster, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, error, bool) {
cluster, err := s.GetCluster(&GetClusterRequest{
ClusterID: req.ClusterID,
Region: req.Region,
})
if err != nil {
return nil, err, false
}

_, isTerminal := terminalStatus[cluster.Status]
return cluster, nil, isTerminal
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for cluster failed")
}
return cluster.(*Cluster), nil
}