@@ -2,18 +2,12 @@ package cloudmap
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
- "fmt"
7
- "time"
8
-
9
- "golang.org/x/time/rate"
10
5
11
6
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/common"
12
7
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/model"
13
8
"github.com/aws/aws-sdk-go-v2/aws"
14
9
sd "github.com/aws/aws-sdk-go-v2/service/servicediscovery"
15
10
"github.com/aws/aws-sdk-go-v2/service/servicediscovery/types"
16
- "k8s.io/apimachinery/pkg/util/wait"
17
11
)
18
12
19
13
const (
@@ -32,9 +26,6 @@ type ServiceDiscoveryApi interface {
32
26
// DiscoverInstances returns a list of service instances registered to a given service.
33
27
DiscoverInstances (ctx context.Context , nsName string , svcName string , queryParameters map [string ]string ) (insts []types.HttpInstanceSummary , err error )
34
28
35
- // ListOperations returns a map of operations to their status matching a list of filters.
36
- ListOperations (ctx context.Context , opFilters []types.OperationFilter ) (operationStatusMap map [string ]types.OperationStatus , err error )
37
-
38
29
// GetOperation returns an operation.
39
30
GetOperation (ctx context.Context , operationId string ) (operation * types.Operation , err error )
40
31
@@ -49,32 +40,25 @@ type ServiceDiscoveryApi interface {
49
40
50
41
// DeregisterInstance de-registers a service instance in Cloud Map.
51
42
DeregisterInstance (ctx context.Context , serviceId string , instanceId string ) (operationId string , err error )
52
-
53
- // PollNamespaceOperation polls a namespace operation, and returns the namespace ID.
54
- PollNamespaceOperation (ctx context.Context , operationId string ) (namespaceId string , err error )
55
43
}
56
44
57
45
type serviceDiscoveryApi struct {
58
- log common.Logger
59
- awsFacade AwsFacade
60
- nsRateLimiter * rate.Limiter
61
- svcRateLimiter * rate.Limiter
62
- opRateLimiter * rate.Limiter
46
+ log common.Logger
47
+ awsFacade AwsFacade
48
+ rateLimiter common.RateLimiter
63
49
}
64
50
65
51
// NewServiceDiscoveryApiFromConfig creates a new AWS Cloud Map API connection manager from an AWS client config.
66
52
func NewServiceDiscoveryApiFromConfig (cfg * aws.Config ) ServiceDiscoveryApi {
67
53
return & serviceDiscoveryApi {
68
- log : common .NewLogger ("cloudmap" , "api" ),
69
- awsFacade : NewAwsFacadeFromConfig (cfg ),
70
- nsRateLimiter : rate .NewLimiter (rate .Every (1 * time .Second ), 5 ), // 1 per second
71
- svcRateLimiter : rate .NewLimiter (rate .Every (2 * time .Second ), 10 ), // 2 per second
72
- opRateLimiter : rate .NewLimiter (rate .Every (100 * time .Second ), 200 ), // 100 per second
54
+ log : common .NewLogger ("cloudmap" , "api" ),
55
+ awsFacade : NewAwsFacadeFromConfig (cfg ),
56
+ rateLimiter : common .NewDefaultRateLimiter (),
73
57
}
74
58
}
75
59
76
60
func (sdApi * serviceDiscoveryApi ) GetNamespaceMap (ctx context.Context ) (map [string ]* model.Namespace , error ) {
77
- err := sdApi .nsRateLimiter .Wait (ctx )
61
+ err := sdApi .rateLimiter .Wait (ctx , common . ListNamespaces )
78
62
if err != nil {
79
63
return nil , err
80
64
}
@@ -105,7 +89,7 @@ func (sdApi *serviceDiscoveryApi) GetNamespaceMap(ctx context.Context) (map[stri
105
89
}
106
90
107
91
func (sdApi * serviceDiscoveryApi ) GetServiceIdMap (ctx context.Context , nsId string ) (map [string ]string , error ) {
108
- err := sdApi .svcRateLimiter .Wait (ctx )
92
+ err := sdApi .rateLimiter .Wait (ctx , common . ListServices )
109
93
if err != nil {
110
94
return nil , err
111
95
}
@@ -151,30 +135,8 @@ func (sdApi *serviceDiscoveryApi) DiscoverInstances(ctx context.Context, nsName
151
135
return out .Instances , nil
152
136
}
153
137
154
- func (sdApi * serviceDiscoveryApi ) ListOperations (ctx context.Context , opFilters []types.OperationFilter ) (map [string ]types.OperationStatus , error ) {
155
- opStatusMap := make (map [string ]types.OperationStatus )
156
-
157
- pages := sd .NewListOperationsPaginator (sdApi .awsFacade , & sd.ListOperationsInput {
158
- Filters : opFilters ,
159
- })
160
-
161
- for pages .HasMorePages () {
162
- output , err := pages .NextPage (ctx )
163
-
164
- if err != nil {
165
- return opStatusMap , err
166
- }
167
-
168
- for _ , sdOp := range output .Operations {
169
- opStatusMap [aws .ToString (sdOp .Id )] = sdOp .Status
170
- }
171
- }
172
-
173
- return opStatusMap , nil
174
- }
175
-
176
138
func (sdApi * serviceDiscoveryApi ) GetOperation (ctx context.Context , opId string ) (operation * types.Operation , err error ) {
177
- err = sdApi .opRateLimiter .Wait (ctx )
139
+ err = sdApi .rateLimiter .Wait (ctx , common . GetOperation )
178
140
if err != nil {
179
141
return nil , err
180
142
}
@@ -236,6 +198,11 @@ func (sdApi *serviceDiscoveryApi) getDnsConfig() types.DnsConfig {
236
198
}
237
199
238
200
func (sdApi * serviceDiscoveryApi ) RegisterInstance (ctx context.Context , svcId string , instId string , instAttrs map [string ]string ) (opId string , err error ) {
201
+ err = sdApi .rateLimiter .Wait (ctx , common .RegisterInstance )
202
+ if err != nil {
203
+ return "" , err
204
+ }
205
+
239
206
regResp , err := sdApi .awsFacade .RegisterInstance (ctx , & sd.RegisterInstanceInput {
240
207
Attributes : instAttrs ,
241
208
InstanceId : & instId ,
@@ -250,6 +217,11 @@ func (sdApi *serviceDiscoveryApi) RegisterInstance(ctx context.Context, svcId st
250
217
}
251
218
252
219
func (sdApi * serviceDiscoveryApi ) DeregisterInstance (ctx context.Context , svcId string , instId string ) (opId string , err error ) {
220
+ err = sdApi .rateLimiter .Wait (ctx , common .DeregisterInstance )
221
+ if err != nil {
222
+ return "" , err
223
+ }
224
+
253
225
deregResp , err := sdApi .awsFacade .DeregisterInstance (ctx , & sd.DeregisterInstanceInput {
254
226
InstanceId : & instId ,
255
227
ServiceId : & svcId ,
@@ -261,31 +233,3 @@ func (sdApi *serviceDiscoveryApi) DeregisterInstance(ctx context.Context, svcId
261
233
262
234
return aws .ToString (deregResp .OperationId ), err
263
235
}
264
-
265
- func (sdApi * serviceDiscoveryApi ) PollNamespaceOperation (ctx context.Context , opId string ) (nsId string , err error ) {
266
- err = wait .Poll (defaultOperationPollInterval , defaultOperationPollTimeout , func () (done bool , err error ) {
267
- sdApi .log .Info ("polling operation" , "opId" , opId )
268
- op , err := sdApi .GetOperation (ctx , opId )
269
-
270
- if err != nil {
271
- return true , err
272
- }
273
-
274
- if op .Status == types .OperationStatusFail {
275
- return true , fmt .Errorf ("failed to create namespace: %s" , aws .ToString (op .ErrorMessage ))
276
- }
277
-
278
- if op .Status == types .OperationStatusSuccess {
279
- nsId = op .Targets [string (types .OperationTargetTypeNamespace )]
280
- return true , nil
281
- }
282
-
283
- return false , nil
284
- })
285
-
286
- if err == wait .ErrWaitTimeout {
287
- err = errors .New (operationPollTimoutErrorMessage )
288
- }
289
-
290
- return nsId , err
291
- }
0 commit comments