Skip to content

Commit 5d559aa

Browse files
authored
Fix multiple instance registration/deregistration (#60)
1 parent 7dddd37 commit 5d559aa

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed

pkg/cloudmap/client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ func (sdc *serviceDiscoveryClient) RegisterEndpoints(ctx context.Context, nsName
157157
opCollector := NewOperationCollector()
158158

159159
for _, endpt := range endpts {
160+
endptId := endpt.Id
161+
endptAttrs := endpt.GetCloudMapAttributes()
160162
opCollector.Add(func() (opId string, err error) {
161-
return sdc.sdApi.RegisterInstance(ctx, svcId, endpt.Id, endpt.GetCloudMapAttributes())
163+
return sdc.sdApi.RegisterInstance(ctx, svcId, endptId, endptAttrs)
162164
})
163165
}
164166

@@ -195,8 +197,9 @@ func (sdc *serviceDiscoveryClient) DeleteEndpoints(ctx context.Context, nsName s
195197
opCollector := NewOperationCollector()
196198

197199
for _, endpt := range endpts {
200+
endptId := endpt.Id
198201
opCollector.Add(func() (opId string, err error) {
199-
return sdc.sdApi.DeregisterInstance(ctx, svcId, endpt.Id)
202+
return sdc.sdApi.DeregisterInstance(ctx, svcId, endptId)
200203
})
201204
}
202205

pkg/cloudmap/client_test.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,70 @@ func TestServiceDiscoveryClient_GetService(t *testing.T) {
288288
// TODO: Add unit tests
289289
}
290290

291-
func TestServiceDiscoveryClient_RegisterEndpoints(t *testing.T) {
291+
func TestServiceDiscoveryClient_GetService_CachedValues(t *testing.T) {
292292
// TODO: Add unit tests
293293
}
294294

295+
func TestServiceDiscoveryClient_RegisterEndpoints(t *testing.T) {
296+
mockController := gomock.NewController(t)
297+
defer mockController.Finish()
298+
299+
sdApi := cloudmap.NewMockServiceDiscoveryApi(mockController)
300+
301+
sdc := getTestSdClient(t, sdApi)
302+
sdc.namespaceCache.Add(test.NsName, test.NsId, time.Minute)
303+
sdc.serviceIdCache.Add(fmt.Sprintf("%s/%s", test.NsName, test.SvcName), test.SvcId, time.Minute)
304+
305+
attrs1 := map[string]string{"AWS_INSTANCE_IPV4": test.EndptIp1, "AWS_INSTANCE_PORT": test.EndptPortStr1}
306+
attrs2 := map[string]string{"AWS_INSTANCE_IPV4": test.EndptIp2, "AWS_INSTANCE_PORT": test.EndptPortStr2}
307+
308+
sdApi.EXPECT().RegisterInstance(context.TODO(), test.SvcId, test.EndptId1, attrs1).
309+
Return(test.OpId1, nil)
310+
sdApi.EXPECT().RegisterInstance(context.TODO(), test.SvcId, test.EndptId2, attrs2).
311+
Return(test.OpId2, nil)
312+
sdApi.EXPECT().ListOperations(context.TODO(), gomock.Any()).
313+
Return(map[string]types.OperationStatus{
314+
test.OpId1: types.OperationStatusSuccess,
315+
test.OpId2: types.OperationStatusSuccess}, nil)
316+
317+
err := sdc.RegisterEndpoints(context.TODO(), test.NsName, test.SvcName,
318+
[]*model.Endpoint{
319+
{
320+
Id: test.EndptId1,
321+
IP: test.EndptIp1,
322+
Port: test.EndptPort1,
323+
},
324+
{
325+
Id: test.EndptId2,
326+
IP: test.EndptIp2,
327+
Port: test.EndptPort2,
328+
},
329+
})
330+
331+
assert.Nil(t, err)
332+
}
333+
295334
func TestServiceDiscoveryClient_DeleteEndpoints(t *testing.T) {
296-
// TODO: Add unit tests
335+
mockController := gomock.NewController(t)
336+
defer mockController.Finish()
337+
338+
sdApi := cloudmap.NewMockServiceDiscoveryApi(mockController)
339+
340+
sdc := getTestSdClient(t, sdApi)
341+
sdc.namespaceCache.Add(test.NsName, test.NsId, time.Minute)
342+
sdc.serviceIdCache.Add(fmt.Sprintf("%s/%s", test.NsName, test.SvcName), test.SvcId, time.Minute)
343+
344+
sdApi.EXPECT().DeregisterInstance(context.TODO(), test.SvcId, test.EndptId1).Return(test.OpId1, nil)
345+
sdApi.EXPECT().DeregisterInstance(context.TODO(), test.SvcId, test.EndptId2).Return(test.OpId2, nil)
346+
sdApi.EXPECT().ListOperations(context.TODO(), gomock.Any()).
347+
Return(map[string]types.OperationStatus{
348+
test.OpId1: types.OperationStatusSuccess,
349+
test.OpId2: types.OperationStatusSuccess}, nil)
350+
351+
err := sdc.DeleteEndpoints(context.TODO(), test.NsName, test.SvcName,
352+
[]*model.Endpoint{{Id: test.EndptId1}, {Id: test.EndptId2}})
353+
354+
assert.Nil(t, err)
297355
}
298356

299357
func TestServiceDiscoveryClient_getNamespace_HappyCase(t *testing.T) {

pkg/controllers/cloudmap_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ const (
2727
// DerivedServiceAnnotation annotates a ServiceImport with derived Service name
2828
DerivedServiceAnnotation = "multicluster.k8s.aws/derived-service"
2929

30-
// LabelServiceName is used to indicate the name of multi-cluster service
31-
// that an EndpointSlice belongs to.
30+
// LabelServiceImportName indicates the name of multi-cluster service that an EndpointSlice belongs to.
3231
LabelServiceImportName = "multicluster.kubernetes.io/service-name"
3332
)
3433

test/test-constants.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ const (
1212
EndptId1 = "endpoint-id-1"
1313
EndptId2 = "endpoint-id-2"
1414
EndptIp1 = "192.168.0.1"
15-
EndptPort1 = 2
16-
EndptPortStr1 = "2"
15+
EndptIp2 = "192.168.0.2"
16+
EndptPort1 = 1
17+
EndptPortStr1 = "1"
18+
EndptPort2 = 2
19+
EndptPortStr2 = "2"
1720
OpId1 = "operation-id-1"
1821
OpId2 = "operation-id-2"
1922
OpStart = 1

0 commit comments

Comments
 (0)