Skip to content

Commit 02ca2f4

Browse files
authored
Merge pull request #5458 from nawazkh/rel-1.17_fix_APIServerLB_nil_pointer
[release-1.17] bug: Fix APIServerLB nil pointer deref
2 parents f9f1a44 + 85a1d00 commit 02ca2f4

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
@@ -700,7 +700,11 @@ func (s *ClusterScope) ControlPlaneOutboundLB() *infrav1.LoadBalancerSpec {
700700

701701
// APIServerLBName returns the API Server LB name.
702702
func (s *ClusterScope) APIServerLBName() string {
703-
return s.APIServerLB().Name
703+
apiServerLB := s.APIServerLB()
704+
if apiServerLB != nil {
705+
return apiServerLB.Name
706+
}
707+
return ""
704708
}
705709

706710
// 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
@@ -4256,3 +4256,46 @@ func TestGroupSpecs(t *testing.T) {
42564256
})
42574257
}
42584258
}
4259+
4260+
func TestAPIServerLBName(t *testing.T) {
4261+
tests := []struct {
4262+
name string
4263+
cluster *ClusterScope
4264+
expected string
4265+
}{
4266+
{
4267+
name: "APIServerLB is not nil",
4268+
cluster: &ClusterScope{
4269+
AzureCluster: &infrav1.AzureCluster{
4270+
Spec: infrav1.AzureClusterSpec{
4271+
NetworkSpec: infrav1.NetworkSpec{
4272+
APIServerLB: infrav1.LoadBalancerSpec{
4273+
Name: "test-lb",
4274+
},
4275+
},
4276+
},
4277+
},
4278+
},
4279+
expected: "test-lb",
4280+
},
4281+
{
4282+
name: "APIServerLB is nil",
4283+
cluster: &ClusterScope{
4284+
AzureCluster: &infrav1.AzureCluster{
4285+
Spec: infrav1.AzureClusterSpec{
4286+
NetworkSpec: infrav1.NetworkSpec{},
4287+
},
4288+
},
4289+
},
4290+
expected: "",
4291+
},
4292+
}
4293+
4294+
for _, tt := range tests {
4295+
t.Run(tt.name, func(t *testing.T) {
4296+
g := NewWithT(t)
4297+
result := tt.cluster.APIServerLBName()
4298+
g.Expect(result).To(Equal(tt.expected))
4299+
})
4300+
}
4301+
}

0 commit comments

Comments
 (0)