|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + goctx "context" |
| 5 | + |
| 6 | + "github.com/go-logr/logr" |
| 7 | + "k8s.io/client-go/tools/record" |
| 8 | + infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/api/v1alpha2" |
| 9 | + "sigs.k8s.io/cluster-api-provider-vsphere/api/v1alpha2/cloud" |
| 10 | + controllerutil "sigs.k8s.io/cluster-api-provider-vsphere/controllers/util" |
| 11 | + "sigs.k8s.io/cluster-api-provider-vsphere/pkg/cloud/vsphere/config" |
| 12 | + "sigs.k8s.io/cluster-api-provider-vsphere/pkg/cloud/vsphere/services" |
| 13 | + "sigs.k8s.io/cluster-api-provider-vsphere/pkg/cloud/vsphere/services/loadbalancer/aws" |
| 14 | + infrautilv1 "sigs.k8s.io/cluster-api-provider-vsphere/pkg/cloud/vsphere/util" |
| 15 | + |
| 16 | + clusterutilv1 "sigs.k8s.io/cluster-api/util" |
| 17 | + ctrl "sigs.k8s.io/controller-runtime" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 20 | +) |
| 21 | + |
| 22 | +type providerFunc func(config *cloud.LoadBalancerConfig) (services.LoadBalancerService, error) |
| 23 | + |
| 24 | +var providerFactory map[string]providerFunc |
| 25 | + |
| 26 | +func init() { |
| 27 | + providerFactory = map[string]providerFunc{ |
| 28 | + cloud.AwsProvider: aws.NewProvider, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// LoadBalancerReconciler reconciles load balancers |
| 33 | +type LoadBalancerReconciler struct { |
| 34 | + client.Client |
| 35 | + ProviderName string |
| 36 | + Recorder record.EventRecorder |
| 37 | + Log logr.Logger |
| 38 | +} |
| 39 | + |
| 40 | +// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=vsphereclusters,verbs=get;list;watch; |
| 41 | +// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=vsphereclusters/status,verbs=get;update;patch |
| 42 | + |
| 43 | +// Reconcile todo |
| 44 | +func (r *LoadBalancerReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, reterr error) { |
| 45 | + logger := r.Log.WithName("loadBalancer") |
| 46 | + |
| 47 | + ctx, result, reterr := controllerutil.GetClusterContext(req, logger, r) |
| 48 | + if ctx == nil { |
| 49 | + return result, reterr |
| 50 | + } |
| 51 | + vsphereCluster := ctx.VSphereCluster |
| 52 | + // check if the cluster does not have Load balancing config |
| 53 | + if vsphereCluster.Spec.LoadBalancerConfiguration == nil { |
| 54 | + return ctrl.Result{}, reterr |
| 55 | + } |
| 56 | + // Always close the context when exiting this function so we can persist any VSphereCluster changes. |
| 57 | + defer func() { |
| 58 | + if err := ctx.Patch(); err != nil && reterr == nil { |
| 59 | + reterr = err |
| 60 | + } |
| 61 | + }() |
| 62 | + |
| 63 | + // Handle deleted clusters |
| 64 | + if !vsphereCluster.DeletionTimestamp.IsZero() { |
| 65 | + return r.reconcileDelete(vsphereCluster) |
| 66 | + } |
| 67 | + |
| 68 | + // Handle non-deleted clusters |
| 69 | + return r.reconcileNormal(ctx, vsphereCluster) |
| 70 | +} |
| 71 | + |
| 72 | +func (r *LoadBalancerReconciler) reconcileDelete(vsphereCluster *infrav1.VSphereCluster) (_ ctrl.Result, reterr error) { |
| 73 | + newProvider := providerFactory[r.ProviderName] |
| 74 | + loadBalancerProvider, reterr := newProvider(vsphereCluster.Spec.LoadBalancerConfiguration) |
| 75 | + if reterr != nil { |
| 76 | + return ctrl.Result{}, reterr |
| 77 | + } |
| 78 | + if reterr = loadBalancerProvider.Delete(vsphereCluster); reterr != nil { |
| 79 | + return reconcile.Result{RequeueAfter: config.DefaultRequeue}, reterr |
| 80 | + } |
| 81 | + vsphereCluster.Finalizers = clusterutilv1.Filter(vsphereCluster.Finalizers, infrav1.LoadBalancerFinalizer) |
| 82 | + return ctrl.Result{}, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (r *LoadBalancerReconciler) reconcileNormal(ctx goctx.Context, vsphereCluster *infrav1.VSphereCluster) (_ ctrl.Result, reterr error) { |
| 86 | + newProvider, ok := providerFactory[r.ProviderName] |
| 87 | + if !ok { |
| 88 | + r.Log.V(3).Info("unable to initialize the provider") |
| 89 | + return ctrl.Result{}, reterr |
| 90 | + } |
| 91 | + loadBalancerProvider, reterr := newProvider(vsphereCluster.Spec.LoadBalancerConfiguration) |
| 92 | + if reterr != nil { |
| 93 | + return ctrl.Result{}, reterr |
| 94 | + } |
| 95 | + r.Log.V(3).Info("reconciling loadbalancer") |
| 96 | + |
| 97 | + machines, reterr := infrautilv1.GetMachinesInCluster(ctx, r.Client, vsphereCluster.Namespace, vsphereCluster.Name) |
| 98 | + if reterr != nil { |
| 99 | + return ctrl.Result{}, reterr |
| 100 | + } |
| 101 | + r.Log.V(6).Info("got machines", "machines", machines) |
| 102 | + |
| 103 | + machines = clusterutilv1.GetControlPlaneMachines(machines) |
| 104 | + vsphereMachines, reterr := infrautilv1.GetVSphereMachinesInCluster(ctx, r.Client, vsphereCluster.Namespace, vsphereCluster.Name) |
| 105 | + if reterr != nil { |
| 106 | + return ctrl.Result{}, reterr |
| 107 | + } |
| 108 | + r.Log.V(3).Info("got vsphere machines", "vsphere-machines", vsphereMachines) |
| 109 | + |
| 110 | + var controlPlaneIPs []string |
| 111 | + for _, controlPlane := range machines { |
| 112 | + vsphereMachine, ok := vsphereMachines[controlPlane.Name] |
| 113 | + if !ok { |
| 114 | + r.Log.V(6).Info("machine not yet linked to the cluster", "machine-name", controlPlane.Name) |
| 115 | + continue |
| 116 | + } |
| 117 | + ip, reterr := infrautilv1.GetMachinePreferredIPAddress(vsphereMachine) |
| 118 | + if reterr != nil && reterr == infrautilv1.ErrParseCIDR { |
| 119 | + return ctrl.Result{}, reterr |
| 120 | + } |
| 121 | + if len(ip) != 0 { |
| 122 | + controlPlaneIPs = append(controlPlaneIPs, ip) |
| 123 | + } |
| 124 | + } |
| 125 | + r.Log.V(3).Info("gathered controlplane IPs", "controlplane-ips", controlPlaneIPs) |
| 126 | + |
| 127 | + apiEndpoint, reterr := loadBalancerProvider.Reconcile(vsphereCluster, controlPlaneIPs) |
| 128 | + if reterr != nil { |
| 129 | + return ctrl.Result{}, reterr |
| 130 | + } |
| 131 | + vsphereCluster.Finalizers = append(vsphereCluster.Finalizers, infrav1.LoadBalancerFinalizer) |
| 132 | + vsphereCluster.Status.APIEndpoints = append(vsphereCluster.Status.APIEndpoints, apiEndpoint) |
| 133 | + vsphereCluster.Status.Ready = true |
| 134 | + return ctrl.Result{}, nil |
| 135 | +} |
| 136 | + |
| 137 | +// SetupWithManager adds this controller to the provided manager. |
| 138 | +func (r *LoadBalancerReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 139 | + return ctrl.NewControllerManagedBy(mgr). |
| 140 | + For(&infrav1.VSphereCluster{}). |
| 141 | + Complete(r) |
| 142 | +} |
0 commit comments