Skip to content

Commit f9979cd

Browse files
authored
Correct additional target group creation when using the ExportedPorts field with HTTPRoutes (#790)
1 parent 04a3aff commit f9979cd

File tree

3 files changed

+125
-15
lines changed

3 files changed

+125
-15
lines changed

pkg/gateway/model_build_targetgroup.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,6 @@ func (t *svcExportTargetGroupModelBuildTask) buildTargetGroupForExportedPort(ctx
225225
spec.K8SServiceNamespace = t.serviceExport.Namespace
226226
spec.K8SProtocolVersion = protocolVersion
227227

228-
// Add a tag for the route type to help with identification
229-
// This is not used by the controller but can be helpful for debugging
230-
if exportedPort.RouteType != "" {
231-
spec.K8SProtocolVersion = exportedPort.RouteType
232-
}
233-
234228
stackTG, err := model.NewTargetGroup(t.stack, spec)
235229
if err != nil {
236230
return nil, err

pkg/gateway/model_build_targetgroup_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ func Test_TGModelByServiceExportWithExportedPorts(t *testing.T) {
693693
wantErrIsNil: true,
694694
wantTargetGroupCount: 3,
695695
wantPorts: []int32{80, 8081, 443},
696-
wantRouteTypes: []string{"HTTP", "GRPC", "TLS"},
696+
wantRouteTypes: []string{vpclattice.TargetGroupProtocolVersionHttp1, vpclattice.TargetGroupProtocolVersionGrpc, ""},
697697
},
698698
{
699699
name: "ServiceExport with single exportedPort",
@@ -731,7 +731,7 @@ func Test_TGModelByServiceExportWithExportedPorts(t *testing.T) {
731731
wantErrIsNil: true,
732732
wantTargetGroupCount: 1,
733733
wantPorts: []int32{80},
734-
wantRouteTypes: []string{"HTTP"},
734+
wantRouteTypes: []string{vpclattice.TargetGroupProtocolVersionHttp1},
735735
},
736736
{
737737
name: "ServiceExport with no exportedPorts (legacy behavior)",
@@ -812,20 +812,18 @@ func Test_TGModelByServiceExportWithExportedPorts(t *testing.T) {
812812
seenPorts[tg.Spec.Port] = true
813813
seenRouteTypes[tg.Spec.K8SProtocolVersion] = true
814814

815-
// Check protocol and protocolVersion based on route type
815+
// Check protocol and protocolVersion based on K8SProtocolVersion
816816
switch tg.Spec.K8SProtocolVersion {
817-
case "HTTP":
817+
case vpclattice.TargetGroupProtocolVersionHttp1:
818818
assert.Equal(t, vpclattice.TargetGroupProtocolHttp, tg.Spec.Protocol)
819819
assert.Equal(t, vpclattice.TargetGroupProtocolVersionHttp1, tg.Spec.ProtocolVersion)
820-
case "GRPC":
820+
case vpclattice.TargetGroupProtocolVersionGrpc:
821821
assert.Equal(t, vpclattice.TargetGroupProtocolHttp, tg.Spec.Protocol)
822822
assert.Equal(t, vpclattice.TargetGroupProtocolVersionGrpc, tg.Spec.ProtocolVersion)
823-
case "TLS":
823+
case "":
824+
// TLS case - no protocol version for TCP
824825
assert.Equal(t, vpclattice.TargetGroupProtocolTcp, tg.Spec.Protocol)
825826
assert.Equal(t, "", tg.Spec.ProtocolVersion)
826-
case vpclattice.TargetGroupProtocolVersionHttp1:
827-
// Legacy behavior
828-
assert.Equal(t, vpclattice.TargetGroupProtocolHttp, tg.Spec.Protocol)
829827
}
830828
}
831829

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)