Skip to content

Commit 2509267

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

File tree

4 files changed

+67
-15
lines changed

4 files changed

+67
-15
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: 16 additions & 5 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 {
@@ -69,11 +69,11 @@ func (*openStackMachineWebhook) ValidateCreate(_ context.Context, objRaw runtime
6969
}
7070
}
7171

72-
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
72+
return nil, webhook.validate(newObj)
7373
}
7474

7575
// 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) {
76+
func (webhook *openStackMachineWebhook) ValidateUpdate(_ context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
7777
newObj, err := castToOpenStackMachine(newObjRaw)
7878
if err != nil {
7979
return nil, err
@@ -117,11 +117,11 @@ func (*openStackMachineWebhook) ValidateUpdate(_ context.Context, oldObjRaw, new
117117
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "cannot be modified"))
118118
}
119119

120-
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
120+
return nil, webhook.validate(newObj)
121121
}
122122

123123
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
124-
func (*openStackMachineWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
124+
func (webhook *openStackMachineWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
125125
return nil, nil
126126
}
127127

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

pkg/webhooks/openstackmachinetemplate_webhook.go

Lines changed: 16 additions & 5 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,11 @@ 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-
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
63+
return nil, webhook.validate(newObj)
6464
}
6565

6666
// 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) {
67+
func (webhook *openStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
6868
var allErrs field.ErrorList
6969
oldObj, err := castToOpenStackMachineTemplate(oldObjRaw)
7070
if err != nil {
@@ -88,11 +88,11 @@ func (*openStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldO
8888
)
8989
}
9090

91-
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
91+
return nil, webhook.validate(newObj)
9292
}
9393

9494
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
95-
func (*openStackMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
95+
func (webhook *openStackMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
9696
return nil, nil
9797
}
9898

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

pkg/webhooks/openstackserver_webhook.go

Lines changed: 16 additions & 5 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,11 @@ func (*openStackServerWebhook) ValidateCreate(_ context.Context, objRaw runtime.
7171
}
7272
}
7373

74-
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
74+
return nil, webhook.validate(newObj)
7575
}
7676

7777
// 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) {
78+
func (webhook *openStackServerWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
7979
oldObj, err := castToOpenStackServer(oldObjRaw)
8080
if err != nil {
8181
return nil, err
@@ -120,11 +120,11 @@ func (*openStackServerWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, ne
120120
)
121121
}
122122

123-
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
123+
return nil, webhook.validate(newObj)
124124
}
125125

126126
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
127-
func (*openStackServerWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
127+
func (webhook *openStackServerWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
128128
return nil, nil
129129
}
130130

@@ -135,3 +135,14 @@ func castToOpenStackServer(obj runtime.Object) (*infrav1alpha1.OpenStackServer,
135135
}
136136
return cast, nil
137137
}
138+
139+
func (webhook *openStackServerWebhook) validate(newObj *infrav1alpha1.OpenStackServer) error {
140+
var allErrs field.ErrorList
141+
142+
allErrs = append(allErrs, newObj.Spec.Image.Validate(*field.NewPath("Spec", "Image"))...)
143+
144+
if len(allErrs) == 0 {
145+
return nil
146+
}
147+
return apierrors.NewInvalid(infrav1.SchemeGroupVersion.WithKind("OpenStackServer").GroupKind(), newObj.Name, allErrs)
148+
}

0 commit comments

Comments
 (0)