Skip to content

Commit 7c95cfc

Browse files
committed
refac(edge) use WaiterHelper for waiters
1 parent f2a2661 commit 7c95cfc

4 files changed

Lines changed: 139 additions & 131 deletions

File tree

services/edge/v1beta1api/wait/wait.go

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,20 @@ var (
3030

3131
// createOrUpdateInstanceWaitHandler contains the shared logic for waiting on instance creation or updates.
3232
func createOrUpdateInstanceWaitHandler(ctx context.Context, getInstance func(ctx context.Context) (*edge.Instance, error)) *wait.AsyncActionHandler[edge.Instance] {
33-
handler := wait.New(func() (waitFinished bool, response *edge.Instance, err error) {
34-
instance, err := getInstance(ctx)
35-
if err != nil {
36-
return false, nil, err
37-
}
38-
39-
if instance == nil {
40-
return false, nil, ErrInstanceNotFound
41-
}
42-
43-
status := instance.Status
44-
switch status {
45-
case INSTANCESTATUS_ACTIVE:
46-
return true, instance, nil
47-
case INSTANCESTATUS_ERROR:
48-
return true, instance, ErrInstanceCreationFailed
49-
case INSTANCESTATUS_RECONCILING:
50-
return false, nil, nil
51-
case INSTANCESTATUS_DELETING:
52-
return true, instance, ErrInstanceIsBeingDeleted
53-
default:
54-
return false, nil, nil
55-
}
56-
})
33+
waitConfig := wait.WaiterHelper[edge.Instance, string]{
34+
FetchInstance: func() (*edge.Instance, error) {
35+
return getInstance(ctx)
36+
},
37+
GetState: func(e *edge.Instance) (string, error) {
38+
if e == nil {
39+
return "", ErrInstanceNotFound
40+
}
41+
return e.Status, nil
42+
},
43+
ActiveState: []string{INSTANCESTATUS_ACTIVE},
44+
ErrorState: []string{INSTANCESTATUS_ERROR, INSTANCESTATUS_DELETING},
45+
}
46+
handler := wait.New(waitConfig.Wait())
5747
handler.SetTimeout(timeoutMinutes * time.Minute)
5848
return handler
5949
}
@@ -74,17 +64,18 @@ func CreateOrUpdateInstanceByNameWaitHandler(ctx context.Context, a edge.Default
7464

7565
// deleteInstanceWaitHandler contains the shared logic for waiting on instance deletion.
7666
func deleteInstanceWaitHandler(ctx context.Context, getInstance func(ctx context.Context) (*edge.Instance, error)) *wait.AsyncActionHandler[edge.Instance] {
77-
handler := wait.New(func() (waitFinished bool, response *edge.Instance, err error) {
78-
_, err = getInstance(ctx)
79-
if err == nil {
80-
return false, nil, nil
81-
}
82-
var oapiErr *oapierror.GenericOpenAPIError
83-
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
84-
return true, nil, nil
85-
}
86-
return false, nil, err
87-
})
67+
waitConfig := wait.WaiterHelper[edge.Instance, string]{
68+
FetchInstance: func() (*edge.Instance, error) {
69+
return getInstance(ctx)
70+
},
71+
GetState: func(e *edge.Instance) (string, error) {
72+
if e == nil {
73+
return "", ErrInstanceNotFound
74+
}
75+
return e.Status, nil
76+
},
77+
}
78+
handler := wait.New(waitConfig.Wait())
8879
handler.SetTimeout(timeoutMinutes * time.Minute)
8980
return handler
9081
}
@@ -105,21 +96,29 @@ func DeleteInstanceByNameWaitHandler(ctx context.Context, a edge.DefaultAPI, pro
10596

10697
// kubeconfigWaitHandlerHelper contains the shared logic for waiting for the instance to become ready before retrieving the kubeconfig.
10798
func kubeconfigWaitHandlerHelper(ctx context.Context, checkInstance func(ctx context.Context) error, getKubeconfig func(ctx context.Context) (*edge.Kubeconfig, error)) *wait.AsyncActionHandler[edge.Kubeconfig] {
108-
handler := wait.New(func() (waitFinished bool, response *edge.Kubeconfig, err error) {
109-
err = checkInstance(ctx)
110-
if err != nil {
111-
return false, nil, err
112-
}
113-
kubeconfig, err := getKubeconfig(ctx)
114-
var oapiErr *oapierror.GenericOpenAPIError
115-
if err != nil {
116-
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
117-
return false, nil, nil
99+
waitConfig := wait.WaiterHelper[edge.Kubeconfig, string]{
100+
FetchInstance: func() (*edge.Kubeconfig, error) {
101+
err := checkInstance(ctx)
102+
if err != nil {
103+
return nil, err
118104
}
119-
return false, nil, err
120-
}
121-
return true, kubeconfig, nil
122-
})
105+
config, err := getKubeconfig(ctx)
106+
if err != nil {
107+
if apiErr, ok := errors.AsType[*oapierror.GenericOpenAPIError](err); ok && apiErr.StatusCode == http.StatusNotFound {
108+
return nil, nil
109+
}
110+
}
111+
return config, err
112+
},
113+
GetState: func(e *edge.Kubeconfig) (string, error) {
114+
if e == nil {
115+
return "NOT_READY", nil
116+
}
117+
return "READY", nil
118+
},
119+
ActiveState: []string{"READY"},
120+
}
121+
handler := wait.New(waitConfig.Wait())
123122
handler.SetTimeout(timeoutMinutes * time.Minute)
124123
return handler
125124
}
@@ -158,21 +157,29 @@ func KubeconfigByInstanceNameWaitHandler(ctx context.Context, a edge.DefaultAPI,
158157

159158
// tokenWaitHandlerHelper contains the shared logic for waiting for the instance to become ready before retrieving the service token.
160159
func tokenWaitHandlerHelper(ctx context.Context, checkInstance func(ctx context.Context) error, getToken func(ctx context.Context) (*edge.Token, error)) *wait.AsyncActionHandler[edge.Token] {
161-
handler := wait.New(func() (waitFinished bool, response *edge.Token, err error) {
162-
err = checkInstance(ctx)
163-
if err != nil {
164-
return false, nil, err
165-
}
166-
token, err := getToken(ctx)
167-
var oapiErr *oapierror.GenericOpenAPIError
168-
if err != nil {
169-
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
170-
return false, nil, nil
160+
waitConfig := wait.WaiterHelper[edge.Token, string]{
161+
FetchInstance: func() (*edge.Token, error) {
162+
err := checkInstance(ctx)
163+
if err != nil {
164+
return nil, err
171165
}
172-
return false, nil, err
173-
}
174-
return true, token, nil
175-
})
166+
token, err := getToken(ctx)
167+
if err != nil {
168+
if apiErr, ok := errors.AsType[*oapierror.GenericOpenAPIError](err); ok && apiErr.StatusCode == http.StatusNotFound {
169+
return nil, nil
170+
}
171+
}
172+
return token, err
173+
},
174+
GetState: func(e *edge.Token) (string, error) {
175+
if e == nil {
176+
return "NOT_READY", nil
177+
}
178+
return "READY", nil
179+
},
180+
ActiveState: []string{"READY"},
181+
}
182+
handler := wait.New(waitConfig.Wait())
176183
handler.SetTimeout(timeoutMinutes * time.Minute)
177184
return handler
178185
}

services/edge/v1beta1api/wait/wait_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ var createOrUpdateInstanceTests = []struct {
129129
desc: "failed creation",
130130
shouldFail: false,
131131
instanceStatus: INSTANCESTATUS_ERROR,
132-
wantErr: errors.New("instance creation failed"),
132+
wantErr: errors.New("state is error"),
133133
},
134134
{
135135
desc: "API fails",

services/edge/wait/wait.go

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,20 @@ type EdgeCloudApiClient interface {
3333

3434
// createOrUpdateInstanceWaitHandler contains the shared logic for waiting on instance creation or updates.
3535
func createOrUpdateInstanceWaitHandler(ctx context.Context, getInstance func(ctx context.Context) (*edge.Instance, error)) *wait.AsyncActionHandler[edge.Instance] {
36-
handler := wait.New(func() (waitFinished bool, response *edge.Instance, err error) {
37-
instance, err := getInstance(ctx)
38-
if err != nil {
39-
return false, nil, err
40-
}
41-
42-
if instance == nil || instance.Status == nil {
43-
return false, nil, ErrInstanceNotFound
44-
}
45-
if instance == nil || instance.Status == nil {
46-
return false, nil, ErrInstanceStatusUndefined
47-
}
48-
49-
status := *instance.Status
50-
switch status {
51-
case edge.INSTANCESTATUS_ACTIVE:
52-
return true, instance, nil
53-
case edge.INSTANCESTATUS_ERROR:
54-
return true, instance, ErrInstanceCreationFailed
55-
case edge.INSTANCESTATUS_RECONCILING:
56-
return false, nil, nil
57-
case edge.INSTANCESTATUS_DELETING:
58-
return true, instance, ErrInstanceIsBeingDeleted
59-
default:
60-
return false, nil, nil
61-
}
62-
})
36+
waitConfig := wait.WaiterHelper[edge.Instance, edge.InstanceStatus]{
37+
FetchInstance: func() (*edge.Instance, error) {
38+
return getInstance(ctx)
39+
},
40+
GetState: func(instance *edge.Instance) (edge.InstanceStatus, error) {
41+
if instance == nil || instance.Status == nil {
42+
return "", ErrInstanceNotFound
43+
}
44+
return *instance.Status, nil
45+
},
46+
ActiveState: []edge.InstanceStatus{edge.INSTANCESTATUS_ACTIVE},
47+
ErrorState: []edge.InstanceStatus{edge.INSTANCESTATUS_ERROR, edge.INSTANCESTATUS_DELETING},
48+
}
49+
handler := wait.New(waitConfig.Wait())
6350
handler.SetTimeout(timeoutMinutes * time.Minute)
6451
return handler
6552
}
@@ -80,17 +67,15 @@ func CreateOrUpdateInstanceByNameWaitHandler(ctx context.Context, a EdgeCloudApi
8067

8168
// deleteInstanceWaitHandler contains the shared logic for waiting on instance deletion.
8269
func deleteInstanceWaitHandler(ctx context.Context, getInstance func(ctx context.Context) (*edge.Instance, error)) *wait.AsyncActionHandler[edge.Instance] {
83-
handler := wait.New(func() (waitFinished bool, response *edge.Instance, err error) {
84-
_, err = getInstance(ctx)
85-
if err == nil {
86-
return false, nil, nil
87-
}
88-
var oapiErr *oapierror.GenericOpenAPIError
89-
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
90-
return true, nil, nil
91-
}
92-
return false, nil, err
93-
})
70+
waitConfig := wait.WaiterHelper[edge.Instance, edge.InstanceStatus]{
71+
FetchInstance: func() (*edge.Instance, error) {
72+
return getInstance(ctx)
73+
},
74+
GetState: func(i *edge.Instance) (edge.InstanceStatus, error) {
75+
return "", nil
76+
},
77+
}
78+
handler := wait.New(waitConfig.Wait())
9479
handler.SetTimeout(timeoutMinutes * time.Minute)
9580
return handler
9681
}
@@ -111,21 +96,29 @@ func DeleteInstanceByNameWaitHandler(ctx context.Context, a EdgeCloudApiClient,
11196

11297
// kubeconfigWaitHandlerHelper contains the shared logic for waiting for the instance to become ready before retrieving the kubeconfig.
11398
func kubeconfigWaitHandlerHelper(ctx context.Context, checkInstance func(ctx context.Context) error, getKubeconfig func(ctx context.Context) (*edge.Kubeconfig, error)) *wait.AsyncActionHandler[edge.Kubeconfig] {
114-
handler := wait.New(func() (waitFinished bool, response *edge.Kubeconfig, err error) {
115-
err = checkInstance(ctx)
116-
if err != nil {
117-
return false, nil, err
118-
}
119-
kubeconfig, err := getKubeconfig(ctx)
120-
var oapiErr *oapierror.GenericOpenAPIError
121-
if err != nil {
122-
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
123-
return false, nil, nil
99+
waitConfig := wait.WaiterHelper[edge.Kubeconfig, string]{
100+
FetchInstance: func() (*edge.Kubeconfig, error) {
101+
err := checkInstance(ctx)
102+
if err != nil {
103+
return nil, err
124104
}
125-
return false, nil, err
126-
}
127-
return true, kubeconfig, nil
128-
})
105+
config, err := getKubeconfig(ctx)
106+
if err != nil {
107+
if apiErr, ok := errors.AsType[*oapierror.GenericOpenAPIError](err); ok && apiErr.StatusCode == http.StatusNotFound {
108+
return nil, nil
109+
}
110+
}
111+
return config, err
112+
},
113+
GetState: func(k *edge.Kubeconfig) (string, error) {
114+
if k == nil {
115+
return "NOT_READY", nil
116+
}
117+
return "READY", nil
118+
},
119+
ActiveState: []string{"READY"},
120+
}
121+
handler := wait.New(waitConfig.Wait())
129122
handler.SetTimeout(timeoutMinutes * time.Minute)
130123
return handler
131124
}
@@ -164,21 +157,29 @@ func KubeconfigByInstanceNameWaitHandler(ctx context.Context, a EdgeCloudApiClie
164157

165158
// tokenWaitHandlerHelper contains the shared logic for waiting for the instance to become ready before retrieving the service token.
166159
func tokenWaitHandlerHelper(ctx context.Context, checkInstance func(ctx context.Context) error, getToken func(ctx context.Context) (*edge.Token, error)) *wait.AsyncActionHandler[edge.Token] {
167-
handler := wait.New(func() (waitFinished bool, response *edge.Token, err error) {
168-
err = checkInstance(ctx)
169-
if err != nil {
170-
return false, nil, err
171-
}
172-
token, err := getToken(ctx)
173-
var oapiErr *oapierror.GenericOpenAPIError
174-
if err != nil {
175-
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
176-
return false, nil, nil
160+
waitConfig := wait.WaiterHelper[edge.Token, string]{
161+
FetchInstance: func() (*edge.Token, error) {
162+
err := checkInstance(ctx)
163+
if err != nil {
164+
return nil, err
177165
}
178-
return false, nil, err
179-
}
180-
return true, token, nil
181-
})
166+
token, err := getToken(ctx)
167+
if err != nil {
168+
if apiErr, ok := errors.AsType[*oapierror.GenericOpenAPIError](err); ok && apiErr.StatusCode == http.StatusNotFound {
169+
return nil, nil
170+
}
171+
}
172+
return token, err
173+
},
174+
GetState: func(t *edge.Token) (string, error) {
175+
if t == nil {
176+
return "NOT_READY", nil
177+
}
178+
return "READY", nil
179+
},
180+
ActiveState: []string{"READY"},
181+
}
182+
handler := wait.New(waitConfig.Wait())
182183
handler.SetTimeout(timeoutMinutes * time.Minute)
183184
return handler
184185
}

services/edge/wait/wait_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ var createOrUpdateInstanceTests = []struct {
173173
desc: "failed creation",
174174
shouldFail: false,
175175
instanceStatus: edge.INSTANCESTATUS_ERROR,
176-
wantErr: errors.New("instance creation failed"),
176+
wantErr: errors.New("state is error"),
177177
},
178178
{
179179
desc: "API fails",

0 commit comments

Comments
 (0)