From 6e15b7582a5d5cb1de3935d150e8d6ffff149532 Mon Sep 17 00:00:00 2001 From: Mohamed Belgaied Hassine Date: Mon, 17 Jun 2024 22:08:42 +0200 Subject: [PATCH] fixed issue when cluster configuration changes to using IP pools from DHCP and IP would still fail to be set Signed-off-by: Mohamed Belgaied Hassine --- controllers/harvestercluster_controller.go | 40 ++++++++++++++-------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/controllers/harvestercluster_controller.go b/controllers/harvestercluster_controller.go index 29a92ba..e3fabc1 100644 --- a/controllers/harvestercluster_controller.go +++ b/controllers/harvestercluster_controller.go @@ -73,6 +73,7 @@ const ( cpVMLabelValuePrefix = "controlplane" requeueTimeThirtySeconds = 30 * time.Second requeueTimeFiveMinutes = 5 * time.Minute + dhcpLbIP = "0.0.0.0" ) // HarvesterClusterReconciler reconciles a HarvesterCluster object. @@ -293,7 +294,7 @@ func (r *HarvesterClusterReconciler) ReconcileNormal(scope ClusterScope) (res ct return ctrl.Result{RequeueAfter: requeueTimeFiveMinutes}, err1 } else { - lbIP := "0.0.0.0" + lbIP := dhcpLbIP if scope.HarvesterCluster.Spec.LoadBalancerConfig.IPAMType == infrav1.POOL { lbIP, err = getIPFromIPPool(scope, lbNamespacedName) if err != nil { @@ -357,31 +358,42 @@ func (r *HarvesterClusterReconciler) ReconcileNormal(scope ClusterScope) (res ct logger.Info("placeholder LoadBalancer IP is empty, waiting for IP to be set ...") if scope.HarvesterCluster.Spec.LoadBalancerConfig.IPAMType == infrav1.POOL { + newLbIP := dhcpLbIP + ipPool, err := scope.HarvesterClient.LoadbalancerV1beta1().IPPools().Get(context.TODO(), scope.HarvesterCluster.Spec.LoadBalancerConfig.IpPoolRef, v1.GetOptions{}) if err != nil { return ctrl.Result{RequeueAfter: requeueTimeThirtySeconds}, err } - if ipPool.Status.AllocatedHistory == nil { - return ctrl.Result{RequeueAfter: requeueTimeThirtySeconds}, fmt.Errorf("IP Pool %s does not have any allocated IPs", ipPool.Name) + if ipPool.Status.AllocatedHistory != nil { + for k, v := range ipPool.Status.AllocatedHistory { + if lbNamespacedName == v { + newLbIP = k + } + } + } + + if newLbIP == dhcpLbIP { + newLbIP, err = getIPFromIPPool(scope, lbNamespacedName) + if err != nil { + return ctrl.Result{RequeueAfter: requeueTimeThirtySeconds}, err + } } - for k, v := range ipPool.Status.AllocatedHistory { - if lbNamespacedName == v { - existingPlaceholderLB.Spec.LoadBalancerIP = k + existingPlaceholderLB.Spec.LoadBalancerIP = newLbIP - _, err = scope.HarvesterClient.CoreV1().Services(scope.HarvesterCluster.Spec.TargetNamespace).Update(context.TODO(), - existingPlaceholderLB, v1.UpdateOptions{}) - if err != nil { - err = errors.Wrap(err, "could not update the placeholder LoadBalancer") + _, err = scope.HarvesterClient.CoreV1().Services(scope.HarvesterCluster.Spec.TargetNamespace).Update(context.TODO(), + existingPlaceholderLB, v1.UpdateOptions{}) + if err != nil { + err = errors.Wrap(err, "could not update the placeholder LoadBalancer") - return ctrl.Result{RequeueAfter: requeueTimeThirtySeconds}, err - } - } + return ctrl.Result{RequeueAfter: requeueTimeThirtySeconds}, err } - return ctrl.Result{RequeueAfter: requeueTimeThirtySeconds}, nil + logger.Info("placeholder LoadBalancer IP updated successfully using IP Pools", "IP", newLbIP) + + return ctrl.Result{Requeue: true}, nil } else { return ctrl.Result{RequeueAfter: requeueTimeThirtySeconds}, nil }