Skip to content

Commit 8137760

Browse files
authored
Merge pull request #3728 from cnmcavoy/cnmcavoy/awsmachine-webhook-validate-consistency
Update AWSMachine webhook validate logic on update to be consistent
2 parents 0271d09 + e23b67d commit 8137760

File tree

2 files changed

+79
-13
lines changed

2 files changed

+79
-13
lines changed

api/v1beta2/awsmachine_webhook.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func (r *AWSMachine) ValidateUpdate(old runtime.Object) error {
7878
var allErrs field.ErrorList
7979

8080
allErrs = append(allErrs, r.validateCloudInitSecret()...)
81+
allErrs = append(allErrs, r.validateAdditionalSecurityGroups()...)
8182
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
8283

8384
newAWSMachineSpec := newAWSMachine["spec"].(map[string]interface{})
@@ -203,7 +204,7 @@ func (r *AWSMachine) validateNonRootVolumes() field.ErrorList {
203204
var allErrs field.ErrorList
204205

205206
for _, volume := range r.Spec.NonRootVolumes {
206-
if VolumeTypesProvisioned.Has(string(r.Spec.RootVolume.Type)) && volume.IOPS == 0 {
207+
if VolumeTypesProvisioned.Has(string(volume.Type)) && volume.IOPS == 0 {
207208
allErrs = append(allErrs, field.Required(field.NewPath("spec.nonRootVolumes.iops"), "iops required if type is 'io1' or 'io2'"))
208209
}
209210

api/v1beta2/awsmachinetemplate_webhook.go

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,78 @@ func (r *AWSMachineTemplate) validateNonRootVolumes() field.ErrorList {
101101
return allErrs
102102
}
103103

104+
func (r *AWSMachineTemplate) validateAdditionalSecurityGroups() field.ErrorList {
105+
var allErrs field.ErrorList
106+
107+
spec := r.Spec.Template.Spec
108+
109+
for _, additionalSecurityGroup := range spec.AdditionalSecurityGroups {
110+
if len(additionalSecurityGroup.Filters) > 0 && additionalSecurityGroup.ID != nil {
111+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "additionalSecurityGroups"), "only one of ID or Filters may be specified, specifying both is forbidden"))
112+
}
113+
}
114+
return allErrs
115+
}
116+
117+
func (r *AWSMachineTemplate) validateCloudInitSecret() field.ErrorList {
118+
var allErrs field.ErrorList
119+
120+
spec := r.Spec.Template.Spec
121+
if spec.CloudInit.InsecureSkipSecretsManager {
122+
if spec.CloudInit.SecretPrefix != "" {
123+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "cloudInit", "secretPrefix"), "cannot be set if spec.template.spec.cloudInit.insecureSkipSecretsManager is true"))
124+
}
125+
if spec.CloudInit.SecretCount != 0 {
126+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "cloudInit", "secretCount"), "cannot be set if spec.template.spec.cloudInit.insecureSkipSecretsManager is true"))
127+
}
128+
if spec.CloudInit.SecureSecretsBackend != "" {
129+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "cloudInit", "secureSecretsBackend"), "cannot be set if spec.template.spec.cloudInit.insecureSkipSecretsManager is true"))
130+
}
131+
}
132+
133+
if (spec.CloudInit.SecretPrefix != "") != (spec.CloudInit.SecretCount != 0) {
134+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "cloudInit", "secretCount"), "must be set together with spec.template.spec.CloudInit.SecretPrefix"))
135+
}
136+
137+
return allErrs
138+
}
139+
140+
func (r *AWSMachineTemplate) cloudInitConfigured() bool {
141+
spec := r.Spec.Template.Spec
142+
configured := false
143+
144+
configured = configured || spec.CloudInit.SecretPrefix != ""
145+
configured = configured || spec.CloudInit.SecretCount != 0
146+
configured = configured || spec.CloudInit.SecureSecretsBackend != ""
147+
configured = configured || spec.CloudInit.InsecureSkipSecretsManager
148+
149+
return configured
150+
}
151+
152+
func (r *AWSMachineTemplate) ignitionEnabled() bool {
153+
return r.Spec.Template.Spec.Ignition != nil
154+
}
155+
156+
func (r *AWSMachineTemplate) validateIgnitionAndCloudInit() field.ErrorList {
157+
var allErrs field.ErrorList
158+
159+
// Feature gate is not enabled but ignition is enabled then send a forbidden error.
160+
if !feature.Gates.Enabled(feature.BootstrapFormatIgnition) && r.ignitionEnabled() {
161+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "ignition"),
162+
"can be set only if the BootstrapFormatIgnition feature gate is enabled"))
163+
}
164+
165+
if r.ignitionEnabled() && r.cloudInitConfigured() {
166+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "cloudInit"),
167+
"cannot be set if spec.template.spec.ignition is set"))
168+
}
169+
170+
return allErrs
171+
}
172+
func (r *AWSMachineTemplate) validateSSHKeyName() field.ErrorList {
173+
return validateSSHKeyName(r.Spec.Template.Spec.SSHKeyName)
174+
}
175+
104176
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
105177
func (r *AWSMachineTemplateWebhook) ValidateCreate(_ context.Context, raw runtime.Object) error {
106178
var allErrs field.ErrorList
@@ -123,20 +195,13 @@ func (r *AWSMachineTemplateWebhook) ValidateCreate(_ context.Context, raw runtim
123195
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "providerID"), "cannot be set in templates"))
124196
}
125197

198+
allErrs = append(allErrs, obj.validateCloudInitSecret()...)
199+
allErrs = append(allErrs, obj.validateIgnitionAndCloudInit()...)
126200
allErrs = append(allErrs, obj.validateRootVolume()...)
127201
allErrs = append(allErrs, obj.validateNonRootVolumes()...)
128-
129-
// Feature gate is not enabled but ignition is enabled then send a forbidden error.
130-
if !feature.Gates.Enabled(feature.BootstrapFormatIgnition) && spec.Ignition != nil {
131-
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "ignition"),
132-
"can be set only if the BootstrapFormatIgnition feature gate is enabled"))
133-
}
134-
135-
cloudInitConfigured := spec.CloudInit.SecureSecretsBackend != "" || spec.CloudInit.InsecureSkipSecretsManager
136-
if cloudInitConfigured && spec.Ignition != nil {
137-
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "cloudInit"),
138-
"cannot be set if spec.template.spec.ignition is set"))
139-
}
202+
allErrs = append(allErrs, obj.validateSSHKeyName()...)
203+
allErrs = append(allErrs, obj.validateAdditionalSecurityGroups()...)
204+
allErrs = append(allErrs, obj.Spec.Template.Spec.AdditionalTags.Validate()...)
140205

141206
return aggregateObjErrors(obj.GroupVersionKind().GroupKind(), obj.Name, allErrs)
142207
}

0 commit comments

Comments
 (0)