|
1 | 1 | package ccm |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/sha256" |
4 | 5 | "fmt" |
5 | 6 | "net/netip" |
| 7 | + "regexp" |
6 | 8 | "slices" |
7 | 9 | "strconv" |
8 | 10 | "strings" |
@@ -200,6 +202,11 @@ var appoximateFlavorsMap = map[string]string{ |
200 | 202 | "22b37153-8817-4c85-9805-92426b2f903c": p10, // t2i.1 |
201 | 203 | } |
202 | 204 |
|
| 205 | +var ( |
| 206 | + // invalidTargetDisplayNameCharsRegexp matches any character that is NOT alphanumeric or a hyphen |
| 207 | + invalidTargetDisplayNameCharsRegexp = regexp.MustCompile(`[^a-zA-Z0-9-]`) |
| 208 | +) |
| 209 | + |
203 | 210 | // proxyProtocolEnableForPort determines whether portNumber should use the TCP proxy protocol (instead of TCP). |
204 | 211 | func proxyProtocolEnableForPort(tcpProxyProtocolEnabled bool, tcpProxyProtocolPortFilter []uint16, portNumber int32) bool { |
205 | 212 | if !tcpProxyProtocolEnabled { |
@@ -480,7 +487,7 @@ func lbSpecFromService( |
480 | 487 | address := node.Status.Addresses[j] |
481 | 488 | if address.Type == corev1.NodeInternalIP { |
482 | 489 | targets = append(targets, loadbalancer.Target{ |
483 | | - DisplayName: &node.Name, |
| 490 | + DisplayName: new(sanitizeNodeName(node.Name)), |
484 | 491 | Ip: &address.Address, |
485 | 492 | }) |
486 | 493 | break |
@@ -754,3 +761,24 @@ func compareLBwithSpec(lb *loadbalancer.LoadBalancer, spec *loadbalancer.CreateL |
754 | 761 |
|
755 | 762 | return fulfills, immutableChanged |
756 | 763 | } |
| 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 | +} |
0 commit comments