diff --git a/pkg/deploy/lattice/targets_manager.go b/pkg/deploy/lattice/targets_manager.go index 7262233a..747767ce 100644 --- a/pkg/deploy/lattice/targets_manager.go +++ b/pkg/deploy/lattice/targets_manager.go @@ -3,6 +3,7 @@ package lattice import ( "context" "errors" + "github.com/golang/glog" "github.com/aws/aws-sdk-go/aws" @@ -96,6 +97,11 @@ func (s *defaultTargetsManager) Create(ctx context.Context, targets *latticemode targetList = append(targetList, &t) } + // No targets to register + if len(targetList) == 0 { + return nil + } + registerRouteInput := vpclattice.RegisterTargetsInput{ TargetGroupIdentifier: &tg.ID, Targets: targetList, diff --git a/pkg/deploy/lattice/targets_manager_test.go b/pkg/deploy/lattice/targets_manager_test.go index 0c2f8b79..6d03c621 100644 --- a/pkg/deploy/lattice/targets_manager_test.go +++ b/pkg/deploy/lattice/targets_manager_test.go @@ -120,12 +120,19 @@ func Test_RegisterTargets_Registerfailed(t *testing.T) { Name: "test", Namespace: "", TargetGroupID: "123456789", - TargetIPList: nil, + TargetIPList: []latticemodel.Target{ + { + TargetIP: "123.456.7.891", + Port: sPort, + }, + }, } + planToRegister := latticemodel.Targets{ ResourceMeta: core.ResourceMeta{}, Spec: targetsSpec, } + registerTargetsOutput := &vpclattice.RegisterTargetsOutput{} latticeDataStore := latticestore.NewLatticeDataStore() @@ -233,3 +240,39 @@ func Test_RegisterTargets_RegisterUnsuccessfully(t *testing.T) { assert.NotNil(t, err) assert.Equal(t, err, errors.New(LATTICE_RETRY)) } + +func Test_RegisterTargets_NoTargets_NoCallRegisterTargets(t *testing.T) { + planToRegister := latticemodel.Targets{ + ResourceMeta: core.ResourceMeta{}, + Spec: latticemodel.TargetsSpec{ + Name: "test", + Namespace: "", + TargetGroupID: "123456789", + TargetIPList: []latticemodel.Target{}, + }, + } + + latticeDataStore := latticestore.NewLatticeDataStore() + tgName := latticestore.TargetGroupName("test", "") + + listTargetOutput := []*vpclattice.TargetSummary{} + + // routename + latticeDataStore.AddTargetGroup(tgName, "vpc-123456789", "123456789", "123456789", false, "") + + c := gomock.NewController(t) + defer c.Finish() + ctx := context.TODO() + mockCloud := mocks_aws.NewMockCloud(c) + mockVpcLatticeSess := mocks.NewMockLattice(c) + + mockVpcLatticeSess.EXPECT().ListTargetsAsList(ctx, gomock.Any()).Return(listTargetOutput, nil) + // Expect not to call RegisterTargets + mockVpcLatticeSess.EXPECT().RegisterTargetsWithContext(ctx, gomock.Any()).MaxTimes(0) + mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes() + + targetsManager := NewTargetsManager(mockCloud, latticeDataStore) + err := targetsManager.Create(ctx, &planToRegister) + + assert.Nil(t, err) +} diff --git a/pkg/deploy/lattice/targets_synthesizer.go b/pkg/deploy/lattice/targets_synthesizer.go index d744f87a..93f77e12 100644 --- a/pkg/deploy/lattice/targets_synthesizer.go +++ b/pkg/deploy/lattice/targets_synthesizer.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/golang/glog" lattice_aws "github.com/aws/aws-application-networking-k8s/pkg/aws" @@ -49,6 +50,7 @@ func (t *targetsSynthesizer) SynthesizeTargets(ctx context.Context, resTargets [ return errors.New(errmsg) } + tgName := latticestore.TargetGroupName(targets.Spec.Name, targets.Spec.Namespace) var targetList []latticestore.Target diff --git a/pkg/gateway/model_build_targets.go b/pkg/gateway/model_build_targets.go index 2da7d9c4..a06a5615 100644 --- a/pkg/gateway/model_build_targets.go +++ b/pkg/gateway/model_build_targets.go @@ -109,6 +109,7 @@ func (t *latticeTargetsModelBuildTask) buildLatticeTargets(ctx context.Context) } definedPorts := make(map[int32]struct{}) + if tg.ByServiceExport { serviceExport := &mcs_api.ServiceExport{} err = t.client.Get(ctx, namespacedName, serviceExport) diff --git a/test/suites/integration/httproute_creation_test.go b/test/suites/integration/httproute_creation_test.go index 1088774f..12166a1f 100644 --- a/test/suites/integration/httproute_creation_test.go +++ b/test/suites/integration/httproute_creation_test.go @@ -12,6 +12,7 @@ import ( "github.com/aws/aws-application-networking-k8s/pkg/model/core" "github.com/aws/aws-application-networking-k8s/test/pkg/test" + "github.com/aws/aws-sdk-go/aws" ) var _ = Describe("HTTPRoute Creation", func() { @@ -82,6 +83,26 @@ var _ = Describe("HTTPRoute Creation", func() { }) }) + Context("Order #4: Service, HttpRoute", func() { + It("Create lattice resources successfully when no Targets available", func() { + httpRoute = testFramework.NewHttpRoute(testGateway, service, "Service") + + // Override port match to make the BackendRef not be able to find + // any port causing no available targets to register + httpRoute.Spec.Rules[0].BackendRefs[0].BackendRef.Port = (*v1beta1.PortNumber)(aws.Int32(100)) + + testFramework.ExpectCreated( + ctx, + httpRoute, + deployment, + service, + ) + + testFramework.GetTargetGroup(ctx, service) + testFramework.GetVpcLatticeService(ctx, core.NewHTTPRoute(*httpRoute)) + }) + }) + AfterEach(func() { testFramework.ExpectDeleted(ctx, httpRoute) testFramework.SleepForRouteDeletion()