Skip to content

Commit f35e801

Browse files
authored
Merge pull request #80 from PDOK/wr/validate-ownerref
Wr/validate ownerref
2 parents 24bee13 + 32e13f5 commit f35e801

19 files changed

+313
-78
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
lint:
11-
name: Run on Ubuntu
11+
name: Linting on Ubuntu
1212
runs-on: ubuntu-latest
1313
steps:
1414
- name: Clone the code

.github/workflows/test-e2e.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
test-e2e:
11-
name: Run on Ubuntu
11+
name: End-2-End on Ubuntu
1212
runs-on: ubuntu-latest
1313
steps:
1414
- name: Clone the code

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
test:
11-
name: Run on Ubuntu
11+
name: Testing on Ubuntu
1212
runs-on: ubuntu-latest
1313
steps:
1414
- name: Clone the code

api/v3/shared_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type WMSWFS interface {
4545
TypedName() string
4646
Options() Options
4747
HasPostgisData() bool
48+
OwnerInfoRef() string
4849

4950
// URL returns the configured service URL
5051
URL() smoothoperatormodel.URL

api/v3/shared_validation.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,47 @@
11
package v3
22

33
import (
4+
"context"
45
"fmt"
56
"slices"
67

8+
smoothoperatorv1 "github.com/pdok/smooth-operator/api/v1"
9+
"sigs.k8s.io/controller-runtime/pkg/client"
10+
711
sharedValidation "github.com/pdok/smooth-operator/pkg/validation"
812
v1 "k8s.io/api/core/v1"
913

1014
apierrors "k8s.io/apimachinery/pkg/api/errors"
1115
"k8s.io/apimachinery/pkg/util/validation/field"
1216
)
1317

14-
func ValidateUpdate[W WMSWFS](newW, oldW W, validate func(W, *[]string, *field.ErrorList)) ([]string, error) {
18+
func ValidateCreate[W WMSWFS](c client.Client, obj W, validate func(W, *[]string, *field.ErrorList)) ([]string, error) {
19+
warnings := []string{}
20+
allErrs := field.ErrorList{}
21+
22+
err := sharedValidation.ValidateLabelsOnCreate(obj.GetLabels())
23+
if err != nil {
24+
allErrs = append(allErrs, err)
25+
}
26+
27+
err = sharedValidation.ValidateIngressRouteURLsContainsBaseURL(obj.IngressRouteURLs(false), obj.URL(), nil)
28+
if err != nil {
29+
allErrs = append(allErrs, err)
30+
}
31+
32+
validate(obj, &warnings, &allErrs)
33+
ValidateOwnerInfo(c, obj, &allErrs)
34+
35+
if len(allErrs) == 0 {
36+
return warnings, nil
37+
}
38+
39+
return warnings, apierrors.NewInvalid(
40+
obj.GroupKind(),
41+
obj.GetName(), allErrs)
42+
}
43+
44+
func ValidateUpdate[W WMSWFS](c client.Client, newW, oldW W, validate func(W, *[]string, *field.ErrorList)) ([]string, error) {
1545
warnings := []string{}
1646
allErrs := field.ErrorList{}
1747

@@ -47,6 +77,7 @@ func ValidateUpdate[W WMSWFS](newW, oldW W, validate func(W, *[]string, *field.E
4777
}
4878

4979
validate(newW, &warnings, &allErrs)
80+
ValidateOwnerInfo(c, newW, &allErrs)
5081

5182
if len(allErrs) == 0 {
5283
return warnings, nil
@@ -137,3 +168,43 @@ func ValidateInspire[O WMSWFS](obj O, allErrs *field.ErrorList) {
137168
}
138169

139170
}
171+
172+
func ValidateOwnerInfo[O WMSWFS](c client.Client, obj O, allErrs *field.ErrorList) {
173+
ownerInfoRef := obj.OwnerInfoRef()
174+
ownerInfo := &smoothoperatorv1.OwnerInfo{}
175+
objectKey := client.ObjectKey{
176+
Namespace: obj.GetNamespace(),
177+
Name: ownerInfoRef,
178+
}
179+
ctx := context.Background()
180+
err := c.Get(ctx, objectKey, ownerInfo)
181+
fieldPath := field.NewPath("spec").Child("service").Child("ownerInfoRef")
182+
if err != nil {
183+
*allErrs = append(*allErrs, field.NotFound(fieldPath, ownerInfoRef))
184+
return
185+
}
186+
187+
if ownerInfo.Spec.NamespaceTemplate == nil {
188+
*allErrs = append(*allErrs, field.Required(fieldPath, "spec.namespaceTemplate missing in "+ownerInfo.Name))
189+
return
190+
}
191+
192+
if ((obj.Inspire() != nil && obj.Inspire().ServiceMetadataURL.CSW != nil) ||
193+
len(obj.DatasetMetadataIDs()) > 0) &&
194+
(ownerInfo.Spec.MetadataUrls == nil || ownerInfo.Spec.MetadataUrls.CSW == nil) {
195+
*allErrs = append(*allErrs, field.Required(fieldPath, "spec.metadataUrls.csw missing in "+ownerInfo.Name))
196+
return
197+
}
198+
199+
switch obj.Type() {
200+
case ServiceTypeWFS:
201+
if ownerInfo.Spec.WFS == nil {
202+
*allErrs = append(*allErrs, field.Required(fieldPath, "spec.WFS missing in "+ownerInfo.Name))
203+
}
204+
case ServiceTypeWMS:
205+
if ownerInfo.Spec.WMS == nil {
206+
*allErrs = append(*allErrs, field.Required(fieldPath, "spec.WMS missing in "+ownerInfo.Name))
207+
}
208+
}
209+
210+
}

api/v3/wfs_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,7 @@ func (wfs *WFS) IngressRouteURLs(includeServiceURLWhenEmpty bool) smoothoperator
332332

333333
return wfs.Spec.IngressRouteURLs
334334
}
335+
336+
func (wfs *WFS) OwnerInfoRef() string {
337+
return wfs.Spec.Service.OwnerInfoRef
338+
}

api/v3/wfs_validation.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,19 @@ import (
44
"slices"
55
"strings"
66

7+
"sigs.k8s.io/controller-runtime/pkg/client"
8+
79
sharedValidation "github.com/pdok/smooth-operator/pkg/validation"
810

9-
apierrors "k8s.io/apimachinery/pkg/api/errors"
10-
"k8s.io/apimachinery/pkg/runtime/schema"
1111
"k8s.io/apimachinery/pkg/util/validation/field"
1212
)
1313

14-
func (wfs *WFS) ValidateCreate() ([]string, error) {
15-
warnings := []string{}
16-
allErrs := field.ErrorList{}
17-
18-
err := sharedValidation.ValidateLabelsOnCreate(wfs.Labels)
19-
if err != nil {
20-
allErrs = append(allErrs, err)
21-
}
22-
23-
err = sharedValidation.ValidateIngressRouteURLsContainsBaseURL(wfs.Spec.IngressRouteURLs, wfs.URL(), nil)
24-
if err != nil {
25-
allErrs = append(allErrs, err)
26-
}
27-
28-
ValidateWFS(wfs, &warnings, &allErrs)
29-
30-
if len(allErrs) == 0 {
31-
return warnings, nil
32-
}
33-
34-
return warnings, apierrors.NewInvalid(
35-
schema.GroupKind{Group: "pdok.nl", Kind: "WFS"},
36-
wfs.Name, allErrs)
14+
func (wfs *WFS) ValidateCreate(c client.Client) ([]string, error) {
15+
return ValidateCreate(c, wfs, ValidateWFS)
3716
}
3817

39-
func (wfs *WFS) ValidateUpdate(wfsOld *WFS) ([]string, error) {
40-
return ValidateUpdate(wfs, wfsOld, ValidateWFS)
18+
func (wfs *WFS) ValidateUpdate(c client.Client, wfsOld *WFS) ([]string, error) {
19+
return ValidateUpdate(c, wfs, wfsOld, ValidateWFS)
4120
}
4221

4322
func ValidateWFS(wfs *WFS, warnings *[]string, allErrs *field.ErrorList) {

api/v3/wms_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,7 @@ func (wms *WMS) IngressRouteURLs(includeServiceURLWhenEmpty bool) smoothoperator
701701

702702
return wms.Spec.IngressRouteURLs
703703
}
704+
705+
func (wms *WMS) OwnerInfoRef() string {
706+
return wms.Spec.Service.OwnerInfoRef
707+
}

api/v3/wms_validation.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,19 @@ import (
55
"maps"
66
"strings"
77

8+
"sigs.k8s.io/controller-runtime/pkg/client"
9+
810
sharedValidation "github.com/pdok/smooth-operator/pkg/validation"
9-
apierrors "k8s.io/apimachinery/pkg/api/errors"
10-
"k8s.io/apimachinery/pkg/runtime/schema"
1111
"k8s.io/apimachinery/pkg/util/validation/field"
1212
"k8s.io/utils/strings/slices"
1313
)
1414

15-
func (wms *WMS) ValidateCreate() ([]string, error) {
16-
warnings := []string{}
17-
allErrs := field.ErrorList{}
18-
19-
err := sharedValidation.ValidateLabelsOnCreate(wms.Labels)
20-
if err != nil {
21-
allErrs = append(allErrs, err)
22-
}
23-
24-
err = sharedValidation.ValidateIngressRouteURLsContainsBaseURL(wms.Spec.IngressRouteURLs, wms.URL(), nil)
25-
if err != nil {
26-
allErrs = append(allErrs, err)
27-
}
28-
29-
ValidateWMS(wms, &warnings, &allErrs)
30-
31-
if len(allErrs) == 0 {
32-
return warnings, nil
33-
}
34-
35-
return warnings, apierrors.NewInvalid(
36-
schema.GroupKind{Group: "pdok.nl", Kind: "WMS"},
37-
wms.Name, allErrs)
15+
func (wms *WMS) ValidateCreate(c client.Client) ([]string, error) {
16+
return ValidateCreate(c, wms, ValidateWMS)
3817
}
3918

40-
func (wms *WMS) ValidateUpdate(wmsOld *WMS) ([]string, error) {
41-
return ValidateUpdate(wms, wmsOld, ValidateWMS)
19+
func (wms *WMS) ValidateUpdate(c client.Client, wmsOld *WMS) ([]string, error) {
20+
return ValidateUpdate(c, wms, wmsOld, ValidateWMS)
4221
}
4322

4423
// TODO fix linting (cyclop,funlen)

internal/controller/shared_controller_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,19 @@ func testMutates[R Reconciler, O pdoknlv3.WMSWFS](reconcilerFn func() R, resourc
239239
Expect(err).NotTo(HaveOccurred())
240240
Expect(owner.Name).Should(Equal("owner"))
241241

242+
Expect(k8sClient.Create(ctx, &owner)).To(Succeed())
243+
242244
var validationError error
243245
switch any(resource).(type) {
244246
case *pdoknlv3.WMS:
245247
wms := any(resource).(*pdoknlv3.WMS)
246-
_, validationError = wms.ValidateCreate()
248+
_, validationError = wms.ValidateCreate(k8sClient)
247249
case *pdoknlv3.WFS:
248250
wfs := any(resource).(*pdoknlv3.WFS)
249-
_, validationError = wfs.ValidateCreate()
251+
_, validationError = wfs.ValidateCreate(k8sClient)
250252
}
251253
Expect(validationError).NotTo(HaveOccurred())
254+
Expect(k8sClient.Delete(ctx, &owner)).To(Succeed())
252255
})
253256

254257
configMapNames := types.HashedConfigMapNames{}

0 commit comments

Comments
 (0)