Skip to content

Commit 0d74d31

Browse files
author
Jelle Dijkstra
committed
WFS validation
1 parent 5c06746 commit 0d74d31

File tree

3 files changed

+74
-63
lines changed

3 files changed

+74
-63
lines changed

api/v3/wfs_validation.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package v3
2+
3+
import (
4+
"fmt"
5+
sharedValidation "github.com/pdok/smooth-operator/pkg/validation"
6+
"strings"
7+
)
8+
9+
func (wfs *WFS) ValidateCreate() ([]string, error) {
10+
warnings := []string{}
11+
reasons := []string{}
12+
13+
err := sharedValidation.ValidateLabelsOnCreate(wfs.Labels)
14+
if err != nil {
15+
reasons = append(reasons, fmt.Sprintf("%v", err))
16+
}
17+
18+
validateWFS(wfs, &warnings, &reasons)
19+
20+
if len(reasons) > 0 {
21+
return warnings, fmt.Errorf("%s", strings.Join(reasons, ". "))
22+
} else {
23+
return warnings, nil
24+
}
25+
}
26+
27+
func (wfs *WFS) ValidateUpdate(wfsOld *WFS) ([]string, error) {
28+
warnings := []string{}
29+
reasons := []string{}
30+
31+
// Check labels did not change
32+
err := sharedValidation.ValidateLabelsOnUpdate(wfsOld.Labels, wfs.Labels)
33+
if err != nil {
34+
reasons = append(reasons, fmt.Sprintf("%v", err))
35+
}
36+
37+
if (wfs.Spec.Service.Inspire == nil && wfsOld.Spec.Service.Inspire != nil) || (wfs.Spec.Service.Inspire != nil && wfsOld.Spec.Service.Inspire == nil) {
38+
reasons = append(reasons, fmt.Sprintf("services cannot change from inspire to not inspire or the other way around"))
39+
}
40+
41+
validateWFS(wfs, &warnings, &reasons)
42+
43+
if len(reasons) > 0 {
44+
return warnings, fmt.Errorf("%s", strings.Join(reasons, ". "))
45+
} else {
46+
return warnings, nil
47+
}
48+
}
49+
50+
func validateWFS(wfs *WFS, warnings *[]string, reasons *[]string) {
51+
if strings.Contains(wfs.GetName(), "wfs") {
52+
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("name should not contain wfs", wfs.GroupVersionKind(), wfs.GetName()))
53+
}
54+
55+
service := wfs.Spec.Service
56+
57+
err := sharedValidation.ValidateBaseURL(service.BaseURL)
58+
if err != nil {
59+
*reasons = append(*reasons, fmt.Sprintf("%v", err))
60+
}
61+
62+
if service.Mapfile == nil && service.DefaultCrs != "EPSG:28992" && service.Bbox == nil {
63+
*reasons = append(*reasons, fmt.Sprintf("service.bbox.defaultCRS is required when service.defaultCRS is not 'EPSG:28992'"))
64+
}
65+
66+
if service.Mapfile != nil {
67+
if service.Bbox != nil {
68+
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("service.bbox is not used when service.mapfile is configured", wfs.GroupVersionKind(), wfs.GetName()))
69+
}
70+
}
71+
}

config/manager/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ kind: Kustomization
55
images:
66
- name: controller
77
newName: local-registry:5000/mapserver-operator
8-
newTag: v3.0.9
8+
newTag: v3.0.10

internal/webhook/v3/wfs_webhook.go

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@ package v3
2727
import (
2828
"context"
2929
"fmt"
30-
"strings"
31-
3230
"k8s.io/apimachinery/pkg/runtime"
3331
ctrl "sigs.k8s.io/controller-runtime"
3432
logf "sigs.k8s.io/controller-runtime/pkg/log"
3533
"sigs.k8s.io/controller-runtime/pkg/webhook"
3634
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3735

3836
pdoknlv3 "github.com/pdok/mapserver-operator/api/v3"
39-
40-
sharedValidation "github.com/pdok/smooth-operator/pkg/validation"
4137
)
4238

4339
// nolint:unused
@@ -77,21 +73,7 @@ func (v *WFSCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Obj
7773
}
7874
wfslog.Info("Validation for WFS upon creation", "name", wfs.GetName())
7975

80-
warnings := admission.Warnings{}
81-
reasons := make([]string, 0)
82-
83-
err := sharedValidation.ValidateLabelsOnCreate(wfs.Labels)
84-
if err != nil {
85-
reasons = append(reasons, fmt.Sprintf("%v", err))
86-
}
87-
88-
validateWFS(wfs, &warnings, &reasons)
89-
90-
if len(reasons) > 0 {
91-
return warnings, fmt.Errorf("%s", strings.Join(reasons, "\n"))
92-
} else {
93-
return warnings, nil
94-
}
76+
return wfs.ValidateCreate()
9577
}
9678

9779
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type WFS.
@@ -106,26 +88,7 @@ func (v *WFSCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj
10688
}
10789
wfslog.Info("Validation for WFS upon update", "name", wfs.GetName())
10890

109-
warnings := admission.Warnings{}
110-
reasons := make([]string, 0)
111-
112-
// Check labels did not change
113-
err := sharedValidation.ValidateLabelsOnUpdate(wfsOld.Labels, wfs.Labels)
114-
if err != nil {
115-
reasons = append(reasons, fmt.Sprintf("%v", err))
116-
}
117-
118-
if (wfs.Spec.Service.Inspire == nil && wfsOld.Spec.Service.Inspire != nil) || (wfs.Spec.Service.Inspire != nil && wfsOld.Spec.Service.Inspire == nil) {
119-
reasons = append(reasons, fmt.Sprintf("services cannot change from inspire to not inspire or the other way around"))
120-
}
121-
122-
validateWFS(wfs, &warnings, &reasons)
123-
124-
if len(reasons) > 0 {
125-
return warnings, fmt.Errorf("%s", strings.Join(reasons, "\n"))
126-
} else {
127-
return warnings, nil
128-
}
91+
return wfs.ValidateUpdate(wfsOld)
12992
}
13093

13194
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type WFS.
@@ -140,26 +103,3 @@ func (v *WFSCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Obj
140103

141104
return nil, nil
142105
}
143-
144-
func validateWFS(wfs *pdoknlv3.WFS, warnings *admission.Warnings, reasons *[]string) {
145-
if strings.Contains(wfs.GetName(), "wfs") {
146-
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("name should not contain wfs", wfs.GroupVersionKind(), wfs.GetName()))
147-
}
148-
149-
service := wfs.Spec.Service
150-
151-
err := sharedValidation.ValidateBaseURL(service.BaseURL)
152-
if err != nil {
153-
*reasons = append(*reasons, fmt.Sprintf("%v", err))
154-
}
155-
156-
if service.Mapfile == nil && service.DefaultCrs != "EPSG:28992" && service.Bbox == nil {
157-
*reasons = append(*reasons, fmt.Sprintf("service.bbox.defaultCRS is required when service.defaultCRS is not 'EPSG:28992'"))
158-
}
159-
160-
if service.Mapfile != nil {
161-
if service.Bbox != nil {
162-
*warnings = append(*warnings, sharedValidation.FormatValidationWarning("service.bbox is not used when service.mapfile is configured", wfs.GroupVersionKind(), wfs.GetName()))
163-
}
164-
}
165-
}

0 commit comments

Comments
 (0)