Skip to content

Commit 8086249

Browse files
authored
Merge pull request #125 from linode-obs/feat/trim-nodebalancer-backend-labels
feat: truncate long labels in nodebalancers
2 parents 4ced791 + 7d957dc commit 8086249

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

cloud/linode/loadbalancers.go

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

647+
func coerceString(s string, minLen, maxLen int, padding string) string {
648+
if len(padding) == 0 {
649+
padding = "x"
650+
}
651+
if len(s) > maxLen {
652+
return s[:maxLen]
653+
} else if len(s) < minLen {
654+
return coerceString(fmt.Sprintf("%s%s", padding, s), minLen, maxLen, padding)
655+
}
656+
return s
657+
}
658+
647659
func (l *loadbalancers) buildNodeBalancerNodeCreateOptions(node *v1.Node, nodePort int32) linodego.NodeBalancerNodeCreateOptions {
648660
return linodego.NodeBalancerNodeCreateOptions{
649661
Address: fmt.Sprintf("%v:%v", getNodePrivateIP(node), nodePort),
650-
Label: node.Name,
651-
Mode: "accept",
652-
Weight: 100,
662+
// NodeBalancer backends must be 3-32 chars in length
663+
// If < 3 chars, pad node name with "node-" prefix
664+
Label: coerceString(node.Name, 3, 32, "node-"),
665+
Mode: "accept",
666+
Weight: 100,
653667
}
654668
}
655669

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)