Skip to content

Commit 94b97cc

Browse files
committed
Add tests + pointer checks
1 parent f830ca8 commit 94b97cc

File tree

2 files changed

+359
-3
lines changed

2 files changed

+359
-3
lines changed

pkg/cloud/services/loadbalancer/loadbalancer.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackClust
143143
func getAPIServerVIPAddress(openStackCluster *infrav1.OpenStackCluster) (string, error) {
144144
switch {
145145
// We only use call this function when creating the loadbalancer, so this case should never be used
146-
case openStackCluster.Status.APIServerLoadBalancer.InternalIP != "":
146+
case openStackCluster.Status.APIServerLoadBalancer != nil && openStackCluster.Status.APIServerLoadBalancer.InternalIP != "":
147147
return openStackCluster.Status.APIServerLoadBalancer.InternalIP, nil
148148

149149
// Explicit fixed IP in the cluster spec
@@ -167,7 +167,7 @@ func getAPIServerVIPAddress(openStackCluster *infrav1.OpenStackCluster) (string,
167167
func getAPIServerFloatingIP(openStackCluster *infrav1.OpenStackCluster) (string, error) {
168168
switch {
169169
// The floating IP was created previously
170-
case openStackCluster.Status.APIServerLoadBalancer.IP != "":
170+
case openStackCluster.Status.APIServerLoadBalancer != nil && openStackCluster.Status.APIServerLoadBalancer.IP != "":
171171
return openStackCluster.Status.APIServerLoadBalancer.IP, nil
172172

173173
// Explicit floating IP in the cluster spec
@@ -214,7 +214,7 @@ func getCanonicalAllowedCIDRs(openStackCluster *infrav1.OpenStackCluster) []stri
214214
}
215215
}
216216

217-
if len(openStackCluster.Status.Router.IPs) > 0 {
217+
if openStackCluster.Status.Router != nil && len(openStackCluster.Status.Router.IPs) > 0 {
218218
allowedCIDRs = append(allowedCIDRs, openStackCluster.Status.Router.IPs...)
219219
}
220220
}
@@ -260,6 +260,10 @@ func (s *Service) getOrCreateAPILoadBalancer(openStackCluster *infrav1.OpenStack
260260
return lb, nil
261261
}
262262

263+
if openStackCluster.Status.Network == nil {
264+
return nil, fmt.Errorf("network is not yet available in OpenStackCluster.Status")
265+
}
266+
263267
// Create the VIP on the first cluster subnet
264268
subnetID := openStackCluster.Status.Network.Subnets[0].ID
265269
s.scope.Logger().Info("Creating load balancer in subnet", "subnetID", subnetID, "name", loadBalancerName)
@@ -312,6 +316,10 @@ func (s *Service) reconcileAPILoadBalancerListener(lb *loadbalancers.LoadBalance
312316
loadBalancerName := getLoadBalancerName(clusterName)
313317
lbPortObjectsName := fmt.Sprintf("%s-%d", loadBalancerName, port)
314318

319+
if openStackCluster.Status.APIServerLoadBalancer == nil {
320+
return fmt.Errorf("APIServerLoadBalancer is not yet available in OpenStackCluster.Status")
321+
}
322+
315323
allowedCIDRs := openStackCluster.Status.APIServerLoadBalancer.AllowedCIDRs
316324

317325
listener, err := s.getOrCreateListener(openStackCluster, lbPortObjectsName, lb.ID, allowedCIDRs, port)

0 commit comments

Comments
 (0)