Skip to content

Commit da95fbb

Browse files
authored
Exporting service and endpoints port attributes (#78)
* Exporting service's Port, TargetPort, Protocol; and endpoint's Port to the Cloumap as an Attributes. Also, create the derived service and endpoint slices using these ports information. Improve reconcile service logic. Also, add the new tests. * Include the protocol to ensure the uniqueness of the Port. Move test literals into constants. Minor refactorings. * Revert the golint refactor
1 parent 315963b commit da95fbb

17 files changed

+962
-236
lines changed

go.sum

Lines changed: 174 additions & 0 deletions
Large diffs are not rendered by default.

integration/scenarios/export_service.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,33 @@ type exportServiceScenario struct {
3030
expectedSvc model.Service
3131
}
3232

33-
func NewExportServiceScenario(cfg *aws.Config, nsName string, svcName string, portStr string, ips string) (ExportServiceScenario, error) {
33+
func NewExportServiceScenario(cfg *aws.Config, nsName string, svcName string, portStr string, servicePortStr string, ips string) (ExportServiceScenario, error) {
3434
endpts := make([]*model.Endpoint, 0)
3535

3636
port, parseError := strconv.ParseUint(portStr, 10, 16)
3737
if parseError != nil {
3838
return nil, parseError
3939
}
40+
servicePort, parseError := strconv.ParseUint(servicePortStr, 10, 16)
41+
if parseError != nil {
42+
return nil, parseError
43+
}
4044

4145
for _, ip := range strings.Split(ips, ",") {
46+
endpointPort := model.Port{
47+
Port: int32(port),
48+
Protocol: model.TCPProtocol,
49+
}
4250
endpts = append(endpts, &model.Endpoint{
43-
Id: model.EndpointIdFromIPAddress(ip),
44-
IP: ip,
45-
Port: int32(port),
46-
Attributes: make(map[string]string, 0),
51+
Id: model.EndpointIdFromIPAddressAndPort(ip, endpointPort),
52+
IP: ip,
53+
ServicePort: model.Port{
54+
Port: int32(servicePort),
55+
TargetPort: portStr,
56+
Protocol: model.TCPProtocol,
57+
},
58+
EndpointPort: endpointPort,
59+
Attributes: make(map[string]string),
4760
})
4861
}
4962

integration/scenarios/runner/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@ import (
1010
)
1111

1212
func main() {
13-
if len(os.Args) != 5 {
14-
fmt.Println("Expected namespace, service, endpoint port, and endpoint IP list arguments")
13+
if len(os.Args) != 6 {
14+
fmt.Println("Expected namespace, service, endpoint port, service port and endpoint IP list arguments")
1515
os.Exit(1)
1616
}
1717

1818
nsName := os.Args[1]
1919
svcName := os.Args[2]
2020
port := os.Args[3]
21-
ips := os.Args[4]
21+
servicePort := os.Args[4]
22+
ips := os.Args[5]
2223

23-
testServiceExport(nsName, svcName, port, ips)
24+
testServiceExport(nsName, svcName, port, servicePort, ips)
2425
}
2526

26-
func testServiceExport(nsName string, svcName string, port string, ips string) {
27+
func testServiceExport(nsName string, svcName string, port string, servicePort string, ips string) {
2728
fmt.Printf("Testing service export integration for namespace %s and service %s\n", nsName, svcName)
2829

29-
export, err := scenarios.NewExportServiceScenario(getAwsConfig(), nsName, svcName, port, ips)
30+
export, err := scenarios.NewExportServiceScenario(getAwsConfig(), nsName, svcName, port, servicePort, ips)
3031
if err != nil {
3132
fmt.Printf("Failed to setup service export integration test scenario: %s", err.Error())
3233
os.Exit(1)

integration/scripts/common.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ SCENARIOS='./integration/scenarios'
88
NAMESPACE='aws-cloud-map-mcs-e2e'
99
SERVICE='e2e-service'
1010
ENDPT_PORT=80
11+
SERVICE_PORT=8080
1112
KIND_SHORT='cloud-map-e2e'
1213
CLUSTER='kind-cloud-map-e2e'
1314
IMAGE='kindest/node:v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729'

integration/scripts/run-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mkdir -p "$LOGS"
1717
CTL_PID=$!
1818
echo "controller PID:$CTL_PID"
1919

20-
go run $SCENARIOS/runner/main.go $NAMESPACE $SERVICE $ENDPT_PORT "$endpts"
20+
go run $SCENARIOS/runner/main.go $NAMESPACE $SERVICE $ENDPT_PORT $SERVICE_PORT "$endpts"
2121
exit_code=$?
2222

2323
if [ "$exit_code" -eq 0 ] ; then

pkg/cloudmap/cache_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ func TestServiceDiscoveryClientCacheGetServiceId_Corrupt(t *testing.T) {
9191

9292
func TestServiceDiscoveryClientCacheGetEndpoints_Found(t *testing.T) {
9393
sdc := NewDefaultServiceDiscoveryClientCache()
94-
sdc.CacheEndpoints(test.NsName, test.SvcName, []*model.Endpoint{test.GetTestEndpoint(), test.GetTestEndpoint2()})
94+
sdc.CacheEndpoints(test.NsName, test.SvcName, []*model.Endpoint{test.GetTestEndpoint1(), test.GetTestEndpoint2()})
9595

9696
endpts, found := sdc.GetEndpoints(test.NsName, test.SvcName)
9797
assert.True(t, found)
98-
assert.Equal(t, []*model.Endpoint{test.GetTestEndpoint(), test.GetTestEndpoint2()}, endpts)
98+
assert.Equal(t, []*model.Endpoint{test.GetTestEndpoint1(), test.GetTestEndpoint2()}, endpts)
9999
}
100100

101101
func TestServiceDiscoveryClientCacheGetEndpoints_NotFound(t *testing.T) {
@@ -117,7 +117,7 @@ func TestServiceDiscoveryClientCacheGetEndpoints_Corrupt(t *testing.T) {
117117

118118
func TestServiceDiscoveryClientEvictEndpoints(t *testing.T) {
119119
sdc := NewDefaultServiceDiscoveryClientCache()
120-
sdc.CacheEndpoints(test.NsName, test.SvcName, []*model.Endpoint{test.GetTestEndpoint(), test.GetTestEndpoint2()})
120+
sdc.CacheEndpoints(test.NsName, test.SvcName, []*model.Endpoint{test.GetTestEndpoint1(), test.GetTestEndpoint2()})
121121
sdc.EvictEndpoints(test.NsName, test.SvcName)
122122

123123
endpts, found := sdc.GetEndpoints(test.NsName, test.SvcName)

pkg/cloudmap/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (sdc *serviceDiscoveryClient) listEndpoints(ctx context.Context, nsName str
233233
for _, inst := range insts {
234234
endpt, endptErr := model.NewEndpointFromInstance(&inst)
235235
if endptErr != nil {
236-
sdc.log.Info(fmt.Sprintf("skipping instance %s to endpoint conversion: %s", *inst.InstanceId, endptErr.Error()))
236+
sdc.log.Error(endptErr, "skipping instance to endpoint conversion", "instanceId", *inst.InstanceId)
237237
continue
238238
}
239239
endpts = append(endpts, endpt)

pkg/cloudmap/client_test.go

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,32 @@ func TestServiceDiscoveryClient_ListServices_HappyCase(t *testing.T) {
4545
{
4646
InstanceId: aws.String(test.EndptId1),
4747
Attributes: map[string]string{
48-
model.Ipv4Attr: test.EndptIp1,
49-
model.PortAttr: test.EndptPortStr1,
48+
model.EndpointIpv4Attr: test.EndptIp1,
49+
model.EndpointPortAttr: test.PortStr1,
50+
model.EndpointPortNameAttr: test.PortName1,
51+
model.EndpointProtocolAttr: test.Protocol1,
52+
model.ServicePortNameAttr: test.PortName1,
53+
model.ServicePortAttr: test.ServicePortStr1,
54+
model.ServiceProtocolAttr: test.Protocol1,
55+
model.ServiceTargetPortAttr: test.PortStr1,
5056
},
5157
},
5258
{
5359
InstanceId: aws.String(test.EndptId2),
5460
Attributes: map[string]string{
55-
model.Ipv4Attr: test.EndptIp2,
56-
model.PortAttr: test.EndptPortStr2,
61+
model.EndpointIpv4Attr: test.EndptIp2,
62+
model.EndpointPortAttr: test.PortStr2,
63+
model.EndpointPortNameAttr: test.PortName2,
64+
model.EndpointProtocolAttr: test.Protocol2,
65+
model.ServicePortNameAttr: test.PortName2,
66+
model.ServicePortAttr: test.ServicePortStr2,
67+
model.ServiceProtocolAttr: test.Protocol2,
68+
model.ServiceTargetPortAttr: test.PortStr2,
5769
},
5870
},
5971
}, nil)
6072
tc.mockCache.EXPECT().CacheEndpoints(test.NsName, test.SvcName,
61-
[]*model.Endpoint{test.GetTestEndpoint(), test.GetTestEndpoint2()})
73+
[]*model.Endpoint{test.GetTestEndpoint1(), test.GetTestEndpoint2()})
6274

6375
svcs, err := tc.client.ListServices(context.TODO(), test.NsName)
6476
assert.Equal(t, []*model.Service{test.GetTestService()}, svcs)
@@ -76,7 +88,7 @@ func TestServiceDiscoveryClient_ListServices_HappyCaseCachedResults(t *testing.T
7688
tc.mockCache.EXPECT().CacheServiceId(test.NsName, test.SvcName, test.SvcId)
7789

7890
tc.mockCache.EXPECT().GetEndpoints(test.NsName, test.SvcName).
79-
Return([]*model.Endpoint{test.GetTestEndpoint(), test.GetTestEndpoint2()}, true)
91+
Return([]*model.Endpoint{test.GetTestEndpoint1(), test.GetTestEndpoint2()}, true)
8092

8193
svcs, err := tc.client.ListServices(context.TODO(), test.NsName)
8294
assert.Equal(t, []*model.Service{test.GetTestService()}, svcs)
@@ -283,20 +295,32 @@ func TestServiceDiscoveryClient_GetService_HappyCase(t *testing.T) {
283295
{
284296
InstanceId: aws.String(test.EndptId1),
285297
Attributes: map[string]string{
286-
model.Ipv4Attr: test.EndptIp1,
287-
model.PortAttr: test.EndptPortStr1,
298+
model.EndpointIpv4Attr: test.EndptIp1,
299+
model.EndpointPortAttr: test.PortStr1,
300+
model.EndpointPortNameAttr: test.PortName1,
301+
model.EndpointProtocolAttr: test.Protocol1,
302+
model.ServicePortNameAttr: test.PortName1,
303+
model.ServicePortAttr: test.ServicePortStr1,
304+
model.ServiceProtocolAttr: test.Protocol1,
305+
model.ServiceTargetPortAttr: test.PortStr1,
288306
},
289307
},
290308
{
291309
InstanceId: aws.String(test.EndptId2),
292310
Attributes: map[string]string{
293-
model.Ipv4Attr: test.EndptIp2,
294-
model.PortAttr: test.EndptPortStr2,
311+
model.EndpointIpv4Attr: test.EndptIp2,
312+
model.EndpointPortAttr: test.PortStr2,
313+
model.EndpointPortNameAttr: test.PortName2,
314+
model.EndpointProtocolAttr: test.Protocol2,
315+
model.ServicePortNameAttr: test.PortName2,
316+
model.ServicePortAttr: test.ServicePortStr2,
317+
model.ServiceProtocolAttr: test.Protocol2,
318+
model.ServiceTargetPortAttr: test.PortStr2,
295319
},
296320
},
297321
}, nil)
298322
tc.mockCache.EXPECT().CacheEndpoints(test.NsName, test.SvcName,
299-
[]*model.Endpoint{test.GetTestEndpoint(), test.GetTestEndpoint2()})
323+
[]*model.Endpoint{test.GetTestEndpoint1(), test.GetTestEndpoint2()})
300324

301325
svc, err := tc.client.GetService(context.TODO(), test.NsName, test.SvcName)
302326
assert.Nil(t, err)
@@ -308,7 +332,7 @@ func TestServiceDiscoveryClient_GetService_CachedValues(t *testing.T) {
308332
defer tc.close()
309333

310334
tc.mockCache.EXPECT().GetEndpoints(test.NsName, test.SvcName).
311-
Return([]*model.Endpoint{test.GetTestEndpoint(), test.GetTestEndpoint2()}, true)
335+
Return([]*model.Endpoint{test.GetTestEndpoint1(), test.GetTestEndpoint2()}, true)
312336

313337
svc, err := tc.client.GetService(context.TODO(), test.NsName, test.SvcName)
314338
assert.Nil(t, err)
@@ -321,8 +345,26 @@ func TestServiceDiscoveryClient_RegisterEndpoints(t *testing.T) {
321345

322346
tc.mockCache.EXPECT().GetServiceId(test.NsName, test.SvcName).Return(test.SvcId, true)
323347

324-
attrs1 := map[string]string{"AWS_INSTANCE_IPV4": test.EndptIp1, "AWS_INSTANCE_PORT": test.EndptPortStr1}
325-
attrs2 := map[string]string{"AWS_INSTANCE_IPV4": test.EndptIp2, "AWS_INSTANCE_PORT": test.EndptPortStr2}
348+
attrs1 := map[string]string{
349+
model.EndpointIpv4Attr: test.EndptIp1,
350+
model.EndpointPortAttr: test.PortStr1,
351+
model.EndpointPortNameAttr: test.PortName1,
352+
model.EndpointProtocolAttr: test.Protocol1,
353+
model.ServicePortNameAttr: test.PortName1,
354+
model.ServicePortAttr: test.ServicePortStr1,
355+
model.ServiceProtocolAttr: test.Protocol1,
356+
model.ServiceTargetPortAttr: test.PortStr1,
357+
}
358+
attrs2 := map[string]string{
359+
model.EndpointIpv4Attr: test.EndptIp2,
360+
model.EndpointPortAttr: test.PortStr2,
361+
model.EndpointPortNameAttr: test.PortName2,
362+
model.EndpointProtocolAttr: test.Protocol2,
363+
model.ServicePortNameAttr: test.PortName2,
364+
model.ServicePortAttr: test.ServicePortStr2,
365+
model.ServiceProtocolAttr: test.Protocol2,
366+
model.ServiceTargetPortAttr: test.PortStr2,
367+
}
326368

327369
tc.mockApi.EXPECT().RegisterInstance(context.TODO(), test.SvcId, test.EndptId1, attrs1).
328370
Return(test.OpId1, nil)
@@ -336,18 +378,7 @@ func TestServiceDiscoveryClient_RegisterEndpoints(t *testing.T) {
336378
tc.mockCache.EXPECT().EvictEndpoints(test.NsName, test.SvcName)
337379

338380
err := tc.client.RegisterEndpoints(context.TODO(), test.NsName, test.SvcName,
339-
[]*model.Endpoint{
340-
{
341-
Id: test.EndptId1,
342-
IP: test.EndptIp1,
343-
Port: test.EndptPort1,
344-
},
345-
{
346-
Id: test.EndptId2,
347-
IP: test.EndptIp2,
348-
Port: test.EndptPort2,
349-
},
350-
})
381+
[]*model.Endpoint{test.GetTestEndpoint1(), test.GetTestEndpoint2()})
351382

352383
assert.Nil(t, err)
353384
}

0 commit comments

Comments
 (0)