This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
hcloudcluster_webhook.go
62 lines (49 loc) · 1.75 KB
/
hcloudcluster_webhook.go
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
package v1alpha3
import (
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
func (r *HcloudCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
func (r *HcloudClusterList) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
// +kubebuilder:webhook:verbs=create;update,path=/validate-cluster-api-provider-hcloud-swine-dev-v1alpha3-hcloudcluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster-api-provider-hcloud.swine.dev,resources=hcloudclusters,versions=v1alpha3,name=validation.hcloudcluster.cluster-api-provider-hcloud.swine.dev
var _ webhook.Validator = &HcloudCluster{}
func (r *HcloudCluster) ValidateCreate() error {
return nil
}
func (r *HcloudCluster) ValidateDelete() error {
return nil
}
func (r *HcloudCluster) ValidateUpdate(old runtime.Object) error {
var allErrs field.ErrorList
oldC, ok := old.(*HcloudCluster)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected an HcloudCluster but got a %T", old))
}
oldLocations := sets.NewString()
for _, l := range oldC.Spec.Locations {
oldLocations.Insert(string(l))
}
newLocations := sets.NewString()
for _, l := range r.Spec.Locations {
newLocations.Insert(string(l))
}
if !oldLocations.Equal(newLocations) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "locations"), r.Spec.Locations, "field is immutable"),
)
}
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
}