diff --git a/pkg/virtualrouter/routes_manager_test.go b/pkg/virtualrouter/routes_manager_test.go index b221c4ba..ab943075 100644 --- a/pkg/virtualrouter/routes_manager_test.go +++ b/pkg/virtualrouter/routes_manager_test.go @@ -694,6 +694,72 @@ func Test_BuildSDKRouteSpec(t *testing.T) { }, }, }, + { + name: "TCP route with Match", + args: args{ + vr: &appmesh.VirtualRouter{ + ObjectMeta: v1.ObjectMeta{ + Namespace: "my-ns", + }, + }, + route: appmesh.Route{ + TCPRoute: &appmesh.TCPRoute{ + Action: appmesh.TCPRouteAction{ + WeightedTargets: []appmesh.WeightedTarget{ + { + VirtualNodeRef: &appmesh.VirtualNodeReference{ + Namespace: aws.String("ns-1"), + Name: "vn-1", + }, + Weight: int64(100), + }, + { + VirtualNodeRef: &appmesh.VirtualNodeReference{ + Namespace: aws.String("ns-2"), + Name: "vn-2", + }, + Weight: int64(90), + }, + }, + }, + Match: &appmesh.TCPRouteMatch{ + Port: aws.Int64(8080), + }, + }, + }, + vnByKey: map[types.NamespacedName]*appmesh.VirtualNode{ + types.NamespacedName{Namespace: "ns-1", Name: "vn-1"}: { + Spec: appmesh.VirtualNodeSpec{ + AWSName: aws.String("vn-1_ns-1"), + }, + }, + types.NamespacedName{Namespace: "ns-2", Name: "vn-2"}: { + Spec: appmesh.VirtualNodeSpec{ + AWSName: aws.String("vn-2_ns-2"), + }, + }, + }, + }, + want: &appmeshsdk.RouteSpec{ + TcpRoute: &appmeshsdk.TcpRoute{ + Action: &appmeshsdk.TcpRouteAction{ + WeightedTargets: []*appmeshsdk.WeightedTarget{ + { + VirtualNode: aws.String("vn-1_ns-1"), + Weight: aws.Int64(100), + }, + { + VirtualNode: aws.String("vn-2_ns-2"), + Weight: aws.Int64(90), + }, + }, + }, + Match: &appmeshsdk.TcpRouteMatch{ + Port: aws.Int64(8080), + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/test/framework/manifest/virtualrouter.go b/test/framework/manifest/virtualrouter.go index 39bec130..0fdbd11e 100644 --- a/test/framework/manifest/virtualrouter.go +++ b/test/framework/manifest/virtualrouter.go @@ -14,6 +14,13 @@ type RouteToWeightedVirtualNodes struct { WeightedTargets []WeightedVirtualNode } +type TcpRouteToWeightedVirtualNodes struct { + Match *TCPRouteMatch + WeightedTargets []WeightedVirtualNode +} +type TCPRouteMatch struct { + Port *int64 +} type WeightedVirtualNode struct { VirtualNode types.NamespacedName Weight int64 @@ -68,6 +75,34 @@ func (b *VRBuilder) BuildRoutes(routeCfgs []RouteToWeightedVirtualNodes) []appme return routes } +func (b *VRBuilder) TcpBuildRoutes(tcpRoutesCFgs []TcpRouteToWeightedVirtualNodes) []appmesh.Route { + + var routes []appmesh.Route + for index, tcpRouteCfg := range tcpRoutesCFgs { + var targets []appmesh.WeightedTarget + for _, weightedTarget := range tcpRouteCfg.WeightedTargets { + targets = append(targets, appmesh.WeightedTarget{ + VirtualNodeRef: &appmesh.VirtualNodeReference{ + Namespace: aws.String(weightedTarget.VirtualNode.Namespace), + Name: weightedTarget.VirtualNode.Name, + }, + Weight: weightedTarget.Weight, + }) + } + //match:= if tcpRouteCfg. + routes = append(routes, appmesh.Route{ + Name: fmt.Sprintf("route-%d", index), + TCPRoute: &appmesh.TCPRoute{ + Match: (*appmesh.TCPRouteMatch)(tcpRouteCfg.Match), + Action: appmesh.TCPRouteAction{ + WeightedTargets: targets, + }, + Timeout: nil, + }, + }) + } + return routes +} func (b *VRBuilder) BuildVirtualRouterListener(protocol appmesh.PortProtocol, port appmesh.PortNumber) appmesh.VirtualRouterListener { return appmesh.VirtualRouterListener{ @@ -78,6 +113,12 @@ func (b *VRBuilder) BuildVirtualRouterListener(protocol appmesh.PortProtocol, po } } +func (b *VRBuilder) BuildTcpRouteMatch(port *int64) *TCPRouteMatch { + return &TCPRouteMatch{ + Port: port, + } +} + func (b *VRBuilder) buildName(instanceName string) string { return instanceName } diff --git a/test/integration/virtualrouter/virtualrouter_test.go b/test/integration/virtualrouter/virtualrouter_test.go index 12f6649a..9885b744 100644 --- a/test/integration/virtualrouter/virtualrouter_test.go +++ b/test/integration/virtualrouter/virtualrouter_test.go @@ -173,6 +173,47 @@ var _ = Describe("VirtualRouter", func() { }) + tcpRouteCfgsWithMatch := []manifest.TcpRouteToWeightedVirtualNodes{{ + WeightedTargets: weightedTargets, + Match: vrBuilder.BuildTcpRouteMatch(aws.Int64(8080)), + }, + } + tcpRoutes := vrBuilder.TcpBuildRoutes(tcpRouteCfgsWithMatch) + vrBuilder.Listeners = []appmesh.VirtualRouterListener{vrBuilder.BuildVirtualRouterListener("tcp", 8080)} + vrName = fmt.Sprintf("vr-%s", utils.RandomDNS1123Label(8)) + vr = vrBuilder.BuildVirtualRouter(vrName, tcpRoutes) + + By("Creating a virtual router resource with TCP listener and Port Match in k8s", func() { + err := vrTest.Create(ctx, f, vr) + Expect(err).NotTo(HaveOccurred()) + }) + + By("Validating the virtual router with TCP listener and Port Match in AWS", func() { + err := vrTest.CheckInAWS(ctx, f, mesh, vr) + Expect(err).NotTo(HaveOccurred()) + + }) + + tcpRouteCfgsWithoutMatch := []manifest.TcpRouteToWeightedVirtualNodes{{ + WeightedTargets: weightedTargets, + }, + } + tcpRoutes = vrBuilder.TcpBuildRoutes(tcpRouteCfgsWithoutMatch) + vrBuilder.Listeners = []appmesh.VirtualRouterListener{vrBuilder.BuildVirtualRouterListener("tcp", 8080)} + vrName = fmt.Sprintf("vr-%s", utils.RandomDNS1123Label(8)) + vr = vrBuilder.BuildVirtualRouter(vrName, tcpRoutes) + + By("Creating a virtual router resource with TCP listener and Port Match in k8s", func() { + err := vrTest.Create(ctx, f, vr) + Expect(err).NotTo(HaveOccurred()) + }) + + By("Validating the virtual router with TCP listener and Port Match in AWS", func() { + err := vrTest.CheckInAWS(ctx, f, mesh, vr) + Expect(err).NotTo(HaveOccurred()) + + }) + routes = vrBuilder.BuildRoutes(routeCfgs) vrBuilder.Listeners = []appmesh.VirtualRouterListener{vrBuilder.BuildVirtualRouterListener("http", 8080)} vrName = fmt.Sprintf("vr-%s", utils.RandomDNS1123Label(8)) @@ -364,6 +405,79 @@ var _ = Describe("VirtualRouter", func() { Expect(err).NotTo(HaveOccurred()) }) + tcpRouteCfgsWithMatch := []manifest.TcpRouteToWeightedVirtualNodes{{ + WeightedTargets: weightedTargets, + Match: vrBuilder.BuildTcpRouteMatch(aws.Int64(8080)), + }, + } + tcpRoutes := vrBuilder.TcpBuildRoutes(tcpRouteCfgsWithMatch) + vrBuilder.Listeners = []appmesh.VirtualRouterListener{vrBuilder.BuildVirtualRouterListener("tcp", 8080)} + vrName = fmt.Sprintf("vr-%s", utils.RandomDNS1123Label(8)) + vr = vrBuilder.BuildVirtualRouter(vrName, tcpRoutes) + + By("Creating a virtual router resource with TCP listener and Port Match in k8s", func() { + err := vrTest.Create(ctx, f, vr) + Expect(err).NotTo(HaveOccurred()) + }) + + By("Validating the virtual router with TCP listener and Port Match in AWS", func() { + err := vrTest.CheckInAWS(ctx, f, mesh, vr) + Expect(err).NotTo(HaveOccurred()) + + }) + By("Update the virtual router to not have Match and validating", func() { + oldVR := vrTest.VirtualRouters[vr.Name].DeepCopy() + + tcpRouteCfgs := []manifest.TcpRouteToWeightedVirtualNodes{{ + WeightedTargets: weightedTargets, + }} + + routes := vrBuilder.TcpBuildRoutes(tcpRouteCfgs) + vrTest.VirtualRouters[vr.Name].Spec.Routes = routes + + err := vrTest.Update(ctx, f, vrTest.VirtualRouters[vr.Name], oldVR) + Expect(err).NotTo(HaveOccurred()) + + err = vrTest.CheckInAWS(ctx, f, mesh, vrTest.VirtualRouters[vr.Name]) + Expect(err).NotTo(HaveOccurred()) + }) + tcpRouteCfgsWithoutMatch := []manifest.TcpRouteToWeightedVirtualNodes{{ + WeightedTargets: weightedTargets, + }, + } + tcpRoutes = vrBuilder.TcpBuildRoutes(tcpRouteCfgsWithoutMatch) + vrBuilder.Listeners = []appmesh.VirtualRouterListener{vrBuilder.BuildVirtualRouterListener("tcp", 8080)} + vrName = fmt.Sprintf("vr-%s", utils.RandomDNS1123Label(8)) + vr = vrBuilder.BuildVirtualRouter(vrName, tcpRoutes) + + By("Creating a virtual router resource with TCP listener and Port Match in k8s", func() { + err := vrTest.Create(ctx, f, vr) + Expect(err).NotTo(HaveOccurred()) + }) + + By("Validating the virtual router with TCP listener and Port Match in AWS", func() { + err := vrTest.CheckInAWS(ctx, f, mesh, vr) + Expect(err).NotTo(HaveOccurred()) + + }) + By("Update the virtual router to have Match and validating", func() { + oldVR := vrTest.VirtualRouters[vr.Name].DeepCopy() + + tcpRouteCfgs := []manifest.TcpRouteToWeightedVirtualNodes{{ + WeightedTargets: weightedTargets, + Match: vrBuilder.BuildTcpRouteMatch(aws.Int64(8080)), + }} + + routes := vrBuilder.TcpBuildRoutes(tcpRouteCfgs) + vrTest.VirtualRouters[vr.Name].Spec.Routes = routes + + err := vrTest.Update(ctx, f, vrTest.VirtualRouters[vr.Name], oldVR) + Expect(err).NotTo(HaveOccurred()) + + err = vrTest.CheckInAWS(ctx, f, mesh, vrTest.VirtualRouters[vr.Name]) + Expect(err).NotTo(HaveOccurred()) + }) + }) It("Delete virtual router scenarios", func() {