Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/provider/azure_loadbalancer_backendpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,13 @@ func (az *Cloud) addNodeIPAddressesToBackendPool(backendPool *armnetwork.Backend
for _, ipAddress := range nodeIPAddresses {
if !hasIPAddressInBackendPool(backendPool, ipAddress) {
name := az.nodePrivateIPToNodeNameMap[ipAddress]
if name == "" {
// The node name in cache could be empty for unknown reasons. Fall back to a name
// derived from the IP address, prefixed to avoid colliding with a real node name,
// so the backend address is not created with an empty name.
name = "ip-" + strings.NewReplacer(".", "-", ":", "-").Replace(ipAddress)
logger.V(2).Info("Node name not found in cache for IP address, generated a name for the backend address", "ip", ipAddress, "generatedName", name)
}
logger.V(4).Info("adding node to the backend pool", "ip", ipAddress, "backendPoolName", ptr.Deref(backendPool.Name, ""))
addresses = append(addresses, &armnetwork.LoadBalancerBackendAddress{
Name: ptr.To(name),
Expand Down
18 changes: 18 additions & 0 deletions pkg/provider/azure_loadbalancer_backendpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,24 @@ func TestAddNodeIPAddressesToBackendPoolDeduplicates(t *testing.T) {
}
}

func TestAddNodeIPAddressesToBackendPoolGeneratesNameWhenNodeNameMissing(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

cloud := GetTestCloud(ctrl)
cloud.nodePrivateIPToNodeNameMap = map[string]string{}

bp := buildTestLoadBalancerBackendPoolWithIPs("test-pool", nil)
changed := cloud.addNodeIPAddressesToBackendPool(bp, []string{"10.0.0.1"})
assert.True(t, changed)

if assert.Len(t, bp.Properties.LoadBalancerBackendAddresses, 1) {
addr := bp.Properties.LoadBalancerBackendAddresses[0]
assert.Equal(t, "10.0.0.1", ptr.Deref(addr.Properties.IPAddress, ""))
assert.Equal(t, "ip-10-0-0-1", ptr.Deref(addr.Name, ""))
}
}

func TestGetBackendPrivateIPsNodeIPConfig(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down
5 changes: 4 additions & 1 deletion pkg/provider/azure_local_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -522,8 +523,10 @@ func getTestBackendAddressPoolWithIPs(lbName, bpName string, ips []string) *armn
}
for _, ip := range ips {
if len(ip) > 0 {
// The local service reconciliation path never populates nodePrivateIPToNodeNameMap,
// so addNodeIPAddressesToBackendPool falls back to a name derived from the IP address.
bp.Properties.LoadBalancerBackendAddresses = append(bp.Properties.LoadBalancerBackendAddresses, &armnetwork.LoadBalancerBackendAddress{
Name: ptr.To(""),
Name: ptr.To("ip-" + strings.NewReplacer(".", "-", ":", "-").Replace(ip)),
Properties: &armnetwork.LoadBalancerBackendAddressPropertiesFormat{
IPAddress: ptr.To(ip),
},
Expand Down
Loading