Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom backendPool not being used #3676

Merged
Merged
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that s.APIServerLB() to be nil? If so, can we run into nil pointer dereference ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, do you think it is valid to use azure.GenerateBackendAddressPoolName(loadBalancerName) as a backup name if s.APIServerLB().BackendPool.Name is nil?

Copy link
Contributor Author

@RadekManak RadekManak Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that s.APIServerLB() to be nil? If so, can we run into nil pointer dereference ?

It cannot be nil. A function above is creating the pointer from a struct field.
// APIServerLB returns the cluster API Server load balancer.
func (s *ClusterScope) APIServerLB() *infrav1.LoadBalancerSpec {
return &s.AzureCluster.Spec.NetworkSpec.APIServerLB
}

azure.GenerateBackendAddressPoolName(loadBalancerName) is a valid default name for the backendPool, but it is already being set by the defaulting webhook if it is empty. I don't believe that adding the defaulting here as well has any benefit.

}

// 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()))
CecileRobertMichon marked this conversation as resolved.
Show resolved Hide resolved
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 @@
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),

Check warning on line 132 in azure/scope/machinepool.go

View check run for this annotation

Codecov / codecov/patch

azure/scope/machinepool.go#L132

Added line #L132 was not covered by tests
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.