Skip to content

Commit

Permalink
feat: pad nodebalancer backend names
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wbh1 committed Nov 28, 2023
1 parent 56e66f6 commit 6326eb2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cloud/linode/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,14 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, clusterNam
return l.createNodeBalancer(ctx, clusterName, service, configs)
}

func trimString(s string, maxLen int) string {
func coerceString(s string, minLen, maxLen int, padding string) string {
if len(padding) == 0 {
padding = "x"
}
if len(s) > maxLen {
return s[:maxLen]
} else if len(s) < minLen {
return coerceString(fmt.Sprintf("%s%s", padding, s), minLen, maxLen, padding)
}
return s
}
Expand All @@ -612,7 +617,8 @@ func (l *loadbalancers) buildNodeBalancerNodeCreateOptions(node *v1.Node, nodePo
return linodego.NodeBalancerNodeCreateOptions{
Address: fmt.Sprintf("%v:%v", getNodeInternalIP(node), nodePort),
// NodeBalancer backends must be 3-32 chars in length
Label: trimString(node.Name, 32),
// If < 3 chars, pad node name with "node-" prefix
Label: coerceString(node.Name, 3, 32, "node-"),
Mode: "accept",
Weight: 100,
}
Expand Down
42 changes: 42 additions & 0 deletions cloud/linode/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1852,3 +1852,45 @@ func addTLSSecret(t *testing.T, kubeClient kubernetes.Interface) {
t.Fatalf("failed to add TLS secret: %s\n", err)
}
}

func Test_LoadbalNodeNameCoercion(t *testing.T) {
type testCase struct {
nodeName string
padding string
expectedOutput string
}
testCases := []testCase{
{
nodeName: "n",
padding: "z",
expectedOutput: "zzn",
},
{
nodeName: "n",
padding: "node-",
expectedOutput: "node-n",
},
{
nodeName: "n",
padding: "",
expectedOutput: "xxn",
},
{
nodeName: "infra-logging-controlplane-3-atl1-us-prod",
padding: "node-",
expectedOutput: "infra-logging-controlplane-3-atl",
},
{
nodeName: "node1",
padding: "node-",
expectedOutput: "node1",
},
}

for _, tc := range testCases {
if out := coerceString(tc.nodeName, 3, 32, tc.padding); out != tc.expectedOutput {
t.Fatalf("Expected loadbal backend name to be %s (got: %s)", tc.expectedOutput, out)
}
}

}

0 comments on commit 6326eb2

Please sign in to comment.