-
Notifications
You must be signed in to change notification settings - Fork 539
/
awsmachine_webhook.go
219 lines (174 loc) · 8.59 KB
/
awsmachine_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
/*
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 (
"reflect"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
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 _ = logf.Log.WithName("awsmachine-resource")
func (r *AWSMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-awsmachine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsmachines,versions=v1beta1,name=validation.awsmachine.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-awsmachine,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=awsmachines,versions=v1beta1,name=mawsmachine.kb.io,name=mutation.awsmachine.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
var (
_ webhook.Validator = &AWSMachine{}
_ webhook.Defaulter = &AWSMachine{}
)
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSMachine) ValidateCreate() error {
var allErrs field.ErrorList
allErrs = append(allErrs, r.validateCloudInitSecret()...)
allErrs = append(allErrs, r.validateRootVolume()...)
allErrs = append(allErrs, r.validateNonRootVolumes()...)
allErrs = append(allErrs, r.validateSSHKeyName()...)
allErrs = append(allErrs, r.validateAdditionalSecurityGroups()...)
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSMachine) ValidateUpdate(old runtime.Object) error {
newAWSMachine, err := runtime.DefaultUnstructuredConverter.ToUnstructured(r)
if err != nil {
return apierrors.NewInvalid(GroupVersion.WithKind("AWSMachine").GroupKind(), r.Name, field.ErrorList{
field.InternalError(nil, errors.Wrap(err, "failed to convert new AWSMachine to unstructured object")),
})
}
oldAWSMachine, err := runtime.DefaultUnstructuredConverter.ToUnstructured(old)
if err != nil {
return apierrors.NewInvalid(GroupVersion.WithKind("AWSMachine").GroupKind(), r.Name, field.ErrorList{
field.InternalError(nil, errors.Wrap(err, "failed to convert old AWSMachine to unstructured object")),
})
}
var allErrs field.ErrorList
allErrs = append(allErrs, r.validateCloudInitSecret()...)
newAWSMachineSpec := newAWSMachine["spec"].(map[string]interface{})
oldAWSMachineSpec := oldAWSMachine["spec"].(map[string]interface{})
// allow changes to providerID
delete(oldAWSMachineSpec, "providerID")
delete(newAWSMachineSpec, "providerID")
// allow changes to instanceID
delete(oldAWSMachineSpec, "instanceID")
delete(newAWSMachineSpec, "instanceID")
// allow changes to additionalTags
delete(oldAWSMachineSpec, "additionalTags")
delete(newAWSMachineSpec, "additionalTags")
// allow changes to additionalSecurityGroups
delete(oldAWSMachineSpec, "additionalSecurityGroups")
delete(newAWSMachineSpec, "additionalSecurityGroups")
// allow changes to secretPrefix, secretCount, and secureSecretsBackend
if cloudInit, ok := oldAWSMachineSpec["cloudInit"].(map[string]interface{}); ok {
delete(cloudInit, "secretPrefix")
delete(cloudInit, "secretCount")
delete(cloudInit, "secureSecretsBackend")
}
if cloudInit, ok := newAWSMachineSpec["cloudInit"].(map[string]interface{}); ok {
delete(cloudInit, "secretPrefix")
delete(cloudInit, "secretCount")
delete(cloudInit, "secureSecretsBackend")
}
if !reflect.DeepEqual(oldAWSMachineSpec, newAWSMachineSpec) {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "cannot be modified"))
}
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
}
func (r *AWSMachine) validateCloudInitSecret() field.ErrorList {
var allErrs field.ErrorList
if r.Spec.CloudInit.InsecureSkipSecretsManager {
if r.Spec.CloudInit.SecretPrefix != "" {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "cloudInit", "secretPrefix"), "cannot be set if spec.cloudInit.insecureSkipSecretsManager is true"))
}
if r.Spec.CloudInit.SecretCount != 0 {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "cloudInit", "secretCount"), "cannot be set if spec.cloudInit.insecureSkipSecretsManager is true"))
}
if r.Spec.CloudInit.SecureSecretsBackend != "" {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "cloudInit", "secureSecretsBackend"), "cannot be set if spec.cloudInit.insecureSkipSecretsManager is true"))
}
}
if (r.Spec.CloudInit.SecretPrefix != "") != (r.Spec.CloudInit.SecretCount != 0) {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "cloudInit", "secretCount"), "must be set together with spec.CloudInit.SecretPrefix"))
}
return allErrs
}
func (r *AWSMachine) validateRootVolume() field.ErrorList {
var allErrs field.ErrorList
if r.Spec.RootVolume == nil {
return allErrs
}
if VolumeTypesProvisioned.Has(string(r.Spec.RootVolume.Type)) && r.Spec.RootVolume.IOPS == 0 {
allErrs = append(allErrs, field.Required(field.NewPath("spec.rootVolume.iops"), "iops required if type is 'io1' or 'io2'"))
}
if r.Spec.RootVolume.Throughput != nil {
if r.Spec.RootVolume.Type != VolumeTypeGP3 {
allErrs = append(allErrs, field.Required(field.NewPath("spec.rootVolume.throughput"), "throughput is valid only for type 'gp3'"))
}
if *r.Spec.RootVolume.Throughput < 0 {
allErrs = append(allErrs, field.Required(field.NewPath("spec.rootVolume.throughput"), "throughput must be nonnegative"))
}
}
if r.Spec.RootVolume.DeviceName != "" {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.rootVolume.deviceName"), "root volume shouldn't have device name"))
}
return allErrs
}
func (r *AWSMachine) validateNonRootVolumes() field.ErrorList {
var allErrs field.ErrorList
for _, volume := range r.Spec.NonRootVolumes {
if VolumeTypesProvisioned.Has(string(r.Spec.RootVolume.Type)) && volume.IOPS == 0 {
allErrs = append(allErrs, field.Required(field.NewPath("spec.nonRootVolumes.iops"), "iops required if type is 'io1' or 'io2'"))
}
if volume.Throughput != nil {
if volume.Type != VolumeTypeGP3 {
allErrs = append(allErrs, field.Required(field.NewPath("spec.nonRootVolumes.throughput"), "throughput is valid only for type 'gp3'"))
}
if *volume.Throughput < 0 {
allErrs = append(allErrs, field.Required(field.NewPath("spec.nonRootVolumes.throughput"), "throughput must be nonnegative"))
}
}
if volume.DeviceName == "" {
allErrs = append(allErrs, field.Required(field.NewPath("spec.nonRootVolumes.deviceName"), "non root volume should have device name"))
}
}
return allErrs
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSMachine) ValidateDelete() error {
return nil
}
// Default implements webhook.Defaulter such that an empty CloudInit will be defined with a default
// SecureSecretsBackend as SecretBackendSecretsManager iff InsecureSkipSecretsManager is unset.
func (r *AWSMachine) Default() {
if !r.Spec.CloudInit.InsecureSkipSecretsManager && r.Spec.CloudInit.SecureSecretsBackend == "" {
r.Spec.CloudInit.SecureSecretsBackend = SecretBackendSecretsManager
}
}
func (r *AWSMachine) validateAdditionalSecurityGroups() field.ErrorList {
var allErrs field.ErrorList
for _, additionalSecurityGroup := range r.Spec.AdditionalSecurityGroups {
if len(additionalSecurityGroup.Filters) > 0 && additionalSecurityGroup.ID != nil {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.additionalSecurityGroups"), "only one of ID or Filters may be specified, specifying both is forbidden"))
}
}
return allErrs
}
func (r *AWSMachine) validateSSHKeyName() field.ErrorList {
return validateSSHKeyName(r.Spec.SSHKeyName)
}