Skip to content

Commit 4cbb13d

Browse files
authored
Sanitize K8s node names to meet load balancer target displayname requirements (#1180)
* Sanitize K8s node names to meet load balancer target displayname requirements * Add PR Review suggestions
1 parent f401f36 commit 4cbb13d

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

pkg/ccm/loadbalancer_spec.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package ccm
22

33
import (
4+
"crypto/sha256"
45
"fmt"
56
"net/netip"
7+
"regexp"
68
"slices"
79
"strconv"
810
"strings"
@@ -200,6 +202,11 @@ var appoximateFlavorsMap = map[string]string{
200202
"22b37153-8817-4c85-9805-92426b2f903c": p10, // t2i.1
201203
}
202204

205+
var (
206+
// invalidTargetDisplayNameCharsRegexp matches any character that is NOT alphanumeric or a hyphen
207+
invalidTargetDisplayNameCharsRegexp = regexp.MustCompile(`[^a-zA-Z0-9-]`)
208+
)
209+
203210
// proxyProtocolEnableForPort determines whether portNumber should use the TCP proxy protocol (instead of TCP).
204211
func proxyProtocolEnableForPort(tcpProxyProtocolEnabled bool, tcpProxyProtocolPortFilter []uint16, portNumber int32) bool {
205212
if !tcpProxyProtocolEnabled {
@@ -480,7 +487,7 @@ func lbSpecFromService(
480487
address := node.Status.Addresses[j]
481488
if address.Type == corev1.NodeInternalIP {
482489
targets = append(targets, loadbalancer.Target{
483-
DisplayName: &node.Name,
490+
DisplayName: new(sanitizeNodeName(node.Name)),
484491
Ip: &address.Address,
485492
})
486493
break
@@ -754,3 +761,24 @@ func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateL
754761

755762
return fulfills, immutableChanged
756763
}
764+
765+
// sanitizeNodeName returns a node name which fits in the DisplayName of a target.
766+
// Replaces not allowed chars with
767+
func sanitizeNodeName(nodeName string) string {
768+
var sanitizedNodeName string
769+
sanitizedNodeName = invalidTargetDisplayNameCharsRegexp.ReplaceAllString(nodeName, "-")
770+
771+
// return node name if not to long and if not contain any invalid chars
772+
if len(sanitizedNodeName) <= 63 &&
773+
nodeName == sanitizedNodeName {
774+
return nodeName
775+
}
776+
777+
if len(sanitizedNodeName) > 54 {
778+
sanitizedNodeName = sanitizedNodeName[0:54]
779+
}
780+
781+
sanitizedNodeName += fmt.Sprintf("-%x", sha256.Sum256([]byte(nodeName)))[:8]
782+
783+
return sanitizedNodeName
784+
}

pkg/ccm/loadbalancer_spec_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,3 +1989,37 @@ var _ = DescribeTable("compareLBwithSpec",
19891989
},
19901990
}),
19911991
)
1992+
1993+
var _ = DescribeTable("sanitizeNodeName",
1994+
func(name, safe string) {
1995+
Expect(sanitizeNodeName(name)).To(Equal(safe))
1996+
},
1997+
Entry("Already safe",
1998+
"localhost",
1999+
"localhost",
2000+
),
2001+
Entry("Already safe",
2002+
"node-01",
2003+
"node-01",
2004+
),
2005+
Entry("Already safe exact 63 chars",
2006+
"node-0123456789012345678901234567890123456789001234567890123456",
2007+
"node-0123456789012345678901234567890123456789001234567890123456",
2008+
),
2009+
Entry("replace dots",
2010+
"node-01.example.com",
2011+
"node-01-example-com-7c98300",
2012+
),
2013+
Entry("shorten",
2014+
"a-very-long-node-01234567890123456789012345678901234567890.example.com",
2015+
"a-very-long-node-0123456789012345678901234567890123456-519d04d",
2016+
),
2017+
Entry("shorten and replace dots",
2018+
"a.very.long.node-01234567890123456789012345678901234567890.example.com",
2019+
"a-very-long-node-0123456789012345678901234567890123456-f80f0fa",
2020+
),
2021+
Entry("replace dots exact 54 chars",
2022+
"a.very.long.node-0123456789012345678901234.example.com",
2023+
"a-very-long-node-0123456789012345678901234-example-com-e241059",
2024+
),
2025+
)

0 commit comments

Comments
 (0)