forked from kubernetes-sigs/cluster-api-provider-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azuremachine_webhook.go
234 lines (197 loc) · 7.95 KB
/
azuremachine_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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
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/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
// SetupAzureMachineWebhookWithManager sets up and registers the webhook with the manager.
func SetupAzureMachineWebhookWithManager(mgr ctrl.Manager) error {
mw := &azureMachineWebhook{Client: mgr.GetClient()}
return ctrl.NewWebhookManagedBy(mgr).
For(&AzureMachine{}).
WithDefaulter(mw).
WithValidator(mw).
Complete()
}
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremachine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azuremachines,versions=v1beta1,name=validation.azuremachine.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-azuremachine,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azuremachines,versions=v1beta1,name=default.azuremachine.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// azureMachineWebhook implements a validating and defaulting webhook for AzureMachines.
type azureMachineWebhook struct {
Client client.Client
}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (mw *azureMachineWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
m, ok := obj.(*AzureMachine)
if !ok {
return nil, apierrors.NewBadRequest("expected an AzureMachine resource")
}
spec := m.Spec
allErrs := ValidateAzureMachineSpec(spec)
roleAssignmentName := ""
if spec.SystemAssignedIdentityRole != nil {
roleAssignmentName = spec.SystemAssignedIdentityRole.Name
}
if errs := ValidateSystemAssignedIdentity(spec.Identity, "", roleAssignmentName, field.NewPath("roleAssignmentName")); len(errs) > 0 {
allErrs = append(allErrs, errs...)
}
if len(allErrs) == 0 {
return nil, nil
}
return nil, apierrors.NewInvalid(GroupVersion.WithKind(AzureMachineKind).GroupKind(), m.Name, allErrs)
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (mw *azureMachineWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
var allErrs field.ErrorList
old, ok := oldObj.(*AzureMachine)
if !ok {
return nil, apierrors.NewBadRequest("expected an AzureMachine resource")
}
m, ok := newObj.(*AzureMachine)
if !ok {
return nil, apierrors.NewBadRequest("expected an AzureMachine resource")
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "Image"),
old.Spec.Image,
m.Spec.Image); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "Identity"),
old.Spec.Identity,
m.Spec.Identity); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "SystemAssignedIdentityRole"),
old.Spec.SystemAssignedIdentityRole,
m.Spec.SystemAssignedIdentityRole); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "UserAssignedIdentities"),
old.Spec.UserAssignedIdentities,
m.Spec.UserAssignedIdentities); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "RoleAssignmentName"),
old.Spec.RoleAssignmentName,
m.Spec.RoleAssignmentName); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "OSDisk"),
old.Spec.OSDisk,
m.Spec.OSDisk); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "DataDisks"),
old.Spec.DataDisks,
m.Spec.DataDisks); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "SSHPublicKey"),
old.Spec.SSHPublicKey,
m.Spec.SSHPublicKey); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "AllocatePublicIP"),
old.Spec.AllocatePublicIP,
m.Spec.AllocatePublicIP); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "EnableIPForwarding"),
old.Spec.EnableIPForwarding,
m.Spec.EnableIPForwarding); err != nil {
allErrs = append(allErrs, err)
}
// Spec.AcceleratedNetworking can only be reset to nil and no other changes apart from that
// is accepted if the field is set.
// Ref issue #3518
if err := webhookutils.ValidateZeroTransition(
field.NewPath("Spec", "AcceleratedNetworking"),
old.Spec.AcceleratedNetworking,
m.Spec.AcceleratedNetworking); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "SpotVMOptions"),
old.Spec.SpotVMOptions,
m.Spec.SpotVMOptions); err != nil {
allErrs = append(allErrs, err)
}
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "SecurityProfile"),
old.Spec.SecurityProfile,
m.Spec.SecurityProfile); err != nil {
allErrs = append(allErrs, err)
}
if old.Spec.Diagnostics != nil {
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "Diagnostics"),
old.Spec.Diagnostics,
m.Spec.Diagnostics); err != nil {
allErrs = append(allErrs, err)
}
}
if !reflect.DeepEqual(m.Spec.NetworkInterfaces, old.Spec.NetworkInterfaces) {
// The defaulting webhook may have migrated values from the old SubnetName field to the new NetworkInterfaces format.
old.Spec.SetNetworkInterfacesDefaults()
// The reconciler will populate the SubnetName on the first interface if the user left it blank.
if old.Spec.NetworkInterfaces[0].SubnetName == "" && m.Spec.NetworkInterfaces[0].SubnetName != "" {
old.Spec.NetworkInterfaces[0].SubnetName = m.Spec.NetworkInterfaces[0].SubnetName
}
// Enforce immutability for all other changes to NetworkInterfaces.
if !reflect.DeepEqual(m.Spec.NetworkInterfaces, old.Spec.NetworkInterfaces) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "networkInterfaces"),
m.Spec.NetworkInterfaces, "field is immutable"),
)
}
}
if err := webhookutils.ValidateImmutable(
field.NewPath("spec", "capacityReservationGroupID"),
old.Spec.CapacityReservationGroupID,
m.Spec.CapacityReservationGroupID); err != nil {
allErrs = append(allErrs, err)
}
if len(allErrs) == 0 {
return nil, nil
}
return nil, apierrors.NewInvalid(GroupVersion.WithKind(AzureMachineKind).GroupKind(), m.Name, allErrs)
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (mw *azureMachineWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return nil, nil
}
// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (mw *azureMachineWebhook) Default(ctx context.Context, obj runtime.Object) error {
m, ok := obj.(*AzureMachine)
if !ok {
return apierrors.NewBadRequest("expected an AzureMachine resource")
}
return m.SetDefaults(mw.Client)
}