Skip to content

Commit 516f1be

Browse files
author
Matthew Chen
committed
error handling for lb vip out of range
1 parent 482ec88 commit 516f1be

4 files changed

+50
-2
lines changed

api/v1beta1/conditions_consts.go

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ const (
6666
LoadBalancerMachineReconciliationFailedReason = "MachineReconciliationFailed"
6767
// LoadBalancerAddressUnavailableReason used when waiting for loadbalancer to have an address.
6868
LoadBalancerAddressUnavailableReason = "AddressUnavailable"
69+
// LoadBalancerVIPOutOfRangeReason used when provided Load Balancer VIP is out of the vip address range.
70+
LoadBalancerVIPOutOfRangeReason = "LoadBalancerVIPOutOfRange"
6971
// LoadBalancerNoReplicasReadyReason used when no replicas are in a ready state.
7072
LoadBalancerNoReplicasReadyReason = "NoReplicasReady"
7173

controllers/azurestackhcicluster_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,4 @@ func (r *AzureStackHCIClusterReconciler) reconcilePhase(clusterScope *scope.Clus
423423
if !azureStackHCICluster.DeletionTimestamp.IsZero() {
424424
azureStackHCICluster.Status.SetTypedPhase(infrav1.AzureStackHCIClusterPhaseDeleting)
425425
}
426-
}
426+
}

controllers/azurestackhcicluster_reconciler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,4 @@ func (r *azureStackHCIClusterReconciler) createOrUpdateVnetName() {
135135
if r.scope.Vnet().Name == "" {
136136
r.scope.Vnet().Name = azurestackhci.GenerateVnetName(r.scope.Name())
137137
}
138-
}
138+
}

controllers/azurestackhciloadbalancer_controller.go

+46
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ package controllers
2020
import (
2121
"context"
2222
"fmt"
23+
"net"
2324
"time"
2425

2526
"github.com/go-logr/logr"
2627
infrav1 "github.com/microsoft/cluster-api-provider-azurestackhci/api/v1beta1"
2728
azurestackhci "github.com/microsoft/cluster-api-provider-azurestackhci/cloud"
2829
"github.com/microsoft/cluster-api-provider-azurestackhci/cloud/scope"
2930
"github.com/microsoft/cluster-api-provider-azurestackhci/cloud/services/loadbalancers"
31+
"github.com/microsoft/cluster-api-provider-azurestackhci/cloud/services/virtualnetworks"
3032
infrav1util "github.com/microsoft/cluster-api-provider-azurestackhci/pkg/util"
3133
"github.com/microsoft/moc-sdk-for-go/services/network"
3234
mocerrors "github.com/microsoft/moc/pkg/errors"
35+
mocnet "github.com/microsoft/moc/pkg/net"
3336
"github.com/pkg/errors"
3437
corev1 "k8s.io/api/core/v1"
3538
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -223,6 +226,13 @@ func (r *AzureStackHCILoadBalancerReconciler) reconcileNormal(lbs *scope.LoadBal
223226
}
224227
}
225228

229+
err = r.reconcileLoadBalancerVIP(lbs, clusterScope)
230+
if err != nil {
231+
r.Recorder.Eventf(lbs.AzureStackHCILoadBalancer, corev1.EventTypeWarning, "FailureReconcileLBVIP", errors.Wrapf(err, "Failed to reconcile Load Balancer VIP").Error())
232+
conditions.MarkFalse(lbs.AzureStackHCILoadBalancer, infrav1.LoadBalancerInfrastructureReadyCondition, infrav1.LoadBalancerVIPOutOfRangeReason, clusterv1.ConditionSeverityError, err.Error())
233+
return reconcile.Result{}, err
234+
}
235+
226236
// When a SDN integration is present, LB replica count will be 0 as the loadbalancing is handled by SDN.
227237
// So fail only if the configured replica count is not 0.
228238
if lbs.GetReplicas() != 0 && lbs.GetReadyReplicas() < 1 {
@@ -370,3 +380,39 @@ func (r *AzureStackHCILoadBalancerReconciler) reconcileStatus(lbs *scope.LoadBal
370380

371381
lbs.SetPhase(infrav1.AzureStackHCILoadBalancerPhaseProvisioned)
372382
}
383+
384+
func (r *AzureStackHCILoadBalancerReconciler) reconcileLoadBalancerVIP(lbs *scope.LoadBalancerScope, clusterScope *scope.ClusterScope) error {
385+
386+
lbs.Info("Attempting to get vnet for loadbalancer vip", "name", lbs.AzureStackHCILoadBalancer.Name)
387+
vnetSpec := &virtualnetworks.Spec{
388+
Name: clusterScope.AzureStackHCICluster.Spec.NetworkSpec.Vnet.Name,
389+
Group: clusterScope.AzureStackHCICluster.Spec.NetworkSpec.Vnet.Group,
390+
}
391+
vnetInterface, err := virtualnetworks.NewService(clusterScope).Get(clusterScope.Context, vnetSpec)
392+
if err != nil {
393+
return err
394+
}
395+
396+
vnet, ok := vnetInterface.(network.VirtualNetwork)
397+
if !ok {
398+
return errors.New("error getting virtualnetwork service")
399+
}
400+
401+
lbVIP := net.ParseIP(lbs.Address())
402+
403+
for _, subnet := range *vnet.Subnets {
404+
for _, ippool := range subnet.IPPools {
405+
406+
if (ippool.Type == network.VIPPOOL) {
407+
startVIP := net.ParseIP(ippool.Start)
408+
endVIP := net.ParseIP(ippool.End)
409+
410+
if (mocnet.RangeContains(startVIP, endVIP, lbVIP)) {
411+
return nil
412+
}
413+
}
414+
}
415+
}
416+
417+
return errors.Errorf("Load Balancer VIP out of VIP Pool range.")// LB VIP: %s Start: %s End: %s", controlPlaneHost, ippool.Start, ippool.End)
418+
}

0 commit comments

Comments
 (0)