-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathlinodeobjectstoragebucket_webhook.go
More file actions
121 lines (99 loc) · 5.16 KB
/
linodeobjectstoragebucket_webhook.go
File metadata and controls
121 lines (99 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright 2023 Akamai Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha2
import (
"context"
"fmt"
"slices"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
"github.com/linode/cluster-api-provider-linode/clients"
)
// log is for logging in this package.
var linodeobjectstoragebucketlog = logf.Log.WithName("linodeobjectstoragebucket-resource")
// SetupLinodeObjectStorageBucketWebhookWithManager registers the webhook for LinodeObjectStorageBucket in the manager.
func SetupLinodeObjectStorageBucketWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(&infrav1alpha2.LinodeObjectStorageBucket{}).
WithValidator(&LinodeObjectStorageBucketCustomValidator{}).
Complete()
}
// +kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1alpha2-linodeobjectstoragebucket,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=linodeobjectstoragebuckets,verbs=create;update,versions=v1alpha2,name=validation.linodeobjectstoragebucket.infrastructure.cluster.x-k8s.io,admissionReviewVersions=v1
// LinodeObjectStorageBucketCustomValidator struct is responsible for validating the LinodeObjectStorageBucket resource
type LinodeObjectStorageBucketCustomValidator struct {
Client client.Client
}
var _ webhook.CustomValidator = &LinodeObjectStorageBucketCustomValidator{}
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type LinodeObjectStorageBucket.
func (v *LinodeObjectStorageBucketCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
bucket, ok := obj.(*infrav1alpha2.LinodeObjectStorageBucket)
if !ok {
return nil, fmt.Errorf("expected a LinodeObjectStorageBucket object but got %T", obj)
}
linodeobjectstoragebucketlog.Info("validate create", "name", bucket.Name)
skipAPIValidation, linodeClient := setupClientWithCredentials(ctx, v.Client, bucket.Spec.CredentialsRef,
bucket.Name, bucket.GetNamespace(), linodemachinelog)
return nil, v.validateLinodeObjectStorageBucket(ctx, bucket, linodeClient, skipAPIValidation)
}
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type LinodeObjectStorageBucket.
func (v *LinodeObjectStorageBucketCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
bucket, ok := newObj.(*infrav1alpha2.LinodeObjectStorageBucket)
if !ok {
return nil, fmt.Errorf("expected a LinodeObjectStorageBucket object but got %T", newObj)
}
linodeobjectstoragebucketlog.Info("validate update", "name", bucket.Name)
return nil, nil
}
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type LinodeObjectStorageBucket.
func (v *LinodeObjectStorageBucketCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
bucket, ok := obj.(*infrav1alpha2.LinodeObjectStorageBucket)
if !ok {
return nil, fmt.Errorf("expected a LinodeObjectStorageBucket object but got %T", obj)
}
linodeobjectstoragebucketlog.Info("validate delete", "name", bucket.Name)
// No validation needed for deletion
return nil, nil
}
func (v *LinodeObjectStorageBucketCustomValidator) validateLinodeObjectStorageBucket(ctx context.Context, bucket *infrav1alpha2.LinodeObjectStorageBucket, linodeClient clients.LinodeClient, skipAPIValidation bool) error {
var errs field.ErrorList
if err := v.validateLinodeObjectStorageBucketSpec(ctx, bucket, linodeClient, skipAPIValidation); err != nil {
errs = slices.Concat(errs, err)
}
if len(errs) == 0 {
return nil
}
return apierrors.NewInvalid(
schema.GroupKind{Group: "infrastructure.cluster.x-k8s.io", Kind: "LinodeObjectStorageBucket"},
bucket.Name, errs)
}
func (v *LinodeObjectStorageBucketCustomValidator) validateLinodeObjectStorageBucketSpec(ctx context.Context, bucket *infrav1alpha2.LinodeObjectStorageBucket, linodeClient clients.LinodeClient, skipAPIValidation bool) field.ErrorList {
var errs field.ErrorList
if skipAPIValidation {
return errs
}
if err := validateObjectStorageRegion(ctx, linodeClient, bucket.Spec.Region, field.NewPath("spec").Child("region")); err != nil {
errs = append(errs, err)
}
if len(errs) == 0 {
return nil
}
return errs
}