Skip to content

Commit 34e1f3c

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

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-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: 17 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,13 @@ 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+
webhook.validate(newObj)
7172

7273
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
7374
}
7475

7576
// 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) {
77+
func (webhook *openStackMachineWebhook) ValidateUpdate(_ context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
7778
newObj, err := castToOpenStackMachine(newObjRaw)
7879
if err != nil {
7980
return nil, err
@@ -117,11 +118,13 @@ func (*openStackMachineWebhook) ValidateUpdate(_ context.Context, oldObjRaw, new
117118
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "cannot be modified"))
118119
}
119120

121+
webhook.validate(newObj)
122+
120123
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
121124
}
122125

123126
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
124-
func (*openStackMachineWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
127+
func (webhook *openStackMachineWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
125128
return nil, nil
126129
}
127130

@@ -132,3 +135,14 @@ func castToOpenStackMachine(obj runtime.Object) (*infrav1.OpenStackMachine, erro
132135
}
133136
return cast, nil
134137
}
138+
139+
func (webhook *openStackMachineWebhook) validate(newObj *infrav1.OpenStackMachine) 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("OpenStackMachine").GroupKind(), newObj.Name, allErrs)
148+
}

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,13 @@ 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+
webhook.validate(newObj)
64+
6365
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
6466
}
6567

6668
// 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) {
69+
func (webhook *openStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
6870
var allErrs field.ErrorList
6971
oldObj, err := castToOpenStackMachineTemplate(oldObjRaw)
7072
if err != nil {
@@ -88,11 +90,13 @@ func (*openStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldO
8890
)
8991
}
9092

93+
webhook.validate(newObj)
94+
9195
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
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: 18 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,13 @@ func (*openStackServerWebhook) ValidateCreate(_ context.Context, objRaw runtime.
7171
}
7272
}
7373

74+
webhook.validate(newObj)
75+
7476
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
7577
}
7678

7779
// 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) {
80+
func (webhook *openStackServerWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
7981
oldObj, err := castToOpenStackServer(oldObjRaw)
8082
if err != nil {
8183
return nil, err
@@ -120,11 +122,13 @@ func (*openStackServerWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, ne
120122
)
121123
}
122124

125+
webhook.validate(newObj)
126+
123127
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
124128
}
125129

126130
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
127-
func (*openStackServerWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
131+
func (webhook *openStackServerWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
128132
return nil, nil
129133
}
130134

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

0 commit comments

Comments
 (0)