Skip to content

Commit 9bdf7a3

Browse files
committed
🐛 fix(loadbalancer): autogenerated names should be <32
1 parent d0aad80 commit 9bdf7a3

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

ccm/cloud/loadbalancer.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,12 @@ func NewLoadBalancer(svc *corev1.Service, addTags map[string]string) (*LoadBalan
210210
case len(lb.Name) == 0:
211211
// autogenerated name, multiple instances (add -a/b/c)
212212
lb.Name = make([]string, 0, lb.Instances)
213-
base := strings.ReplaceAll(string(svc.UID), "-", "") + "-"
213+
base := strings.ReplaceAll(string(svc.UID), "-", "")
214+
// UIDs are UUIDs, therefore more than 30 chars without '-', but we never know - things might change
215+
if len(base) > 30 {
216+
base = base[:30]
217+
}
218+
base += "-"
214219
for i := range lb.Instances {
215220
lb.Name = append(lb.Name, base+string([]byte{'a' + byte(i)}))
216221
}

ccm/cloud/loadbalancer_config_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,81 @@ func TestNewLoadBalancer(t *testing.T) {
6565
Connection: cloud.Connection{IdleTimeout: 60},
6666
IngressAddress: cloud.Hostname,
6767
},
68+
}, {
69+
name: "autogenerated names are truncated to 32 chars (non multi-az)",
70+
svc: &corev1.Service{
71+
ObjectMeta: metav1.ObjectMeta{
72+
Name: "foo",
73+
UID: "5dbb147b-3878-4d10-bf2d-96b2b463b8eb",
74+
},
75+
Spec: corev1.ServiceSpec{
76+
SessionAffinity: corev1.ServiceAffinityNone,
77+
Ports: []corev1.ServicePort{{Protocol: corev1.ProtocolTCP, Port: 42, NodePort: 43}},
78+
},
79+
},
80+
lb: &cloud.LoadBalancer{
81+
Name: []string{"5dbb147b38784d10bf2d96b2b463b8eb"},
82+
Instances: 1,
83+
ServiceName: "foo",
84+
Listeners: []cloud.Listener{{
85+
Port: 42,
86+
BackendPort: 43,
87+
}},
88+
ListenerDefaults: cloud.ListenerDefaults{
89+
SSLPorts: []string{"*"},
90+
},
91+
TargetRole: cloud.DefaultLoadBalancerConfiguration.TargetRole,
92+
HealthCheck: cloud.HealthCheck{
93+
Port: 43,
94+
Protocol: "tcp",
95+
Interval: cloud.DefaultLoadBalancerConfiguration.HealthCheck.Interval,
96+
Timeout: cloud.DefaultLoadBalancerConfiguration.HealthCheck.Timeout,
97+
HealthyThreshold: cloud.DefaultLoadBalancerConfiguration.HealthCheck.HealthyThreshold,
98+
UnhealthyThreshold: cloud.DefaultLoadBalancerConfiguration.HealthCheck.UnhealthyThreshold,
99+
},
100+
AllowFrom: ipNetSet("0.0.0.0/0"),
101+
Connection: cloud.Connection{IdleTimeout: 60},
102+
IngressAddress: cloud.Hostname,
103+
},
104+
}, {
105+
name: "autogenerated names are truncated to 32 chars (multi-az)",
106+
svc: &corev1.Service{
107+
ObjectMeta: metav1.ObjectMeta{
108+
Name: "foo",
109+
UID: "5dbb147b-3878-4d10-bf2d-96b2b463b8eb",
110+
Annotations: map[string]string{
111+
"service.beta.kubernetes.io/osc-load-balancer-instances": "2",
112+
},
113+
},
114+
Spec: corev1.ServiceSpec{
115+
SessionAffinity: corev1.ServiceAffinityNone,
116+
Ports: []corev1.ServicePort{{Protocol: corev1.ProtocolTCP, Port: 42, NodePort: 43}},
117+
},
118+
},
119+
lb: &cloud.LoadBalancer{
120+
Name: []string{"5dbb147b38784d10bf2d96b2b463b8-a", "5dbb147b38784d10bf2d96b2b463b8-b"},
121+
Instances: 2,
122+
ServiceName: "foo",
123+
Listeners: []cloud.Listener{{
124+
Port: 42,
125+
BackendPort: 43,
126+
}},
127+
ListenerDefaults: cloud.ListenerDefaults{
128+
SSLPorts: []string{"*"},
129+
},
130+
TargetRole: cloud.DefaultLoadBalancerConfiguration.TargetRole,
131+
HealthCheck: cloud.HealthCheck{
132+
Port: 43,
133+
Protocol: "tcp",
134+
Interval: cloud.DefaultLoadBalancerConfiguration.HealthCheck.Interval,
135+
Timeout: cloud.DefaultLoadBalancerConfiguration.HealthCheck.Timeout,
136+
HealthyThreshold: cloud.DefaultLoadBalancerConfiguration.HealthCheck.HealthyThreshold,
137+
UnhealthyThreshold: cloud.DefaultLoadBalancerConfiguration.HealthCheck.UnhealthyThreshold,
138+
},
139+
AllowFrom: ipNetSet("0.0.0.0/0"),
140+
Connection: cloud.Connection{IdleTimeout: 60},
141+
IngressAddress: cloud.Hostname,
142+
},
68143
}, {
69144
name: "Annotations are loaded",
70145
svc: &corev1.Service{

0 commit comments

Comments
 (0)