diff --git a/controllers/gateway_controller.go b/controllers/gateway_controller.go index 0010a92a..47b681fb 100644 --- a/controllers/gateway_controller.go +++ b/controllers/gateway_controller.go @@ -19,7 +19,6 @@ package controllers import ( "context" "fmt" - "github.com/aws/aws-application-networking-k8s/pkg/aws" "github.com/aws/aws-application-networking-k8s/pkg/config" "github.com/aws/aws-application-networking-k8s/pkg/deploy" @@ -251,13 +250,19 @@ func (r *GatewayReconciler) reconcileGatewayResources(ctx context.Context, gw *g return err } - serviceNetworkStatus, err := r.datastore.GetServiceNetworkStatus(gw.Name, config.AccountID) - if err = r.updateGatewayStatus(ctx, &serviceNetworkStatus, gw); err != nil { + snInfo, err := r.cloud.Lattice().FindServiceNetwork(ctx, gw.Name, config.AccountID) + if err != nil { return err } + if snInfo == nil { + return fmt.Errorf("Service network %s for account %s not found", gw.Name, config.AccountID) + } - return nil + if err = r.updateGatewayStatus(ctx, *snInfo.SvcNetwork.Arn, gw); err != nil { + return err + } + return nil } func (r *GatewayReconciler) cleanupGatewayResources(ctx context.Context, gw *gateway_api.Gateway) error { @@ -267,7 +272,7 @@ func (r *GatewayReconciler) cleanupGatewayResources(ctx context.Context, gw *gat func (r *GatewayReconciler) updateGatewayStatus( ctx context.Context, - serviceNetworkStatus *latticestore.ServiceNetwork, + snArn string, gw *gateway_api.Gateway, ) error { gwOld := gw.DeepCopy() @@ -277,15 +282,11 @@ func (r *GatewayReconciler) updateGatewayStatus( Status: metav1.ConditionTrue, ObservedGeneration: gw.Generation, Reason: string(gateway_api.GatewayReasonProgrammed), - Message: fmt.Sprintf("aws-gateway-arn: %s", serviceNetworkStatus.ARN), + Message: fmt.Sprintf("aws-gateway-arn: %s", snArn), }) - // TODO following is causing crash on some platform, see https://t.corp.amazon.com/b7c9ea6c-5168-4616-b718-c1bdf78dbdf1/communication - //gw.Annotations["gateway.networking.k8s.io/aws-gateway-id"] = serviceNetworkStatus.ID - if err := r.client.Status().Patch(ctx, gw, client.MergeFrom(gwOld)); err != nil { - return fmt.Errorf("update gw status error, gw: %s, status: %s, err: %w", - gw.Name, serviceNetworkStatus.Status, err) + return fmt.Errorf("update gw status error, gw: %s, err: %w", gw.Name, err) } return nil } diff --git a/pkg/aws/cloud.go b/pkg/aws/cloud.go index 9878a4b0..5e624b76 100644 --- a/pkg/aws/cloud.go +++ b/pkg/aws/cloud.go @@ -2,7 +2,6 @@ package aws import ( "fmt" - "github.com/aws/aws-application-networking-k8s/pkg/aws/services" "github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog" "github.com/aws/aws-sdk-go/aws/request" @@ -15,8 +14,6 @@ const ( TagManagedBy = TagBase + "ManagedBy" ) -type Tags = map[string]*string - //go:generate mockgen -destination cloud_mocks.go -package aws github.com/aws/aws-application-networking-k8s/pkg/aws Cloud type CloudConfig struct { @@ -31,13 +28,13 @@ type Cloud interface { Lattice() services.Lattice // creates lattice tags with default values populated - DefaultTags() Tags + DefaultTags() services.Tags + + // check if tags map has managedBy tag + ContainsManagedBy(tags services.Tags) bool // check if managedBy tag set for lattice resource IsArnManaged(arn string) (bool, error) - - // check if tags map has managedBy tag - ContainsManagedBy(tags Tags) bool } // NewCloud constructs new Cloud implementation. @@ -92,13 +89,13 @@ func (c *defaultCloud) Config() CloudConfig { return c.cfg } -func (c *defaultCloud) DefaultTags() Tags { - tags := Tags{} +func (c *defaultCloud) DefaultTags() services.Tags { + tags := services.Tags{} tags[TagManagedBy] = &c.managedByTag return tags } -func (c *defaultCloud) ContainsManagedBy(tags Tags) bool { +func (c *defaultCloud) ContainsManagedBy(tags services.Tags) bool { tag, ok := tags[TagManagedBy] if !ok || tag == nil { return false diff --git a/pkg/aws/services/vpclattice.go b/pkg/aws/services/vpclattice.go index adf94a20..2598ce42 100644 --- a/pkg/aws/services/vpclattice.go +++ b/pkg/aws/services/vpclattice.go @@ -5,6 +5,7 @@ import ( "os" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/vpclattice" "github.com/aws/aws-sdk-go/service/vpclattice/vpclatticeiface" @@ -13,6 +14,12 @@ import ( //go:generate mockgen -destination vpclattice_mocks.go -package services github.com/aws/aws-application-networking-k8s/pkg/aws/services Lattice +type Tags = map[string]*string + +type ServiceNetworkInfo struct { + SvcNetwork vpclattice.ServiceNetworkSummary + Tags Tags +} type Lattice interface { vpclatticeiface.VPCLatticeAPI ListServiceNetworksAsList(ctx context.Context, input *vpclattice.ListServiceNetworksInput) ([]*vpclattice.ServiceNetworkSummary, error) @@ -21,6 +28,7 @@ type Lattice interface { ListTargetsAsList(ctx context.Context, input *vpclattice.ListTargetsInput) ([]*vpclattice.TargetSummary, error) ListServiceNetworkVpcAssociationsAsList(ctx context.Context, input *vpclattice.ListServiceNetworkVpcAssociationsInput) ([]*vpclattice.ServiceNetworkVpcAssociationSummary, error) ListServiceNetworkServiceAssociationsAsList(ctx context.Context, input *vpclattice.ListServiceNetworkServiceAssociationsInput) ([]*vpclattice.ServiceNetworkServiceAssociationSummary, error) + FindServiceNetwork(ctx context.Context, name string, accountId string) (*ServiceNetworkInfo, error) } type defaultLattice struct { @@ -169,3 +177,66 @@ func (d *defaultLattice) ListServiceNetworkServiceAssociationsAsList(ctx context return result, nil } + +func (d *defaultLattice) FindServiceNetwork(ctx context.Context, name string, optionalAccountId string) (*ServiceNetworkInfo, error) { + input := vpclattice.ListServiceNetworksInput{} + + for { + + resp, err := d.ListServiceNetworksWithContext(ctx, &input) + if err != nil { + return nil, err + } + + for _, r := range resp.Items { + if aws.StringValue(r.Name) != name { + continue + } + acctIdMatches, err1 := accountIdMatches(optionalAccountId, *r.Arn) + if err1 != nil { + return nil, err1 + } + if !acctIdMatches { + glog.V(6).Infoln("ServiceNetwork found but does not match account id ", name, r.Arn, optionalAccountId) + continue + } + + glog.V(6).Infoln("Found ServiceNetwork ", name, r.Arn, optionalAccountId) + + tagsInput := vpclattice.ListTagsForResourceInput{ + ResourceArn: r.Arn, + } + + tagsOutput, err2 := d.ListTagsForResourceWithContext(ctx, &tagsInput) + if err2 != nil { + return nil, err2 + } + + return &ServiceNetworkInfo{ + SvcNetwork: *r, + Tags: tagsOutput.Tags, + }, nil + } + + if resp.NextToken == nil { + break + } + + input.NextToken = resp.NextToken + } + + return nil, nil +} + +func accountIdMatches(accountId string, itemArn string) (bool, error) { + if accountId == "" { + return true, nil + } + + parsedArn, err := arn.Parse(itemArn) + if err != nil { + return false, err + } + + return accountId == parsedArn.AccountID, nil +} diff --git a/pkg/aws/services/vpclattice_mocks.go b/pkg/aws/services/vpclattice_mocks.go index edd8a0c3..a75e39c7 100644 --- a/pkg/aws/services/vpclattice_mocks.go +++ b/pkg/aws/services/vpclattice_mocks.go @@ -1036,6 +1036,21 @@ func (mr *MockLatticeMockRecorder) DeregisterTargetsWithContext(arg0, arg1 inter return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeregisterTargetsWithContext", reflect.TypeOf((*MockLattice)(nil).DeregisterTargetsWithContext), varargs...) } +// FindServiceNetwork mocks base method. +func (m *MockLattice) FindServiceNetwork(arg0 context.Context, arg1, arg2 string) (*ServiceNetworkInfo, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindServiceNetwork", arg0, arg1, arg2) + ret0, _ := ret[0].(*ServiceNetworkInfo) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// FindServiceNetwork indicates an expected call of FindServiceNetwork. +func (mr *MockLatticeMockRecorder) FindServiceNetwork(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindServiceNetwork", reflect.TypeOf((*MockLattice)(nil).FindServiceNetwork), arg0, arg1, arg2) +} + // GetAccessLogSubscription mocks base method. func (m *MockLattice) GetAccessLogSubscription(arg0 *vpclattice.GetAccessLogSubscriptionInput) (*vpclattice.GetAccessLogSubscriptionOutput, error) { m.ctrl.T.Helper() diff --git a/pkg/aws/services/vpclattice_test.go b/pkg/aws/services/vpclattice_test.go index fc452891..253e7501 100644 --- a/pkg/aws/services/vpclattice_test.go +++ b/pkg/aws/services/vpclattice_test.go @@ -2,10 +2,14 @@ package services import ( "context" + "fmt" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/arn" "testing" "github.com/aws/aws-sdk-go/service/vpclattice" "github.com/golang/mock/gomock" + "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -32,10 +36,10 @@ func Test_defaultLattice_ListServiceNetworksAsList(t *testing.T) { for _, tt := range tests { c := gomock.NewController(t) defer c.Finish() - mockLatticeService := NewMockLattice(c) + mockLattice := NewMockLattice(c) d := &defaultLattice{ - VPCLatticeAPI: mockLatticeService, + VPCLatticeAPI: mockLattice, } input := &vpclattice.ListServiceNetworksInput{ @@ -59,9 +63,9 @@ func Test_defaultLattice_ListServiceNetworksAsList(t *testing.T) { Items: []*vpclattice.ServiceNetworkSummary{sampleMesh}, NextToken: nil, } - mockLatticeService.EXPECT().ListServiceNetworksWithContext(tt.ctx, input).Return(listMeshOutput1, nil) - mockLatticeService.EXPECT().ListServiceNetworksWithContext(tt.ctx, input).Return(listMeshOutput2, nil) - mockLatticeService.EXPECT().ListServiceNetworksWithContext(tt.ctx, input).Return(listMeshOutput3, nil) + mockLattice.EXPECT().ListServiceNetworksWithContext(tt.ctx, input).Return(listMeshOutput1, nil) + mockLattice.EXPECT().ListServiceNetworksWithContext(tt.ctx, input).Return(listMeshOutput2, nil) + mockLattice.EXPECT().ListServiceNetworksWithContext(tt.ctx, input).Return(listMeshOutput3, nil) got, err := d.ListServiceNetworksAsList(tt.ctx, input) assert.Nil(t, err) assert.Equal(t, got, []*vpclattice.ServiceNetworkSummary{sampleMesh, sampleMesh, sampleMesh, sampleMesh, sampleMesh}) @@ -85,10 +89,10 @@ func Test_defaultLattice_ListServicesAsList(t *testing.T) { for _, tt := range tests { c := gomock.NewController(t) defer c.Finish() - mockLatticeService := NewMockLattice(c) + mockLattice := NewMockLattice(c) d := &defaultLattice{ - VPCLatticeAPI: mockLatticeService, + VPCLatticeAPI: mockLattice, } input := &vpclattice.ListServicesInput{ @@ -106,8 +110,8 @@ func Test_defaultLattice_ListServicesAsList(t *testing.T) { Items: []*vpclattice.ServiceSummary{sampleService}, NextToken: nil, } - mockLatticeService.EXPECT().ListServicesWithContext(tt.ctx, input).Return(listOutput1, nil) - mockLatticeService.EXPECT().ListServicesWithContext(tt.ctx, input).Return(listOutput2, nil) + mockLattice.EXPECT().ListServicesWithContext(tt.ctx, input).Return(listOutput1, nil) + mockLattice.EXPECT().ListServicesWithContext(tt.ctx, input).Return(listOutput2, nil) got, err := d.ListServicesAsList(tt.ctx, input) assert.Nil(t, err) assert.Equal(t, got, []*vpclattice.ServiceSummary{sampleService, sampleService}) @@ -131,10 +135,10 @@ func Test_defaultLattice_ListTGsAsList(t *testing.T) { for _, tt := range tests { c := gomock.NewController(t) defer c.Finish() - mockLatticeService := NewMockLattice(c) + mockLattice := NewMockLattice(c) d := &defaultLattice{ - VPCLatticeAPI: mockLatticeService, + VPCLatticeAPI: mockLattice, } input := &vpclattice.ListTargetGroupsInput{ @@ -148,7 +152,7 @@ func Test_defaultLattice_ListTGsAsList(t *testing.T) { Items: []*vpclattice.TargetGroupSummary{sample}, NextToken: nil, } - mockLatticeService.EXPECT().ListTargetGroupsWithContext(tt.ctx, input).Return(listOutput1, nil) + mockLattice.EXPECT().ListTargetGroupsWithContext(tt.ctx, input).Return(listOutput1, nil) got, err := d.ListTargetGroupsAsList(tt.ctx, input) assert.Nil(t, err) @@ -173,10 +177,10 @@ func Test_defaultLattice_ListTargetsAsList(t *testing.T) { for _, tt := range tests { c := gomock.NewController(t) defer c.Finish() - mockLatticeService := NewMockLattice(c) + mockLattice := NewMockLattice(c) d := &defaultLattice{ - VPCLatticeAPI: mockLatticeService, + VPCLatticeAPI: mockLattice, } input := &vpclattice.ListTargetsInput{ @@ -190,7 +194,7 @@ func Test_defaultLattice_ListTargetsAsList(t *testing.T) { Items: []*vpclattice.TargetSummary{sample, sample}, NextToken: nil, } - mockLatticeService.EXPECT().ListTargetsWithContext(tt.ctx, input).Return(listOutput1, nil) + mockLattice.EXPECT().ListTargetsWithContext(tt.ctx, input).Return(listOutput1, nil) got, err := d.ListTargetsAsList(tt.ctx, input) assert.Nil(t, err) @@ -215,10 +219,10 @@ func Test_defaultLattice_ListServiceNetworkVpcAssociationsAsList(t *testing.T) { for _, tt := range tests { c := gomock.NewController(t) defer c.Finish() - mockLatticeService := NewMockLattice(c) + mockLattice := NewMockLattice(c) d := &defaultLattice{ - VPCLatticeAPI: mockLatticeService, + VPCLatticeAPI: mockLattice, } input := &vpclattice.ListServiceNetworkVpcAssociationsInput{ @@ -232,7 +236,7 @@ func Test_defaultLattice_ListServiceNetworkVpcAssociationsAsList(t *testing.T) { Items: []*vpclattice.ServiceNetworkVpcAssociationSummary{sample}, NextToken: nil, } - mockLatticeService.EXPECT().ListServiceNetworkVpcAssociationsWithContext(tt.ctx, input).Return(listOutput1, nil) + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsWithContext(tt.ctx, input).Return(listOutput1, nil) got, err := d.ListServiceNetworkVpcAssociationsAsList(tt.ctx, input) assert.Nil(t, err) @@ -257,10 +261,10 @@ func Test_defaultLattice_ListServiceNetworkServiceAssociationsAsList(t *testing. for _, tt := range tests { c := gomock.NewController(t) defer c.Finish() - mockLatticeService := NewMockLattice(c) + mockLattice := NewMockLattice(c) d := &defaultLattice{ - VPCLatticeAPI: mockLatticeService, + VPCLatticeAPI: mockLattice, } input := &vpclattice.ListServiceNetworkServiceAssociationsInput{ @@ -272,10 +276,211 @@ func Test_defaultLattice_ListServiceNetworkServiceAssociationsAsList(t *testing. Items: []*vpclattice.ServiceNetworkServiceAssociationSummary{}, NextToken: nil, } - mockLatticeService.EXPECT().ListServiceNetworkServiceAssociationsWithContext(tt.ctx, input).Return(listOutput1, nil) + mockLattice.EXPECT().ListServiceNetworkServiceAssociationsWithContext(tt.ctx, input).Return(listOutput1, nil) got, err := d.ListServiceNetworkServiceAssociationsAsList(tt.ctx, input) assert.Nil(t, err) assert.Equal(t, got, []*vpclattice.ServiceNetworkServiceAssociationSummary{}) } } + +func getTestArn(accountId string) string { + //arn::vpc-lattice:::/ + return fmt.Sprintf("arn:aws:vpc-lattice:region:%s:resource/id", accountId) +} + +func Test_defaultLattice_FindServiceNetwork_happyPath(t *testing.T) { + ctx := context.TODO() + c := gomock.NewController(t) + mockLattice := NewMockLattice(c) + d := &defaultLattice{VPCLatticeAPI: mockLattice} + + snName := "sn-name" + acctId := "123456" + arn := getTestArn(acctId) + + item := &vpclattice.ServiceNetworkSummary{ + Name: &snName, + Arn: &arn, + Id: aws.String("id"), + } + + listOutput := &vpclattice.ListServiceNetworksOutput{ + Items: []*vpclattice.ServiceNetworkSummary{item}, + NextToken: nil, + } + + mockLattice.EXPECT().ListServiceNetworksWithContext(gomock.Any(), gomock.Any()).Return(listOutput, nil).AnyTimes() + mockLattice.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return( + &vpclattice.ListTagsForResourceOutput{}, nil).AnyTimes() + + itemFound, err1 := d.FindServiceNetwork(ctx, snName, acctId) + assert.Nil(t, err1) + assert.NotNil(t, itemFound) + assert.Equal(t, snName, *itemFound.SvcNetwork.Name) + + emptyAccountItemFound, err2 := d.FindServiceNetwork(ctx, snName, "") + assert.Nil(t, err2) + assert.NotNil(t, emptyAccountItemFound) + assert.Equal(t, snName, *emptyAccountItemFound.SvcNetwork.Name) + + mismatchedAccountId := "555555" + itemNotFound, err3 := d.FindServiceNetwork(ctx, snName, mismatchedAccountId) + assert.Nil(t, err3) + assert.Nil(t, itemNotFound) +} + +func Test_defaultLattice_FindServiceNetwork_disambiguateByAccount(t *testing.T) { + ctx := context.TODO() + c := gomock.NewController(t) + mockLattice := NewMockLattice(c) + d := &defaultLattice{VPCLatticeAPI: mockLattice} + + acct1 := "12345" + acct2 := "88888" + + listOutput := &vpclattice.ListServiceNetworksOutput{ + Items: []*vpclattice.ServiceNetworkSummary{ + { + Arn: aws.String(getTestArn(acct1)), + Name: aws.String("duplicated-name"), + Id: aws.String("id"), + }, + { + Arn: aws.String(getTestArn(acct2)), + Name: aws.String("duplicated-name"), + Id: aws.String("id"), + }, + }, + NextToken: nil, + } + + mockLattice.EXPECT().ListServiceNetworksWithContext(gomock.Any(), gomock.Any()).Return( + listOutput, nil).AnyTimes() + mockLattice.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return( + &vpclattice.ListTagsForResourceOutput{ + Tags: map[string]*string{ + "foo": aws.String("bar"), + }, + }, nil).AnyTimes() + + item1, err1 := d.FindServiceNetwork(ctx, "duplicated-name", acct1) + assert.Nil(t, err1) + assert.NotNil(t, item1) + arn1, _ := arn.Parse(*item1.SvcNetwork.Arn) + assert.Equal(t, acct1, arn1.AccountID) + // make sure tags come back too + assert.Equal(t, "bar", *item1.Tags["foo"]) + + item2, err2 := d.FindServiceNetwork(ctx, "duplicated-name", acct2) + assert.Nil(t, err2) + assert.NotNil(t, item2) + arn2, _ := arn.Parse(*item2.SvcNetwork.Arn) + assert.Equal(t, acct2, arn2.AccountID) + + // will just return the first item it finds - is NOT predictable but doesn't fail + emptyAcctItem, err3 := d.FindServiceNetwork(ctx, "duplicated-name", "") + assert.Nil(t, err3) + assert.NotNil(t, emptyAcctItem) +} + +func Test_defaultLattice_FindServiceNetwork_noResults(t *testing.T) { + ctx := context.TODO() + c := gomock.NewController(t) + mockLattice := NewMockLattice(c) + d := &defaultLattice{VPCLatticeAPI: mockLattice} + + mockLattice.EXPECT().ListServiceNetworksWithContext(gomock.Any(), gomock.Any()).Return( + &vpclattice.ListServiceNetworksOutput{}, nil).AnyTimes() + mockLattice.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return( + &vpclattice.ListTagsForResourceOutput{}, nil).AnyTimes() + + item, err := d.FindServiceNetwork(ctx, "foo", "1234") + assert.Nil(t, err) + assert.Nil(t, item) +} + +func Test_defaultLattice_FindServiceNetwork_manyResults(t *testing.T) { + ctx := context.TODO() + c := gomock.NewController(t) + mockLattice := NewMockLattice(c) + d := &defaultLattice{VPCLatticeAPI: mockLattice} + + one := "1" + two := "2" + tokens := []*string{&one, &two, nil} + results := [][]*vpclattice.ServiceNetworkSummary{{}, {}, {}} + + for i, _ := range results { + for j := 1; j <= 5; j++ { + // ids will be 11 - 15, 21 - 21, 31 - 35 + id := fmt.Sprintf("%d%d", i+1, j) + results[i] = append(results[i], + &vpclattice.ServiceNetworkSummary{ + Arn: aws.String(getTestArn("1111" + id)), + Name: aws.String("name-" + id), + Id: aws.String("id-" + id), + }) + } + } + + rIdx := 0 + + mockLattice.EXPECT().ListServiceNetworksWithContext(gomock.Any(), gomock.Any()).DoAndReturn( + func(_ context.Context, req *vpclattice.ListServiceNetworksInput, _ ...interface{}) (*vpclattice.ListServiceNetworksOutput, error) { + result := + &vpclattice.ListServiceNetworksOutput{ + Items: results[rIdx], + NextToken: tokens[rIdx], + } + rIdx = (rIdx + 1) % len(results) + return result, nil + }).AnyTimes() + mockLattice.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return( + &vpclattice.ListTagsForResourceOutput{}, nil).AnyTimes() + + page3item, err3 := d.FindServiceNetwork(ctx, "name-35", "111135") + assert.Nil(t, err3) + assert.NotNil(t, page3item) + assert.Equal(t, "name-35", *page3item.SvcNetwork.Name) + + page1item, err1 := d.FindServiceNetwork(ctx, "name-13", "111113") + assert.Nil(t, err1) + assert.NotNil(t, page1item) + assert.Equal(t, "name-13", *page1item.SvcNetwork.Name) + + page2item, err2 := d.FindServiceNetwork(ctx, "name-21", "111121") + assert.Nil(t, err2) + assert.NotNil(t, page2item) + assert.Equal(t, "name-21", *page2item.SvcNetwork.Name) +} + +func Test_defaultLattice_FindServiceNetwork_errorsRaised(t *testing.T) { + ctx := context.TODO() + c := gomock.NewController(t) + mockLattice := NewMockLattice(c) + d := &defaultLattice{VPCLatticeAPI: mockLattice} + + mockLattice.EXPECT().ListServiceNetworksWithContext(gomock.Any(), gomock.Any()).Return( + nil, errors.Errorf("LIST_ERR")).Times(1) + + _, listErr := d.FindServiceNetwork(ctx, "foo", "1234") + assert.NotNil(t, listErr) + + mockLattice.EXPECT().ListServiceNetworksWithContext(gomock.Any(), gomock.Any()).Return( + &vpclattice.ListServiceNetworksOutput{ + Items: []*vpclattice.ServiceNetworkSummary{ + { + Arn: aws.String(getTestArn("1234")), + Name: aws.String("foo"), + Id: aws.String("id"), + }, + }, + NextToken: nil, + }, nil).Times(1) + mockLattice.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return( + nil, errors.Errorf("TAG_ERR")).Times(1) + + _, tagErr := d.FindServiceNetwork(ctx, "foo", "1234") + assert.NotNil(t, tagErr) +} diff --git a/pkg/deploy/lattice/service_manager.go b/pkg/deploy/lattice/service_manager.go index a1d721e9..86e67504 100644 --- a/pkg/deploy/lattice/service_manager.go +++ b/pkg/deploy/lattice/service_manager.go @@ -79,13 +79,17 @@ func (m *defaultServiceManager) createServiceAndAssociate(ctx context.Context, s } func (m *defaultServiceManager) createAssociation(ctx context.Context, svcId *string, snName string) error { - sn, err := m.datastore.GetServiceNetworkStatus(snName, m.cloud.Config().AccountId) + snInfo, err := m.cloud.Lattice().FindServiceNetwork(ctx, snName, m.cloud.Config().AccountId) if err != nil { return err } + if snInfo == nil { + return fmt.Errorf("Service network %s for account %s not found", snName, m.cloud.Config().AccountId) + } + assocReq := &CreateSnSvcAssocReq{ ServiceIdentifier: svcId, - ServiceNetworkIdentifier: aws.String(sn.ID), + ServiceNetworkIdentifier: snInfo.SvcNetwork.Id, Tags: m.cloud.DefaultTags(), } assocResp, err := m.cloud.Lattice().CreateServiceNetworkServiceAssociationWithContext(ctx, assocReq) diff --git a/pkg/deploy/lattice/service_manager_test.go b/pkg/deploy/lattice/service_manager_test.go index 4144ccbe..457c3419 100644 --- a/pkg/deploy/lattice/service_manager_test.go +++ b/pkg/deploy/lattice/service_manager_test.go @@ -40,9 +40,7 @@ func TestServiceManagerInteg(t *testing.T) { }, } - ds.AddServiceNetwork("sn", cfg.AccountId, "sn-arn", "sn-id", "sn-status") - - // service does not exists in lattice + // service does not exist in lattice lat.EXPECT(). ListServicesAsList(gomock.Any(), gomock.Any()). Return([]*SvcSummary{}, nil) @@ -74,6 +72,22 @@ func TestServiceManagerInteg(t *testing.T) { }). Times(1) + // expect a call to find the service network + lat.EXPECT(). + FindServiceNetwork(gomock.Any(), gomock.Any(), gomock.Any()). + DoAndReturn( + func(ctx context.Context, name string, accountId string) (*services.ServiceNetworkInfo, error) { + return &services.ServiceNetworkInfo{ + SvcNetwork: vpclattice.ServiceNetworkSummary{ + Arn: aws.String("sn-arn"), + Id: aws.String("sn-id"), + Name: aws.String(name), + }, + Tags: nil, + }, nil + }). + Times(1) + status, err := m.Create(ctx, svc) assert.Nil(t, err) assert.Equal(t, "arn", status.Arn) @@ -99,11 +113,6 @@ func TestServiceManagerInteg(t *testing.T) { }, } - // populate storage with managed sn's - for _, sn := range []string{snKeep, snDelete, snAdd} { - ds.AddServiceNetwork(sn, cfg.AccountId, sn+"-arn", sn+"-id", sn+"-status") - } - // service exists in lattice lat.EXPECT(). ListServicesAsList(gomock.Any(), gomock.Any()). @@ -164,6 +173,22 @@ func TestServiceManagerInteg(t *testing.T) { }). Times(1) + // expect calls to find the service network + lat.EXPECT(). + FindServiceNetwork(gomock.Any(), gomock.Any(), gomock.Any()). + DoAndReturn( + func(ctx context.Context, name string, accountId string) (*services.ServiceNetworkInfo, error) { + return &services.ServiceNetworkInfo{ + SvcNetwork: vpclattice.ServiceNetworkSummary{ + Arn: aws.String(name + "-arn"), + Id: aws.String(name + "-id"), + Name: aws.String(name), + }, + Tags: nil, + }, nil + }). + AnyTimes() + status, err := m.Create(ctx, svc) assert.Nil(t, err) assert.Equal(t, "svc-arn", status.Arn) diff --git a/pkg/deploy/lattice/service_network_manager.go b/pkg/deploy/lattice/service_network_manager.go index 5f70b99f..3fe9667d 100644 --- a/pkg/deploy/lattice/service_network_manager.go +++ b/pkg/deploy/lattice/service_network_manager.go @@ -52,7 +52,7 @@ type defaultServiceNetworkManager struct { // CreateServiceNetworkVpcAssociationInput returns ServiceNetworkVpcAssociationStatusFailed/ServiceNetworkVpcAssociationStatusCreateInProgress/MeshVpcAssociationStatusDeleteInProgress func (m *defaultServiceNetworkManager) Create(ctx context.Context, service_network *latticemodel.ServiceNetwork) (latticemodel.ServiceNetworkStatus, error) { // check if exists - service_networkSummary, err := m.findServiceNetworkByName(ctx, service_network.Spec.Name) + service_networkSummary, err := m.cloud.Lattice().FindServiceNetwork(ctx, service_network.Spec.Name, "") if err != nil { return latticemodel.ServiceNetworkStatus{ServiceNetworkARN: "", ServiceNetworkID: ""}, err } @@ -88,8 +88,8 @@ func (m *defaultServiceNetworkManager) Create(ctx context.Context, service_netwo } else { glog.V(6).Infof("service_network[%v] exists, further check association", service_network) - service_networkID = aws.StringValue(service_networkSummary.snSummary.Id) - service_networkArn = aws.StringValue(service_networkSummary.snSummary.Arn) + service_networkID = aws.StringValue(service_networkSummary.SvcNetwork.Id) + service_networkArn = aws.StringValue(service_networkSummary.SvcNetwork.Arn) isServiceNetworkAssociatedWithVPC, service_networkAssociatedWithCurrentVPCId, _, err = m.isServiceNetworkAssociatedWithVPC(ctx, service_networkID) if err != nil { return latticemodel.ServiceNetworkStatus{ServiceNetworkARN: "", ServiceNetworkID: ""}, err @@ -171,7 +171,7 @@ func (m *defaultServiceNetworkManager) List(ctx context.Context) ([]string, erro } func (m *defaultServiceNetworkManager) Delete(ctx context.Context, service_network string) error { - service_networkSummary, err := m.findServiceNetworkByName(ctx, service_network) + service_networkSummary, err := m.cloud.Lattice().FindServiceNetwork(ctx, service_network, "") if err != nil { return err } @@ -182,7 +182,7 @@ func (m *defaultServiceNetworkManager) Delete(ctx context.Context, service_netwo } vpcLatticeSess := m.cloud.Lattice() - service_networkID := aws.StringValue(service_networkSummary.snSummary.Id) + service_networkID := aws.StringValue(service_networkSummary.SvcNetwork.Id) _, service_networkAssociatedWithCurrentVPCId, assocResp, err := m.isServiceNetworkAssociatedWithVPC(ctx, service_networkID) if err != nil { @@ -208,9 +208,8 @@ func (m *defaultServiceNetworkManager) Delete(ctx context.Context, service_netwo // check if this VPC is the one created the service network needToDelete := false - if service_networkSummary.snTags != nil && service_networkSummary.snTags.Tags != nil { - snTags := service_networkSummary.snTags - vpcOwner, ok := snTags.Tags[latticemodel.K8SServiceNetworkOwnedByVPC] + if service_networkSummary.Tags != nil { + vpcOwner, ok := service_networkSummary.Tags[latticemodel.K8SServiceNetworkOwnedByVPC] if ok && *vpcOwner == config.VpcID { needToDelete = true } else { @@ -248,40 +247,6 @@ func (m *defaultServiceNetworkManager) Delete(ctx context.Context, service_netwo } } -// Find service_network by name return service_network,err if service_network exists, otherwise return nil, nil. -func (m *defaultServiceNetworkManager) findServiceNetworkByName(ctx context.Context, targetServiceNetwork string) (*serviceNetworkOutput, error) { - vpcLatticeSess := m.cloud.Lattice() - service_networkListInput := vpclattice.ListServiceNetworksInput{} - resp, err := vpcLatticeSess.ListServiceNetworksAsList(ctx, &service_networkListInput) - if err == nil { - for _, r := range resp { - if aws.StringValue(r.Name) == targetServiceNetwork { - glog.V(6).Infoln("Found ServiceNetwork named ", targetServiceNetwork) - - tagsInput := vpclattice.ListTagsForResourceInput{ - ResourceArn: r.Arn, - } - tagsOutput, err := vpcLatticeSess.ListTagsForResourceWithContext(ctx, &tagsInput) - - if err != nil { - tagsOutput = nil - } - - snOutput := serviceNetworkOutput{ - snSummary: r, - snTags: tagsOutput, - } - - // treat err as no tag - return &snOutput, nil - } - } - return nil, err - } else { - return nil, err - } -} - // If service_network exists, check if service_network has already associated with VPC func (m *defaultServiceNetworkManager) isServiceNetworkAssociatedWithVPC(ctx context.Context, service_networkID string) (bool, *string, []*vpclattice.ServiceNetworkVpcAssociationSummary, error) { vpcLatticeSess := m.cloud.Lattice() diff --git a/pkg/deploy/lattice/service_network_manager_test.go b/pkg/deploy/lattice/service_network_manager_test.go index e016535f..643e0555 100644 --- a/pkg/deploy/lattice/service_network_manager_test.go +++ b/pkg/deploy/lattice/service_network_manager_test.go @@ -32,8 +32,6 @@ func Test_CreateServiceNetwork_MeshNotExist_NoNeedToAssociate(t *testing.T) { Id: &id, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{} - createServiceNetworkInput := &vpclattice.CreateServiceNetworkInput{ Name: &name, Tags: make(map[string]*string), @@ -44,10 +42,12 @@ func Test_CreateServiceNetwork_MeshNotExist_NoNeedToAssociate(t *testing.T) { defer c.Finish() ctx := context.TODO() mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess := mocks.NewMockLattice(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkWithContext(ctx, createServiceNetworkInput).Return(meshCreateOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice := mocks.NewMockLattice(c) + mockLattice.EXPECT().CreateServiceNetworkWithContext(ctx, createServiceNetworkInput).Return(meshCreateOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() + + mockLattice.EXPECT(). + FindServiceNetwork(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1) meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &meshCreateInput) @@ -74,7 +74,6 @@ func Test_CreateServiceNetwork_MeshNotExist_NeedToAssociate(t *testing.T) { Id: &id, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{} createServiceNetworkInput := &vpclattice.CreateServiceNetworkInput{ Name: &name, @@ -86,9 +85,9 @@ func Test_CreateServiceNetwork_MeshNotExist_NeedToAssociate(t *testing.T) { defer c.Finish() ctx := context.TODO() mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess := mocks.NewMockLattice(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkWithContext(ctx, createServiceNetworkInput).Return(meshCreateOutput, nil) + mockLattice := mocks.NewMockLattice(c) + + mockLattice.EXPECT().CreateServiceNetworkWithContext(ctx, createServiceNetworkInput).Return(meshCreateOutput, nil) meshId := "12345678912345678912" createServiceNetworkVpcAssociationInput := &vpclattice.CreateServiceNetworkVpcAssociationInput{ ServiceNetworkIdentifier: &meshId, @@ -98,8 +97,14 @@ func Test_CreateServiceNetwork_MeshNotExist_NeedToAssociate(t *testing.T) { createServiceNetworkVPCAssociationOutput := &vpclattice.CreateServiceNetworkVpcAssociationOutput{ Status: &associationStatus, } - mockVpcLatticeSess.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT(). + CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput). + Return(createServiceNetworkVPCAssociationOutput, nil) + + mockLattice.EXPECT(). + FindServiceNetwork(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1) + + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &meshCreateInput) @@ -118,23 +123,15 @@ func Test_CreateServiceNetwork_ListFailed(t *testing.T) { }, Status: &latticemodel.ServiceNetworkStatus{ServiceNetworkARN: "", ServiceNetworkID: ""}, } - arn := "12345678912345678912" - id := "12345678912345678912" - name := "test" - item := vpclattice.ServiceNetworkSummary{ - Arn: &arn, - Id: &id, - Name: &name, - } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&item} c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, errors.New("ERROR")) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess) + + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return(nil, errors.New("ERROR")) + mockCloud.EXPECT().Lattice().Return(mockLattice) meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &meshCreateInput) @@ -165,7 +162,6 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_ServiceNetworkVpcAssociationStat Id: &meshId, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&item} status := vpclattice.ServiceNetworkVpcAssociationStatusCreateInProgress items := vpclattice.ServiceNetworkVpcAssociationSummary{ @@ -180,12 +176,15 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_ServiceNetworkVpcAssociationStat c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(nil, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: nil, + }, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &meshCreateInput) @@ -215,7 +214,6 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_ServiceNetworkVpcAssociationStat Id: &meshId, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&item} status := vpclattice.ServiceNetworkVpcAssociationStatusDeleteInProgress items := vpclattice.ServiceNetworkVpcAssociationSummary{ @@ -230,12 +228,15 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_ServiceNetworkVpcAssociationStat c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(nil, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: nil, + }, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &meshCreateInput) @@ -264,7 +265,6 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_ServiceNetworkVpcAssociationStat Id: &meshId, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&item} status := vpclattice.ServiceNetworkVpcAssociationStatusActive items := vpclattice.ServiceNetworkVpcAssociationSummary{ @@ -279,12 +279,15 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_ServiceNetworkVpcAssociationStat c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(nil, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: nil, + }, nil) + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &meshCreateInput) @@ -311,7 +314,6 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_AssociateToNotAssociate(t *testi Id: &meshId, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&item} status := vpclattice.ServiceNetworkVpcAssociationStatusActive items := vpclattice.ServiceNetworkVpcAssociationSummary{ @@ -326,17 +328,20 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_AssociateToNotAssociate(t *testi c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(nil, nil) + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: nil, + }, nil) + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) deleteInProgressStatus := vpclattice.ServiceNetworkVpcAssociationStatusDeleteInProgress deleteServiceNetworkVpcAssociationOutput := &vpclattice.DeleteServiceNetworkVpcAssociationOutput{Status: &deleteInProgressStatus} - mockVpcLatticeSess.EXPECT().DeleteServiceNetworkVpcAssociationWithContext(ctx, gomock.Any()).Return(deleteServiceNetworkVpcAssociationOutput, nil) + mockLattice.EXPECT().DeleteServiceNetworkVpcAssociationWithContext(ctx, gomock.Any()).Return(deleteServiceNetworkVpcAssociationOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) _, err := meshManager.Create(ctx, &meshCreateInput) @@ -364,7 +369,6 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_ServiceNetworkVpcAssociationStat Id: &meshId, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&item} status := vpclattice.ServiceNetworkVpcAssociationStatusCreateFailed items := vpclattice.ServiceNetworkVpcAssociationSummary{ @@ -388,17 +392,20 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_ServiceNetworkVpcAssociationStat c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) snTagsOuput := &vpclattice.ListTagsForResourceOutput{ Tags: make(map[string]*string), } snTagsOuput.Tags[latticemodel.K8SServiceNetworkOwnedByVPC] = &config.VpcID - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(snTagsOuput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: snTagsOuput.Tags, + }, nil) + mockLattice.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &meshCreateInput) @@ -427,7 +434,6 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_MeshAssociatedWithOtherVPC(t *te Id: &meshId, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&item} status := vpclattice.ServiceNetworkVpcAssociationStatusCreateFailed items := vpclattice.ServiceNetworkVpcAssociationSummary{ @@ -451,18 +457,21 @@ func Test_CreateServiceNetwork_MeshAlreadyExist_MeshAssociatedWithOtherVPC(t *te c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) snTagsOuput := &vpclattice.ListTagsForResourceOutput{ Tags: make(map[string]*string), } dummy_vpc := "dummy-vpc-id" snTagsOuput.Tags[latticemodel.K8SServiceNetworkOwnedByVPC] = &dummy_vpc - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(snTagsOuput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: snTagsOuput.Tags, + }, nil) + mockLattice.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &meshCreateInput) @@ -486,7 +495,6 @@ func Test_CreateServiceNetwork_MeshNotExist_ServiceNetworkVpcAssociationStatusFa meshArn := "12345678912345678912" name := "test" - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{} associationStatus := vpclattice.ServiceNetworkVpcAssociationStatusCreateFailed createServiceNetworkVPCAssociationOutput := &vpclattice.CreateServiceNetworkVpcAssociationOutput{ Status: &associationStatus, @@ -510,12 +518,12 @@ func Test_CreateServiceNetwork_MeshNotExist_ServiceNetworkVpcAssociationStatusFa c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkWithContext(ctx, meshCreateInput).Return(meshCreateOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return(nil, nil) + mockLattice.EXPECT().CreateServiceNetworkWithContext(ctx, meshCreateInput).Return(meshCreateOutput, nil) + mockLattice.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &CreateInput) @@ -539,7 +547,6 @@ func Test_CreateServiceNetwork_MeshNOTExist_ServiceNetworkVpcAssociationStatusCr meshId := "12345678912345678912" meshArn := "12345678912345678912" name := "test" - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{} associationStatus := vpclattice.ServiceNetworkVpcAssociationStatusCreateInProgress createServiceNetworkVPCAssociationOutput := &vpclattice.CreateServiceNetworkVpcAssociationOutput{ Status: &associationStatus, @@ -562,12 +569,12 @@ func Test_CreateServiceNetwork_MeshNOTExist_ServiceNetworkVpcAssociationStatusCr c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkWithContext(ctx, meshCreateInput).Return(meshCreateOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return(nil, nil) + mockLattice.EXPECT().CreateServiceNetworkWithContext(ctx, meshCreateInput).Return(meshCreateOutput, nil) + mockLattice.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &CreateInput) @@ -591,7 +598,6 @@ func Test_CreateServiceNetwork_MeshNotExist_ServiceNetworkVpcAssociationStatusDe meshId := "12345678912345678912" meshArn := "12345678912345678912" name := "test" - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{} associationStatus := vpclattice.ServiceNetworkVpcAssociationStatusDeleteInProgress createServiceNetworkVPCAssociationOutput := &vpclattice.CreateServiceNetworkVpcAssociationOutput{ Status: &associationStatus, @@ -614,12 +620,12 @@ func Test_CreateServiceNetwork_MeshNotExist_ServiceNetworkVpcAssociationStatusDe c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkWithContext(ctx, meshCreateInput).Return(meshCreateOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return(nil, nil) + mockLattice.EXPECT().CreateServiceNetworkWithContext(ctx, meshCreateInput).Return(meshCreateOutput, nil) + mockLattice.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &CreateInput) @@ -643,7 +649,6 @@ func Test_CreateServiceNetwork_MeshNotExist_ServiceNetworkVpcAssociationReturnsE meshId := "12345678912345678912" meshArn := "12345678912345678912" name := "test" - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{} createServiceNetworkVPCAssociationOutput := &vpclattice.CreateServiceNetworkVpcAssociationOutput{} meshCreateOutput := &vpclattice.CreateServiceNetworkOutput{ Arn: &meshArn, @@ -663,12 +668,12 @@ func Test_CreateServiceNetwork_MeshNotExist_ServiceNetworkVpcAssociationReturnsE c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkWithContext(ctx, meshCreateInput).Return(meshCreateOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, errors.New("ERROR")) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return(nil, nil) + mockLattice.EXPECT().CreateServiceNetworkWithContext(ctx, meshCreateInput).Return(meshCreateOutput, nil) + mockLattice.EXPECT().CreateServiceNetworkVpcAssociationWithContext(ctx, createServiceNetworkVpcAssociationInput).Return(createServiceNetworkVPCAssociationOutput, errors.New("ERROR")) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &CreateInput) @@ -696,7 +701,6 @@ func Test_CreateMesh_MeshNotExist_MeshCreateFailed(t *testing.T) { Id: &id, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{} meshCreateInput := &vpclattice.CreateServiceNetworkInput{ Name: &name, Tags: make(map[string]*string), @@ -707,11 +711,11 @@ func Test_CreateMesh_MeshNotExist_MeshCreateFailed(t *testing.T) { c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().CreateServiceNetworkWithContext(ctx, meshCreateInput).Return(meshCreateOutput, errors.New("ERROR")) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return(nil, nil) + mockLattice.EXPECT().CreateServiceNetworkWithContext(ctx, meshCreateInput).Return(meshCreateOutput, errors.New("ERROR")) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) resp, err := meshManager.Create(ctx, &CreateInput) @@ -723,15 +727,14 @@ func Test_CreateMesh_MeshNotExist_MeshCreateFailed(t *testing.T) { } func Test_DeleteMesh_MeshNotExist(t *testing.T) { - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{} c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess) + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return(nil, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice) meshManager := NewDefaultServiceNetworkManager(mockCloud) err := meshManager.Delete(ctx, "test") @@ -739,7 +742,7 @@ func Test_DeleteMesh_MeshNotExist(t *testing.T) { assert.Nil(t, err) } -// delte a service network, which has no association and also was created by this VPC +// delete a service network, which has no association and also was created by this VPC func Test_DeleteMesh_MeshExistsNoAssociation(t *testing.T) { arn := "123456789" id := "123456789" @@ -749,7 +752,6 @@ func Test_DeleteMesh_MeshExistsNoAssociation(t *testing.T) { Id: &id, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&item} statusServiceNetworkVPCOutput := []*vpclattice.ServiceNetworkVpcAssociationSummary{} @@ -759,18 +761,21 @@ func Test_DeleteMesh_MeshExistsNoAssociation(t *testing.T) { c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) snTagsOuput := &vpclattice.ListTagsForResourceOutput{ Tags: make(map[string]*string), } snTagsOuput.Tags[latticemodel.K8SServiceNetworkOwnedByVPC] = &config.VpcID - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(snTagsOuput, nil) - mockVpcLatticeSess.EXPECT().DeleteServiceNetworkWithContext(ctx, deleteMeshInout).Return(deleteMeshOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: snTagsOuput.Tags, + }, nil) + mockLattice.EXPECT().DeleteServiceNetworkWithContext(ctx, deleteMeshInout).Return(deleteMeshOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) err := meshManager.Delete(ctx, "test") @@ -785,12 +790,11 @@ func Test_DeleteMesh_MeshExistsAssociatedWithVPC_Deleting(t *testing.T) { arn := "123456789" id := "123456789" name := "test" - itemMesh := vpclattice.ServiceNetworkSummary{ + item := vpclattice.ServiceNetworkSummary{ Arn: &arn, Id: &id, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&itemMesh} associationArn := "123456789" associationID := "123456789" @@ -814,18 +818,21 @@ func Test_DeleteMesh_MeshExistsAssociatedWithVPC_Deleting(t *testing.T) { c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) snTagsOuput := &vpclattice.ListTagsForResourceOutput{ Tags: make(map[string]*string), } snTagsOuput.Tags[latticemodel.K8SServiceNetworkOwnedByVPC] = &config.VpcID - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(snTagsOuput, nil) - mockVpcLatticeSess.EXPECT().DeleteServiceNetworkVpcAssociationWithContext(ctx, deleteServiceNetworkVpcAssociationInput).Return(deleteServiceNetworkVpcAssociationOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: snTagsOuput.Tags, + }, nil) + mockLattice.EXPECT().DeleteServiceNetworkVpcAssociationWithContext(ctx, deleteServiceNetworkVpcAssociationInput).Return(deleteServiceNetworkVpcAssociationOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) err := meshManager.Delete(ctx, "test") @@ -838,12 +845,11 @@ func Test_DeleteMesh_MeshExistsAssociatedWithOtherVPC(t *testing.T) { arn := "123456789" id := "123456789" name := "test" - itemMesh := vpclattice.ServiceNetworkSummary{ + item := vpclattice.ServiceNetworkSummary{ Arn: &arn, Id: &id, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&itemMesh} associationArn := "123456789" associationID := "123456789" @@ -863,17 +869,20 @@ func Test_DeleteMesh_MeshExistsAssociatedWithOtherVPC(t *testing.T) { c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) snTagsOuput := &vpclattice.ListTagsForResourceOutput{ Tags: make(map[string]*string), } snTagsOuput.Tags[latticemodel.K8SServiceNetworkOwnedByVPC] = &config.VpcID - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(snTagsOuput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: snTagsOuput.Tags, + }, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) err := meshManager.Delete(ctx, "test") @@ -886,12 +895,11 @@ func Test_DeleteMesh_MeshExistsAssociatedWithOtherVPC_NotCreatedByVPC(t *testing arn := "123456789" id := "123456789" name := "test" - itemMesh := vpclattice.ServiceNetworkSummary{ + item := vpclattice.ServiceNetworkSummary{ Arn: &arn, Id: &id, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&itemMesh} associationArn := "123456789" associationID := "123456789" @@ -911,12 +919,15 @@ func Test_DeleteMesh_MeshExistsAssociatedWithOtherVPC_NotCreatedByVPC(t *testing c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(nil, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: nil, + }, nil) + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) err := meshManager.Delete(ctx, "test") @@ -928,12 +939,11 @@ func Test_DeleteMesh_MeshExistsAssociatedWithOtherVPC_CreatedByVPC(t *testing.T) arn := "123456789" id := "123456789" name := "test" - itemMesh := vpclattice.ServiceNetworkSummary{ + item := vpclattice.ServiceNetworkSummary{ Arn: &arn, Id: &id, Name: &name, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&itemMesh} associationArn := "123456789" associationID := "123456789" @@ -953,17 +963,19 @@ func Test_DeleteMesh_MeshExistsAssociatedWithOtherVPC_CreatedByVPC(t *testing.T) c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockVpcLatticeSess.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) + mockLattice.EXPECT().ListServiceNetworkVpcAssociationsAsList(ctx, gomock.Any()).Return(statusServiceNetworkVPCOutput, nil) snTagsOutput := &vpclattice.ListTagsForResourceOutput{ Tags: make(map[string]*string), } snTagsOutput.Tags[latticemodel.K8SServiceNetworkOwnedByVPC] = &config.VpcID - mockVpcLatticeSess.EXPECT().ListTagsForResourceWithContext(ctx, gomock.Any()).Return(snTagsOutput, nil) - - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + mockLattice.EXPECT().FindServiceNetwork(ctx, gomock.Any(), gomock.Any()).Return( + &mocks.ServiceNetworkInfo{ + SvcNetwork: item, + Tags: snTagsOutput.Tags, + }, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes() meshManager := NewDefaultServiceNetworkManager(mockCloud) err := meshManager.Delete(ctx, "test") @@ -976,26 +988,26 @@ func Test_ListMesh_MeshExists(t *testing.T) { arn := "123456789" id := "123456789" name1 := "test1" - itemMesh1 := vpclattice.ServiceNetworkSummary{ + item1 := vpclattice.ServiceNetworkSummary{ Arn: &arn, Id: &id, Name: &name1, } name2 := "test2" - itemMesh2 := vpclattice.ServiceNetworkSummary{ + item2 := vpclattice.ServiceNetworkSummary{ Arn: &arn, Id: &id, Name: &name2, } - listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&itemMesh1, &itemMesh2} + listServiceNetworkOutput := []*vpclattice.ServiceNetworkSummary{&item1, &item2} c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess) + mockLattice.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice) meshManager := NewDefaultServiceNetworkManager(mockCloud) meshList, err := meshManager.List(ctx) @@ -1010,10 +1022,10 @@ func Test_ListMesh_NoMesh(t *testing.T) { c := gomock.NewController(t) defer c.Finish() ctx := context.TODO() - mockVpcLatticeSess := mocks.NewMockLattice(c) + mockLattice := mocks.NewMockLattice(c) mockCloud := mocks_aws.NewMockCloud(c) - mockVpcLatticeSess.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) - mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess) + mockLattice.EXPECT().ListServiceNetworksAsList(ctx, gomock.Any()).Return(listServiceNetworkOutput, nil) + mockCloud.EXPECT().Lattice().Return(mockLattice) meshManager := NewDefaultServiceNetworkManager(mockCloud) meshList, err := meshManager.List(ctx) diff --git a/pkg/deploy/lattice/service_network_synthesizer.go b/pkg/deploy/lattice/service_network_synthesizer.go index 120b4a3b..d58a49f5 100644 --- a/pkg/deploy/lattice/service_network_synthesizer.go +++ b/pkg/deploy/lattice/service_network_synthesizer.go @@ -5,19 +5,17 @@ import ( "errors" "github.com/golang/glog" - "github.com/aws/aws-application-networking-k8s/pkg/latticestore" "github.com/aws/aws-application-networking-k8s/pkg/model/core" latticemodel "github.com/aws/aws-application-networking-k8s/pkg/model/lattice" "sigs.k8s.io/controller-runtime/pkg/client" gateway_api "sigs.k8s.io/gateway-api/apis/v1beta1" ) -func NewServiceNetworkSynthesizer(client client.Client, serviceNetworkManager ServiceNetworkManager, stack core.Stack, latticeDataStore *latticestore.LatticeDataStore) *serviceNetworkSynthesizer { +func NewServiceNetworkSynthesizer(client client.Client, serviceNetworkManager ServiceNetworkManager, stack core.Stack) *serviceNetworkSynthesizer { return &serviceNetworkSynthesizer{ Client: client, serviceNetworkManager: serviceNetworkManager, stack: stack, - latticeDataStore: latticeDataStore, } } @@ -26,7 +24,6 @@ type serviceNetworkSynthesizer struct { serviceNetworkManager ServiceNetworkManager stack core.Stack - latticeDataStore *latticestore.LatticeDataStore } func (s *serviceNetworkSynthesizer) Synthesize(ctx context.Context) error { @@ -54,61 +51,28 @@ func (s *serviceNetworkSynthesizer) Synthesize(ctx context.Context) error { } func (s *serviceNetworkSynthesizer) synthesizeTriggeredGateways(ctx context.Context) error { - var resServiceNetworks []*latticemodel.ServiceNetwork - var ret = "" + var serviceNetworks []*latticemodel.ServiceNetwork + var ret = "" // only tracks the last error encountered, others are in the logs - s.stack.ListResources(&resServiceNetworks) - glog.V(6).Infof("Start synthesizing Triggered ServiceNetworks/Gateways ...%v \n", resServiceNetworks) + s.stack.ListResources(&serviceNetworks) + glog.V(6).Infof("Start synthesizing Triggered ServiceNetworks/Gateways ...%v \n", serviceNetworks) - // handling add - for _, resServiceNetwork := range resServiceNetworks { + for _, resServiceNetwork := range serviceNetworks { if resServiceNetwork.Spec.IsDeleted { - glog.V(6).Infof("Synthersing Gateway: Del %v\n", resServiceNetwork.Spec.Name) - - // TODO need to check if servicenetwork is referenced by gateway in other namespace - gwList := &gateway_api.GatewayList{} - s.Client.List(context.TODO(), gwList) - snUsedByGateway := false - for _, gw := range gwList.Items { - if gw.Name == resServiceNetwork.Spec.Name && - gw.Namespace != resServiceNetwork.Spec.Namespace { - glog.V(6).Infof("Skip deleting gw %v, namespace %v, since it is still used", gw.Name, gw.Namespace) - snUsedByGateway = true - break - } - } - - if snUsedByGateway { - glog.V(6).Infof("Skiping deleting gw: %v since it is still used by gateway(s)", - resServiceNetwork.Spec.Name) - - continue - } - - err := s.serviceNetworkManager.Delete(ctx, resServiceNetwork.Spec.Name) - if err != nil { - ret = LATTICE_RETRY - } else { - glog.V(6).Infof("Synthersing Gateway: successfully deleted gateway %v\n", resServiceNetwork.Spec.Name) - s.latticeDataStore.DelServiceNetwork(resServiceNetwork.Spec.Name, resServiceNetwork.Spec.Account) + deleteRet := s.deleteServiceNetwork(ctx, resServiceNetwork) + if deleteRet != "" { + ret = deleteRet } - continue - } else { - glog.V(6).Infof("Synthersing Gateway: Add %v\n", resServiceNetwork.Spec.Name) + glog.V(6).Infof("Synthesizing Gateway: Add %v\n", resServiceNetwork.Spec.Name) serviceNetworkStatus, err := s.serviceNetworkManager.Create(ctx, resServiceNetwork) if err != nil { - glog.V(6).Infof("Synthersizing Gateway failed for gateway %v error =%v\n ", resServiceNetwork.Spec.Name, err) - // update data store with status - s.latticeDataStore.AddServiceNetwork(resServiceNetwork.Spec.Name, resServiceNetwork.Spec.Account, serviceNetworkStatus.ServiceNetworkARN, serviceNetworkStatus.ServiceNetworkID, latticestore.DATASTORE_SERVICE_NETWORK_CREATE_IN_PROGRESS) + glog.V(6).Infof("Synthesizing Gateway failed for gateway %v error =%v\n ", resServiceNetwork.Spec.Name, err) ret = LATTICE_RETRY - - continue + } else { + glog.V(6).Infof("Synthesizing Gateway succeeded for gateway %v, status %v \n", resServiceNetwork.Spec.Name, serviceNetworkStatus) } - - glog.V(6).Infof("Synthersizing Gateway succeeded for gateway %v, status %v \n", resServiceNetwork.Spec.Name, serviceNetworkStatus) - s.latticeDataStore.AddServiceNetwork(resServiceNetwork.Spec.Name, resServiceNetwork.Spec.Account, serviceNetworkStatus.ServiceNetworkARN, serviceNetworkStatus.ServiceNetworkID, latticestore.DATASTORE_SERVICE_NETWORK_CREATED) } } @@ -117,7 +81,39 @@ func (s *serviceNetworkSynthesizer) synthesizeTriggeredGateways(ctx context.Cont } else { return nil } +} + +func (s *serviceNetworkSynthesizer) deleteServiceNetwork(ctx context.Context, resServiceNetwork *latticemodel.ServiceNetwork) string { + glog.V(6).Infof("Synthesizing Gateway: Del %v\n", resServiceNetwork.Spec.Name) + + // TODO need to check if service network is referenced by gateway in other namespace + gwList := &gateway_api.GatewayList{} + s.Client.List(context.TODO(), gwList) + snUsedByGateway := false + for _, gw := range gwList.Items { + if gw.Name == resServiceNetwork.Spec.Name && + gw.Namespace != resServiceNetwork.Spec.Namespace { + glog.V(6).Infof("Skip deleting gw %v, namespace %v, since it is still used", gw.Name, gw.Namespace) + snUsedByGateway = true + break + } + } + + if snUsedByGateway { + glog.V(6).Infof("Skipping deleting gw: %v since it is still used by gateway(s)", + resServiceNetwork.Spec.Name) + + return "" + } + + err := s.serviceNetworkManager.Delete(ctx, resServiceNetwork.Spec.Name) + if err != nil { + return LATTICE_RETRY + } else { + glog.V(6).Infof("Synthesizing Gateway: successfully deleted gateway %v\n", resServiceNetwork.Spec.Name) + } + return "" } func (s *serviceNetworkSynthesizer) synthesizeSDKServiceNetworks(ctx context.Context) error { @@ -135,7 +131,7 @@ func (s *serviceNetworkSynthesizer) synthesizeSDKServiceNetworks(ctx context.Con s.Client.List(context.TODO(), gwList) for _, sdkServiceNetwork := range sdkServiceNetworks { - glog.V(6).Infof("Synthersizing Gateway: checking if sdkServiceNetwork %v needed to be deleted \n", sdkServiceNetwork) + glog.V(6).Infof("Synthesizing Gateway: checking if sdkServiceNetwork %v needed to be deleted \n", sdkServiceNetwork) toBeDeleted := false diff --git a/pkg/deploy/lattice/service_network_synthesizer_test.go b/pkg/deploy/lattice/service_network_synthesizer_test.go index 7d913aec..b95ecef8 100644 --- a/pkg/deploy/lattice/service_network_synthesizer_test.go +++ b/pkg/deploy/lattice/service_network_synthesizer_test.go @@ -12,24 +12,19 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" gateway_api "sigs.k8s.io/gateway-api/apis/v1beta1" - "github.com/aws/aws-application-networking-k8s/pkg/config" - "github.com/aws/aws-application-networking-k8s/pkg/gateway" - "github.com/aws/aws-application-networking-k8s/pkg/latticestore" - mock_client "github.com/aws/aws-application-networking-k8s/mocks/controller-runtime/client" + "github.com/aws/aws-application-networking-k8s/pkg/gateway" latticemodel "github.com/aws/aws-application-networking-k8s/pkg/model/lattice" ) func Test_SynthesizeTriggeredGateways(t *testing.T) { now := metav1.Now() tests := []struct { - name string - gw *gateway_api.Gateway - gwUsedByOtherNS bool - meshManagerErr error - wantSynthesizerErr error - wantDataStoreErr error - wantDataStoreStatus string + name string + gw *gateway_api.Gateway + gwUsedByOtherNS bool + meshManagerErr error + wantSynthesizerErr error }{ { name: "Adding a new Mesh successfully", @@ -38,10 +33,8 @@ func Test_SynthesizeTriggeredGateways(t *testing.T) { Name: "mesh1", }, }, - meshManagerErr: nil, - wantSynthesizerErr: nil, - wantDataStoreErr: nil, - wantDataStoreStatus: "", + meshManagerErr: nil, + wantSynthesizerErr: nil, }, { name: "Adding a new Mesh associating in progress", @@ -50,10 +43,8 @@ func Test_SynthesizeTriggeredGateways(t *testing.T) { Name: "mesh2", }, }, - meshManagerErr: errors.New(LATTICE_RETRY), - wantSynthesizerErr: errors.New(LATTICE_RETRY), - wantDataStoreErr: nil, - wantDataStoreStatus: "", + meshManagerErr: errors.New(LATTICE_RETRY), + wantSynthesizerErr: errors.New(LATTICE_RETRY), }, { @@ -65,11 +56,9 @@ func Test_SynthesizeTriggeredGateways(t *testing.T) { DeletionTimestamp: &now, }, }, - meshManagerErr: nil, - gwUsedByOtherNS: false, - wantSynthesizerErr: nil, - wantDataStoreErr: errors.New(latticestore.DATASTORE_SERVICE_NETWORK_NOT_EXIST), - wantDataStoreStatus: latticestore.DATASTORE_SERVICE_NETWORK_NOT_EXIST, + meshManagerErr: nil, + gwUsedByOtherNS: false, + wantSynthesizerErr: nil, }, { name: "Deleting Mesh Skipped due to other NS still uses it", @@ -80,11 +69,9 @@ func Test_SynthesizeTriggeredGateways(t *testing.T) { DeletionTimestamp: &now, }, }, - meshManagerErr: nil, - gwUsedByOtherNS: true, - wantSynthesizerErr: nil, - wantDataStoreErr: nil, - wantDataStoreStatus: "", + meshManagerErr: nil, + gwUsedByOtherNS: true, + wantSynthesizerErr: nil, }, { name: "Deleting Mesh Successfully in progress", @@ -95,10 +82,8 @@ func Test_SynthesizeTriggeredGateways(t *testing.T) { DeletionTimestamp: &now, }, }, - meshManagerErr: errors.New(LATTICE_RETRY), - wantSynthesizerErr: errors.New(LATTICE_RETRY), - wantDataStoreErr: nil, - wantDataStoreStatus: "", + meshManagerErr: errors.New(LATTICE_RETRY), + wantSynthesizerErr: errors.New(LATTICE_RETRY), }, } @@ -114,9 +99,6 @@ func Test_SynthesizeTriggeredGateways(t *testing.T) { stack, mesh, _ := builder.Build(context.Background(), tt.gw) var meshStatus latticemodel.ServiceNetworkStatus - - ds := latticestore.NewLatticeDataStore() - mockMeshManager := NewMockServiceNetworkManager(c) // testing deleting staled mesh (gateway) @@ -125,8 +107,6 @@ func Test_SynthesizeTriggeredGateways(t *testing.T) { // testing add or delete of triggered gateway(mesh) if !tt.gw.DeletionTimestamp.IsZero() { // testing delete - // insert the record in cache and verify it will be deleted later - ds.AddServiceNetwork(tt.gw.Name, config.AccountID, "ARN", "id", latticestore.DATASTORE_SERVICE_NETWORK_CREATED) gwList := &gateway_api.GatewayList{} @@ -166,25 +146,10 @@ func Test_SynthesizeTriggeredGateways(t *testing.T) { mockMeshManager.EXPECT().Create(ctx, mesh).Return(meshStatus, tt.meshManagerErr) } - meshMeshSynthesizer := NewServiceNetworkSynthesizer(mock_client, mockMeshManager, stack, ds) + meshMeshSynthesizer := NewServiceNetworkSynthesizer(mock_client, mockMeshManager, stack) err := meshMeshSynthesizer.synthesizeTriggeredGateways(ctx) - assert.Equal(t, tt.wantSynthesizerErr, err) - - // verify the local cache for triggered gateway add or delete - output, err := ds.GetServiceNetworkStatus(tt.gw.Name, config.AccountID) - - fmt.Printf("GetMeshStatus:%v, err %v\n", output, err) - if tt.gw.DeletionTimestamp.IsZero() { - // Verify record being added to local store correctly - assert.Equal(t, meshStatus.ServiceNetworkARN, output.ARN) - assert.Equal(t, meshStatus.ServiceNetworkID, output.ID) - } - - assert.Equal(t, tt.wantDataStoreErr, err) - } - } type sdkMeshDef struct { @@ -236,8 +201,6 @@ func Test_SythesizeSDKMeshs(t *testing.T) { defer c.Finish() ctx := context.TODO() - ds := latticestore.NewLatticeDataStore() - mockMeshManager := NewMockServiceNetworkManager(c) // testing deleting staled mesh (gateway) @@ -253,7 +216,7 @@ func Test_SythesizeSDKMeshs(t *testing.T) { fmt.Printf("sdkMesh %v\n", sdkMesh) sdkMeshsReturned = append(sdkMeshsReturned, sdkMesh.name) fmt.Printf("sdkMeshsReturned --loop %v\n", sdkMeshsReturned) - ds.AddServiceNetwork(sdkMesh.name, config.AccountID, "staleMeshARN", "staleMeshId", latticestore.DATASTORE_SERVICE_NETWORK_CREATED) + if !sdkMesh.isStale { gwList.Items = append(gwList.Items, gateway_api.Gateway{ @@ -286,13 +249,8 @@ func Test_SythesizeSDKMeshs(t *testing.T) { } mockMeshManager.EXPECT().List(ctx).Return(sdkMeshsReturned, nil) - - meshMeshSynthesizer := NewServiceNetworkSynthesizer(mock_client, mockMeshManager, nil, ds) - + meshMeshSynthesizer := NewServiceNetworkSynthesizer(mock_client, mockMeshManager, nil) err := meshMeshSynthesizer.synthesizeSDKServiceNetworks(ctx) - assert.Equal(t, tt.wantSynthesizerErr, err) - } - } diff --git a/pkg/deploy/lattice/target_group_synthesizer.go b/pkg/deploy/lattice/target_group_synthesizer.go index 25e9a57f..5d3eb967 100644 --- a/pkg/deploy/lattice/target_group_synthesizer.go +++ b/pkg/deploy/lattice/target_group_synthesizer.go @@ -138,7 +138,7 @@ func (t *TargetGroupSynthesizer) SynthesizeTriggeredTargetGroup(ctx context.Cont returnErr = true continue } else { - t.log.Infof("Synthersizing Target Group: successfully deleted target group %v", resTargetGroup) + t.log.Infof("Synthesizing Target Group: successfully deleted target group %v", resTargetGroup) t.latticeDataStore.DelTargetGroup(resTargetGroup.Spec.Name, resTargetGroup.Spec.Config.K8SHTTPRouteName, false) } diff --git a/pkg/deploy/stack_deployer.go b/pkg/deploy/stack_deployer.go index fae25440..070df412 100644 --- a/pkg/deploy/stack_deployer.go +++ b/pkg/deploy/stack_deployer.go @@ -67,7 +67,7 @@ func deploy(ctx context.Context, stack core.Stack, synthesizers []ResourceSynthe func (d *serviceNetworkStackDeployer) Deploy(ctx context.Context, stack core.Stack) error { synthesizers := []ResourceSynthesizer{ - lattice.NewServiceNetworkSynthesizer(d.k8sclient, d.latticeServiceNetworkManager, stack, d.latticeDataStore), + lattice.NewServiceNetworkSynthesizer(d.k8sclient, d.latticeServiceNetworkManager, stack), } return deploy(ctx, stack, synthesizers) } diff --git a/pkg/latticestore/latticestore.go b/pkg/latticestore/latticestore.go index 0a27a664..0d9283be 100644 --- a/pkg/latticestore/latticestore.go +++ b/pkg/latticestore/latticestore.go @@ -26,20 +26,6 @@ const ( // this package is used to cache lattice info that relates to K8S object. // e.g. the AWSARN for the matching K8S object -type ServiceNetworkKey struct { - Name string - AccountID string -} - -type ServiceNetwork struct { - Key ServiceNetworkKey `json:"meshkey"` - ARN string `json:"arn"` - ID string `json:"id"` - Status string `json:"status"` -} - -type ServiceNetworkPool map[ServiceNetworkKey]*ServiceNetwork - type LatticeServiceKey struct { Name string Namespace string @@ -96,14 +82,12 @@ type TargetGroupPool map[TargetGroupKey]*TargetGroup type LatticeDataStore struct { log gwlog.Logger lock sync.Mutex - serviceNetworks ServiceNetworkPool latticeServices LatticeServicePool targetGroups TargetGroupPool listeners ListenerPool } type LatticeDataStoreInfo struct { - ServiceNetworks map[string]ServiceNetwork LatticeServices map[string]LatticeService TargetGroups map[string]TargetGroup Listeners map[string]Listener @@ -114,7 +98,6 @@ var defaultLatticeDataStore *LatticeDataStore func NewLatticeDataStoreWithLog(log gwlog.Logger) *LatticeDataStore { defaultLatticeDataStore = &LatticeDataStore{ log: log, - serviceNetworks: make(ServiceNetworkPool), latticeServices: make(LatticeServicePool), targetGroups: make(TargetGroupPool), listeners: make(ListenerPool), @@ -131,17 +114,11 @@ func dumpCurrentLatticeDataStore(ds *LatticeDataStore) *LatticeDataStoreInfo { defer ds.lock.Unlock() var store = LatticeDataStoreInfo{ - ServiceNetworks: make(map[string]ServiceNetwork), LatticeServices: make(map[string]LatticeService), TargetGroups: make(map[string]TargetGroup), Listeners: make(map[string]Listener), } - for _, sn := range ds.serviceNetworks { - key := fmt.Sprintf("%s-%s", sn.Key.Name, sn.Key.AccountID) - store.ServiceNetworks[key] = *sn - } - for _, svc := range ds.latticeServices { key := fmt.Sprintf("%s-%s", svc.LatticeServiceKey.Name, svc.LatticeServiceKey.Namespace) store.LatticeServices[key] = *svc @@ -165,63 +142,6 @@ func GetDefaultLatticeDataStore() *LatticeDataStore { return defaultLatticeDataStore } -func (ds *LatticeDataStore) AddServiceNetwork(name string, account string, arn string, id string, status string) error { - ds.lock.Lock() - defer ds.lock.Unlock() - - ds.log.Debugf("AddServiceNetwork name: %s, account: %s, arn: %s, id: %s", name, account, arn, id) - - Key := ServiceNetworkKey{Name: name, AccountID: account} - _, ok := ds.serviceNetworks[Key] - - if ok { - ds.log.Debugf("UpdateServiceNetwork name: %s, account :%s, arn: %s, id: %s", name, account, arn, id) - } - - ds.serviceNetworks[Key] = &ServiceNetwork{ - Key: Key, - ARN: arn, - ID: id, - Status: status, - } - - return nil -} - -func (ds *LatticeDataStore) DelServiceNetwork(name string, account string) error { - ds.lock.Lock() - defer ds.lock.Unlock() - - ds.log.Debugf("DelServiceNetwork name: %s, account: %s", name, account) - - key := ServiceNetworkKey{Name: name, AccountID: account} - _, ok := ds.serviceNetworks[key] - - if !ok { - ds.log.Debugf("Deleting unknown service network: name: %s, account: %s", name, account) - return errors.New(DATASTORE_SERVICE_NETWORK_NOT_EXIST) - } - - delete(ds.serviceNetworks, key) - return nil -} - -func (ds *LatticeDataStore) GetServiceNetworkStatus(name string, account string) (ServiceNetwork, error) { - ds.lock.Lock() - defer ds.lock.Unlock() - key := ServiceNetworkKey{Name: name, AccountID: account} - - mesh, ok := ds.serviceNetworks[key] - - if !ok { - ds.log.Debugf("GetServiceNetworkStatus NOT found name: %s, account: %s", name, account) - return ServiceNetwork{}, errors.New(DATASTORE_SERVICE_NETWORK_NOT_EXIST) - } - - return *mesh, nil - -} - func (ds *LatticeDataStore) AddLatticeService(name string, namespace string, arn string, id string, dns string) error { ds.lock.Lock() defer ds.lock.Unlock() diff --git a/pkg/latticestore/latticestore_test.go b/pkg/latticestore/latticestore_test.go index 1d0d5965..f7828c44 100644 --- a/pkg/latticestore/latticestore_test.go +++ b/pkg/latticestore/latticestore_test.go @@ -26,78 +26,6 @@ func Test_GetDefaultLatticeDataStore(t *testing.T) { assert.Equal(t, inputDataStore, defaultDataStore, "") } -func Test_ServiceNetwork(t *testing.T) { - inputDataStore := NewLatticeDataStore() - - meshName := "mesh1" - meshName2 := "mesh2" - acct := "12345678" - arn := "arn" - id := "id" - - // Test GetServiceNetworkStatus for an unknown mesh object - mesh, err := inputDataStore.GetServiceNetworkStatus(meshName, acct) - assert.Equal(t, errors.New(DATASTORE_SERVICE_NETWORK_NOT_EXIST), err) - - // add mesh, happy path - err = inputDataStore.AddServiceNetwork(meshName, acct, arn, id, DATASTORE_SERVICE_NETWORK_CREATE_IN_PROGRESS) - assert.Equal(t, err, nil) - - store := dumpCurrentLatticeDataStore(inputDataStore) - fmt.Printf("store:%v \n", store) - assert.Equal(t, 1, len(store.ServiceNetworks), "") - - // verify GetServiceNetworkStatus OK - mesh, err = inputDataStore.GetServiceNetworkStatus(meshName, acct) - assert.Nil(t, err) - assert.Equal(t, meshName, mesh.Key.Name) - assert.Equal(t, acct, mesh.Key.AccountID) - assert.Equal(t, arn, mesh.ARN) - assert.Equal(t, id, mesh.ID) - assert.Equal(t, DATASTORE_SERVICE_NETWORK_CREATE_IN_PROGRESS, mesh.Status) - - fmt.Printf("mesh %v\n", mesh) - - // add same mesh again, nothing change - err = inputDataStore.AddServiceNetwork(meshName, acct, arn, id, DATASTORE_SERVICE_NETWORK_CREATED) - assert.Equal(t, err, nil) - - store = dumpCurrentLatticeDataStore(inputDataStore) - fmt.Printf("store:%v \n", store) - assert.Equal(t, 1, len(store.ServiceNetworks), "") - - mesh, err = inputDataStore.GetServiceNetworkStatus(meshName, acct) - assert.Nil(t, err) - assert.Equal(t, meshName, mesh.Key.Name) - assert.Equal(t, acct, mesh.Key.AccountID) - assert.Equal(t, arn, mesh.ARN) - assert.Equal(t, id, mesh.ID) - assert.Equal(t, DATASTORE_SERVICE_NETWORK_CREATED, mesh.Status) - - err = inputDataStore.AddServiceNetwork(meshName2, acct, arn, id, DATASTORE_SERVICE_NETWORK_CREATED) - assert.Equal(t, err, nil) - - store = dumpCurrentLatticeDataStore(inputDataStore) - fmt.Printf("store:%v \n", store) - assert.Equal(t, 2, len(store.ServiceNetworks), "") - - // delete 2nd mesh - err = inputDataStore.DelServiceNetwork(meshName2, acct) - assert.Nil(t, err) - - store = dumpCurrentLatticeDataStore(inputDataStore) - fmt.Printf("store:%v \n", store) - assert.Equal(t, 1, len(store.ServiceNetworks), "") - - // get deleted meshName2 should return error, still able to get 1st mesh - mesh, err = inputDataStore.GetServiceNetworkStatus(meshName2, acct) - assert.Equal(t, errors.New(DATASTORE_SERVICE_NETWORK_NOT_EXIST), err) - - mesh, err = inputDataStore.GetServiceNetworkStatus(meshName, acct) - assert.Nil(t, err) - -} - func Test_LatticeService(t *testing.T) { inputDataStore := NewLatticeDataStore() diff --git a/pkg/utils/common.go b/pkg/utils/common.go index 9ae4bf4a..1a31204e 100644 --- a/pkg/utils/common.go +++ b/pkg/utils/common.go @@ -1,18 +1,12 @@ package utils -import "strings" +import ( + "strings" +) type MapFunc[T any, U any] func(T) U type FilterFunc[T any] func(T) bool -// TODO: should be check by API call (Mingxi) -func ArntoId(arn string) string { - if len(arn) == 0 { - return "" - } - return arn[len(arn)-22:] -} - func Truncate(name string, length int) string { if len(name) > length { name = name[:length]