forked from kubernetes-sigs/cluster-api-provider-cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudstackcluster_webhook.go
157 lines (131 loc) · 6.44 KB
/
cloudstackcluster_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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright 2022 The Kubernetes Authors.
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 v1beta3
import (
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/webhookutil"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
// log is for logging in this package.
var cloudstackclusterlog = logf.Log.WithName("cloudstackcluster-resource")
func (r *CloudStackCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta3-cloudstackcluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=cloudstackclusters,versions=v1beta3,name=validation.cloudstackcluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta3-cloudstackcluster,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=cloudstackclusters,versions=v1beta3,name=default.cloudstackcluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
var (
_ webhook.Defaulter = &CloudStackCluster{}
_ webhook.Validator = &CloudStackCluster{}
)
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *CloudStackCluster) Default() {
cloudstackclusterlog.V(1).Info("entered api default setting webhook", "api resource name", r.Name)
// No defaulted values supported yet.
}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *CloudStackCluster) ValidateCreate() error {
cloudstackclusterlog.V(1).Info("entered validate create webhook", "api resource name", r.Name)
var errorList field.ErrorList
// Require FailureDomains and their respective sub-fields.
if len(r.Spec.FailureDomains) == 0 {
errorList = append(errorList, field.Required(field.NewPath("spec", "FailureDomains"), "FailureDomains"))
} else {
for _, fdSpec := range r.Spec.FailureDomains { // Require failureDomain names meet k8s qualified name spec.
for _, errMsg := range validation.IsDNS1123Subdomain(fdSpec.Name) {
errorList = append(errorList, field.Invalid(
field.NewPath("spec", "failureDomains", "name"), fdSpec.Name, errMsg))
}
if fdSpec.Zone.Network.Name == "" && fdSpec.Zone.Network.ID == "" {
errorList = append(errorList, field.Required(
field.NewPath("spec", "failureDomains", "Zone", "Network"),
"each Zone requires a Network specification"))
}
if fdSpec.ACSEndpoint.Name == "" || fdSpec.ACSEndpoint.Namespace == "" {
errorList = append(errorList, field.Required(
field.NewPath("spec", "failureDomains", "ACSEndpoint"),
"Name and Namespace are required"))
}
}
}
return webhookutil.AggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, errorList)
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *CloudStackCluster) ValidateUpdate(old runtime.Object) error {
cloudstackclusterlog.V(1).Info("entered validate update webhook", "api resource name", r.Name)
spec := r.Spec
oldCluster, ok := old.(*CloudStackCluster)
if !ok {
return errors.NewBadRequest(fmt.Sprintf("expected a CloudStackCluster but got a %T", old))
}
oldSpec := oldCluster.Spec
errorList := field.ErrorList(nil)
if err := ValidateFailureDomainUpdates(oldSpec.FailureDomains, spec.FailureDomains); err != nil {
errorList = append(errorList, err)
}
if oldSpec.ControlPlaneEndpoint.Host != "" { // Need to allow one time endpoint setting via CAPC cluster controller.
errorList = webhookutil.EnsureEqualStrings(
spec.ControlPlaneEndpoint.Host, oldSpec.ControlPlaneEndpoint.Host, "controlplaneendpoint.host", errorList)
errorList = webhookutil.EnsureEqualStrings(
string(spec.ControlPlaneEndpoint.Port), string(oldSpec.ControlPlaneEndpoint.Port),
"controlplaneendpoint.port", errorList)
}
return webhookutil.AggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, errorList)
}
// ValidateFailureDomainUpdates verifies that at least one failure domain has not been deleted, and
// failure domains that are held over have not been modified.
func ValidateFailureDomainUpdates(oldFDs, newFDs []CloudStackFailureDomainSpec) *field.Error {
newFDsByName := map[string]CloudStackFailureDomainSpec{}
for _, newFD := range newFDs {
newFDsByName[newFD.Name] = newFD
}
atLeastOneRemains := false
for _, oldFD := range oldFDs {
if newFD, present := newFDsByName[oldFD.Name]; present {
atLeastOneRemains = true
if !FailureDomainsEqual(newFD, oldFD) {
return field.Forbidden(field.NewPath("spec", "FailureDomains"),
fmt.Sprintf("Cannot change FailureDomain %s", oldFD.Name))
}
}
}
if !atLeastOneRemains {
return field.Forbidden(field.NewPath("spec", "FailureDomains"), "At least one FailureDomain must be unchanged on update.")
}
return nil
}
// FailureDomainsEqual is a manual deep equal on failure domains.
func FailureDomainsEqual(fd1, fd2 CloudStackFailureDomainSpec) bool {
return fd1.Name == fd2.Name &&
fd1.ACSEndpoint == fd2.ACSEndpoint &&
fd1.Account == fd2.Account &&
fd1.Domain == fd2.Domain &&
fd1.Zone.Name == fd2.Zone.Name &&
fd1.Zone.ID == fd2.Zone.ID &&
fd1.Zone.Network.Name == fd2.Zone.Network.Name &&
fd1.Zone.Network.ID == fd2.Zone.Network.ID &&
fd1.Zone.Network.Type == fd2.Zone.Network.Type
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *CloudStackCluster) ValidateDelete() error {
cloudstackclusterlog.V(1).Info("entered validate delete webhook", "api resource name", r.Name)
// No deletion validations. Deletion webhook not enabled.
return nil
}