Skip to content

Commit

Permalink
fix: Improve infra port name collisions (envoyproxy#2052)
Browse files Browse the repository at this point in the history
* fix: Improve infra port name collisions

Signed-off-by: Karol Szwaj <karol.szwaj@gmail.com>

* simplify string generation

Signed-off-by: Karol Szwaj <karol.szwaj@gmail.com>

* always hash container port name

Signed-off-by: Karol Szwaj <karol.szwaj@gmail.com>

---------

Signed-off-by: Karol Szwaj <karol.szwaj@gmail.com>
  • Loading branch information
cnvergence authored Oct 25, 2023
1 parent 4fa3d77 commit 49e6163
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 51 deletions.
7 changes: 4 additions & 3 deletions internal/infrastructure/kubernetes/proxy/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ var (
`"private_key":{"filename":"%s"}}}]}`, XdsTLSCertFilename, XdsTLSKeyFilename)
)

// ExpectedResourceHashedName returns expected resource hashed name.
// ExpectedResourceHashedName returns expected resource hashed name including up to the 48 characters of the original name.
func ExpectedResourceHashedName(name string) string {
hashedName := providerutils.GetHashedName(name)
hashedName := providerutils.GetHashedName(name, 48)
return fmt.Sprintf("%s-%s", config.EnvoyPrefix, hashedName)
}

Expand Down Expand Up @@ -115,7 +115,8 @@ func expectedProxyContainers(infra *ir.ProxyInfra,
return nil, fmt.Errorf("invalid protocol %q", p.Protocol)
}
port := corev1.ContainerPort{
Name: providerutils.ExpectedContainerPortHashedName(p.Name),
// hashed container port name including up to the 6 characters of the port name and the maximum of 15 characters.
Name: providerutils.GetHashedName(p.Name, 6),
ContainerPort: p.ContainerPort,
Protocol: protocol,
}
Expand Down
6 changes: 2 additions & 4 deletions internal/infrastructure/kubernetes/proxy/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package proxy
import (
"fmt"
"strconv"
"strings"

"golang.org/x/exp/maps"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -73,10 +72,9 @@ func (r *ResourceRender) Service() (*corev1.Service, error) {
if port.Protocol == ir.UDPProtocolType {
protocol = corev1.ProtocolUDP
}
// Listeners on merged gateways will have a port name {GatewayNamespace}/{GatewayName}/{ListenerName}.
portName := strings.ReplaceAll(port.Name, "/", "-")

p := corev1.ServicePort{
Name: portName,
Name: ExpectedResourceHashedName(port.Name),
Protocol: protocol,
Port: port.ServicePort,
TargetPort: target,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ spec:
name: envoy
ports:
- containerPort: 8080
name: EnvoyHTTPPort
name: EnvoyH-d76a15e2
protocol: TCP
- containerPort: 8443
name: EnvoyHTTPSPort
name: EnvoyH-6658f727
protocol: TCP
resources:
requests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ spec:
name: envoy
ports:
- containerPort: 8080
name: EnvoyHTTPPort
name: EnvoyH-d76a15e2
protocol: TCP
- containerPort: 8443
name: EnvoyHTTPSPort
name: EnvoyH-6658f727
protocol: TCP
resources:
requests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ spec:
name: envoy
ports:
- containerPort: 8080
name: EnvoyHTTPPort
name: EnvoyH-d76a15e2
protocol: TCP
- containerPort: 8443
name: EnvoyHTTPSPort
name: EnvoyH-6658f727
protocol: TCP
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ spec:
name: envoy
ports:
- containerPort: 8080
name: EnvoyHTTPPort
name: EnvoyH-d76a15e2
protocol: TCP
- containerPort: 8443
name: EnvoyHTTPSPort
name: EnvoyH-6658f727
protocol: TCP
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ spec:
name: envoy
ports:
- containerPort: 8080
name: EnvoyHTTPPort
name: EnvoyH-d76a15e2
protocol: TCP
- containerPort: 8443
name: EnvoyHTTPSPort
name: EnvoyH-6658f727
protocol: TCP
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ spec:
name: envoy
ports:
- containerPort: 8080
name: EnvoyHTTPPort
name: EnvoyH-d76a15e2
protocol: TCP
- containerPort: 8443
name: EnvoyHTTPSPort
name: EnvoyH-6658f727
protocol: TCP
resources:
requests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ spec:
name: envoy
ports:
- containerPort: 8080
name: EnvoyHTTPPort
name: EnvoyH-d76a15e2
protocol: TCP
- containerPort: 8443
name: EnvoyHTTPSPort
name: EnvoyH-6658f727
protocol: TCP
- containerPort: 19001
name: metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ spec:
name: envoy
ports:
- containerPort: 8080
name: EnvoyHTTPPort
name: EnvoyH-d76a15e2
protocol: TCP
- containerPort: 8443
name: EnvoyHTTPSPort
name: EnvoyH-6658f727
protocol: TCP
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ spec:
name: envoy
ports:
- containerPort: 8080
name: EnvoyHTTPPort
name: EnvoyH-d76a15e2
protocol: TCP
- containerPort: 8443
name: EnvoyHTTPSPort
name: EnvoyH-6658f727
protocol: TCP
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ spec:
name: envoy
ports:
- containerPort: 8080
name: EnvoyHTTPPort
name: EnvoyH-d76a15e2
protocol: TCP
- containerPort: 8443
name: EnvoyHTTPSPort
name: EnvoyH-6658f727
protocol: TCP
resources:
requests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ metadata:
namespace: envoy-gateway-system
spec:
ports:
- name: EnvoyHTTPPort
- name: envoy-EnvoyHTTPPort-d76a15e2
port: 0
protocol: TCP
targetPort: 8080
- name: EnvoyHTTPSPort
- name: envoy-EnvoyHTTPSPort-6658f727
port: 0
protocol: TCP
targetPort: 8443
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ metadata:
spec:
externalTrafficPolicy: Local
ports:
- name: EnvoyHTTPPort
- name: envoy-EnvoyHTTPPort-d76a15e2
port: 0
protocol: TCP
targetPort: 8080
- name: EnvoyHTTPSPort
- name: envoy-EnvoyHTTPSPort-6658f727
port: 0
protocol: TCP
targetPort: 8443
Expand Down
24 changes: 4 additions & 20 deletions internal/provider/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ func NamespacedName(obj client.Object) types.NamespacedName {
}
}

// GetHashedName returns a partially hashed name for the string including up to 48 characters of the original name before the hash.
// GetHashedName returns a partially hashed name for the string including up to the given length of the original name characters before the hash.
// Input `nsName` should be formatted as `{Namespace}/{ResourceName}`.
func GetHashedName(nsName string) string {
func GetHashedName(nsName string, length int) string {
hashedName := HashString(nsName)
// replace `/` with `-` to create a valid K8s resource name
resourceName := strings.ReplaceAll(nsName, "/", "-")

if len(resourceName) > 48 {
return fmt.Sprintf("%s-%s", resourceName[0:48], hashedName[0:8])
if length > 0 && len(resourceName) > length {
return fmt.Sprintf("%s-%s", resourceName[0:length], hashedName[0:8])
}
return fmt.Sprintf("%s-%s", resourceName, hashedName[0:8])
}
Expand All @@ -40,18 +39,3 @@ func HashString(str string) string {
h.Write([]byte(str))
return strings.ToLower(fmt.Sprintf("%x", h.Sum(nil)))
}

// ExpectedContainerPortHashedName returns expected container port name with max length of 15 characters.
// If mergedGateways is enabled or listener port name is larger than 15 characters it will return partially hashed name.
// Listeners on merged gateways have a infraIR port name {GatewayNamespace}/{GatewayName}/{ListenerName}.
func ExpectedContainerPortHashedName(name string) string {
if len(name) > 15 {
hashedName := HashString(name)
// replace `/` with `-` to create a valid K8s resource name
resourceName := strings.ReplaceAll(name, "/", "-")
listenerName := string(resourceName[2])

return fmt.Sprintf("%s-%s", listenerName, hashedName[0:14-len(listenerName)])
}
return name
}

0 comments on commit 49e6163

Please sign in to comment.