|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aws/aws-application-networking-k8s/pkg/model/core" |
| 10 | + "github.com/aws/aws-application-networking-k8s/test/pkg/test" |
| 11 | + "github.com/aws/aws-sdk-go/aws" |
| 12 | + "github.com/aws/aws-sdk-go/service/vpclattice" |
| 13 | + . "github.com/onsi/ginkgo/v2" |
| 14 | + . "github.com/onsi/gomega" |
| 15 | + "github.com/samber/lo" |
| 16 | + appsv1 "k8s.io/api/apps/v1" |
| 17 | + v1 "k8s.io/api/core/v1" |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 20 | + |
| 21 | + anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = Describe("GRPCRoute Service Export/Import Test", Ordered, func() { |
| 25 | + var ( |
| 26 | + grpcDeployment *appsv1.Deployment |
| 27 | + grpcSvc *v1.Service |
| 28 | + grpcRoute *gwv1.GRPCRoute |
| 29 | + serviceExport *anv1alpha1.ServiceExport |
| 30 | + serviceImport *anv1alpha1.ServiceImport |
| 31 | + ) |
| 32 | + |
| 33 | + It("Create k8s resource", func() { |
| 34 | + // Create a gRPC service and deployment |
| 35 | + grpcDeployment, grpcSvc = testFramework.NewGrpcHelloWorld(test.GrpcAppOptions{AppName: "my-grpc-exportedports", Namespace: k8snamespace}) |
| 36 | + testFramework.ExpectCreated(ctx, grpcDeployment, grpcSvc) |
| 37 | + |
| 38 | + // Create ServiceImport |
| 39 | + serviceImport = testFramework.CreateServiceImport(grpcSvc) |
| 40 | + testFramework.ExpectCreated(ctx, serviceImport) |
| 41 | + |
| 42 | + // Create ServiceExport with exportedPorts field |
| 43 | + serviceExport = &anv1alpha1.ServiceExport{ |
| 44 | + TypeMeta: metav1.TypeMeta{ |
| 45 | + APIVersion: "application-networking.k8s.aws/v1alpha1", |
| 46 | + Kind: "ServiceExport", |
| 47 | + }, |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: grpcSvc.Name, |
| 50 | + Namespace: grpcSvc.Namespace, |
| 51 | + Annotations: map[string]string{ |
| 52 | + "application-networking.k8s.aws/federation": "amazon-vpc-lattice", |
| 53 | + }, |
| 54 | + }, |
| 55 | + Spec: anv1alpha1.ServiceExportSpec{ |
| 56 | + ExportedPorts: []anv1alpha1.ExportedPort{ |
| 57 | + { |
| 58 | + Port: grpcSvc.Spec.Ports[0].Port, |
| 59 | + RouteType: "GRPC", |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | + testFramework.ExpectCreated(ctx, serviceExport) |
| 65 | + |
| 66 | + // Create GRPCRoute |
| 67 | + grpcRoute = testFramework.NewGRPCRoute(k8snamespace, testGateway, []gwv1.GRPCRouteRule{ |
| 68 | + { |
| 69 | + Matches: []gwv1.GRPCRouteMatch{ |
| 70 | + { |
| 71 | + Method: &gwv1.GRPCMethodMatch{ |
| 72 | + Service: lo.ToPtr("helloworld.Greeter"), |
| 73 | + Method: lo.ToPtr("SayHello"), |
| 74 | + Type: lo.ToPtr(gwv1.GRPCMethodMatchExact), |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + BackendRefs: []gwv1.GRPCBackendRef{ |
| 79 | + { |
| 80 | + BackendRef: gwv1.BackendRef{ |
| 81 | + BackendObjectReference: gwv1.BackendObjectReference{ |
| 82 | + Name: gwv1.ObjectName(grpcSvc.Name), |
| 83 | + Namespace: lo.ToPtr(gwv1.Namespace(grpcSvc.Namespace)), |
| 84 | + Kind: lo.ToPtr(gwv1.Kind("ServiceImport")), |
| 85 | + Port: lo.ToPtr(gwv1.PortNumber(grpcSvc.Spec.Ports[0].Port)), |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }) |
| 92 | + testFramework.ExpectCreated(ctx, grpcRoute) |
| 93 | + }) |
| 94 | + |
| 95 | + It("Verify lattice resource & traffic", func() { |
| 96 | + route, _ := core.NewRoute(grpcRoute) |
| 97 | + vpcLatticeService := testFramework.GetVpcLatticeService(ctx, route) |
| 98 | + fmt.Printf("vpcLatticeService: %v \n", vpcLatticeService) |
| 99 | + |
| 100 | + // Get the target group and verify it's configured for gRPC |
| 101 | + tgSummary := testFramework.GetTargetGroupWithProtocol(ctx, grpcSvc, "http", "grpc") |
| 102 | + tg, err := testFramework.LatticeClient.GetTargetGroup(&vpclattice.GetTargetGroupInput{ |
| 103 | + TargetGroupIdentifier: aws.String(*tgSummary.Id), |
| 104 | + }) |
| 105 | + Expect(tg).To(Not(BeNil())) |
| 106 | + Expect(err).To(BeNil()) |
| 107 | + Expect(*tgSummary.VpcIdentifier).To(Equal(os.Getenv("CLUSTER_VPC_ID"))) |
| 108 | + |
| 109 | + // Verify the target group is configured for gRPC |
| 110 | + Expect(*tgSummary.Protocol).To(Equal("HTTP")) |
| 111 | + Expect(*tg.Config.ProtocolVersion).To(Equal("GRPC")) |
| 112 | + |
| 113 | + // Verify targets are registered |
| 114 | + Eventually(func(g Gomega) { |
| 115 | + targets := testFramework.GetTargets(ctx, tgSummary, grpcDeployment) |
| 116 | + for _, target := range targets { |
| 117 | + g.Expect(*target.Port).To(BeEquivalentTo(grpcSvc.Spec.Ports[0].TargetPort.IntVal)) |
| 118 | + } |
| 119 | + }).WithTimeout(3 * time.Minute).WithOffset(1).Should(Succeed()) |
| 120 | + |
| 121 | + log.Println("Verifying traffic") |
| 122 | + grpcurlCmdOptions := test.RunGrpcurlCmdOptions{ |
| 123 | + GrpcServerHostName: *vpcLatticeService.DnsEntry.DomainName, |
| 124 | + GrpcServerPort: "443", |
| 125 | + Service: "helloworld.Greeter", |
| 126 | + Method: "SayHello", |
| 127 | + ReqParamsJsonString: `{"name": "ExportedPorts"}`, |
| 128 | + UseTLS: true, |
| 129 | + } |
| 130 | + Eventually(func(g Gomega) { |
| 131 | + stdoutStr, stderrStr, err := testFramework.RunGrpcurlCmd(grpcurlCmdOptions) |
| 132 | + g.Expect(err).To(BeNil()) |
| 133 | + g.Expect(stderrStr).To(BeEmpty()) |
| 134 | + g.Expect(stdoutStr).To(ContainSubstring("ExportedPorts")) |
| 135 | + }).Should(Succeed()) |
| 136 | + }) |
| 137 | + |
| 138 | + AfterAll(func() { |
| 139 | + testFramework.ExpectDeletedThenNotFound(ctx, |
| 140 | + grpcRoute, |
| 141 | + grpcDeployment, |
| 142 | + grpcSvc, |
| 143 | + serviceImport, |
| 144 | + serviceExport, |
| 145 | + ) |
| 146 | + }) |
| 147 | +}) |
0 commit comments