|
| 1 | +/* |
| 2 | +Copyright 2023 Akamai Technologies, Inc. |
| 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 v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "slices" |
| 23 | + |
| 24 | + "github.com/linode/linodego" |
| 25 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + "k8s.io/apimachinery/pkg/api/resource" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 29 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 30 | + ctrl "sigs.k8s.io/controller-runtime" |
| 31 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 34 | + |
| 35 | + . "github.com/linode/cluster-api-provider-linode/clients" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + // The list of valid device slots that data device disks may attach to. |
| 40 | + // NOTE: sda is reserved for the OS device disk. |
| 41 | + LinodeMachineDevicePaths = []string{"sdb", "sdc", "sdd", "sde", "sdf", "sdg", "sdh"} |
| 42 | + |
| 43 | + // The maximum number of device disks allowed per [Configuration Profile per Linode’s Instance]. |
| 44 | + // |
| 45 | + // [Configuration Profile per Linode’s Instance]: https://www.linode.com/docs/api/linode-instances/#configuration-profile-view |
| 46 | + LinodeMachineMaxDisk = 8 |
| 47 | + |
| 48 | + // The maximum number of data device disks allowed in a Linode’s Instance's configuration profile. |
| 49 | + // NOTE: The first device disk is reserved for the OS disk |
| 50 | + LinodeMachineMaxDataDisk = LinodeMachineMaxDisk - 1 |
| 51 | +) |
| 52 | + |
| 53 | +// log is for logging in this package. |
| 54 | +var linodemachinelog = logf.Log.WithName("linodemachine-resource") |
| 55 | + |
| 56 | +// SetupWebhookWithManager will setup the manager to manage the webhooks |
| 57 | +func (r *LinodeMachine) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 58 | + return ctrl.NewWebhookManagedBy(mgr). |
| 59 | + For(r). |
| 60 | + Complete() |
| 61 | +} |
| 62 | + |
| 63 | +// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! |
| 64 | + |
| 65 | +// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable updation and deletion validation. |
| 66 | +//+kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1alpha1-linodemachine,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=linodemachines,verbs=create,versions=v1alpha1,name=vlinodemachine.kb.io,admissionReviewVersions=v1 |
| 67 | + |
| 68 | +var _ webhook.Validator = &LinodeMachine{} |
| 69 | + |
| 70 | +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type |
| 71 | +func (r *LinodeMachine) ValidateCreate() (admission.Warnings, error) { |
| 72 | + linodemachinelog.Info("validate create", "name", r.Name) |
| 73 | + |
| 74 | + ctx, cancel := context.WithTimeout(context.Background(), defaultWebhookTimeout) |
| 75 | + defer cancel() |
| 76 | + |
| 77 | + return nil, r.validateLinodeMachine(ctx, &defaultLinodeClient) |
| 78 | +} |
| 79 | + |
| 80 | +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type |
| 81 | +func (r *LinodeMachine) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { |
| 82 | + linodemachinelog.Info("validate update", "name", r.Name) |
| 83 | + |
| 84 | + // TODO(user): fill in your validation logic upon object update. |
| 85 | + return nil, nil |
| 86 | +} |
| 87 | + |
| 88 | +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type |
| 89 | +func (r *LinodeMachine) ValidateDelete() (admission.Warnings, error) { |
| 90 | + linodemachinelog.Info("validate delete", "name", r.Name) |
| 91 | + |
| 92 | + // TODO(user): fill in your validation logic upon object deletion. |
| 93 | + return nil, nil |
| 94 | +} |
| 95 | + |
| 96 | +func (r *LinodeMachine) validateLinodeMachine(ctx context.Context, client LinodeClient) error { |
| 97 | + var errs field.ErrorList |
| 98 | + |
| 99 | + if err := r.validateLinodeMachineSpec(ctx, client); err != nil { |
| 100 | + errs = slices.Concat(errs, err) |
| 101 | + } |
| 102 | + |
| 103 | + if len(errs) == 0 { |
| 104 | + return nil |
| 105 | + } |
| 106 | + return apierrors.NewInvalid( |
| 107 | + schema.GroupKind{Group: "infrastructure.cluster.x-k8s.io", Kind: "LinodeMachine"}, |
| 108 | + r.Name, errs) |
| 109 | +} |
| 110 | + |
| 111 | +func (r *LinodeMachine) validateLinodeMachineSpec(ctx context.Context, client LinodeClient) field.ErrorList { |
| 112 | + var errs field.ErrorList |
| 113 | + |
| 114 | + if err := validateRegion(ctx, client, r.Spec.Region, field.NewPath("spec").Child("region")); err != nil { |
| 115 | + errs = append(errs, err) |
| 116 | + } |
| 117 | + plan, err := validateLinodeType(ctx, client, r.Spec.Type, field.NewPath("spec").Child("type")) |
| 118 | + if err != nil { |
| 119 | + errs = append(errs, err) |
| 120 | + } |
| 121 | + if err := r.validateLinodeMachineDisks(plan); err != nil { |
| 122 | + errs = append(errs, err) |
| 123 | + } |
| 124 | + |
| 125 | + if len(errs) == 0 { |
| 126 | + return nil |
| 127 | + } |
| 128 | + return errs |
| 129 | +} |
| 130 | + |
| 131 | +func (r *LinodeMachine) validateLinodeMachineDisks(plan *linodego.LinodeType) *field.Error { |
| 132 | + // The Linode plan information is required to perform disk validation |
| 133 | + if plan == nil { |
| 134 | + return nil |
| 135 | + } |
| 136 | + |
| 137 | + var ( |
| 138 | + // The Linode API represents storage sizes in megabytes (MB) |
| 139 | + // https://www.linode.com/docs/api/linode-types/#type-view |
| 140 | + planSize = resource.MustParse(fmt.Sprintf("%d%s", plan.Disk, "M")) |
| 141 | + remainSize = &resource.Quantity{} |
| 142 | + err *field.Error |
| 143 | + ) |
| 144 | + planSize.DeepCopyInto(remainSize) |
| 145 | + |
| 146 | + if remainSize, err = validateDisk(r.Spec.OSDisk, field.NewPath("spec").Child("osDisk"), remainSize, &planSize); err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + if _, err := validateDataDisks(r.Spec.DataDisks, field.NewPath("spec").Child("dataDisks"), remainSize, &planSize); err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func validateDataDisks(disks map[string]*InstanceDisk, path *field.Path, remainSize, planSize *resource.Quantity) (*resource.Quantity, *field.Error) { |
| 157 | + devs := []string{} |
| 158 | + |
| 159 | + for dev, disk := range disks { |
| 160 | + if !slices.Contains(LinodeMachineDevicePaths, dev) { |
| 161 | + return nil, field.Forbidden(path.Child(dev), fmt.Sprintf("allowed device paths: %v", LinodeMachineDevicePaths)) |
| 162 | + } |
| 163 | + if slices.Contains(devs, dev) { |
| 164 | + return nil, field.Duplicate(path.Child(dev), "duplicate device path") |
| 165 | + } |
| 166 | + devs = append(devs, dev) |
| 167 | + if len(devs) > LinodeMachineMaxDataDisk { |
| 168 | + return nil, field.TooMany(path, len(devs), LinodeMachineMaxDataDisk) |
| 169 | + } |
| 170 | + |
| 171 | + var err *field.Error |
| 172 | + if remainSize, err = validateDisk(disk, path.Child(dev), remainSize, planSize); err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + } |
| 176 | + return remainSize, nil |
| 177 | +} |
| 178 | + |
| 179 | +func validateDisk(disk *InstanceDisk, path *field.Path, remainSize, planSize *resource.Quantity) (*resource.Quantity, *field.Error) { |
| 180 | + if disk == nil { |
| 181 | + return remainSize, nil |
| 182 | + } |
| 183 | + |
| 184 | + if disk.Size.Sign() < 1 { |
| 185 | + return nil, field.Invalid(path, disk.Size.String(), "invalid size") |
| 186 | + } |
| 187 | + if remainSize.Cmp(disk.Size) == -1 { |
| 188 | + return nil, field.Invalid(path, disk.Size.String(), fmt.Sprintf("sum disk sizes exceeds plan storage: %s", planSize.String())) |
| 189 | + } |
| 190 | + |
| 191 | + // Decrement the remaining amount of space available |
| 192 | + remainSize.Sub(disk.Size) |
| 193 | + return remainSize, nil |
| 194 | +} |
0 commit comments