Skip to content

Commit

Permalink
chore(tests): matching ports for TCP and TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer04 committed Oct 20, 2023
1 parent 1336046 commit 1100749
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
66 changes: 31 additions & 35 deletions test/integration/tcproute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/kong/kubernetes-testing-framework/pkg/clusters"
ktfkong "github.com/kong/kubernetes-testing-framework/pkg/clusters/addons/kong"
"github.com/kong/kubernetes-testing-framework/pkg/utils/kubernetes/generators"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
Expand All @@ -27,6 +28,8 @@ import (
"github.com/kong/kubernetes-ingress-controller/v2/test/internal/helpers"
)

const gatewayTCPPortName = "tcp"

func TestTCPRouteEssentials(t *testing.T) {
ctx := context.Background()
RunWhenKongExpressionRouter(t)
Expand All @@ -49,12 +52,12 @@ func TestTCPRouteEssentials(t *testing.T) {
require.NoError(t, err)
cleaner.Add(gwc)

t.Log("deploying a gateway to the test cluster using unmanaged gateway mode and port 8888")
t.Logf("deploying a gateway to the test cluster using unmanaged gateway mode and port %d", ktfkong.DefaultTCPServicePort)
gatewayName := uuid.NewString()
gateway, err := DeployGateway(ctx, gatewayClient, ns.Name, gatewayClassName, func(gw *gatewayapi.Gateway) {
gw.Name = gatewayName
gw.Spec.Listeners = []gatewayapi.Listener{{
Name: "tcp",
Name: gatewayTCPPortName,
Protocol: gatewayapi.TCPProtocolType,
Port: gatewayapi.PortNumber(ktfkong.DefaultTCPServicePort),
}}
Expand Down Expand Up @@ -94,55 +97,49 @@ func TestTCPRouteEssentials(t *testing.T) {

t.Logf("exposing deployment %s/%s via service", deployment1.Namespace, deployment1.Name)
service1 := generators.NewServiceForDeployment(deployment1, corev1.ServiceTypeLoadBalancer)
// we have to override the ports so that we can map the default TCP port from
// the Kong Gateway deployment to the tcpecho port, as this is what will be
// used to route the traffic at the Gateway (at the time of writing, the
// Kong Gateway doesn't support an API for dynamically adding these ports. The
// ports must be added manually to the config or ENV).
// Use the same port as the default TCP port from the Kong Gateway deployment
// to the tcpecho port, as this is what will be used to route the traffic at the Gateway.
const service1Port = ktfkong.DefaultTCPServicePort
service1.Spec.Ports = []corev1.ServicePort{{
Name: "tcp",
Protocol: corev1.ProtocolTCP,
Port: ktfkong.DefaultTCPServicePort,
Port: service1Port,
TargetPort: intstr.FromInt(test.EchoTCPPort),
}}
service1, err = env.Cluster().Client().CoreV1().Services(ns.Name).Create(ctx, service1, metav1.CreateOptions{})
assert.NoError(t, err)
cleaner.Add(service1)

t.Logf("exposing deployment %s/%s via service", deployment2.Namespace, deployment2.Name)
const service2Port = 8080 // Use a different port that listening on the Gateway for TCP.
service2 := generators.NewServiceForDeployment(deployment2, corev1.ServiceTypeLoadBalancer)
// we have to override the ports so that we can map the default TCP port from
// the Kong Gateway deployment to the tcpecho port, as this is what will be
// used to route the traffic at the Gateway (at the time of writing, the
// Kong Gateway doesn't support an API for dynamically adding these ports. The
// ports must be added manually to the config or ENV).
service2.Spec.Ports = []corev1.ServicePort{{
Name: "tcp",
Protocol: corev1.ProtocolTCP,
Port: ktfkong.DefaultTCPServicePort,
Port: service2Port,
TargetPort: intstr.FromInt(test.EchoTCPPort),
}}
service2, err = env.Cluster().Client().CoreV1().Services(ns.Name).Create(ctx, service2, metav1.CreateOptions{})
assert.NoError(t, err)
cleaner.Add(service2)

t.Logf("creating a tcproute to access deployment %s via kong", deployment1.Name)
tcpPortDefault := gatewayapi.PortNumber(ktfkong.DefaultTCPServicePort)
t.Logf("creating a TCPRoute to access deployment %s via kong", deployment1.Name)
tcpRoute := &gatewayapi.TCPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: uuid.NewString(),
},
Spec: gatewayapi.TCPRouteSpec{
CommonRouteSpec: gatewayapi.CommonRouteSpec{
ParentRefs: []gatewayapi.ParentReference{{
Name: gatewayapi.ObjectName(gatewayName),
Name: gatewayapi.ObjectName(gatewayName),
SectionName: lo.ToPtr(gatewayapi.SectionName(gatewayTCPPortName)),
}},
},
Rules: []gatewayapi.TCPRouteRule{{
BackendRefs: []gatewayapi.BackendRef{{
BackendObjectReference: gatewayapi.BackendObjectReference{
Name: gatewayapi.ObjectName(service1.Name),
Port: &tcpPortDefault,
Port: lo.ToPtr(gatewayapi.PortNumber(service1Port)),
},
}},
}},
Expand Down Expand Up @@ -254,7 +251,7 @@ func TestTCPRouteEssentials(t *testing.T) {
gateway, err = DeployGateway(ctx, gatewayClient, ns.Name, gatewayClassName, func(gw *gatewayapi.Gateway) {
gw.Name = gatewayName
gw.Spec.Listeners = []gatewayapi.Listener{{
Name: "tcp",
Name: gatewayTCPPortName,
Protocol: gatewayapi.TCPProtocolType,
Port: gatewayapi.PortNumber(ktfkong.DefaultTCPServicePort),
}}
Expand Down Expand Up @@ -292,7 +289,7 @@ func TestTCPRouteEssentials(t *testing.T) {
gateway, err = DeployGateway(ctx, gatewayClient, ns.Name, gatewayClassName, func(gw *gatewayapi.Gateway) {
gw.Name = gatewayName
gw.Spec.Listeners = []gatewayapi.Listener{{
Name: "tcp",
Name: gatewayTCPPortName,
Protocol: gatewayapi.TCPProtocolType,
Port: gatewayapi.PortNumber(ktfkong.DefaultTCPServicePort),
}}
Expand All @@ -317,13 +314,13 @@ func TestTCPRouteEssentials(t *testing.T) {
{
BackendObjectReference: gatewayapi.BackendObjectReference{
Name: gatewayapi.ObjectName(service1.Name),
Port: &tcpPortDefault,
Port: lo.ToPtr(gatewayapi.PortNumber(service1Port)),
},
},
{
BackendObjectReference: gatewayapi.BackendObjectReference{
Name: gatewayapi.ObjectName(service2.Name),
Port: &tcpPortDefault,
Port: lo.ToPtr(gatewayapi.PortNumber(service2Port)),
},
},
}
Expand Down Expand Up @@ -365,7 +362,7 @@ func TestTCPRouteEssentials(t *testing.T) {
_, err = DeployGateway(ctx, gatewayClient, ns.Name, gatewayClassName, func(gw *gatewayapi.Gateway) {
gw.Name = gatewayName
gw.Spec.Listeners = []gatewayapi.Listener{{
Name: "tcp",
Name: gatewayTCPPortName,
Protocol: gatewayapi.TCPProtocolType,
Port: gatewayapi.PortNumber(ktfkong.DefaultTCPServicePort),
}}
Expand Down Expand Up @@ -428,7 +425,7 @@ func TestTCPRouteReferenceGrant(t *testing.T) {
gateway, err := DeployGateway(ctx, gatewayClient, ns.Name, gatewayClassName, func(gw *gatewayapi.Gateway) {
gw.Name = gatewayName
gw.Spec.Listeners = []gatewayapi.Listener{{
Name: "tcp",
Name: gatewayTCPPortName,
Protocol: gatewayapi.TCPProtocolType,
Port: gatewayapi.PortNumber(ktfkong.DefaultTCPServicePort),
}}
Expand Down Expand Up @@ -468,35 +465,33 @@ func TestTCPRouteReferenceGrant(t *testing.T) {

t.Logf("exposing deployment %s/%s via service", deployment1.Namespace, deployment1.Name)
service1 := generators.NewServiceForDeployment(deployment1, corev1.ServiceTypeLoadBalancer)
// we have to override the ports so that we can map the default TCP port from
// the Kong Gateway deployment to the tcpecho port, as this is what will be
// used to route the traffic at the Gateway (at the time of writing, the
// Kong Gateway doesn't support an API for dynamically adding these ports. The
// ports must be added manually to the config or ENV).
// Use the same port as the default TCP port from the Kong Gateway deployment
// to the tcpecho port, as this is what will be used to route the traffic at the Gateway.
const service1Port = ktfkong.DefaultTCPServicePort
service1.Spec.Ports = []corev1.ServicePort{{
Name: "tcp",
Protocol: corev1.ProtocolTCP,
Port: ktfkong.DefaultTCPServicePort,
Port: service1Port,
TargetPort: intstr.FromInt(test.EchoTCPPort),
}}
service1, err = env.Cluster().Client().CoreV1().Services(ns.Name).Create(ctx, service1, metav1.CreateOptions{})
assert.NoError(t, err)
cleaner.Add(service1)

t.Logf("exposing deployment %s/%s via service", deployment2.Namespace, deployment2.Name)
const service2Port = 8080 // Use a different port that listening on the Gateway for TCP.
service2 := generators.NewServiceForDeployment(deployment2, corev1.ServiceTypeLoadBalancer)
service2.Spec.Ports = []corev1.ServicePort{{
Name: "tcp",
Protocol: corev1.ProtocolTCP,
Port: ktfkong.DefaultTCPServicePort,
Port: service2Port,
TargetPort: intstr.FromInt(test.EchoTCPPort),
}}
service2, err = env.Cluster().Client().CoreV1().Services(otherNs.Name).Create(ctx, service2, metav1.CreateOptions{})
assert.NoError(t, err)
cleaner.Add(service2)

t.Logf("creating a tcproute to access deployment %s via kong", deployment1.Name)
tcpPortDefault := gatewayapi.PortNumber(ktfkong.DefaultTCPServicePort)
remoteNamespace := gatewayapi.Namespace(otherNs.Name)
tcproute := &gatewayapi.TCPRoute{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -505,22 +500,23 @@ func TestTCPRouteReferenceGrant(t *testing.T) {
Spec: gatewayapi.TCPRouteSpec{
CommonRouteSpec: gatewayapi.CommonRouteSpec{
ParentRefs: []gatewayapi.ParentReference{{
Name: gatewayapi.ObjectName(gatewayName),
Name: gatewayapi.ObjectName(gatewayName),
SectionName: lo.ToPtr(gatewayapi.SectionName(gatewayTCPPortName)),
}},
},
Rules: []gatewayapi.TCPRouteRule{{
BackendRefs: []gatewayapi.BackendRef{
{
BackendObjectReference: gatewayapi.BackendObjectReference{
Name: gatewayapi.ObjectName(service1.Name),
Port: &tcpPortDefault,
Port: lo.ToPtr(gatewayapi.PortNumber(service1Port)),
},
},
{
BackendObjectReference: gatewayapi.BackendObjectReference{
Name: gatewayapi.ObjectName(service2.Name),
Namespace: &remoteNamespace,
Port: &tcpPortDefault,
Port: lo.ToPtr(gatewayapi.PortNumber(service2Port)),
},
},
},
Expand Down
11 changes: 10 additions & 1 deletion test/integration/tlsroute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
"github.com/kong/kubernetes-testing-framework/pkg/clusters"
ktfkong "github.com/kong/kubernetes-testing-framework/pkg/clusters/addons/kong"
"github.com/kong/kubernetes-testing-framework/pkg/utils/kubernetes/generators"
"github.com/samber/lo"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
gatewayclient "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"

"github.com/kong/kubernetes-ingress-controller/v2/internal/gatewayapi"
Expand Down Expand Up @@ -204,7 +206,14 @@ func TestTLSRoutePassthroughReferenceGrant(t *testing.T) {
cleaner.Add(service)

t.Logf("exposing deployment %s/%s via service", deployment2.Namespace, deployment2.Name)
const service2Port = 8443 // Use a different port that listening on the Gateway for TLS.
service2 := generators.NewServiceForDeployment(deployment2, corev1.ServiceTypeLoadBalancer)
service2.Spec.Ports = []corev1.ServicePort{{
Name: "tls",
Protocol: corev1.ProtocolTCP,
Port: service2Port,
TargetPort: intstr.FromInt(tlsEchoPort),
}}
service2, err = env.Cluster().Client().CoreV1().Services(ns.Name).Create(ctx, service2, metav1.CreateOptions{})
require.NoError(t, err)
cleaner.Add(service2)
Expand Down Expand Up @@ -233,7 +242,7 @@ func TestTLSRoutePassthroughReferenceGrant(t *testing.T) {
{
BackendObjectReference: gatewayapi.BackendObjectReference{
Name: gatewayapi.ObjectName(service2.Name),
Port: &backendTLSPort,
Port: lo.ToPtr(gatewayapi.PortNumber(service2Port)),
},
},
},
Expand Down

0 comments on commit 1100749

Please sign in to comment.