Skip to content

Commit 1a55d0f

Browse files
committed
fix: should truncate long subnet name on lb rules
1 parent 939da5e commit 1a55d0f

File tree

5 files changed

+113
-12
lines changed

5 files changed

+113
-12
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ func (az *Cloud) getServiceLoadBalancerStatus(service *v1.Service, lb *network.L
405405
return nil, nil
406406
}
407407
isInternal := requiresInternalLoadBalancer(service)
408-
lbFrontendIPConfigName := az.getFrontendIPConfigName(service, subnet(service))
408+
lbFrontendIPConfigName := az.getFrontendIPConfigName(service)
409409
serviceName := getServiceName(service)
410410
for _, ipConfiguration := range *lb.FrontendIPConfigurations {
411411
if lbFrontendIPConfigName == *ipConfiguration.Name {
@@ -693,7 +693,7 @@ func (az *Cloud) reconcileLoadBalancer(clusterName string, service *v1.Service,
693693
}
694694
lbName := *lb.Name
695695
klog.V(2).Infof("reconcileLoadBalancer for service(%s): lb(%s) wantLb(%t) resolved load balancer name", serviceName, lbName, wantLb)
696-
lbFrontendIPConfigName := az.getFrontendIPConfigName(service, subnet(service))
696+
lbFrontendIPConfigName := az.getFrontendIPConfigName(service)
697697
lbFrontendIPConfigID := az.getFrontendIPConfigID(lbName, lbFrontendIPConfigName)
698698
lbBackendPoolName := getBackendPoolName(az.ipv6DualStackEnabled, clusterName, service)
699699
lbBackendPoolID := az.getBackendPoolID(lbName, lbBackendPoolName)
@@ -1026,7 +1026,7 @@ func (az *Cloud) reconcileLoadBalancerRule(
10261026
}
10271027

10281028
for _, protocol := range protocols {
1029-
lbRuleName := az.getLoadBalancerRuleName(service, protocol, port.Port, subnet(service))
1029+
lbRuleName := az.getLoadBalancerRuleName(service, protocol, port.Port)
10301030
klog.V(2).Infof("reconcileLoadBalancerRule lb name (%s) rule name (%s)", lbName, lbRuleName)
10311031

10321032
transportProto, _, probeProto, err := getProtocolsFromKubernetesProtocol(protocol)

staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ func TestGetServiceLoadBalancerStatus(t *testing.T) {
16271627
},
16281628
{
16291629
desc: "getServiceLoadBalancerStatus shall return nil if lb.FrontendIPConfigurations.name != " +
1630-
"az.getFrontendIPConfigName(service, subnet(service))",
1630+
"az.getFrontendIPConfigName(service)",
16311631
service: &internalService,
16321632
lb: &lb3,
16331633
},

staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,21 @@ func getBackendPoolName(ipv6DualStackEnabled bool, clusterName string, service *
273273
return clusterName
274274
}
275275

276-
func (az *Cloud) getLoadBalancerRuleName(service *v1.Service, protocol v1.Protocol, port int32, subnetName *string) string {
276+
func (az *Cloud) getLoadBalancerRuleName(service *v1.Service, protocol v1.Protocol, port int32) string {
277277
prefix := az.getRulePrefix(service)
278-
if subnetName == nil {
279-
return fmt.Sprintf("%s-%s-%d", prefix, protocol, port)
278+
ruleName := fmt.Sprintf("%s-%s-%d", prefix, protocol, port)
279+
subnet := subnet(service)
280+
if subnet == nil {
281+
return ruleName
280282
}
281-
return fmt.Sprintf("%s-%s-%s-%d", prefix, *subnetName, protocol, port)
283+
284+
// Load balancer rule name must be less or equal to 80 charactors, so excluding the hyphen two segments cannot exceed 79
285+
subnetSegment := *subnet
286+
if len(ruleName) + len(subnetSegment) > 79 {
287+
subnetSegment = subnetSegment[:79 - len(ruleName)]
288+
}
289+
290+
return fmt.Sprintf("%s-%s-%s-%d", prefix, subnetSegment, protocol, port)
282291
}
283292

284293
func (az *Cloud) getSecurityRuleName(service *v1.Service, port v1.ServicePort, sourceAddrPrefix string) string {
@@ -316,10 +325,17 @@ func (az *Cloud) serviceOwnsFrontendIP(fip network.FrontendIPConfiguration, serv
316325
return strings.HasPrefix(*fip.Name, baseName)
317326
}
318327

319-
func (az *Cloud) getFrontendIPConfigName(service *v1.Service, subnetName *string) string {
328+
func (az *Cloud) getFrontendIPConfigName(service *v1.Service) string {
320329
baseName := az.GetLoadBalancerName(context.TODO(), "", service)
330+
subnetName := subnet(service)
321331
if subnetName != nil {
322-
return fmt.Sprintf("%s-%s", baseName, *subnetName)
332+
ipcName := fmt.Sprintf("%s-%s", baseName, *subnetName)
333+
334+
// Azure lb front end configuration name must not exceed 80 charactors
335+
if len(ipcName) > 80 {
336+
ipcName = ipcName[:80]
337+
}
338+
return ipcName
323339
}
324340
return baseName
325341
}

staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package azure
2020

2121
import (
2222
"testing"
23+
"strconv"
2324

2425
"github.com/stretchr/testify/assert"
2526

@@ -253,3 +254,87 @@ func TestGetAzureLoadBalancerName(t *testing.T) {
253254
assert.Equal(t, c.expected, loadbalancerName, c.description)
254255
}
255256
}
257+
258+
func TestGetLoadBalancingRuleName(t *testing.T) {
259+
az := getTestCloud()
260+
az.PrimaryAvailabilitySetName = "primary"
261+
262+
svc := &v1.Service{
263+
ObjectMeta: meta.ObjectMeta{
264+
Annotations: map[string]string{
265+
ServiceAnnotationLoadBalancerInternalSubnet: "subnet",
266+
ServiceAnnotationLoadBalancerInternal: "true",
267+
},
268+
UID: "257b9655-5137-4ad2-b091-ef3f07043ad3",
269+
},
270+
}
271+
272+
cases := []struct {
273+
description string
274+
subnetName string
275+
isInternal bool
276+
useStandardLB bool
277+
protocol v1.Protocol
278+
port int32
279+
expected string
280+
}{
281+
{
282+
description: "internal lb should have subnet name on the rule name",
283+
subnetName: "shortsubnet",
284+
isInternal: true,
285+
useStandardLB: true,
286+
protocol: v1.ProtocolTCP,
287+
port: 9000,
288+
expected: "a257b965551374ad2b091ef3f07043ad-shortsubnet-TCP-9000",
289+
},
290+
{
291+
description: "internal standard lb should have subnet name on the rule name but truncated to 80 charactors",
292+
subnetName: "averylonnnngggnnnnnnnnnnnnnnnnnnnnnngggggggggggggggggggggggggggggggggggggsubet",
293+
isInternal: true,
294+
useStandardLB: true,
295+
protocol: v1.ProtocolTCP,
296+
port: 9000,
297+
expected: "a257b965551374ad2b091ef3f07043ad-averylonnnngggnnnnnnnnnnnnnnnnnnnnnngg-TCP-9000",
298+
},
299+
{
300+
description: "internal basic lb should have subnet name on the rule name but truncated to 80 charactors",
301+
subnetName: "averylonnnngggnnnnnnnnnnnnnnnnnnnnnngggggggggggggggggggggggggggggggggggggsubet",
302+
isInternal: true,
303+
useStandardLB: false,
304+
protocol: v1.ProtocolTCP,
305+
port: 9000,
306+
expected: "a257b965551374ad2b091ef3f07043ad-averylonnnngggnnnnnnnnnnnnnnnnnnnnnngg-TCP-9000",
307+
},
308+
{
309+
description: "external standard lb should not have subnet name on the rule name",
310+
subnetName: "shortsubnet",
311+
isInternal: false,
312+
useStandardLB: true,
313+
protocol: v1.ProtocolTCP,
314+
port: 9000,
315+
expected: "a257b965551374ad2b091ef3f07043ad-TCP-9000",
316+
},
317+
{
318+
description: "external basic lb should not have subnet name on the rule name",
319+
subnetName: "shortsubnet",
320+
isInternal: false,
321+
useStandardLB: false,
322+
protocol: v1.ProtocolTCP,
323+
port: 9000,
324+
expected: "a257b965551374ad2b091ef3f07043ad-TCP-9000",
325+
},
326+
}
327+
328+
for _, c := range cases {
329+
if c.useStandardLB {
330+
az.Config.LoadBalancerSku = loadBalancerSkuStandard
331+
} else {
332+
az.Config.LoadBalancerSku = loadBalancerSkuBasic
333+
}
334+
svc.Annotations[ServiceAnnotationLoadBalancerInternalSubnet] = c.subnetName
335+
svc.Annotations[ServiceAnnotationLoadBalancerInternal] = strconv.FormatBool(c.isInternal)
336+
337+
loadbalancerName := az.getLoadBalancerRuleName(svc, c.protocol, c.port)
338+
assert.Equal(t, c.expected, loadbalancerName, c.description)
339+
}
340+
}

staging/src/k8s.io/legacy-cloud-providers/azure/azure_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,14 +1238,14 @@ func validateLoadBalancer(t *testing.T, loadBalancer *network.LoadBalancer, serv
12381238
if len(svc.Spec.Ports) > 0 {
12391239
expectedFrontendIPCount++
12401240
expectedFrontendIP := ExpectedFrontendIPInfo{
1241-
Name: az.getFrontendIPConfigName(&svc, subnet(&svc)),
1241+
Name: az.getFrontendIPConfigName(&svc),
12421242
Subnet: subnet(&svc),
12431243
}
12441244
expectedFrontendIPs = append(expectedFrontendIPs, expectedFrontendIP)
12451245
}
12461246
for _, wantedRule := range svc.Spec.Ports {
12471247
expectedRuleCount++
1248-
wantedRuleName := az.getLoadBalancerRuleName(&svc, wantedRule.Protocol, wantedRule.Port, subnet(&svc))
1248+
wantedRuleName := az.getLoadBalancerRuleName(&svc, wantedRule.Protocol, wantedRule.Port)
12491249
foundRule := false
12501250
for _, actualRule := range *loadBalancer.LoadBalancingRules {
12511251
if strings.EqualFold(*actualRule.Name, wantedRuleName) &&

0 commit comments

Comments
 (0)