@@ -20,16 +20,19 @@ package controllers
20
20
import (
21
21
"context"
22
22
"fmt"
23
+ "net"
23
24
"time"
24
25
25
26
"github.com/go-logr/logr"
26
27
infrav1 "github.com/microsoft/cluster-api-provider-azurestackhci/api/v1beta1"
27
28
azurestackhci "github.com/microsoft/cluster-api-provider-azurestackhci/cloud"
28
29
"github.com/microsoft/cluster-api-provider-azurestackhci/cloud/scope"
29
30
"github.com/microsoft/cluster-api-provider-azurestackhci/cloud/services/loadbalancers"
31
+ "github.com/microsoft/cluster-api-provider-azurestackhci/cloud/services/virtualnetworks"
30
32
infrav1util "github.com/microsoft/cluster-api-provider-azurestackhci/pkg/util"
31
33
"github.com/microsoft/moc-sdk-for-go/services/network"
32
34
mocerrors "github.com/microsoft/moc/pkg/errors"
35
+ mocnet "github.com/microsoft/moc/pkg/net"
33
36
"github.com/pkg/errors"
34
37
corev1 "k8s.io/api/core/v1"
35
38
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -223,6 +226,13 @@ func (r *AzureStackHCILoadBalancerReconciler) reconcileNormal(lbs *scope.LoadBal
223
226
}
224
227
}
225
228
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
+
226
236
// When a SDN integration is present, LB replica count will be 0 as the loadbalancing is handled by SDN.
227
237
// So fail only if the configured replica count is not 0.
228
238
if lbs .GetReplicas () != 0 && lbs .GetReadyReplicas () < 1 {
@@ -370,3 +380,39 @@ func (r *AzureStackHCILoadBalancerReconciler) reconcileStatus(lbs *scope.LoadBal
370
380
371
381
lbs .SetPhase (infrav1 .AzureStackHCILoadBalancerPhaseProvisioned )
372
382
}
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