Skip to content

Commit

Permalink
fix: Ensure vnet ID will be attached to backend pool in local service…
Browse files Browse the repository at this point in the history
… reconciliation loops
  • Loading branch information
nilo19 authored and k8s-infra-cherrypick-robot committed May 26, 2024
1 parent f89c454 commit 1f09c1d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
38 changes: 29 additions & 9 deletions pkg/provider/azure_loadbalancer_backendpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,21 @@ func newBackendPoolTypeNodeIP(c *Cloud) BackendPool {
return &backendPoolTypeNodeIP{c}
}

func (az *Cloud) getVnetResourceID() string {
rg := az.ResourceGroup
if len(az.VnetResourceGroup) > 0 {
rg = az.VnetResourceGroup
}
return fmt.Sprintf(
"/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s",
az.SubscriptionID,
rg,
az.VnetName,
)
}

func (bi *backendPoolTypeNodeIP) EnsureHostsInPool(service *v1.Service, nodes []*v1.Node, _, _, clusterName, lbName string, backendPool network.BackendAddressPool) error {
isIPv6 := isBackendPoolIPv6(pointer.StringDeref(backendPool.Name, ""))
vnetResourceGroup := bi.ResourceGroup
if len(bi.VnetResourceGroup) > 0 {
vnetResourceGroup = bi.VnetResourceGroup
}
vnetID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s", bi.SubscriptionID, vnetResourceGroup, bi.VnetName)

var (
changed bool
Expand Down Expand Up @@ -425,10 +433,6 @@ func (bi *backendPoolTypeNodeIP) EnsureHostsInPool(service *v1.Service, nodes []
lbBackendPoolName := bi.getBackendPoolNameForService(service, clusterName, isIPv6)
if strings.EqualFold(pointer.StringDeref(backendPool.Name, ""), lbBackendPoolName) &&
backendPool.BackendAddressPoolPropertiesFormat != nil {
backendPool.VirtualNetwork = &network.SubResource{
ID: &vnetID,
}

if backendPool.LoadBalancerBackendAddresses == nil {
lbBackendPoolAddresses := make([]network.LoadBalancerBackendAddress, 0)
backendPool.LoadBalancerBackendAddresses = &lbBackendPoolAddresses
Expand Down Expand Up @@ -792,6 +796,22 @@ func newBackendPool(lb *network.LoadBalancer, isBackendPoolPreConfigured bool, p
}

func (az *Cloud) addNodeIPAddressesToBackendPool(backendPool *network.BackendAddressPool, nodeIPAddresses []string) bool {
vnetID := az.getVnetResourceID()
if backendPool.BackendAddressPoolPropertiesFormat != nil {
if backendPool.VirtualNetwork == nil ||
backendPool.VirtualNetwork.ID == nil {
backendPool.VirtualNetwork = &network.SubResource{
ID: &vnetID,
}
}
} else {
backendPool.BackendAddressPoolPropertiesFormat = &network.BackendAddressPoolPropertiesFormat{
VirtualNetwork: &network.SubResource{
ID: &vnetID,
},
}
}

if backendPool.LoadBalancerBackendAddresses == nil {
lbBackendPoolAddresses := make([]network.LoadBalancerBackendAddress, 0)
backendPool.LoadBalancerBackendAddresses = &lbBackendPoolAddresses
Expand Down
4 changes: 4 additions & 0 deletions pkg/provider/azure_local_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"
"github.com/stretchr/testify/assert"

"go.uber.org/mock/gomock"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -397,6 +398,9 @@ func getTestBackendAddressPoolWithIPs(lbName, bpName string, ips []string) netwo
ID: pointer.String(fmt.Sprintf("/subscriptions/subscriptionID/resourceGroups/rg/providers/Microsoft.Network/loadBalancers/%s/backendAddressPools/%s", lbName, bpName)),
Name: pointer.String(bpName),
BackendAddressPoolPropertiesFormat: &network.BackendAddressPoolPropertiesFormat{
VirtualNetwork: &network.SubResource{
ID: pointer.String("/subscriptions/subscriptionID/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet"),
},
Location: pointer.String("eastus"),
LoadBalancerBackendAddresses: &[]network.LoadBalancerBackendAddress{},
},
Expand Down

0 comments on commit 1f09c1d

Please sign in to comment.