|
| 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 | + appsv1 "k8s.io/api/apps/v1" |
| 16 | + v1 "k8s.io/api/core/v1" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 19 | + |
| 20 | + anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1" |
| 21 | +) |
| 22 | + |
| 23 | +var _ = Describe("HTTPRoute Service Export/Import Test", Ordered, func() { |
| 24 | + var ( |
| 25 | + httpDeployment *appsv1.Deployment |
| 26 | + httpSvc *v1.Service |
| 27 | + httpRoute *gwv1.HTTPRoute |
| 28 | + serviceExport *anv1alpha1.ServiceExport |
| 29 | + serviceImport *anv1alpha1.ServiceImport |
| 30 | + ) |
| 31 | + |
| 32 | + It("Create k8s resource", func() { |
| 33 | + // Create an HTTP service and deployment |
| 34 | + httpDeployment, httpSvc = testFramework.NewHttpApp(test.HTTPAppOptions{Name: "my-http-exportedports", Namespace: k8snamespace}) |
| 35 | + testFramework.ExpectCreated(ctx, httpDeployment, httpSvc) |
| 36 | + |
| 37 | + // Create ServiceImport |
| 38 | + serviceImport = testFramework.CreateServiceImport(httpSvc) |
| 39 | + testFramework.ExpectCreated(ctx, serviceImport) |
| 40 | + |
| 41 | + // Create ServiceExport with exportedPorts field |
| 42 | + serviceExport = &anv1alpha1.ServiceExport{ |
| 43 | + TypeMeta: metav1.TypeMeta{ |
| 44 | + APIVersion: "application-networking.k8s.aws/v1alpha1", |
| 45 | + Kind: "ServiceExport", |
| 46 | + }, |
| 47 | + ObjectMeta: metav1.ObjectMeta{ |
| 48 | + Name: httpSvc.Name, |
| 49 | + Namespace: httpSvc.Namespace, |
| 50 | + Annotations: map[string]string{ |
| 51 | + "application-networking.k8s.aws/federation": "amazon-vpc-lattice", |
| 52 | + }, |
| 53 | + }, |
| 54 | + Spec: anv1alpha1.ServiceExportSpec{ |
| 55 | + ExportedPorts: []anv1alpha1.ExportedPort{ |
| 56 | + { |
| 57 | + Port: httpSvc.Spec.Ports[0].Port, |
| 58 | + RouteType: "HTTP", |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + testFramework.ExpectCreated(ctx, serviceExport) |
| 64 | + |
| 65 | + httpRoute = testFramework.NewHttpRoute(testGateway, httpSvc, "ServiceImport") |
| 66 | + testFramework.ExpectCreated(ctx, httpRoute) |
| 67 | + }) |
| 68 | + |
| 69 | + It("Verify lattice resource & traffic", func() { |
| 70 | + route := core.NewHTTPRoute(*httpRoute) |
| 71 | + vpcLatticeService := testFramework.GetVpcLatticeService(ctx, route) |
| 72 | + fmt.Printf("vpcLatticeService: %v \n", vpcLatticeService) |
| 73 | + |
| 74 | + // Get the target group and verify it's configured for HTTP |
| 75 | + tgSummary := testFramework.GetTargetGroupWithProtocol(ctx, httpSvc, "http", "http1") |
| 76 | + tg, err := testFramework.LatticeClient.GetTargetGroup(&vpclattice.GetTargetGroupInput{ |
| 77 | + TargetGroupIdentifier: aws.String(*tgSummary.Id), |
| 78 | + }) |
| 79 | + Expect(tg).To(Not(BeNil())) |
| 80 | + Expect(err).To(BeNil()) |
| 81 | + Expect(*tgSummary.VpcIdentifier).To(Equal(os.Getenv("CLUSTER_VPC_ID"))) |
| 82 | + |
| 83 | + // Verify the target group is configured for HTTP |
| 84 | + Expect(*tgSummary.Protocol).To(Equal("HTTP")) |
| 85 | + Expect(*tg.Config.ProtocolVersion).To(Equal("HTTP1")) |
| 86 | + |
| 87 | + // Verify targets are registered |
| 88 | + Eventually(func(g Gomega) { |
| 89 | + targets := testFramework.GetTargets(ctx, tgSummary, httpDeployment) |
| 90 | + for _, target := range targets { |
| 91 | + g.Expect(*target.Port).To(BeEquivalentTo(httpSvc.Spec.Ports[0].TargetPort.IntVal)) |
| 92 | + } |
| 93 | + }).WithTimeout(3 * time.Minute).WithOffset(1).Should(Succeed()) |
| 94 | + |
| 95 | + log.Println("Verifying traffic") |
| 96 | + dnsName := testFramework.GetVpcLatticeServiceDns(httpRoute.Name, httpRoute.Namespace) |
| 97 | + pods := testFramework.GetPodsByDeploymentName(httpDeployment.Name, httpDeployment.Namespace) |
| 98 | + Expect(len(pods)).To(BeEquivalentTo(1)) |
| 99 | + pod := pods[0] |
| 100 | + |
| 101 | + Eventually(func(g Gomega) { |
| 102 | + cmd := fmt.Sprintf("curl %s", dnsName) |
| 103 | + stdout, _, err := testFramework.PodExec(pod, cmd) |
| 104 | + g.Expect(err).To(BeNil()) |
| 105 | + g.Expect(stdout).To(ContainSubstring("handler pod")) |
| 106 | + }).Should(Succeed()) |
| 107 | + }) |
| 108 | + |
| 109 | + AfterAll(func() { |
| 110 | + testFramework.ExpectDeletedThenNotFound(ctx, |
| 111 | + httpRoute, |
| 112 | + httpDeployment, |
| 113 | + httpSvc, |
| 114 | + serviceImport, |
| 115 | + serviceExport, |
| 116 | + ) |
| 117 | + }) |
| 118 | +}) |
0 commit comments