Skip to content

Commit

Permalink
fix: dont shift listener ports for Standalone mode (#5027)
Browse files Browse the repository at this point in the history
* fix: dont shift listener ports for Standalone mode

Fixes: #4981

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* test

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* fix lint

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

---------

Signed-off-by: Arko Dasgupta <arko@tetrate.io>
  • Loading branch information
arkodg authored Jan 10, 2025
1 parent dff0531 commit 84f2ad2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 33 deletions.
19 changes: 0 additions & 19 deletions internal/gatewayapi/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,25 +249,6 @@ func OwnerLabels(gateway *gwapiv1.Gateway, mergeGateways bool) map[string]string
return GatewayOwnerLabels(gateway.Namespace, gateway.Name)
}

// servicePortToContainerPort translates a service port into an ephemeral
// container port.
func servicePortToContainerPort(servicePort int32, envoyProxy *egv1a1.EnvoyProxy) int32 {
if envoyProxy != nil {
if !envoyProxy.NeedToSwitchPorts() {
return servicePort
}
}

// If the service port is a privileged port (1-1023)
// add a constant to the value converting it into an ephemeral port.
// This allows the container to bind to the port without needing a
// CAP_NET_BIND_SERVICE capability.
if servicePort < minEphemeralPort {
return servicePort + wellKnownPortShift
}
return servicePort
}

// computeHosts returns a list of intersecting listener hostnames and route hostnames
// that don't intersect with other listener hostnames.
func computeHosts(routeHostnames []string, listenerContext *ListenerContext) []string {
Expand Down
26 changes: 25 additions & 1 deletion internal/gatewayapi/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource

// Add the listener to the Xds IR
servicePort := &protocolPort{protocol: listener.Protocol, port: int32(listener.Port)}
containerPort := servicePortToContainerPort(int32(listener.Port), gateway.envoyProxy)
containerPort := t.servicePortToContainerPort(int32(listener.Port), gateway.envoyProxy)
switch listener.Protocol {
case gwapiv1.HTTPProtocolType, gwapiv1.HTTPSProtocolType:
irListener := &ir.HTTPListener{
Expand Down Expand Up @@ -552,3 +552,27 @@ func validCELExpression(expr string) bool {
_, issue := celEnv.Parse(expr)
return issue.Err() == nil
}

// servicePortToContainerPort translates a service port into an ephemeral
// container port.
func (t *Translator) servicePortToContainerPort(servicePort int32, envoyProxy *egv1a1.EnvoyProxy) int32 {
if t.ListenerPortShiftDisabled {
return servicePort
}

if envoyProxy != nil {
if !envoyProxy.NeedToSwitchPorts() {
return servicePort
}
}

// If the service port is a privileged port (1-1023)
// add a constant to the value converting it into an ephemeral port.
// This allows the container to bind to the port without needing a
// CAP_NET_BIND_SERVICE capability.
if servicePort < minEphemeralPort {
return servicePort + wellKnownPortShift
}

return servicePort
}
17 changes: 9 additions & 8 deletions internal/gatewayapi/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,15 @@ func (r *Runner) subscribeAndTranslate(ctx context.Context) {
for _, resources := range *val {
// Translate and publish IRs.
t := &gatewayapi.Translator{
GatewayControllerName: r.Server.EnvoyGateway.Gateway.ControllerName,
GatewayClassName: gwapiv1.ObjectName(resources.GatewayClass.Name),
GlobalRateLimitEnabled: r.EnvoyGateway.RateLimit != nil,
EnvoyPatchPolicyEnabled: r.EnvoyGateway.ExtensionAPIs != nil && r.EnvoyGateway.ExtensionAPIs.EnableEnvoyPatchPolicy,
BackendEnabled: r.EnvoyGateway.ExtensionAPIs != nil && r.EnvoyGateway.ExtensionAPIs.EnableBackend,
Namespace: r.Namespace,
MergeGateways: gatewayapi.IsMergeGatewaysEnabled(resources),
WasmCache: r.wasmCache,
GatewayControllerName: r.Server.EnvoyGateway.Gateway.ControllerName,
GatewayClassName: gwapiv1.ObjectName(resources.GatewayClass.Name),
GlobalRateLimitEnabled: r.EnvoyGateway.RateLimit != nil,
EnvoyPatchPolicyEnabled: r.EnvoyGateway.ExtensionAPIs != nil && r.EnvoyGateway.ExtensionAPIs.EnableEnvoyPatchPolicy,
BackendEnabled: r.EnvoyGateway.ExtensionAPIs != nil && r.EnvoyGateway.ExtensionAPIs.EnableBackend,
Namespace: r.Namespace,
MergeGateways: gatewayapi.IsMergeGatewaysEnabled(resources),
WasmCache: r.wasmCache,
ListenerPortShiftDisabled: r.EnvoyGateway.Provider != nil && r.EnvoyGateway.Provider.IsRunningOnHost(),
}

// If an extension is loaded, pass its supported groups/kinds to the translator
Expand Down
5 changes: 5 additions & 0 deletions internal/gatewayapi/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ type Translator struct {

// WasmCache is the cache for Wasm modules.
WasmCache wasm.Cache

// ListenerPortShiftDisabled disables translating the
// gateway listener port into a non privileged port
// and reuses the specified value.
ListenerPortShiftDisabled bool
}

type TranslateResult struct {
Expand Down
16 changes: 11 additions & 5 deletions internal/gatewayapi/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,10 @@ func TestIsValidCrossNamespaceRef(t *testing.T) {

func TestServicePortToContainerPort(t *testing.T) {
testCases := []struct {
servicePort int32
containerPort int32
envoyProxy *egv1a1.EnvoyProxy
servicePort int32
containerPort int32
envoyProxy *egv1a1.EnvoyProxy
listenerPortShiftDisabled bool
}{
{
servicePort: 99,
Expand Down Expand Up @@ -826,10 +827,15 @@ func TestServicePortToContainerPort(t *testing.T) {
},
},
},
{
servicePort: 99,
containerPort: 99,
listenerPortShiftDisabled: true,
},
}

for _, tc := range testCases {
got := servicePortToContainerPort(tc.servicePort, tc.envoyProxy)
translator := &Translator{ListenerPortShiftDisabled: tc.listenerPortShiftDisabled}
got := translator.servicePortToContainerPort(tc.servicePort, tc.envoyProxy)
assert.Equal(t, tc.containerPort, got)
}
}
Expand Down

0 comments on commit 84f2ad2

Please sign in to comment.