Skip to content

Commit ee2a77b

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

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-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: 18 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 {
@@ -67,13 +67,14 @@ func (*openStackMachineWebhook) ValidateCreate(_ context.Context, objRaw runtime
6767
if ptr.Deref(port.DisablePortSecurity, false) && len(port.SecurityGroups) > 0 {
6868
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "ports"), "cannot have security groups when DisablePortSecurity is set to true"))
6969
}
70+
//return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
7071
}
7172

72-
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
73+
return nil, webhook.validate(newObj)
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
@@ -115,13 +116,14 @@ func (*openStackMachineWebhook) ValidateUpdate(_ context.Context, oldObjRaw, new
115116

116117
if !reflect.DeepEqual(oldOpenStackMachineSpec, newOpenStackMachineSpec) {
117118
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "cannot be modified"))
119+
//return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
118120
}
119121

120-
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
122+
return nil, webhook.validate(newObj)
121123
}
122124

123125
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
124-
func (*openStackMachineWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
126+
func (webhook *openStackMachineWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
125127
return nil, nil
126128
}
127129

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

pkg/webhooks/openstackmachinetemplate_webhook.go

Lines changed: 18 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
@@ -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("OpenStackMachineTemplate").GroupKind(), newObj.Name, allErrs)
118+
}

pkg/webhooks/openstackserver_webhook.go

Lines changed: 18 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 {
@@ -69,13 +69,14 @@ func (*openStackServerWebhook) ValidateCreate(_ context.Context, objRaw runtime.
6969
if ptr.Deref(port.DisablePortSecurity, false) && len(port.SecurityGroups) > 0 {
7070
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "ports"), "cannot have security groups when DisablePortSecurity is set to true"))
7171
}
72+
//return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
7273
}
7374

74-
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
75+
return nil, webhook.validate(newObj)
7576
}
7677

7778
// 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) {
79+
func (webhook *openStackServerWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, newObjRaw runtime.Object) (admission.Warnings, error) {
7980
oldObj, err := castToOpenStackServer(oldObjRaw)
8081
if err != nil {
8182
return nil, err
@@ -118,13 +119,14 @@ func (*openStackServerWebhook) ValidateUpdate(ctx context.Context, oldObjRaw, ne
118119
allErrs = append(allErrs,
119120
field.Forbidden(field.NewPath("spec"), "OpenStackServer spec field is immutable. Please create a new resource instead."),
120121
)
122+
//return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
121123
}
122124

123-
return aggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, allErrs)
125+
return nil, webhook.validate(newObj)
124126
}
125127

126128
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
127-
func (*openStackServerWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
129+
func (webhook *openStackServerWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
128130
return nil, nil
129131
}
130132

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

0 commit comments

Comments
 (0)