@@ -48,7 +48,7 @@ type openStackMachineTemplateWebhook struct{}
4848var _ webhook.CustomValidator = & openStackMachineTemplateWebhook {}
4949
5050// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
51- func (* openStackMachineTemplateWebhook ) ValidateCreate (_ context.Context , objRaw runtime.Object ) (admission.Warnings , error ) {
51+ func (webhook * openStackMachineTemplateWebhook ) ValidateCreate (_ context.Context , objRaw runtime.Object ) (admission.Warnings , error ) {
5252 newObj , err := castToOpenStackMachineTemplate (objRaw )
5353 if err != nil {
5454 return nil , err
@@ -58,13 +58,14 @@ func (*openStackMachineTemplateWebhook) ValidateCreate(_ context.Context, objRaw
5858
5959 if newObj .Spec .Template .Spec .ProviderID != nil {
6060 allErrs = append (allErrs , field .Forbidden (field .NewPath ("spec" , "template" , "spec" , "providerID" ), "cannot be set in templates" ))
61+ //return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
6162 }
6263
63- return aggregateObjErrors ( newObj . GroupVersionKind (). GroupKind (), newObj . Name , allErrs )
64+ return nil , webhook . validate ( newObj )
6465}
6566
6667// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
67- func (* openStackMachineTemplateWebhook ) ValidateUpdate (ctx context.Context , oldObjRaw , newObjRaw runtime.Object ) (admission.Warnings , error ) {
68+ func (webhook * openStackMachineTemplateWebhook ) ValidateUpdate (ctx context.Context , oldObjRaw , newObjRaw runtime.Object ) (admission.Warnings , error ) {
6869 var allErrs field.ErrorList
6970 oldObj , err := castToOpenStackMachineTemplate (oldObjRaw )
7071 if err != nil {
@@ -86,13 +87,14 @@ func (*openStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldO
8687 allErrs = append (allErrs ,
8788 field .Invalid (field .NewPath ("spec" , "template" , "spec" ), newObj .Spec .Template .Spec , "OpenStackMachineTemplate spec.template.spec field is immutable. Please create a new resource instead. Ref doc: https://cluster-api.sigs.k8s.io/tasks/change-machine-template.html" ),
8889 )
90+ //return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
8991 }
9092
91- return aggregateObjErrors ( newObj . GroupVersionKind (). GroupKind (), newObj . Name , allErrs )
93+ return nil , webhook . validate ( newObj )
9294}
9395
9496// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
95- func (* openStackMachineTemplateWebhook ) ValidateDelete (_ context.Context , _ runtime.Object ) (admission.Warnings , error ) {
97+ func (webhook * openStackMachineTemplateWebhook ) ValidateDelete (_ context.Context , _ runtime.Object ) (admission.Warnings , error ) {
9698 return nil , nil
9799}
98100
@@ -103,3 +105,14 @@ func castToOpenStackMachineTemplate(obj runtime.Object) (*infrav1.OpenStackMachi
103105 }
104106 return cast , nil
105107}
108+
109+ func (webhook * openStackMachineTemplateWebhook ) validate (newObj * infrav1.OpenStackMachineTemplate ) error {
110+ var allErrs field.ErrorList
111+
112+ allErrs = append (allErrs , newObj .Spec .Template .Spec .Image .Validate (* field .NewPath ("Spec" , "Template" , "Spec" , "Image" ))... )
113+
114+ if len (allErrs ) == 0 {
115+ return nil
116+ }
117+ return apierrors .NewInvalid (infrav1 .SchemeGroupVersion .WithKind ("OpenStackMachine" ).GroupKind (), newObj .Name , allErrs )
118+ }
0 commit comments