Skip to content

Commit 8f09fd6

Browse files
authored
Merge pull request kubernetes#76691 from feiskyer/slb-multi-backendpool
Fix Azure SLB support for multiple backend pools
2 parents 257e686 + ef6f88d commit 8f09fd6

File tree

4 files changed

+123
-16
lines changed

4 files changed

+123
-16
lines changed

pkg/cloudprovider/providers/azure/azure_standard.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -676,17 +676,20 @@ func (as *availabilitySet) ensureHostInPool(service *v1.Service, nodeName types.
676676
// sets, the same network interface couldn't be added to more than one load balancer of
677677
// the same type. Omit those nodes (e.g. masters) so Azure ARM won't complain
678678
// about this.
679+
newBackendPoolsIDs := make([]string, 0, len(newBackendPools))
679680
for _, pool := range newBackendPools {
680-
backendPool := *pool.ID
681-
matches := backendPoolIDRE.FindStringSubmatch(backendPool)
682-
if len(matches) == 2 {
683-
lbName := matches[1]
684-
if strings.HasSuffix(lbName, InternalLoadBalancerNameSuffix) == isInternal {
685-
klog.V(4).Infof("Node %q has already been added to LB %q, omit adding it to a new one", nodeName, lbName)
686-
return nil
687-
}
681+
if pool.ID != nil {
682+
newBackendPoolsIDs = append(newBackendPoolsIDs, *pool.ID)
688683
}
689684
}
685+
isSameLB, oldLBName, err := isBackendPoolOnSameLB(backendPoolID, newBackendPoolsIDs)
686+
if err != nil {
687+
return err
688+
}
689+
if !isSameLB {
690+
klog.V(4).Infof("Node %q has already been added to LB %q, omit adding it to a new one", nodeName, oldLBName)
691+
return nil
692+
}
690693
}
691694

692695
newBackendPools = append(newBackendPools,

pkg/cloudprovider/providers/azure/azure_vmss.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -785,17 +785,20 @@ func (ss *scaleSet) ensureHostsInVMSetPool(service *v1.Service, backendPoolID st
785785
// the same network interface couldn't be added to more than one load balancer of
786786
// the same type. Omit those nodes (e.g. masters) so Azure ARM won't complain
787787
// about this.
788+
newBackendPoolsIDs := make([]string, 0, len(newBackendPools))
788789
for _, pool := range newBackendPools {
789-
backendPool := *pool.ID
790-
matches := backendPoolIDRE.FindStringSubmatch(backendPool)
791-
if len(matches) == 2 {
792-
lbName := matches[1]
793-
if strings.HasSuffix(lbName, InternalLoadBalancerNameSuffix) == isInternal {
794-
klog.V(4).Infof("vmss %q has already been added to LB %q, omit adding it to a new one", vmSetName, lbName)
795-
return nil
796-
}
790+
if pool.ID != nil {
791+
newBackendPoolsIDs = append(newBackendPoolsIDs, *pool.ID)
797792
}
798793
}
794+
isSameLB, oldLBName, err := isBackendPoolOnSameLB(backendPoolID, newBackendPoolsIDs)
795+
if err != nil {
796+
return err
797+
}
798+
if !isSameLB {
799+
klog.V(4).Infof("VMSS %q has already been added to LB %q, omit adding it to a new one", vmSetName, oldLBName)
800+
return nil
801+
}
799802
}
800803

801804
newBackendPools = append(newBackendPools,

pkg/cloudprovider/providers/azure/azure_wrap.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,29 @@ func convertResourceGroupNameToLower(resourceID string) (string, error) {
336336
resourceGroup := matches[1]
337337
return strings.Replace(resourceID, resourceGroup, strings.ToLower(resourceGroup), 1), nil
338338
}
339+
340+
// isBackendPoolOnSameLB checks whether newBackendPoolID is on the same load balancer as existingBackendPools.
341+
// Since both public and internal LBs are supported, lbName and lbName-internal are treated as same.
342+
// If not same, the lbName for existingBackendPools would also be returned.
343+
func isBackendPoolOnSameLB(newBackendPoolID string, existingBackendPools []string) (bool, string, error) {
344+
matches := backendPoolIDRE.FindStringSubmatch(newBackendPoolID)
345+
if len(matches) != 2 {
346+
return false, "", fmt.Errorf("new backendPoolID %q is in wrong format", newBackendPoolID)
347+
}
348+
349+
newLBName := matches[1]
350+
newLBNameTrimmed := strings.TrimRight(newLBName, InternalLoadBalancerNameSuffix)
351+
for _, backendPool := range existingBackendPools {
352+
matches := backendPoolIDRE.FindStringSubmatch(backendPool)
353+
if len(matches) != 2 {
354+
return false, "", fmt.Errorf("existing backendPoolID %q is in wrong format", backendPool)
355+
}
356+
357+
lbName := matches[1]
358+
if !strings.EqualFold(strings.TrimRight(lbName, InternalLoadBalancerNameSuffix), newLBNameTrimmed) {
359+
return false, lbName, nil
360+
}
361+
}
362+
363+
return true, "", nil
364+
}

pkg/cloudprovider/providers/azure/azure_wrap_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,78 @@ func TestConvertResourceGroupNameToLower(t *testing.T) {
198198
assert.Equal(t, test.expected, real, test.desc)
199199
}
200200
}
201+
202+
func TestIsBackendPoolOnSameLB(t *testing.T) {
203+
tests := []struct {
204+
backendPoolID string
205+
existingBackendPools []string
206+
expected bool
207+
expectedLBName string
208+
expectError bool
209+
}{
210+
{
211+
backendPoolID: "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/lb1/backendAddressPools/pool1",
212+
existingBackendPools: []string{
213+
"/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/lb1/backendAddressPools/pool2",
214+
},
215+
expected: true,
216+
expectedLBName: "",
217+
},
218+
{
219+
backendPoolID: "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/lb1-internal/backendAddressPools/pool1",
220+
existingBackendPools: []string{
221+
"/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/lb1/backendAddressPools/pool2",
222+
},
223+
expected: true,
224+
expectedLBName: "",
225+
},
226+
{
227+
backendPoolID: "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/lb1/backendAddressPools/pool1",
228+
existingBackendPools: []string{
229+
"/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/lb1-internal/backendAddressPools/pool2",
230+
},
231+
expected: true,
232+
expectedLBName: "",
233+
},
234+
{
235+
backendPoolID: "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/lb1/backendAddressPools/pool1",
236+
existingBackendPools: []string{
237+
"/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/lb2/backendAddressPools/pool2",
238+
},
239+
expected: false,
240+
expectedLBName: "lb2",
241+
},
242+
{
243+
backendPoolID: "wrong-backendpool-id",
244+
existingBackendPools: []string{
245+
"/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/lb1/backendAddressPools/pool2",
246+
},
247+
expectError: true,
248+
},
249+
{
250+
backendPoolID: "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/lb1/backendAddressPools/pool1",
251+
existingBackendPools: []string{
252+
"wrong-existing-backendpool-id",
253+
},
254+
expectError: true,
255+
},
256+
{
257+
backendPoolID: "wrong-backendpool-id",
258+
existingBackendPools: []string{
259+
"wrong-existing-backendpool-id",
260+
},
261+
expectError: true,
262+
},
263+
}
264+
265+
for _, test := range tests {
266+
isSameLB, lbName, err := isBackendPoolOnSameLB(test.backendPoolID, test.existingBackendPools)
267+
if test.expectError {
268+
assert.Error(t, err)
269+
continue
270+
}
271+
272+
assert.Equal(t, test.expected, isSameLB)
273+
assert.Equal(t, test.expectedLBName, lbName)
274+
}
275+
}

0 commit comments

Comments
 (0)