66 "fmt"
77 "log"
88 "net/http"
9- "time"
109
1110 "github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
1211 "github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
@@ -77,20 +76,14 @@ func (s *Service) DeleteKubernetesCluster(ctx context.Context, r *request.Delete
7776// specified state. If the state changes favorably, cluster details are returned. The method will give up
7877// after the specified timeout
7978func (s * Service ) WaitForKubernetesClusterState (ctx context.Context , r * request.WaitForKubernetesClusterStateRequest ) (* upcloud.KubernetesCluster , error ) {
80- attempts := 0
81- sleepDuration := time .Second * 5
82-
83- for {
84- attempts ++
85- time .Sleep (sleepDuration )
86-
87- details , err := s .GetKubernetesCluster (ctx , & request.GetKubernetesClusterRequest {
79+ return retry (ctx , func (i int , c context.Context ) (* upcloud.KubernetesCluster , error ) {
80+ details , err := s .GetKubernetesCluster (c , & request.GetKubernetesClusterRequest {
8881 UUID : r .UUID ,
8982 })
9083 if err != nil {
9184 // Ignore first two 404 responses to avoid errors caused by possible false NOT_FOUND responses right after cluster has been created.
9285 var ucErr * upcloud.Problem
93- if errors .As (err , & ucErr ) && ucErr .Status == http .StatusNotFound && attempts < 3 {
86+ if errors .As (err , & ucErr ) && ucErr .Status == http .StatusNotFound && i < 3 {
9487 log .Printf ("ERROR: %+v" , err )
9588 } else {
9689 return nil , err
@@ -100,57 +93,44 @@ func (s *Service) WaitForKubernetesClusterState(ctx context.Context, r *request.
10093 if details .State == r .DesiredState {
10194 return details , nil
10295 }
103-
104- if time .Duration (attempts )* sleepDuration >= r .Timeout {
105- return nil , fmt .Errorf ("timeout reached while waiting for Kubernetes cluster to enter state \" %s\" " , r .DesiredState )
106- }
107- }
96+ return nil , nil
97+ }, nil )
10898}
10999
110100// WaitForKubernetesNodeGroupState blocks execution until the specified Kubernetes node group has entered the
111101// specified state. If the state changes favorably, node group is returned. The method will give up
112102// after the specified timeout
113103func (s * Service ) WaitForKubernetesNodeGroupState (ctx context.Context , r * request.WaitForKubernetesNodeGroupStateRequest ) (* upcloud.KubernetesNodeGroup , error ) {
114- attempts := 0
115- sleepDuration := time .Second * 5
116-
117- for {
118- attempts ++
119- time .Sleep (sleepDuration )
120-
121- ng , err := s .GetKubernetesNodeGroup (ctx , & request.GetKubernetesNodeGroupRequest {
104+ return retry (ctx , func (i int , c context.Context ) (* upcloud.KubernetesNodeGroup , error ) {
105+ ng , err := s .GetKubernetesNodeGroup (c , & request.GetKubernetesNodeGroupRequest {
122106 ClusterUUID : r .ClusterUUID ,
123107 Name : r .Name ,
124108 })
125109 if err != nil {
126- // Ignore first two 404 responses to avoid errors caused by possible false NOT_FOUND responses right after cluster or node group has been created.
110+ // Ignore first two 404 responses to avoid errors caused by possible false NOT_FOUND responses right after cluster has been created.
127111 var ucErr * upcloud.Problem
128- if ! (errors .As (err , & ucErr ) && ucErr .Status == http .StatusNotFound ) || attempts >= 3 {
112+ if errors .As (err , & ucErr ) && ucErr .Status == http .StatusNotFound && i < 3 {
113+ log .Printf ("ERROR: %+v" , err )
114+ } else {
129115 return nil , err
130116 }
131117 }
132118
133119 if ng .State == r .DesiredState {
134120 return & ng .KubernetesNodeGroup , nil
135121 }
136-
137- if time .Duration (attempts )* sleepDuration >= r .Timeout {
138- return nil , fmt .Errorf ("timeout reached while waiting for Kubernetes node group to enter state \" %s\" " , r .DesiredState )
139- }
140- }
122+ return nil , nil
123+ }, nil )
141124}
142125
143126// GetKubernetesKubeconfig retrieves kubeconfig of a Kubernetes cluster.
144127func (s * Service ) GetKubernetesKubeconfig (ctx context.Context , r * request.GetKubernetesKubeconfigRequest ) (string , error ) {
145- // TODO: should timeout be part of GetKubernetesKubeconfigRequest ?
146- const timeout time.Duration = 10 * time .Minute
147128 data := struct {
148129 Kubeconfig string `json:"kubeconfig"`
149130 }{}
150131
151132 _ , err := s .WaitForKubernetesClusterState (ctx , & request.WaitForKubernetesClusterStateRequest {
152133 DesiredState : upcloud .KubernetesClusterStateRunning ,
153- Timeout : timeout ,
154134 UUID : r .UUID ,
155135 })
156136 if err != nil {
0 commit comments