-
Notifications
You must be signed in to change notification settings - Fork 415
/
azuremanagedmachinepool_webhook.go
319 lines (275 loc) · 9.8 KB
/
azuremanagedmachinepool_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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
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"
"fmt"
"reflect"
"github.com/Azure/go-autorest/autorest/to"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/maps"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedmachinepool,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedmachinepools,verbs=create;update,versions=v1beta1,name=default.azuremanagedmachinepools.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (m *AzureManagedMachinePool) Default(client client.Client) {
if m.Labels == nil {
m.Labels = make(map[string]string)
}
m.Labels[LabelAgentPoolMode] = m.Spec.Mode
if m.Spec.Name == nil || *m.Spec.Name == "" {
m.Spec.Name = &m.Name
}
if m.Spec.OSType == nil {
m.Spec.OSType = to.StringPtr(DefaultOSType)
}
}
//+kubebuilder:webhook:verbs=update;delete,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedmachinepool,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedmachinepools,versions=v1beta1,name=validation.azuremanagedmachinepools.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (m *AzureManagedMachinePool) ValidateCreate(client client.Client) error {
validators := []func() error{
m.validateMaxPods,
m.validateOSType,
m.validateName,
}
var errs []error
for _, validator := range validators {
if err := validator(); err != nil {
errs = append(errs, err)
}
}
return kerrors.NewAggregate(errs)
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (m *AzureManagedMachinePool) ValidateUpdate(oldRaw runtime.Object, client client.Client) error {
old := oldRaw.(*AzureManagedMachinePool)
var allErrs field.ErrorList
if m.Spec.SKU != old.Spec.SKU {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "SKU"),
m.Spec.SKU,
"field is immutable"))
}
if old.Spec.OSDiskSizeGB != nil {
// Prevent OSDiskSizeGB modification if it was already set to some value
if m.Spec.OSDiskSizeGB == nil {
// unsetting the field is not allowed
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "OSDiskSizeGB"),
m.Spec.OSDiskSizeGB,
"field is immutable, unsetting is not allowed"))
} else if *m.Spec.OSDiskSizeGB != *old.Spec.OSDiskSizeGB {
// changing the field is not allowed
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "OSDiskSizeGB"),
*m.Spec.OSDiskSizeGB,
"field is immutable"))
}
}
if !reflect.DeepEqual(m.Spec.Taints, old.Spec.Taints) {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "Taints"),
m.Spec.Taints,
"field is immutable"))
}
// custom headers are immutable
oldCustomHeaders := maps.FilterByKeyPrefix(old.ObjectMeta.Annotations, azure.CustomHeaderPrefix)
newCustomHeaders := maps.FilterByKeyPrefix(m.ObjectMeta.Annotations, azure.CustomHeaderPrefix)
if !reflect.DeepEqual(oldCustomHeaders, newCustomHeaders) {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("metadata", "annotations"),
m.ObjectMeta.Annotations,
fmt.Sprintf("annotations with '%s' prefix are immutable", azure.CustomHeaderPrefix)))
}
if !ensureStringSlicesAreEqual(m.Spec.AvailabilityZones, old.Spec.AvailabilityZones) {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "AvailabilityZones"),
m.Spec.AvailabilityZones,
"field is immutable"))
}
if m.Spec.Mode != string(NodePoolModeSystem) && old.Spec.Mode == string(NodePoolModeSystem) {
// validate for last system node pool
if err := m.validateLastSystemNodePool(client); err != nil {
allErrs = append(allErrs, field.Invalid(
field.NewPath("Spec", "Mode"),
m.Spec.Mode,
"Last system node pool cannot be mutated to user node pool"))
}
}
if old.Spec.MaxPods != nil {
// Prevent MaxPods modification if it was already set to some value
if m.Spec.MaxPods == nil {
// unsetting the field is not allowed
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "MaxPods"),
m.Spec.MaxPods,
"field is immutable, unsetting is not allowed"))
} else if *m.Spec.MaxPods != *old.Spec.MaxPods {
// changing the field is not allowed
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "MaxPods"),
*m.Spec.MaxPods,
"field is immutable"))
}
}
if old.Spec.OsDiskType != nil {
// Prevent OSDiskType modification if it was already set to some value
if m.Spec.OsDiskType == nil || to.String(m.Spec.OsDiskType) == "" {
// unsetting the field is not allowed
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "OsDiskType"),
m.Spec.OsDiskType,
"field is immutable, unsetting is not allowed"))
} else if *m.Spec.OsDiskType != *old.Spec.OsDiskType {
// changing the field is not allowed
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "OsDiskType"),
m.Spec.OsDiskType,
"field is immutable"))
}
}
if old.Spec.EnableUltraSSD != nil {
// Prevent EnabledUltraSSD modification if it was already set to some value
if m.Spec.EnableUltraSSD == nil {
// unsetting the field is not allowed
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "EnableUltraSSD"),
m.Spec.EnableUltraSSD,
"field is immutable, unsetting is not allowed"))
} else if *m.Spec.EnableUltraSSD != *old.Spec.EnableUltraSSD {
// changing the field is not allowed
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "EnableUltraSSD"),
m.Spec.EnableUltraSSD,
"field is immutable"))
}
} else {
if m.Spec.EnableUltraSSD != nil {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("Spec", "EnableUltraSSD"),
m.Spec.EnableUltraSSD,
"field is immutable, unsetting is not allowed"))
}
}
if len(allErrs) != 0 {
return apierrors.NewInvalid(GroupVersion.WithKind("AzureManagedMachinePool").GroupKind(), m.Name, allErrs)
}
return nil
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (m *AzureManagedMachinePool) ValidateDelete(client client.Client) error {
if m.Spec.Mode != string(NodePoolModeSystem) {
return nil
}
return errors.Wrapf(m.validateLastSystemNodePool(client), "if the delete is triggered via owner MachinePool please refer to trouble shooting section in https://capz.sigs.k8s.io/topics/managedcluster.html")
}
// validateLastSystemNodePool is used to check if the existing system node pool is the last system node pool.
// If it is a last system node pool it cannot be deleted or mutated to user node pool as AKS expects min 1 system node pool.
func (m *AzureManagedMachinePool) validateLastSystemNodePool(cli client.Client) error {
ctx := context.Background()
// Fetch the Cluster.
clusterName, ok := m.Labels[clusterv1.ClusterLabelName]
if !ok {
return nil
}
ownerCluster := &clusterv1.Cluster{}
key := client.ObjectKey{
Namespace: m.Namespace,
Name: clusterName,
}
if err := cli.Get(ctx, key, ownerCluster); err != nil {
return err
}
if !ownerCluster.DeletionTimestamp.IsZero() {
return nil
}
opt1 := client.InNamespace(m.Namespace)
opt2 := client.MatchingLabels(map[string]string{
clusterv1.ClusterLabelName: clusterName,
LabelAgentPoolMode: string(NodePoolModeSystem),
})
ammpList := &AzureManagedMachinePoolList{}
if err := cli.List(ctx, ammpList, opt1, opt2); err != nil {
return err
}
if len(ammpList.Items) <= 1 {
return errors.New("AKS Cluster must have at least one system pool")
}
return nil
}
func (m *AzureManagedMachinePool) validateMaxPods() error {
if m.Spec.MaxPods != nil {
if to.Int32(m.Spec.MaxPods) < 10 || to.Int32(m.Spec.MaxPods) > 250 {
return field.Invalid(
field.NewPath("Spec", "MaxPods"),
m.Spec.MaxPods,
"MaxPods must be between 10 and 250")
}
}
return nil
}
func (m *AzureManagedMachinePool) validateOSType() error {
if m.Spec.Mode == string(NodePoolModeSystem) {
if m.Spec.OSType != nil && *m.Spec.OSType != azure.LinuxOS {
return field.Forbidden(
field.NewPath("Spec", "OSType"),
"System node pooll must have OSType 'Linux'")
}
}
return nil
}
func (m *AzureManagedMachinePool) validateName() error {
if m.Spec.OSType != nil && *m.Spec.OSType == azure.WindowsOS {
if len(m.Name) > 6 {
return field.Invalid(
field.NewPath("Name"),
m.Name,
"Windows agent pool name can not be longer than 6 characters.")
}
}
return nil
}
func ensureStringSlicesAreEqual(a []string, b []string) bool {
if len(a) != len(b) {
return false
}
m := map[string]bool{}
for _, v := range a {
m[v] = true
}
for _, v := range b {
if _, ok := m[v]; !ok {
return false
}
}
return true
}