-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathazurecluster_webhook.go
More file actions
218 lines (188 loc) · 9.05 KB
/
Copy pathazurecluster_webhook.go
File metadata and controls
218 lines (188 loc) · 9.05 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Copyright 2021 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 v1beta1
import (
"context"
"reflect"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
webhookutils "sigs.k8s.io/cluster-api-provider-azure/util/webhook"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
// SetupWebhookWithManager sets up and registers the webhook with the manager.
func (c *AzureCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr, c).
WithCustomDefaulter(c).
WithCustomValidator(c).
Complete()
}
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azurecluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azureclusters,versions=v1beta1,name=validation.azurecluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-azurecluster,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azureclusters,versions=v1beta1,name=default.azurecluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
var _ webhook.CustomValidator = &AzureCluster{}
var _ webhook.CustomDefaulter = &AzureCluster{}
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type.
func (c *AzureCluster) Default(_ context.Context, obj runtime.Object) error {
cluster, ok := obj.(*AzureCluster)
if !ok {
return apierrors.NewBadRequest("expected an AzureCluster")
}
cluster.setDefaults()
return nil
}
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
func (c *AzureCluster) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
cluster, ok := obj.(*AzureCluster)
if !ok {
return nil, apierrors.NewBadRequest("expected an AzureCluster")
}
return cluster.validateCluster(nil)
}
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
func (c *AzureCluster) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
var allErrs field.ErrorList
old, ok := oldObj.(*AzureCluster)
if !ok {
return nil, apierrors.NewBadRequest("expected an AzureCluster")
}
cluster, ok := newObj.(*AzureCluster)
if !ok {
return nil, apierrors.NewBadRequest("expected an AzureCluster")
}
if err := webhookutils.ValidateImmutable(
field.NewPath("spec", "resourceGroup"),
old.Spec.ResourceGroup,
cluster.Spec.ResourceGroup); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("spec", "subscriptionID"),
old.Spec.SubscriptionID,
cluster.Spec.SubscriptionID); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("spec", "location"),
old.Spec.Location,
cluster.Spec.Location); err != nil {
allErrs = append(allErrs, err)
}
if old.Spec.ControlPlaneEndpoint.Host != "" && cluster.Spec.ControlPlaneEndpoint.Host != old.Spec.ControlPlaneEndpoint.Host {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "controlPlaneEndpoint", "host"),
cluster.Spec.ControlPlaneEndpoint.Host, "field is immutable"),
)
}
if old.Spec.ControlPlaneEndpoint.Port != 0 && cluster.Spec.ControlPlaneEndpoint.Port != old.Spec.ControlPlaneEndpoint.Port {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "controlPlaneEndpoint", "port"),
cluster.Spec.ControlPlaneEndpoint.Port, "field is immutable"),
)
}
if !reflect.DeepEqual(cluster.Spec.AzureEnvironment, old.Spec.AzureEnvironment) {
// The equality failure could be because of default mismatch between v1alpha3 and v1beta1. This happens because
// the new object `r` will have run through the default webhooks but the old object `old` would not have so.
// This means if the old object was in v1alpha3, it would not get the new defaults set in v1beta1 resulting
// in object inequality. To workaround this, we set the v1beta1 defaults here so that the old object also gets
// the new defaults.
old.setAzureEnvironmentDefault()
// if it's still not equal, return error.
if !reflect.DeepEqual(cluster.Spec.AzureEnvironment, old.Spec.AzureEnvironment) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "azureEnvironment"),
cluster.Spec.AzureEnvironment, "field is immutable"),
)
}
}
if err := webhookutils.ValidateImmutable(
field.NewPath("spec", "networkSpec", "privateDNSZoneName"),
old.Spec.NetworkSpec.PrivateDNSZoneName,
cluster.Spec.NetworkSpec.PrivateDNSZoneName); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("spec", "networkSpec", "privateDNSZoneResourceGroup"),
old.Spec.NetworkSpec.PrivateDNSZoneResourceGroup,
cluster.Spec.NetworkSpec.PrivateDNSZoneResourceGroup); err != nil {
allErrs = append(allErrs, err)
}
// Allow enabling azure bastion but avoid disabling it.
if old.Spec.BastionSpec.AzureBastion != nil && !reflect.DeepEqual(old.Spec.BastionSpec.AzureBastion, cluster.Spec.BastionSpec.AzureBastion) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "bastionSpec", "azureBastion"),
cluster.Spec.BastionSpec.AzureBastion, "azure bastion cannot be removed from a cluster"),
)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("spec", "networkSpec", "controlPlaneOutboundLB"),
old.Spec.NetworkSpec.ControlPlaneOutboundLB,
cluster.Spec.NetworkSpec.ControlPlaneOutboundLB); err != nil {
allErrs = append(allErrs, err)
}
allErrs = append(allErrs, cluster.validateSubnetUpdate(old)...)
if len(allErrs) == 0 {
return cluster.validateCluster(old)
}
return nil, apierrors.NewInvalid(GroupVersion.WithKind(AzureClusterKind).GroupKind(), cluster.Name, allErrs)
}
// validateSubnetUpdate validates a ClusterSpec.NetworkSpec.Subnets for immutability.
func (c *AzureCluster) validateSubnetUpdate(old *AzureCluster) field.ErrorList {
var allErrs field.ErrorList
oldSubnetMap := make(map[string]SubnetSpec, len(old.Spec.NetworkSpec.Subnets))
oldSubnetIndex := make(map[string]int, len(old.Spec.NetworkSpec.Subnets))
for i, subnet := range old.Spec.NetworkSpec.Subnets {
oldSubnetMap[subnet.Name] = subnet
oldSubnetIndex[subnet.Name] = i
}
for i, subnet := range c.Spec.NetworkSpec.Subnets {
if oldSubnet, ok := oldSubnetMap[subnet.Name]; ok {
// Verify the CIDR blocks haven't changed for an owned Vnet.
// A non-owned Vnet's CIDR block can change based on what's
// defined in the spec vs what's been loaded from Azure directly.
// This technically allows the cidr block to be modified in the brief
// moments before the Vnet is created (because the tags haven't been
// set yet) but once the Vnet has been created it becomes immutable.
if old.Spec.NetworkSpec.Vnet.Tags.HasOwned(old.Name) && !reflect.DeepEqual(subnet.CIDRBlocks, oldSubnet.CIDRBlocks) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "networkSpec", "subnets").Index(oldSubnetIndex[subnet.Name]).Child("CIDRBlocks"),
c.Spec.NetworkSpec.Subnets[i].CIDRBlocks, "field is immutable"),
)
}
if subnet.RouteTable.Name != oldSubnet.RouteTable.Name {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "networkSpec", "subnets").Index(oldSubnetIndex[subnet.Name]).Child("RouteTable").Child("Name"),
c.Spec.NetworkSpec.Subnets[i].RouteTable.Name, "field is immutable"),
)
}
if (subnet.NatGateway.Name != oldSubnet.NatGateway.Name) && (oldSubnet.NatGateway.Name != "") {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "networkSpec", "subnets").Index(oldSubnetIndex[subnet.Name]).Child("NatGateway").Child("Name"),
c.Spec.NetworkSpec.Subnets[i].NatGateway.Name, "field is immutable"),
)
}
if subnet.SecurityGroup.Name != oldSubnet.SecurityGroup.Name {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "networkSpec", "subnets").Index(oldSubnetIndex[subnet.Name]).Child("SecurityGroup").Child("Name"),
c.Spec.NetworkSpec.Subnets[i].SecurityGroup.Name, "field is immutable"),
)
}
}
}
return allErrs
}
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
func (c *AzureCluster) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
return nil, nil
}