Skip to content

Commit 04de42d

Browse files
committed
bug: Fix APIServerLB nil pointer deref
This commit fixes a possible nil pointer deference when returning the name of the API Server LoadBalancer. In some cases like externally managed infrastructure, there might not be an API Server LoadBalancer in the AzureCluster CR. Signed-off-by: Bryan Cox <[email protected]>
1 parent 2e817c3 commit 04de42d

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

azure/scope/cluster.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,11 @@ func (s *ClusterScope) ControlPlaneOutboundLB() *infrav1.LoadBalancerSpec {
769769

770770
// APIServerLBName returns the API Server LB name.
771771
func (s *ClusterScope) APIServerLBName() string {
772-
return s.APIServerLB().Name
772+
apiServerLB := s.APIServerLB()
773+
if apiServerLB != nil {
774+
return apiServerLB.Name
775+
}
776+
return ""
773777
}
774778

775779
// IsAPIServerPrivate returns true if the API Server LB is of type Internal.

azure/scope/cluster_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3973,3 +3973,46 @@ func TestGroupSpecs(t *testing.T) {
39733973
})
39743974
}
39753975
}
3976+
3977+
func TestAPIServerLBName(t *testing.T) {
3978+
tests := []struct {
3979+
name string
3980+
cluster *ClusterScope
3981+
expected string
3982+
}{
3983+
{
3984+
name: "APIServerLB is not nil",
3985+
cluster: &ClusterScope{
3986+
AzureCluster: &infrav1.AzureCluster{
3987+
Spec: infrav1.AzureClusterSpec{
3988+
NetworkSpec: infrav1.NetworkSpec{
3989+
APIServerLB: &infrav1.LoadBalancerSpec{
3990+
Name: "test-lb",
3991+
},
3992+
},
3993+
},
3994+
},
3995+
},
3996+
expected: "test-lb",
3997+
},
3998+
{
3999+
name: "APIServerLB is nil",
4000+
cluster: &ClusterScope{
4001+
AzureCluster: &infrav1.AzureCluster{
4002+
Spec: infrav1.AzureClusterSpec{
4003+
NetworkSpec: infrav1.NetworkSpec{},
4004+
},
4005+
},
4006+
},
4007+
expected: "",
4008+
},
4009+
}
4010+
4011+
for _, tt := range tests {
4012+
t.Run(tt.name, func(t *testing.T) {
4013+
g := NewWithT(t)
4014+
result := tt.cluster.APIServerLBName()
4015+
g.Expect(result).To(Equal(tt.expected))
4016+
})
4017+
}
4018+
}

0 commit comments

Comments
 (0)