|
| 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 | + "slices" |
| 22 | + |
| 23 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 26 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 31 | + |
| 32 | + . "github.com/linode/cluster-api-provider-linode/clients" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + // The capability string indicating a region supports Object Storage: [Object Storage Availability] |
| 37 | + // |
| 38 | + // [Object Storage Availability]: https://www.linode.com/docs/products/storage/object-storage/#availability |
| 39 | + LinodeObjectStorageCapability = "Object Storage" |
| 40 | +) |
| 41 | + |
| 42 | +// log is for logging in this package. |
| 43 | +var linodeobjectstoragebucketlog = logf.Log.WithName("linodeobjectstoragebucket-resource") |
| 44 | + |
| 45 | +// SetupWebhookWithManager will setup the manager to manage the webhooks |
| 46 | +func (r *LinodeObjectStorageBucket) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 47 | + return ctrl.NewWebhookManagedBy(mgr). |
| 48 | + For(r). |
| 49 | + Complete() |
| 50 | +} |
| 51 | + |
| 52 | +// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable updation and deletion validation. |
| 53 | +//+kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1alpha1-linodeobjectstoragebucket,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=linodeobjectstoragebuckets,verbs=create,versions=v1alpha1,name=vlinodeobjectstoragebucket.kb.io,admissionReviewVersions=v1 |
| 54 | + |
| 55 | +var _ webhook.Validator = &LinodeObjectStorageBucket{} |
| 56 | + |
| 57 | +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type |
| 58 | +func (r *LinodeObjectStorageBucket) ValidateCreate() (admission.Warnings, error) { |
| 59 | + linodeobjectstoragebucketlog.Info("validate create", "name", r.Name) |
| 60 | + |
| 61 | + ctx, cancel := context.WithTimeout(context.Background(), defaultWebhookTimeout) |
| 62 | + defer cancel() |
| 63 | + |
| 64 | + return nil, r.validateLinodeObjectStorageBucket(ctx, &defaultLinodeClient) |
| 65 | +} |
| 66 | + |
| 67 | +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type |
| 68 | +func (r *LinodeObjectStorageBucket) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { |
| 69 | + linodeobjectstoragebucketlog.Info("validate update", "name", r.Name) |
| 70 | + |
| 71 | + // TODO(user): fill in your validation logic upon object update. |
| 72 | + return nil, nil |
| 73 | +} |
| 74 | + |
| 75 | +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type |
| 76 | +func (r *LinodeObjectStorageBucket) ValidateDelete() (admission.Warnings, error) { |
| 77 | + linodeobjectstoragebucketlog.Info("validate delete", "name", r.Name) |
| 78 | + |
| 79 | + // TODO(user): fill in your validation logic upon object deletion. |
| 80 | + return nil, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (r *LinodeObjectStorageBucket) validateLinodeObjectStorageBucket(ctx context.Context, client LinodeClient) error { |
| 84 | + var errs field.ErrorList |
| 85 | + |
| 86 | + if err := r.validateLinodeObjectStorageBucketSpec(ctx, client); err != nil { |
| 87 | + errs = slices.Concat(errs, err) |
| 88 | + } |
| 89 | + |
| 90 | + if len(errs) == 0 { |
| 91 | + return nil |
| 92 | + } |
| 93 | + return apierrors.NewInvalid( |
| 94 | + schema.GroupKind{Group: "infrastructure.cluster.x-k8s.io", Kind: "LinodeObjectStorageBucket"}, |
| 95 | + r.Name, errs) |
| 96 | +} |
| 97 | + |
| 98 | +func (r *LinodeObjectStorageBucket) validateLinodeObjectStorageBucketSpec(ctx context.Context, client LinodeClient) field.ErrorList { |
| 99 | + var errs field.ErrorList |
| 100 | + |
| 101 | + if err := validateObjectStorageCluster(ctx, client, r.Spec.Cluster, field.NewPath("spec").Child("cluster")); err != nil { |
| 102 | + errs = append(errs, err) |
| 103 | + } |
| 104 | + |
| 105 | + if len(errs) == 0 { |
| 106 | + return nil |
| 107 | + } |
| 108 | + return errs |
| 109 | +} |
0 commit comments