Skip to content

Commit 7d957dc

Browse files
committed
feat: pad nodebalancer backend names
Nodebalancers must have a name that is 3-32 characters in length. This adds support to pad the node name if it's too short, since in Kubernetes it's valid to have node names of only 1 or 2 characters.
1 parent 2c9e60a commit 7d957dc

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

cloud/linode/loadbalancers.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,14 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, clusterNam
644644
return l.createNodeBalancer(ctx, clusterName, service, configs)
645645
}
646646

647-
func trimString(s string, maxLen int) string {
647+
func coerceString(s string, minLen, maxLen int, padding string) string {
648+
if len(padding) == 0 {
649+
padding = "x"
650+
}
648651
if len(s) > maxLen {
649652
return s[:maxLen]
653+
} else if len(s) < minLen {
654+
return coerceString(fmt.Sprintf("%s%s", padding, s), minLen, maxLen, padding)
650655
}
651656
return s
652657
}
@@ -655,7 +660,8 @@ func (l *loadbalancers) buildNodeBalancerNodeCreateOptions(node *v1.Node, nodePo
655660
return linodego.NodeBalancerNodeCreateOptions{
656661
Address: fmt.Sprintf("%v:%v", getNodePrivateIP(node), nodePort),
657662
// NodeBalancer backends must be 3-32 chars in length
658-
Label: trimString(node.Name, 32),
663+
// If < 3 chars, pad node name with "node-" prefix
664+
Label: coerceString(node.Name, 3, 32, "node-"),
659665
Mode: "accept",
660666
Weight: 100,
661667
}

cloud/linode/loadbalancers_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,3 +2114,45 @@ func addTLSSecret(t *testing.T, kubeClient kubernetes.Interface) {
21142114
t.Fatalf("failed to add TLS secret: %s\n", err)
21152115
}
21162116
}
2117+
2118+
func Test_LoadbalNodeNameCoercion(t *testing.T) {
2119+
type testCase struct {
2120+
nodeName string
2121+
padding string
2122+
expectedOutput string
2123+
}
2124+
testCases := []testCase{
2125+
{
2126+
nodeName: "n",
2127+
padding: "z",
2128+
expectedOutput: "zzn",
2129+
},
2130+
{
2131+
nodeName: "n",
2132+
padding: "node-",
2133+
expectedOutput: "node-n",
2134+
},
2135+
{
2136+
nodeName: "n",
2137+
padding: "",
2138+
expectedOutput: "xxn",
2139+
},
2140+
{
2141+
nodeName: "infra-logging-controlplane-3-atl1-us-prod",
2142+
padding: "node-",
2143+
expectedOutput: "infra-logging-controlplane-3-atl",
2144+
},
2145+
{
2146+
nodeName: "node1",
2147+
padding: "node-",
2148+
expectedOutput: "node1",
2149+
},
2150+
}
2151+
2152+
for _, tc := range testCases {
2153+
if out := coerceString(tc.nodeName, 3, 32, tc.padding); out != tc.expectedOutput {
2154+
t.Fatalf("Expected loadbal backend name to be %s (got: %s)", tc.expectedOutput, out)
2155+
}
2156+
}
2157+
2158+
}

0 commit comments

Comments
 (0)