|
1 | 1 | package v3 |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "strings" |
6 | 5 |
|
| 6 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 7 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 8 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 9 | + |
7 | 10 | sharedValidation "github.com/pdok/smooth-operator/pkg/validation" |
8 | 11 | ) |
9 | 12 |
|
10 | 13 | func (wfs *WFS) ValidateCreate() ([]string, error) { |
11 | 14 | warnings := []string{} |
12 | | - reasons := []string{} |
| 15 | + allErrs := field.ErrorList{} |
13 | 16 |
|
14 | 17 | err := sharedValidation.ValidateLabelsOnCreate(wfs.Labels) |
15 | 18 | if err != nil { |
16 | | - reasons = append(reasons, fmt.Sprintf("%v", err)) |
| 19 | + allErrs = append(allErrs, err) |
17 | 20 | } |
18 | 21 |
|
19 | | - ValidateWFS(wfs, &warnings, &reasons) |
| 22 | + ValidateWFS(wfs, &warnings, &allErrs) |
20 | 23 |
|
21 | | - if len(reasons) > 0 { |
22 | | - return warnings, fmt.Errorf("%s", strings.Join(reasons, ". ")) |
| 24 | + if len(allErrs) == 0 { |
| 25 | + return warnings, nil |
23 | 26 | } |
24 | 27 |
|
25 | | - return warnings, nil |
| 28 | + return warnings, apierrors.NewInvalid( |
| 29 | + schema.GroupKind{Group: "pdok.nl", Kind: "WFS"}, |
| 30 | + wfs.Name, allErrs) |
26 | 31 | } |
27 | 32 |
|
28 | 33 | func (wfs *WFS) ValidateUpdate(wfsOld *WFS) ([]string, error) { |
29 | 34 | warnings := []string{} |
30 | | - reasons := []string{} |
| 35 | + allErrs := field.ErrorList{} |
31 | 36 |
|
32 | | - // Check labels did not change |
33 | | - err := sharedValidation.ValidateLabelsOnUpdate(wfsOld.Labels, wfs.Labels) |
34 | | - if err != nil { |
35 | | - reasons = append(reasons, fmt.Sprintf("%v", err)) |
36 | | - } |
| 37 | + sharedValidation.ValidateLabelsOnUpdate(wfsOld.Labels, wfs.Labels, &allErrs) |
37 | 38 |
|
38 | | - sharedValidation.CheckBaseUrlImmutability(wfsOld, wfs, &reasons) |
| 39 | + sharedValidation.CheckBaseUrlImmutability(wfsOld, wfs, &allErrs) |
39 | 40 |
|
40 | 41 | if (wfs.Spec.Service.Inspire == nil && wfsOld.Spec.Service.Inspire != nil) || (wfs.Spec.Service.Inspire != nil && wfsOld.Spec.Service.Inspire == nil) { |
41 | | - reasons = append(reasons, "services cannot change from inspire to not inspire or the other way around") |
| 42 | + allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Child("service").Child("inspire"), "cannot change from inspire to not inspire or the other way around")) |
42 | 43 | } |
43 | 44 |
|
44 | | - ValidateWFS(wfs, &warnings, &reasons) |
| 45 | + ValidateWFS(wfs, &warnings, &allErrs) |
45 | 46 |
|
46 | | - if len(reasons) > 0 { |
47 | | - return warnings, fmt.Errorf("%s", strings.Join(reasons, ". ")) |
| 47 | + if len(allErrs) == 0 { |
| 48 | + return warnings, nil |
48 | 49 | } |
49 | 50 |
|
50 | | - return warnings, nil |
| 51 | + return warnings, apierrors.NewInvalid( |
| 52 | + schema.GroupKind{Group: "pdok.nl", Kind: "WFS"}, |
| 53 | + wfs.Name, allErrs) |
51 | 54 | } |
52 | 55 |
|
53 | | -func ValidateWFS(wfs *WFS, warnings *[]string, reasons *[]string) { |
| 56 | +func ValidateWFS(wfs *WFS, warnings *[]string, allErrs *field.ErrorList) { |
54 | 57 | if strings.Contains(wfs.GetName(), "wfs") { |
55 | | - *warnings = append(*warnings, sharedValidation.FormatValidationWarning("name should not contain wfs", wfs.GroupVersionKind(), wfs.GetName())) |
| 58 | + sharedValidation.AddWarning( |
| 59 | + warnings, |
| 60 | + *field.NewPath("metadata").Child("name"), |
| 61 | + "name should not contain wfs", |
| 62 | + wfs.GroupVersionKind(), |
| 63 | + wfs.GetName(), |
| 64 | + ) |
56 | 65 | } |
57 | 66 |
|
58 | 67 | service := wfs.Spec.Service |
| 68 | + path := field.NewPath("spec").Child("service") |
59 | 69 |
|
60 | 70 | err := sharedValidation.ValidateBaseURL(service.URL) |
61 | 71 | if err != nil { |
62 | | - *reasons = append(*reasons, fmt.Sprintf("%v", err)) |
| 72 | + *allErrs = append(*allErrs, field.Invalid(path.Child("url"), service.URL, err.Error())) |
63 | 73 | } |
64 | 74 |
|
65 | 75 | if service.Mapfile == nil && service.DefaultCrs != "EPSG:28992" && service.Bbox == nil { |
66 | | - *reasons = append(*reasons, "service.bbox.defaultCRS is required when service.defaultCRS is not 'EPSG:28992'") |
| 76 | + *allErrs = append(*allErrs, field.Required(path.Child("bbox").Child("defaultCRS"), "when service.defaultCRS is not 'EPSG:28992'")) |
67 | 77 | } |
68 | 78 |
|
69 | | - if service.Mapfile != nil { |
70 | | - if service.Bbox != nil { |
71 | | - *warnings = append(*warnings, sharedValidation.FormatValidationWarning("service.bbox is not used when service.mapfile is configured", wfs.GroupVersionKind(), wfs.GetName())) |
72 | | - } |
| 79 | + if service.Mapfile != nil && service.Bbox != nil { |
| 80 | + sharedValidation.AddWarning(warnings, *path.Child("bbox"), "is not used when service.mapfile is configured", wfs.GroupVersionKind(), wfs.GetName()) |
73 | 81 | } |
74 | 82 | } |
0 commit comments