|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "github.com/pkg/errors" |
| 26 | + infrav1 "github.com/spectrocloud/cluster-api-provider-maas/api/v1beta1" |
| 27 | + "github.com/spectrocloud/cluster-api-provider-vsphere-static-ip/pkg/ipam" |
| 28 | + "github.com/spectrocloud/cluster-api-provider-vsphere-static-ip/pkg/ipam/factory" |
| 29 | + _ "github.com/spectrocloud/cluster-api-provider-vsphere-static-ip/pkg/ipam/metal3io" |
| 30 | + "github.com/spectrocloud/cluster-api-provider-vsphere-static-ip/pkg/util" |
| 31 | + "k8s.io/apimachinery/pkg/runtime" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | + capi "sigs.k8s.io/cluster-api/api/v1beta1" |
| 34 | + clusterutilv1 "sigs.k8s.io/cluster-api/util" |
| 35 | + ctrl "sigs.k8s.io/controller-runtime" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 37 | +) |
| 38 | + |
| 39 | +// MaasMachineReconciler reconciles a MaasMachine object |
| 40 | +type MaasMachineReconciler struct { |
| 41 | + client.Client |
| 42 | + Log logr.Logger |
| 43 | + Scheme *runtime.Scheme |
| 44 | +} |
| 45 | + |
| 46 | +// +kubebuilder:rbac:groups=controlplane.cluster.x-k8s.io,resources=kubeadmcontrolplanes,verbs=get;list;watch |
| 47 | +// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=maasmachinetemplates,verbs=get;list;watch;update;patch |
| 48 | +// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=maasmachines,verbs=get;list;watch;update;patch |
| 49 | +// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=maasmachines/status,verbs=get;update;patch |
| 50 | +// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines;machines/status,verbs=get;list;watch |
| 51 | +// +kubebuilder:rbac:groups="",resources=events,verbs=get;list;watch;create;update;patch |
| 52 | +// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch |
| 53 | +// +kubebuilder:rbac:groups=ipam.metal3.io,resources=ippools,verbs=get;list;watch;update;patch |
| 54 | +// +kubebuilder:rbac:groups=ipam.metal3.io,resources=ippools/status,verbs=get;update;patch |
| 55 | +// +kubebuilder:rbac:groups=ipam.metal3.io,resources=ipclaims,verbs=get;list;watch;create;update;patch;delete |
| 56 | +// +kubebuilder:rbac:groups=ipam.metal3.io,resources=ipclaims/status,verbs=get;update;patch |
| 57 | +// +kubebuilder:rbac:groups=ipam.metal3.io,resources=ipaddresses,verbs=get;list;watch;create;update;patch;delete |
| 58 | +// +kubebuilder:rbac:groups=ipam.metal3.io,resources=ipaddresses/status,verbs=get;update;patch |
| 59 | + |
| 60 | +func (r *MaasMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 61 | + log := r.Log.WithValues("maasmachine", req.NamespacedName) |
| 62 | + var res *ctrl.Result |
| 63 | + var err error |
| 64 | + |
| 65 | + maasMachine := &infrav1.MaasMachine{} |
| 66 | + if err := r.Get(ctx, req.NamespacedName, maasMachine); err != nil { |
| 67 | + return ctrl.Result{}, util.IgnoreNotFound(err) |
| 68 | + } |
| 69 | + |
| 70 | + //handle the case where gvk is empty |
| 71 | + if maasMachine.GroupVersionKind().Empty() { |
| 72 | + log.V(0).Info("setting the missing gvk for maasMachine") |
| 73 | + maasMachine.Kind = "MaasMachine" |
| 74 | + maasMachine.APIVersion = infrav1.GroupVersion.String() |
| 75 | + } |
| 76 | + |
| 77 | + // fetch the capi machine. |
| 78 | + machine, err := clusterutilv1.GetOwnerMachine(ctx, r.Client, maasMachine.ObjectMeta) |
| 79 | + if err != nil { |
| 80 | + return ctrl.Result{}, err |
| 81 | + } |
| 82 | + if machine == nil { |
| 83 | + log.V(0).Info("waiting for machine controller to set ownerRef on MaasMachine") |
| 84 | + return ctrl.Result{}, nil |
| 85 | + } |
| 86 | + |
| 87 | + // fetch the capi cluster |
| 88 | + cluster, err := clusterutilv1.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta) |
| 89 | + if err != nil { |
| 90 | + log.V(0).Info("machine is missing cluster label or cluster does not exist") |
| 91 | + return ctrl.Result{}, nil |
| 92 | + } |
| 93 | + |
| 94 | + res, err = r.reconcileMaasMachineIPAddress(cluster, maasMachine) |
| 95 | + if err != nil { |
| 96 | + log.Error(err, "failed to reconcile MaasMachine IP") |
| 97 | + } |
| 98 | + |
| 99 | + if res == nil { |
| 100 | + res = &ctrl.Result{} |
| 101 | + } |
| 102 | + |
| 103 | + return *res, err |
| 104 | +} |
| 105 | + |
| 106 | +func (r *MaasMachineReconciler) reconcileMaasMachineIPAddress(cluster *capi.Cluster, maasMachine *infrav1.MaasMachine) (*ctrl.Result, error) { |
| 107 | + if maasMachine == nil { |
| 108 | + r.Log.V(0).Info("invalid MaasMachine, skipping reconcile IPAddress") |
| 109 | + return &ctrl.Result{}, nil |
| 110 | + } |
| 111 | + |
| 112 | + log := r.Log.WithValues("maasMachine", maasMachine.Name, "namespace", maasMachine.Namespace) |
| 113 | + log.V(0).Info("reconcile IP address for MaasMachine") |
| 114 | + |
| 115 | + // Check if static IP is enabled |
| 116 | + if maasMachine.Spec.StaticIP == nil { |
| 117 | + log.V(0).Info("MaasMachine static IP is not enabled, skipping IP allocation") |
| 118 | + return &ctrl.Result{}, nil |
| 119 | + } |
| 120 | + |
| 121 | + // Check if IP address is already allocated |
| 122 | + if maasMachine.Spec.StaticIP.IP != "" { |
| 123 | + log.V(0).Info("IP address is already allocated for MaasMachine", "ipAddress", maasMachine.Spec.StaticIP.IP) |
| 124 | + return &ctrl.Result{}, nil |
| 125 | + } |
| 126 | + |
| 127 | + dataPatch := client.MergeFrom(maasMachine.DeepCopy()) |
| 128 | + newIpamFunc, ok := factory.IpamFactory[ipam.IpamTypeMetal3io] |
| 129 | + if !ok { |
| 130 | + log.V(0).Info("ipam type not supported") |
| 131 | + return &ctrl.Result{}, nil |
| 132 | + } |
| 133 | + |
| 134 | + ipamFunc := newIpamFunc(r.Client, log) |
| 135 | + |
| 136 | + poolMatchLabels, err := r.getIPPoolMatchLabels(r.Client, maasMachine) |
| 137 | + if err != nil { |
| 138 | + log.Error(err, "failed to get IPPool match labels") |
| 139 | + return &ctrl.Result{}, nil |
| 140 | + } |
| 141 | + ipPool, err := ipamFunc.GetAvailableIPPool(poolMatchLabels, cluster.ObjectMeta) |
| 142 | + if err != nil { |
| 143 | + log.Error(err, "failed to get an available IPPool") |
| 144 | + return &ctrl.Result{}, nil |
| 145 | + } |
| 146 | + if ipPool == nil { |
| 147 | + log.V(0).Info("waiting for IPPool to be available") |
| 148 | + return &ctrl.Result{}, nil |
| 149 | + } |
| 150 | + |
| 151 | + ipName := maasMachine.Name |
| 152 | + ip, err := ipamFunc.GetIP(ipName, ipPool) |
| 153 | + if err != nil { |
| 154 | + return &ctrl.Result{}, errors.Wrapf(err, "failed to get allocated IP address for MaasMachine %s", maasMachine.Name) |
| 155 | + } |
| 156 | + |
| 157 | + if ip == nil { |
| 158 | + if _, err := ipamFunc.AllocateIP(ipName, ipPool, maasMachine); err != nil { |
| 159 | + return &ctrl.Result{}, errors.Wrapf(err, "failed to allocate IP address for MaasMachine: %s", maasMachine.Name) |
| 160 | + } |
| 161 | + |
| 162 | + log.V(0).Info("waiting for IP address to be available for the MaasMachine") |
| 163 | + return &ctrl.Result{RequeueAfter: 30 * time.Second}, nil |
| 164 | + } |
| 165 | + |
| 166 | + if err := util.ValidateIP(ip); err != nil { |
| 167 | + return &ctrl.Result{}, errors.Wrapf(err, "invalid IP address retrieved for MaasMachine: %s", maasMachine.Name) |
| 168 | + } |
| 169 | + |
| 170 | + log.V(0).Info("static IP selected for MaasMachine", "IPAddressName", ip.GetName()) |
| 171 | + |
| 172 | + ipAddr := util.GetAddress(ip) |
| 173 | + log.V(0).Info("assigning IP address to MaasMachine", "IPAddress", ipAddr) |
| 174 | + |
| 175 | + // Set the IP address and network configuration in the MaasMachine spec |
| 176 | + if maasMachine.Spec.StaticIP == nil { |
| 177 | + maasMachine.Spec.StaticIP = &infrav1.StaticIPConfig{} |
| 178 | + } |
| 179 | + |
| 180 | + // Update the MaasMachineTemplate with the IP address |
| 181 | + vmTemplateName, ok := maasMachine.GetAnnotations()[capi.TemplateClonedFromNameAnnotation] |
| 182 | + if ok { |
| 183 | + maasMachineTemplate := &infrav1.MaasMachineTemplate{} |
| 184 | + key := types.NamespacedName{Namespace: maasMachine.Namespace, Name: vmTemplateName} |
| 185 | + |
| 186 | + if err := r.Get(context.TODO(), key, maasMachineTemplate); err == nil { |
| 187 | + // Check if template already has the correct IP |
| 188 | + if maasMachineTemplate.Spec.Template.Spec.StaticIP == nil || |
| 189 | + maasMachineTemplate.Spec.Template.Spec.StaticIP.IP != ipAddr { |
| 190 | + templatePatch := client.MergeFrom(maasMachineTemplate.DeepCopy()) |
| 191 | + |
| 192 | + // Initialize StaticIP if nil |
| 193 | + if maasMachineTemplate.Spec.Template.Spec.StaticIP == nil { |
| 194 | + maasMachineTemplate.Spec.Template.Spec.StaticIP = &infrav1.StaticIPConfig{} |
| 195 | + } |
| 196 | + |
| 197 | + maasMachineTemplate.Spec.Template.Spec.StaticIP.IP = ipAddr |
| 198 | + |
| 199 | + if err := r.Patch(context.TODO(), maasMachineTemplate, templatePatch); err != nil { |
| 200 | + log.Error(err, "failed to patch MaasMachineTemplate with static IP") |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + // Set the allocated IP address |
| 206 | + maasMachine.Spec.StaticIP.IP = ipAddr |
| 207 | + |
| 208 | + if err := r.Patch(context.TODO(), maasMachine, dataPatch); err != nil { |
| 209 | + return &ctrl.Result{}, errors.Wrapf(err, "failed to patch MaasMachine %s", maasMachine.Name) |
| 210 | + } |
| 211 | + |
| 212 | + log.V(0).Info("successfully reconciled IP address for MaasMachine") |
| 213 | + |
| 214 | + return &ctrl.Result{}, nil |
| 215 | +} |
| 216 | + |
| 217 | +func (r *MaasMachineReconciler) getIPPoolMatchLabels(cli client.Client, maasMachine *infrav1.MaasMachine) (map[string]string, error) { |
| 218 | + |
| 219 | + //match labels for the IPPool are retrieved from the MaasMachineTemplate |
| 220 | + vmTemplateName, ok := maasMachine.GetAnnotations()[capi.TemplateClonedFromNameAnnotation] |
| 221 | + if !ok { |
| 222 | + return nil, fmt.Errorf("MaasMachine %s has no value set in the 'cloned-from-name' annotation", maasMachine.Name) |
| 223 | + } |
| 224 | + |
| 225 | + maasMachineTemplate := &infrav1.MaasMachineTemplate{} |
| 226 | + key := types.NamespacedName{Namespace: maasMachine.Namespace, Name: vmTemplateName} |
| 227 | + if err := cli.Get(context.Background(), key, maasMachineTemplate); err != nil { |
| 228 | + return nil, fmt.Errorf("failed to get MaasMachineTemplate %s", vmTemplateName) |
| 229 | + } |
| 230 | + |
| 231 | + return maasMachineTemplate.GetLabels(), nil |
| 232 | +} |
| 233 | + |
| 234 | +func (r *MaasMachineReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 235 | + return ctrl.NewControllerManagedBy(mgr). |
| 236 | + For(&infrav1.MaasMachine{}). |
| 237 | + Complete(r) |
| 238 | +} |
0 commit comments