Skip to content

Commit ab79667

Browse files
committed
Add image validation for opestack machine
Signed-off-by: smoshiur1237 <[email protected]>
1 parent ae9422e commit ab79667

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

api/v1beta1/types.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"k8s.io/apimachinery/pkg/util/validation/field"
2021
"k8s.io/utils/ptr"
2122

2223
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/optional"
@@ -75,6 +76,24 @@ func (f *ImageFilter) IsZero() bool {
7576
return f.Name == nil && len(f.Tags) == 0
7677
}
7778

79+
// Validate performs validation on [ImageParam], returning a list of field errors using the provided base path.
80+
// It is intended to be used in the validation webhooks of resources containing [ImageParam].
81+
func (i *ImageParam) Validate(base field.Path) field.ErrorList {
82+
var errors field.ErrorList
83+
// Not possible to validate the image if it is missing
84+
if i == nil {
85+
errors = append(errors, field.Required(&base, "Image is required"))
86+
return errors
87+
}
88+
if i.Filter.Name == nil || i.Filter.Tags == nil {
89+
errors = append(errors, field.Required(base.Child("Image filter"), "Either Name or Tags of image are missing"))
90+
}
91+
if i.ImageRef.Name == "" {
92+
errors = append(errors, field.Required(base.Child("ORC image Referecne"), "ORC image is missing"))
93+
}
94+
return errors
95+
}
96+
7897
type ExternalRouterIPParam struct {
7998
// The FixedIP in the corresponding subnet
8099
FixedIP string `json:"fixedIP,omitempty"`

pkg/webhooks/openstackmachine_webhook.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type openStackMachineWebhook struct{}
4848
var _ webhook.CustomValidator = &openStackMachineWebhook{}
4949

5050
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
51-
func (*openStackMachineWebhook) ValidateCreate(_ context.Context, objRaw runtime.Object) (admission.Warnings, error) {
51+
func (webhook *openStackMachineWebhook) ValidateCreate(_ context.Context, objRaw runtime.Object) (admission.Warnings, error) {
5252
var allErrs field.ErrorList
5353
newObj, err := castToOpenStackMachine(objRaw)
5454
if err != nil {
@@ -68,12 +68,16 @@ func (*openStackMachineWebhook) ValidateCreate(_ context.Context, objRaw runtime
6868
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "ports"), "cannot have security groups when DisablePortSecurity is set to true"))
6969
}
7070
}
71+
err = webhook.validate(newObj)
72+
if err != nil {
73+
return nil, err
74+
}
7175

7276
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
7377
}
7478

7579
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
76-
func (*openStackMachineWebhook) ValidateUpdate(_ context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
80+
func (webhook *openStackMachineWebhook) ValidateUpdate(_ context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
7781
newObj, err := castToOpenStackMachine(newObjRaw)
7882
if err != nil {
7983
return nil, err
@@ -121,7 +125,7 @@ func (*openStackMachineWebhook) ValidateUpdate(_ context.Context, oldObjRaw, new
121125
}
122126

123127
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
124-
func (*openStackMachineWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
128+
func (webhook *openStackMachineWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
125129
return nil, nil
126130
}
127131

@@ -132,3 +136,14 @@ func castToOpenStackMachine(obj runtime.Object) (*infrav1.OpenStackMachine, erro
132136
}
133137
return cast, nil
134138
}
139+
140+
func (webhook *openStackMachineWebhook) validate(newObj *infrav1.OpenStackMachine) error {
141+
var allErrs field.ErrorList
142+
143+
allErrs = append(allErrs, newObj.Spec.Image.Validate(*field.NewPath("Spec", "Image"))...)
144+
145+
if len(allErrs) == 0 {
146+
return nil
147+
}
148+
return apierrors.NewInvalid(infrav1.SchemeGroupVersion.WithKind("OpenStackMachine").GroupKind(), newObj.Name, allErrs)
149+
}

pkg/webhooks/openstackmachinetemplate_webhook.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type openStackMachineTemplateWebhook struct{}
4848
var _ 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
@@ -60,11 +60,15 @@ func (*openStackMachineTemplateWebhook) ValidateCreate(_ context.Context, objRaw
6060
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "providerID"), "cannot be set in templates"))
6161
}
6262

63+
err = webhook.validate(newObj)
64+
if err != nil {
65+
return nil, err
66+
}
6367
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
6468
}
6569

6670
// 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) {
71+
func (webhook *openStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
6872
var allErrs field.ErrorList
6973
oldObj, err := castToOpenStackMachineTemplate(oldObjRaw)
7074
if err != nil {
@@ -92,7 +96,7 @@ func (*openStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldO
9296
}
9397

9498
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
95-
func (*openStackMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
99+
func (webhook *openStackMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
96100
return nil, nil
97101
}
98102

@@ -103,3 +107,14 @@ func castToOpenStackMachineTemplate(obj runtime.Object) (*infrav1.OpenStackMachi
103107
}
104108
return cast, nil
105109
}
110+
111+
func (webhook *openStackMachineTemplateWebhook) validate(newObj *infrav1.OpenStackMachineTemplate) error {
112+
var allErrs field.ErrorList
113+
114+
allErrs = append(allErrs, newObj.Spec.Template.Spec.Image.Validate(*field.NewPath("Spec", "Template", "Spec", "Image"))...)
115+
116+
if len(allErrs) == 0 {
117+
return nil
118+
}
119+
return apierrors.NewInvalid(infrav1.SchemeGroupVersion.WithKind("OpenStackMachineTemplate").GroupKind(), newObj.Name, allErrs)
120+
}

pkg/webhooks/openstackserver_webhook.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type openStackServerWebhook struct{}
5050
var _ webhook.CustomValidator = &openStackServerWebhook{}
5151

5252
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
53-
func (*openStackServerWebhook) ValidateCreate(_ context.Context, objRaw runtime.Object) (admission.Warnings, error) {
53+
func (webhook *openStackServerWebhook) ValidateCreate(_ context.Context, objRaw runtime.Object) (admission.Warnings, error) {
5454
var allErrs field.ErrorList
5555
newObj, err := castToOpenStackServer(objRaw)
5656
if err != nil {
@@ -71,11 +71,16 @@ func (*openStackServerWebhook) ValidateCreate(_ context.Context, objRaw runtime.
7171
}
7272
}
7373

74+
err = webhook.validate(newObj)
75+
if err != nil {
76+
return nil, err
77+
}
78+
7479
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
7580
}
7681

7782
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
78-
func (*openStackServerWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
83+
func (webhook *openStackServerWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
7984
oldObj, err := castToOpenStackServer(oldObjRaw)
8085
if err != nil {
8186
return nil, err
@@ -124,7 +129,7 @@ func (*openStackServerWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, ne
124129
}
125130

126131
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
127-
func (*openStackServerWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
132+
func (webhook *openStackServerWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
128133
return nil, nil
129134
}
130135

@@ -135,3 +140,14 @@ func castToOpenStackServer(obj runtime.Object) (*infrav1alpha1.OpenStackServer,
135140
}
136141
return cast, nil
137142
}
143+
144+
func (webhook *openStackServerWebhook) validate(newObj *infrav1alpha1.OpenStackServer) error {
145+
var allErrs field.ErrorList
146+
147+
allErrs = append(allErrs, newObj.Spec.Image.Validate(*field.NewPath("Spec", "Image"))...)
148+
149+
if len(allErrs) == 0 {
150+
return nil
151+
}
152+
return apierrors.NewInvalid(infrav1.SchemeGroupVersion.WithKind("OpenStackServer").GroupKind(), newObj.Name, allErrs)
153+
}

0 commit comments

Comments
 (0)