Skip to content

Commit

Permalink
Fix custom backendPool not being used
Browse files Browse the repository at this point in the history
In some places the default backendPool name was being used instead of
the specified value.
  • Loading branch information
RadekManak committed Jun 29, 2023
1 parent 7059146 commit f947836
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 49 deletions.
2 changes: 1 addition & 1 deletion azure/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type NetworkDescriber interface {
ControlPlaneRouteTable() infrav1.RouteTable
APIServerLB() *infrav1.LoadBalancerSpec
APIServerLBName() string
APIServerLBPoolName(string) string
APIServerLBPoolName() string
IsAPIServerPrivate() bool
GetPrivateDNSZoneName() string
OutboundLBName(string) string
Expand Down
16 changes: 8 additions & 8 deletions azure/mock_azure/azure_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 20 additions & 16 deletions azure/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,33 +711,37 @@ func (s *ClusterScope) GetPrivateDNSZoneName() string {
}

// APIServerLBPoolName returns the API Server LB backend pool name.
func (s *ClusterScope) APIServerLBPoolName(loadBalancerName string) string {
return azure.GenerateBackendAddressPoolName(loadBalancerName)
func (s *ClusterScope) APIServerLBPoolName() string {
return s.APIServerLB().BackendPool.Name
}

// OutboundLBName returns the name of the outbound LB.
func (s *ClusterScope) OutboundLBName(role string) string {
// OutboundLB returns the outbound LB.
func (s *ClusterScope) OutboundLB(role string) *infrav1.LoadBalancerSpec {
if role == infrav1.Node {
if s.NodeOutboundLB() == nil {
return ""
}
return s.NodeOutboundLB().Name
return s.NodeOutboundLB()
}
if s.IsAPIServerPrivate() {
if s.ControlPlaneOutboundLB() == nil {
return ""
}
return s.ControlPlaneOutboundLB().Name
return s.ControlPlaneOutboundLB()
}
return s.APIServerLB()
}

// OutboundLBName returns the name of the outbound LB.
func (s *ClusterScope) OutboundLBName(role string) string {
lb := s.OutboundLB(role)
if lb == nil {
return ""
}
return s.APIServerLBName()
return lb.Name
}

// OutboundPoolName returns the outbound LB backend pool name.
func (s *ClusterScope) OutboundPoolName(loadBalancerName string) string {
if loadBalancerName == "" {
func (s *ClusterScope) OutboundPoolName(role string) string {
lb := s.OutboundLB(role)
if lb == nil {
return ""
}
return azure.GenerateOutboundBackendAddressPoolName(loadBalancerName)
return lb.BackendPool.Name
}

// ResourceGroup returns the cluster resource group.
Expand Down
21 changes: 19 additions & 2 deletions azure/scope/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,11 @@ func TestAPIServerLBPoolName(t *testing.T) {
},
},
Spec: infrav1.AzureClusterSpec{
NetworkSpec: infrav1.NetworkSpec{
APIServerLB: infrav1.LoadBalancerSpec{
Name: tc.lbName,
},
},
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
SubscriptionID: "123",
},
Expand All @@ -1985,8 +1990,9 @@ func TestAPIServerLBPoolName(t *testing.T) {
AzureCluster: azureCluster,
Client: fakeClient,
})
clusterScope.AzureCluster.SetBackendPoolNameDefault()
g.Expect(err).NotTo(HaveOccurred())
got := clusterScope.APIServerLBPoolName(tc.lbName)
got := clusterScope.APIServerLBPoolName()
g.Expect(tc.expectLBpoolName).Should(Equal(got))
})
}
Expand Down Expand Up @@ -2125,6 +2131,7 @@ func TestOutboundLBName(t *testing.T) {
AzureCluster: azureCluster,
Client: fakeClient,
})
clusterScope.AzureCluster.SetBackendPoolNameDefault()
g.Expect(err).NotTo(HaveOccurred())
got := clusterScope.OutboundLBName(tc.role)
g.Expect(tc.expected).Should(Equal(got))
Expand Down Expand Up @@ -2253,6 +2260,7 @@ func TestBackendPoolName(t *testing.T) {
AzureCluster: azureCluster,
Client: fakeClient,
})
clusterScope.AzureCluster.SetBackendPoolNameDefault()
g.Expect(err).NotTo(HaveOccurred())
got := clusterScope.LBSpecs()
g.Expect(len(got)).To(Equal(3))
Expand Down Expand Up @@ -2325,7 +2333,15 @@ func TestOutboundPoolName(t *testing.T) {
},
}

if tc.loadBalancerName != "" {
azureCluster.Spec.NetworkSpec.NodeOutboundLB = &infrav1.LoadBalancerSpec{
Name: tc.loadBalancerName,
}
}

initObjects := []runtime.Object{cluster, azureCluster}
azureCluster.Default()

fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(initObjects...).Build()

clusterScope, err := NewClusterScope(context.TODO(), ClusterScopeParams{
Expand All @@ -2336,8 +2352,9 @@ func TestOutboundPoolName(t *testing.T) {
AzureCluster: azureCluster,
Client: fakeClient,
})
clusterScope.AzureCluster.SetBackendPoolNameDefault()
g.Expect(err).NotTo(HaveOccurred())
got := clusterScope.OutboundPoolName(tc.loadBalancerName)
got := clusterScope.OutboundPoolName(infrav1.Node)
g.Expect(tc.expectOutboundPoolName).Should(Equal(got))
})
}
Expand Down
13 changes: 4 additions & 9 deletions azure/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,13 @@ func (m *MachineScope) BuildNICSpec(nicName string, infrav1NetworkInterface infr

if m.Role() == infrav1.ControlPlane {
spec.PublicLBName = m.OutboundLBName(m.Role())
spec.PublicLBAddressPoolName = m.OutboundPoolName(m.OutboundLBName(m.Role()))
spec.PublicLBAddressPoolName = m.OutboundPoolName(m.Role())
if m.IsAPIServerPrivate() {
spec.InternalLBName = m.APIServerLBName()
spec.InternalLBAddressPoolName = m.APIServerLBPoolName(m.APIServerLBName())
spec.InternalLBAddressPoolName = m.APIServerLBPoolName()
} else {
spec.PublicLBNATRuleName = m.Name()
spec.PublicLBAddressPoolName = m.APIServerLBPoolName(m.APIServerLBName())
spec.PublicLBAddressPoolName = m.APIServerLBPoolName()
}
}

Expand All @@ -293,12 +293,7 @@ func (m *MachineScope) BuildNICSpec(nicName string, infrav1NetworkInterface infr
// If the NAT gateway is not enabled and node has no public IP, then the NIC needs to reference the LB to get outbound traffic.
if m.Role() == infrav1.Node && !m.Subnet().IsNatGatewayEnabled() && !m.AzureMachine.Spec.AllocatePublicIP {
spec.PublicLBName = m.OutboundLBName(m.Role())
spec.PublicLBAddressPoolName = m.OutboundPoolName(m.OutboundLBName(m.Role()))
}
// If the NAT gateway is not enabled and node has no public IP, then the NIC needs to reference the LB to get outbound traffic.
if m.Role() == infrav1.Node && !m.Subnet().IsNatGatewayEnabled() && !m.AzureMachine.Spec.AllocatePublicIP {
spec.PublicLBName = m.OutboundLBName(m.Role())
spec.PublicLBAddressPoolName = m.OutboundPoolName(m.OutboundLBName(m.Role()))
spec.PublicLBAddressPoolName = m.OutboundPoolName(m.Role())
}
}

Expand Down
21 changes: 21 additions & 0 deletions azure/scope/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,9 @@ func TestMachineScope_NICSpecs(t *testing.T) {
},
NodeOutboundLB: &infrav1.LoadBalancerSpec{
Name: "outbound-lb",
BackendPool: infrav1.BackendPool{
Name: "outbound-lb-outboundBackendPool",
},
},
},
},
Expand Down Expand Up @@ -1819,6 +1822,9 @@ func TestMachineScope_NICSpecs(t *testing.T) {
},
NodeOutboundLB: &infrav1.LoadBalancerSpec{
Name: "outbound-lb",
BackendPool: infrav1.BackendPool{
Name: "outbound-lb-outboundBackendPool",
},
},
},
},
Expand Down Expand Up @@ -2147,6 +2153,9 @@ func TestMachineScope_NICSpecs(t *testing.T) {
LoadBalancerClassSpec: infrav1.LoadBalancerClassSpec{
Type: infrav1.Internal,
},
BackendPool: infrav1.BackendPool{
Name: "api-lb-backendPool",
},
},
NodeOutboundLB: &infrav1.LoadBalancerSpec{
Name: "outbound-lb",
Expand Down Expand Up @@ -2254,6 +2263,9 @@ func TestMachineScope_NICSpecs(t *testing.T) {
},
APIServerLB: infrav1.LoadBalancerSpec{
Name: "api-lb",
BackendPool: infrav1.BackendPool{
Name: "api-lb-backendPool",
},
},
NodeOutboundLB: &infrav1.LoadBalancerSpec{
Name: "outbound-lb",
Expand Down Expand Up @@ -2361,6 +2373,9 @@ func TestMachineScope_NICSpecs(t *testing.T) {
},
APIServerLB: infrav1.LoadBalancerSpec{
Name: "api-lb",
BackendPool: infrav1.BackendPool{
Name: "api-lb-backendPool",
},
},
NodeOutboundLB: &infrav1.LoadBalancerSpec{
Name: "outbound-lb",
Expand Down Expand Up @@ -2472,6 +2487,9 @@ func TestMachineScope_NICSpecs(t *testing.T) {
},
NodeOutboundLB: &infrav1.LoadBalancerSpec{
Name: "outbound-lb",
BackendPool: infrav1.BackendPool{
Name: "outbound-lb-outboundBackendPool",
},
},
},
},
Expand Down Expand Up @@ -2747,6 +2765,9 @@ func TestMachineScope_NICSpecs(t *testing.T) {
},
NodeOutboundLB: &infrav1.LoadBalancerSpec{
Name: "outbound-lb",
BackendPool: infrav1.BackendPool{
Name: "outbound-lb-outboundBackendPool",
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion azure/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (m *MachinePoolScope) ScaleSetSpec() azure.ScaleSetSpec {
VNetName: m.Vnet().Name,
VNetResourceGroup: m.Vnet().ResourceGroup,
PublicLBName: m.OutboundLBName(infrav1.Node),
PublicLBAddressPoolName: azure.GenerateOutboundBackendAddressPoolName(m.OutboundLBName(infrav1.Node)),
PublicLBAddressPoolName: m.OutboundPoolName(infrav1.Node),
AcceleratedNetworking: m.AzureMachinePool.Spec.Template.NetworkInterfaces[0].AcceleratedNetworking,
Identity: m.AzureMachinePool.Spec.Identity,
UserAssignedIdentities: m.AzureMachinePool.Spec.UserAssignedIdentities,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f947836

Please sign in to comment.