Skip to content

Commit ce584c5

Browse files
authored
Cache namespace and service maps (#124)
1 parent b75ae48 commit ce584c5

File tree

11 files changed

+279
-291
lines changed

11 files changed

+279
-291
lines changed

integration/janitor/janitor.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,31 @@ func NewDefaultJanitor() CloudMapJanitor {
3939
func (j *cloudMapJanitor) Cleanup(ctx context.Context, nsName string) {
4040
fmt.Printf("Cleaning up all test resources in Cloud Map for namespace : %s\n", nsName)
4141

42-
nsList, err := j.sdApi.ListNamespaces(ctx)
42+
nsMap, err := j.sdApi.GetNamespaceMap(ctx)
4343
j.checkOrFail(err, "", "could not find namespace to clean")
4444

45-
var nsId string
46-
for _, ns := range nsList {
47-
if ns.Name == nsName {
48-
nsId = ns.Id
49-
}
50-
}
51-
52-
if nsId == "" {
45+
ns, found := nsMap[nsName]
46+
if !found {
5347
fmt.Println("namespace does not exist in account, nothing to clean")
5448
return
5549
}
5650

57-
fmt.Printf("found namespace to clean: %s\n", nsId)
51+
fmt.Printf("found namespace to clean: %s\n", ns.Id)
5852

59-
svcs, err := j.sdApi.ListServices(ctx, nsId)
53+
svcIdMap, err := j.sdApi.GetServiceIdMap(ctx, ns.Id)
6054
j.checkOrFail(err,
61-
fmt.Sprintf("namespace has %d services to clean", len(svcs)),
55+
fmt.Sprintf("namespace has %d services to clean", len(svcIdMap)),
6256
"could not find services to clean")
6357

64-
for _, svc := range svcs {
65-
fmt.Printf("found service to clean: %s\n", svc.Id)
66-
j.deregisterInstances(ctx, nsName, svc.Name, svc.Id)
58+
for svcName, svcId := range svcIdMap {
59+
fmt.Printf("found service to clean: %s\n", svcId)
60+
j.deregisterInstances(ctx, nsName, svcName, svcId)
6761

68-
delSvcErr := j.sdApi.DeleteService(ctx, svc.Id)
62+
delSvcErr := j.sdApi.DeleteService(ctx, svcId)
6963
j.checkOrFail(delSvcErr, "service deleted", "could not cleanup service")
7064
}
7165

72-
opId, err := j.sdApi.DeleteNamespace(ctx, nsId)
66+
opId, err := j.sdApi.DeleteNamespace(ctx, ns.Id)
7367
if err == nil {
7468
fmt.Println("namespace delete in progress")
7569
_, err = j.sdApi.PollNamespaceOperation(ctx, opId)

integration/janitor/janitor_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ func TestCleanupHappyCase(t *testing.T) {
2828
tj := getTestJanitor(t)
2929
defer tj.close()
3030

31-
tj.mockApi.EXPECT().ListNamespaces(context.TODO()).
32-
Return([]*model.Namespace{{Id: test.HttpNsId, Name: test.HttpNsName}}, nil)
33-
tj.mockApi.EXPECT().ListServices(context.TODO(), test.HttpNsId).
34-
Return([]*model.Resource{{Id: test.SvcId, Name: test.SvcName}}, nil)
31+
tj.mockApi.EXPECT().GetNamespaceMap(context.TODO()).
32+
Return(map[string]*model.Namespace{test.HttpNsName: test.GetTestHttpNamespace()}, nil)
33+
tj.mockApi.EXPECT().GetServiceIdMap(context.TODO(), test.HttpNsId).
34+
Return(map[string]string{test.SvcName: test.SvcId}, nil)
3535
tj.mockApi.EXPECT().DiscoverInstances(context.TODO(), test.HttpNsName, test.SvcName).
3636
Return([]types.HttpInstanceSummary{{InstanceId: aws.String(test.EndptId1)}}, nil)
3737

@@ -54,8 +54,8 @@ func TestCleanupNothingToClean(t *testing.T) {
5454
tj := getTestJanitor(t)
5555
defer tj.close()
5656

57-
tj.mockApi.EXPECT().ListNamespaces(context.TODO()).
58-
Return([]*model.Namespace{}, nil)
57+
tj.mockApi.EXPECT().GetNamespaceMap(context.TODO()).
58+
Return(map[string]*model.Namespace{}, nil)
5959

6060
tj.janitor.Cleanup(context.TODO(), test.HttpNsName)
6161
assert.False(t, *tj.failed)

pkg/cloudmap/api.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ const (
2020
// ServiceDiscoveryApi handles the AWS Cloud Map API request and response processing logic, and converts results to
2121
// internal data structures. It manages all interactions with the AWS SDK.
2222
type ServiceDiscoveryApi interface {
23-
// ListNamespaces returns a list of all namespaces.
24-
ListNamespaces(ctx context.Context) (namespaces []*model.Namespace, err error)
23+
// GetNamespaceMap returns a map of all namespaces in the Cloud Map account indexed by namespace name.
24+
GetNamespaceMap(ctx context.Context) (namespaces map[string]*model.Namespace, err error)
2525

26-
// ListServices returns a list of services for a given namespace.
27-
ListServices(ctx context.Context, namespaceId string) (services []*model.Resource, err error)
26+
// GetServiceIdMap returns a map of all service IDs for a given namespace indexed by service name.
27+
GetServiceIdMap(ctx context.Context, namespaceId string) (serviceIdMap map[string]string, err error)
2828

2929
// DiscoverInstances returns a list of service instances registered to a given service.
3030
DiscoverInstances(ctx context.Context, nsName string, svcName string) (insts []types.HttpInstanceSummary, err error)
@@ -64,52 +64,53 @@ func NewServiceDiscoveryApiFromConfig(cfg *aws.Config) ServiceDiscoveryApi {
6464
}
6565
}
6666

67-
func (sdApi *serviceDiscoveryApi) ListNamespaces(ctx context.Context) (namespaces []*model.Namespace, err error) {
68-
pages := sd.NewListNamespacesPaginator(sdApi.awsFacade, &sd.ListNamespacesInput{})
67+
func (sdApi *serviceDiscoveryApi) GetNamespaceMap(ctx context.Context) (map[string]*model.Namespace, error) {
68+
namespaceMap := make(map[string]*model.Namespace)
6969

70+
pages := sd.NewListNamespacesPaginator(sdApi.awsFacade, &sd.ListNamespacesInput{})
7071
for pages.HasMorePages() {
7172
output, err := pages.NextPage(ctx)
7273
if err != nil {
73-
return namespaces, err
74+
return nil, err
7475
}
7576

7677
for _, ns := range output.Namespaces {
77-
if namespaceType := model.ConvertNamespaceType(ns.Type); !namespaceType.IsUnsupported() {
78-
namespaces = append(namespaces, &model.Namespace{
79-
Id: aws.ToString(ns.Id),
80-
Name: aws.ToString(ns.Name),
81-
Type: namespaceType,
82-
})
78+
namespaceType := model.ConvertNamespaceType(ns.Type)
79+
if namespaceType.IsUnsupported() {
80+
continue
81+
}
82+
namespaceMap[aws.ToString(ns.Name)] = &model.Namespace{
83+
Id: aws.ToString(ns.Id),
84+
Name: aws.ToString(ns.Name),
85+
Type: namespaceType,
8386
}
8487
}
8588
}
8689

87-
return namespaces, nil
90+
return namespaceMap, nil
8891
}
8992

90-
func (sdApi *serviceDiscoveryApi) ListServices(ctx context.Context, nsId string) (svcs []*model.Resource, err error) {
93+
func (sdApi *serviceDiscoveryApi) GetServiceIdMap(ctx context.Context, nsId string) (map[string]string, error) {
94+
serviceIdMap := make(map[string]string)
95+
9196
filter := types.ServiceFilter{
9297
Name: types.ServiceFilterNameNamespaceId,
9398
Values: []string{nsId},
9499
}
95100

96101
pages := sd.NewListServicesPaginator(sdApi.awsFacade, &sd.ListServicesInput{Filters: []types.ServiceFilter{filter}})
97-
98102
for pages.HasMorePages() {
99103
output, err := pages.NextPage(ctx)
100104
if err != nil {
101-
return svcs, err
105+
return nil, err
102106
}
103107

104108
for _, svc := range output.Services {
105-
svcs = append(svcs, &model.Resource{
106-
Id: aws.ToString(svc.Id),
107-
Name: aws.ToString(svc.Name),
108-
})
109+
serviceIdMap[aws.ToString(svc.Name)] = aws.ToString(svc.Id)
109110
}
110111
}
111112

112-
return svcs, nil
113+
return serviceIdMap, nil
113114
}
114115

115116
func (sdApi *serviceDiscoveryApi) DiscoverInstances(ctx context.Context, nsName string, svcName string) (insts []types.HttpInstanceSummary, err error) {
@@ -127,8 +128,8 @@ func (sdApi *serviceDiscoveryApi) DiscoverInstances(ctx context.Context, nsName
127128
return out.Instances, nil
128129
}
129130

130-
func (sdApi *serviceDiscoveryApi) ListOperations(ctx context.Context, opFilters []types.OperationFilter) (opStatusMap map[string]types.OperationStatus, err error) {
131-
opStatusMap = make(map[string]types.OperationStatus)
131+
func (sdApi *serviceDiscoveryApi) ListOperations(ctx context.Context, opFilters []types.OperationFilter) (map[string]types.OperationStatus, error) {
132+
opStatusMap := make(map[string]types.OperationStatus)
132133

133134
pages := sd.NewListOperationsPaginator(sdApi.awsFacade, &sd.ListOperationsInput{
134135
Filters: opFilters,
@@ -190,7 +191,7 @@ func (sdApi *serviceDiscoveryApi) CreateService(ctx context.Context, namespace m
190191
}
191192

192193
svcId = aws.ToString(output.Service.Id)
193-
sdApi.log.Info("service created", "svcId", svcId)
194+
sdApi.log.Info("service created", "namespace", namespace.Name, "name", svcName, "id", svcId)
194195
return svcId, nil
195196
}
196197

pkg/cloudmap/api_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
cloudmapMock "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/mocks/pkg/cloudmap"
1010
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/common"
11-
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/model"
1211
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/test"
1312
"github.com/aws/aws-sdk-go-v2/aws"
1413
sd "github.com/aws/aws-sdk-go-v2/service/servicediscovery"
@@ -23,7 +22,7 @@ func TestNewServiceDiscoveryApi(t *testing.T) {
2322
assert.NotNil(t, sdc)
2423
}
2524

26-
func TestServiceDiscoveryApi_ListNamespaces_HappyCase(t *testing.T) {
25+
func TestServiceDiscoveryApi_GetNamespaceMap_HappyCase(t *testing.T) {
2726
mockController := gomock.NewController(t)
2827
defer mockController.Finish()
2928

@@ -39,12 +38,13 @@ func TestServiceDiscoveryApi_ListNamespaces_HappyCase(t *testing.T) {
3938
awsFacade.EXPECT().ListNamespaces(context.TODO(), &sd.ListNamespacesInput{}).
4039
Return(&sd.ListNamespacesOutput{Namespaces: []types.NamespaceSummary{ns}}, nil)
4140

42-
namespaces, _ := sdApi.ListNamespaces(context.TODO())
41+
namespaces, err := sdApi.GetNamespaceMap(context.TODO())
42+
assert.Nil(t, err, "No error for happy case")
4343
assert.True(t, len(namespaces) == 1)
44-
assert.Equal(t, test.GetTestDnsNamespace(), namespaces[0], "No error for happy case")
44+
assert.Equal(t, test.GetTestDnsNamespace(), namespaces[test.DnsNsName])
4545
}
4646

47-
func TestServiceDiscoveryApi_ListNamespaces_SkipPublicDNSNotSupported(t *testing.T) {
47+
func TestServiceDiscoveryApi_GetNamespaceMap_SkipPublicDNSNotSupported(t *testing.T) {
4848
mockController := gomock.NewController(t)
4949
defer mockController.Finish()
5050

@@ -60,11 +60,12 @@ func TestServiceDiscoveryApi_ListNamespaces_SkipPublicDNSNotSupported(t *testing
6060
awsFacade.EXPECT().ListNamespaces(context.TODO(), &sd.ListNamespacesInput{}).
6161
Return(&sd.ListNamespacesOutput{Namespaces: []types.NamespaceSummary{ns}}, nil)
6262

63-
namespaces, _ := sdApi.ListNamespaces(context.TODO())
64-
assert.True(t, len(namespaces) == 0, "Successfully skipped DNS_PUBLIC from the output")
63+
namespaces, err := sdApi.GetNamespaceMap(context.TODO())
64+
assert.Nil(t, err, "No error for happy case")
65+
assert.Empty(t, namespaces, "Successfully skipped DNS_PUBLIC from the output")
6566
}
6667

67-
func TestServiceDiscoveryApi_ListServices_HappyCase(t *testing.T) {
68+
func TestServiceDiscoveryApi_GetServiceIdMap_HappyCase(t *testing.T) {
6869
mockController := gomock.NewController(t)
6970
defer mockController.Finish()
7071

@@ -81,10 +82,10 @@ func TestServiceDiscoveryApi_ListServices_HappyCase(t *testing.T) {
8182
{Id: aws.String(test.SvcId), Name: aws.String(test.SvcName)},
8283
}}, nil)
8384

84-
svcs, err := sdApi.ListServices(context.TODO(), test.HttpNsId)
85+
svcs, err := sdApi.GetServiceIdMap(context.TODO(), test.HttpNsId)
8586
assert.Nil(t, err, "No error for happy case")
8687
assert.True(t, len(svcs) == 1)
87-
assert.Equal(t, svcs[0], &model.Resource{Id: test.SvcId, Name: test.SvcName})
88+
assert.Equal(t, svcs[test.SvcName], test.SvcId)
8889
}
8990

9091
func TestServiceDiscoveryApi_DiscoverInstances_HappyCase(t *testing.T) {
@@ -184,7 +185,8 @@ func TestServiceDiscoveryApi_CreateService_CreateForHttpNamespace(t *testing.T)
184185
},
185186
}, nil)
186187

187-
retSvcId, _ := sdApi.CreateService(context.TODO(), *test.GetTestHttpNamespace(), svcName)
188+
retSvcId, err := sdApi.CreateService(context.TODO(), *test.GetTestHttpNamespace(), svcName)
189+
assert.Nil(t, err)
188190
assert.Equal(t, svcId, retSvcId, "Successfully created service")
189191
}
190192

@@ -212,7 +214,8 @@ func TestServiceDiscoveryApi_CreateService_CreateForDnsNamespace(t *testing.T) {
212214
},
213215
}, nil)
214216

215-
retSvcId, _ := sdApi.CreateService(context.TODO(), *test.GetTestDnsNamespace(), svcName)
217+
retSvcId, err := sdApi.CreateService(context.TODO(), *test.GetTestDnsNamespace(), svcName)
218+
assert.Nil(t, err)
216219
assert.Equal(t, svcId, retSvcId, "Successfully created service")
217220
}
218221

0 commit comments

Comments
 (0)