Skip to content

Commit 6d789dd

Browse files
authored
refactor: remove Timeout option from WaitFor* methods (#291)
1 parent 7960d70 commit 6d789dd

21 files changed

+193
-183
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
1515

1616
### Removed
1717
- **Breaking**, Managed Database: connection related methods in favor of session
18+
- **Breaking**: remove `Timeout` option from `WaitFor*` methods. Use `context.WithTimeout` to define a timeout for these functions.
1819

1920
## [6.12.0]
2021

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ fmt.Println(fmt.Sprintf("Server %s with UUID %s created", serverDetails.Title, s
155155
err = svc.WaitForServerState(context.Background(), &request.WaitForServerStateRequest{
156156
UUID: serverDetails.UUID,
157157
DesiredState: upcloud.ServerStateStarted,
158-
Timeout: time.Minute * 5,
159158
})
160159

161160
if err != nil {

examples/upcloud-cli/main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"flag"
77
"fmt"
88
"os"
9-
"time"
109

1110
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
1211
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/client"
@@ -121,7 +120,6 @@ func createServer(s *service.Service) error {
121120
details, err = s.WaitForServerState(ctx, &request.WaitForServerStateRequest{
122121
UUID: details.UUID,
123122
DesiredState: upcloud.ServerStateStarted,
124-
Timeout: 1 * time.Minute,
125123
})
126124
if err != nil {
127125
fmt.Fprintf(os.Stderr, "Unable to wait for server: %#v", err)
@@ -160,7 +158,6 @@ func deleteServers(s *service.Service) error {
160158
_, err = s.WaitForServerState(ctx, &request.WaitForServerStateRequest{
161159
UUID: server.UUID,
162160
DesiredState: upcloud.ServerStateStopped,
163-
Timeout: 1 * time.Minute,
164161
})
165162
if err != nil {
166163
fmt.Fprintf(os.Stderr, "Failed to wait for server to reach desired state: %#v", err)

upcloud/request/kubernetes.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package request
33
import (
44
"encoding/json"
55
"fmt"
6-
"time"
76

87
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
98
)
@@ -93,7 +92,6 @@ func (r *DeleteKubernetesClusterRequest) RequestURL() string {
9392
// to enter a desired state
9493
type WaitForKubernetesClusterStateRequest struct {
9594
DesiredState upcloud.KubernetesClusterState `json:"-"`
96-
Timeout time.Duration `json:"-"`
9795
UUID string `json:"-"`
9896
}
9997

@@ -105,7 +103,6 @@ func (r *WaitForKubernetesClusterStateRequest) RequestURL() string {
105103
// to enter a desired state
106104
type WaitForKubernetesNodeGroupStateRequest struct {
107105
DesiredState upcloud.KubernetesNodeGroupState `json:"-"`
108-
Timeout time.Duration `json:"-"`
109106
ClusterUUID string `json:"-"`
110107
Name string `json:"-"`
111108
}

upcloud/request/managed_database.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ func (r *GetManagedDatabaseVersionsRequest) RequestURL() string {
477477
type WaitForManagedDatabaseStateRequest struct {
478478
UUID string
479479
DesiredState upcloud.ManagedDatabaseState
480-
Timeout time.Duration
481480
}
482481

483482
// StartManagedDatabaseRequest represents a request to start an existing managed database instance

upcloud/request/managed_object_storage.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package request
22

33
import (
44
"fmt"
5-
"time"
65

76
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
87
)
@@ -291,7 +290,6 @@ func (r *DeleteManagedObjectStorageUserAccessKeyRequest) RequestURL() string {
291290
// to enter a desired state
292291
type WaitForManagedObjectStorageOperationalStateRequest struct {
293292
DesiredState upcloud.ManagedObjectStorageOperationalState `json:"-"`
294-
Timeout time.Duration `json:"-"`
295293
UUID string `json:"-"`
296294
}
297295

@@ -304,7 +302,6 @@ func (r *WaitForManagedObjectStorageOperationalStateRequest) RequestURL() string
304302
// to enter a desired state
305303
type WaitForManagedObjectStorageUserOperationalStateRequest struct {
306304
DesiredState upcloud.ManagedObjectStorageUserOperationalState `json:"-"`
307-
Timeout time.Duration `json:"-"`
308305
ServiceUUID string `json:"-"`
309306
Username string `json:"-"`
310307
}
@@ -318,7 +315,6 @@ func (r *WaitForManagedObjectStorageUserOperationalStateRequest) RequestURL() st
318315
// to be deleted
319316
type WaitForManagedObjectStorageDeletionRequest struct {
320317
DesiredState upcloud.ManagedObjectStorageOperationalState `json:"-"`
321-
Timeout time.Duration `json:"-"`
322318
UUID string `json:"-"`
323319
}
324320

upcloud/request/server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ type WaitForServerStateRequest struct {
229229
UUID string
230230
DesiredState string
231231
UndesiredState string
232-
Timeout time.Duration
233232
}
234233

235234
// StartServerRequest represents a request to start a server

upcloud/request/storage.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package request
33
import (
44
"encoding/json"
55
"fmt"
6-
"time"
76

87
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
98
)
@@ -238,7 +237,6 @@ func (r TemplatizeStorageRequest) MarshalJSON() ([]byte, error) {
238237
type WaitForStorageStateRequest struct {
239238
UUID string
240239
DesiredState string
241-
Timeout time.Duration
242240
}
243241

244242
// LoadCDROMRequest represents a request to load a storage as a CD-ROM in the CD-ROM device of a server
@@ -355,7 +353,6 @@ func (r *GetStorageImportDetailsRequest) RequestURL() string {
355353
// for storage import to complete.
356354
type WaitForStorageImportCompletionRequest struct {
357355
StorageUUID string
358-
Timeout time.Duration
359356
}
360357

361358
// ResizeStorageFilesystemRequest represents a request to resize storage filesystem

upcloud/service/kubernetes.go

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
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
7978
func (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
113103
func (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.
144127
func (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 {

upcloud/service/kubernetes_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"net/http"
88
"testing"
9-
"time"
109

1110
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
1211
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/client"
@@ -524,7 +523,6 @@ func TestWaitForKubernetesClusterState(t *testing.T) {
524523
_, err := svc.WaitForKubernetesClusterState(context.Background(), &request.WaitForKubernetesClusterStateRequest{
525524
UUID: "_UUID_",
526525
DesiredState: upcloud.KubernetesClusterStateRunning,
527-
Timeout: time.Second * 20,
528526
})
529527
assert.NoError(t, err)
530528
assert.Equal(t, 3, requestsMade)
@@ -584,7 +582,6 @@ func TestWaitForKubernetesNodeGroupState(t *testing.T) {
584582
_, err := svc.WaitForKubernetesNodeGroupState(context.Background(), &request.WaitForKubernetesNodeGroupStateRequest{
585583
ClusterUUID: "_UUID_",
586584
DesiredState: upcloud.KubernetesNodeGroupStateRunning,
587-
Timeout: time.Second * 20,
588585
Name: "_NAME_",
589586
})
590587
assert.NoError(t, err)

0 commit comments

Comments
 (0)