Skip to content

Commit c8955d6

Browse files
authored
Merge pull request #43 from PDOK/wr/fix-validation-duplicate
Wr/fix validation duplicate
2 parents ef78060 + ffe0719 commit c8955d6

File tree

10 files changed

+98
-79
lines changed

10 files changed

+98
-79
lines changed

.golangci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ run:
44
timeout: 5m
55

66
# Modules download mode (do not modify go.mod)
7-
module-download-mode: readonly
7+
modules-download-mode: readonly
88

99
# Include test files (see below to exclude certain linters)
1010
tests: true
@@ -21,7 +21,9 @@ issues:
2121
- gosec
2222

2323
output:
24-
formats: colored-line-number
24+
formats:
25+
- format: colored-line-number
26+
path: stdout
2527
print-issued-lines: true
2628
print-linter-name: true
2729

api/v3/shared_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net/url"
99
"strings"
1010

11+
"k8s.io/apimachinery/pkg/runtime/schema"
12+
1113
autoscalingv2 "k8s.io/api/autoscaling/v2"
1214
corev1 "k8s.io/api/core/v1"
1315
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -37,6 +39,8 @@ type WMSWFS interface {
3739
*WFS | *WMS
3840
metav1.Object
3941

42+
GroupKind() schema.GroupKind
43+
Inspire() *Inspire
4044
Mapfile() *Mapfile
4145
PodSpecPatch() *corev1.PodSpec
4246
HorizontalPodAutoscalerPatch() *HorizontalPodAutoscalerPatch

api/v3/shared_validation.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package v3
2+
3+
import (
4+
"github.com/pdok/smooth-operator/model"
5+
sharedValidation "github.com/pdok/smooth-operator/pkg/validation"
6+
apierrors "k8s.io/apimachinery/pkg/api/errors"
7+
"k8s.io/apimachinery/pkg/util/validation/field"
8+
)
9+
10+
func ValidateUpdate[W WMSWFS](newW, oldW W, validate func(W, *[]string, *field.ErrorList)) ([]string, error) {
11+
warnings := []string{}
12+
allErrs := field.ErrorList{}
13+
14+
sharedValidation.ValidateLabelsOnUpdate(oldW.GetLabels(), newW.GetLabels(), &allErrs)
15+
16+
path := field.NewPath("spec").Child("service").Child("url")
17+
oldURL, err := model.ParseURL(oldW.URLPath())
18+
if err != nil {
19+
allErrs = append(allErrs, field.InternalError(path, err))
20+
}
21+
newURL, err := model.ParseURL(oldW.URLPath())
22+
if err != nil {
23+
allErrs = append(allErrs, field.InternalError(path, err))
24+
}
25+
sharedValidation.CheckUrlImmutability(
26+
model.URL{URL: oldURL},
27+
model.URL{URL: newURL},
28+
&allErrs,
29+
path,
30+
)
31+
32+
if (newW.Inspire() == nil && oldW.Inspire() != nil) || (newW.Inspire() != nil && oldW.Inspire() == nil) {
33+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Child("service").Child("inspire"), "cannot change from inspire to not inspire or the other way around"))
34+
}
35+
36+
validate(newW, &warnings, &allErrs)
37+
38+
if len(allErrs) == 0 {
39+
return warnings, nil
40+
}
41+
return warnings, apierrors.NewInvalid(
42+
newW.GroupKind(),
43+
newW.GetName(), allErrs)
44+
}

api/v3/wfs_types.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ SOFTWARE.
2525
package v3
2626

2727
import (
28+
"slices"
29+
2830
shared_model "github.com/pdok/smooth-operator/model"
2931
corev1 "k8s.io/api/core/v1"
3032
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31-
"slices"
33+
"k8s.io/apimachinery/pkg/runtime/schema"
3234
)
3335

3436
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
@@ -215,6 +217,14 @@ func (wfs *WFS) HasPostgisData() bool {
215217
return false
216218
}
217219

220+
func (wfs *WFS) GroupKind() schema.GroupKind {
221+
return schema.GroupKind{Group: GroupVersion.Group, Kind: wfs.Kind}
222+
}
223+
224+
func (wfs *WFS) Inspire() *Inspire {
225+
return wfs.Spec.Service.Inspire
226+
}
227+
218228
func (wfs *WFS) Mapfile() *Mapfile {
219229
return wfs.Spec.Service.Mapfile
220230
}
@@ -254,8 +264,3 @@ func (wfs *WFS) GeoPackages() []*Gpkg {
254264

255265
return gpkgs
256266
}
257-
258-
//nolint:revive
259-
func (wfs *WFS) GetBaseUrl() string {
260-
return wfs.Spec.Service.URL
261-
}

api/v3/wfs_validation.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,8 @@ func (wfs *WFS) ValidateCreate() ([]string, error) {
3030
wfs.Name, allErrs)
3131
}
3232

33-
// TODO fix linting (dupl)
3433
func (wfs *WFS) ValidateUpdate(wfsOld *WFS) ([]string, error) {
35-
warnings := []string{}
36-
allErrs := field.ErrorList{}
37-
38-
sharedValidation.ValidateLabelsOnUpdate(wfsOld.Labels, wfs.Labels, &allErrs)
39-
40-
sharedValidation.CheckBaseUrlImmutability(wfsOld, wfs, &allErrs)
41-
42-
if (wfs.Spec.Service.Inspire == nil && wfsOld.Spec.Service.Inspire != nil) || (wfs.Spec.Service.Inspire != nil && wfsOld.Spec.Service.Inspire == nil) {
43-
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Child("service").Child("inspire"), "cannot change from inspire to not inspire or the other way around"))
44-
}
45-
46-
ValidateWFS(wfs, &warnings, &allErrs)
47-
48-
if len(allErrs) == 0 {
49-
return warnings, nil
50-
}
51-
52-
return warnings, apierrors.NewInvalid(
53-
schema.GroupKind{Group: "pdok.nl", Kind: "WFS"},
54-
wfs.Name, allErrs)
34+
return ValidateUpdate(wfs, wfsOld, ValidateWFS)
5535
}
5636

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

api/v3/wms_types.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"slices"
3030
"sort"
3131

32+
"k8s.io/apimachinery/pkg/runtime/schema"
33+
3234
shared_model "github.com/pdok/smooth-operator/model"
3335
corev1 "k8s.io/api/core/v1"
3436
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -118,9 +120,9 @@ type WMSService struct {
118120
Layer Layer `json:"layer"`
119121
}
120122

121-
func (s WMSService) KeywordsIncludingInspireKeyword() []string {
122-
keywords := s.Keywords
123-
if s.Inspire != nil && !slices.Contains(keywords, "infoMapAccessService") {
123+
func (wmsService WMSService) KeywordsIncludingInspireKeyword() []string {
124+
keywords := wmsService.Keywords
125+
if wmsService.Inspire != nil && !slices.Contains(keywords, "infoMapAccessService") {
124126
keywords = append(keywords, "infoMapAccessService")
125127
}
126128

@@ -542,6 +544,14 @@ func (wms *WMS) HasPostgisData() bool {
542544
return false
543545
}
544546

547+
func (wms *WMS) GroupKind() schema.GroupKind {
548+
return schema.GroupKind{Group: GroupVersion.Group, Kind: wms.Kind}
549+
}
550+
551+
func (wms *WMS) Inspire() *Inspire {
552+
return wms.Spec.Service.Inspire
553+
}
554+
545555
func (wms *WMS) Mapfile() *Mapfile {
546556
return wms.Spec.Service.Mapfile
547557
}
@@ -573,27 +583,19 @@ func (wms *WMS) URLPath() string {
573583
func (wms *WMS) GeoPackages() []*Gpkg {
574584
gpkgs := make([]*Gpkg, 0)
575585

576-
// TODO fix linting (nestif)
577-
if wms.Spec.Service.Layer.Layers != nil {
578-
for _, layer := range wms.Spec.Service.Layer.Layers {
579-
if layer.Data != nil {
580-
if layer.Data.Gpkg != nil {
581-
gpkgs = append(gpkgs, layer.Data.Gpkg)
582-
}
583-
} else if layer.Layers != nil {
584-
for _, childLayer := range layer.Layers {
585-
if childLayer.Data != nil && childLayer.Data.Gpkg != nil {
586-
gpkgs = append(gpkgs, childLayer.Data.Gpkg)
587-
}
586+
for _, layer := range wms.Spec.Service.Layer.Layers {
587+
if layer.Data != nil {
588+
if layer.Data.Gpkg != nil {
589+
gpkgs = append(gpkgs, layer.Data.Gpkg)
590+
}
591+
} else if layer.Layers != nil {
592+
for _, childLayer := range layer.Layers {
593+
if childLayer.Data != nil && childLayer.Data.Gpkg != nil {
594+
gpkgs = append(gpkgs, childLayer.Data.Gpkg)
588595
}
589596
}
590597
}
591598
}
592599

593600
return gpkgs
594601
}
595-
596-
//nolint:revive
597-
func (wms *WMS) GetBaseUrl() string {
598-
return wms.Spec.Service.URL
599-
}

api/v3/wms_validation.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,8 @@ func (wms *WMS) ValidateCreate() ([]string, error) {
3232
wms.Name, allErrs)
3333
}
3434

35-
// TODO fix linting (dupl)
3635
func (wms *WMS) ValidateUpdate(wmsOld *WMS) ([]string, error) {
37-
warnings := []string{}
38-
allErrs := field.ErrorList{}
39-
40-
sharedValidation.ValidateLabelsOnUpdate(wmsOld.Labels, wms.Labels, &allErrs)
41-
42-
sharedValidation.CheckBaseUrlImmutability(wmsOld, wms, &allErrs)
43-
44-
if (wms.Spec.Service.Inspire == nil && wmsOld.Spec.Service.Inspire != nil) || (wms.Spec.Service.Inspire != nil && wmsOld.Spec.Service.Inspire == nil) {
45-
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Child("service").Child("inspire"), "cannot change from inspire to not inspire or the other way around"))
46-
}
47-
48-
ValidateWMS(wms, &warnings, &allErrs)
49-
50-
if len(allErrs) == 0 {
51-
return warnings, nil
52-
}
53-
54-
return warnings, apierrors.NewInvalid(
55-
schema.GroupKind{Group: "pdok.nl", Kind: "WFS"},
56-
wms.Name, allErrs)
36+
return ValidateUpdate(wms, wmsOld, ValidateWMS)
5737
}
5838

5939
// TODO fix linting (cyclop)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/pdok/featureinfo-generator v1.4.0-beta1
1414
github.com/pdok/ogc-capabilities-generator v1.0.0-beta7
1515
github.com/pdok/ogc-specifications v1.0.0-beta7
16-
github.com/pdok/smooth-operator v0.1.0
16+
github.com/pdok/smooth-operator v0.1.1
1717
github.com/peterbourgon/ff v1.7.1
1818
github.com/stretchr/testify v1.10.0
1919
github.com/traefik/traefik/v3 v3.3.4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ github.com/pdok/ogc-capabilities-generator v1.0.0-beta7 h1:w0dP2RQX0KEEovrq57NCM
155155
github.com/pdok/ogc-capabilities-generator v1.0.0-beta7/go.mod h1:slk89sAgmWU5NCIKwGyciQWnm0RHmwhJYQ431E2CwSk=
156156
github.com/pdok/ogc-specifications v1.0.0-beta7 h1:AFSO8iCYbD1MrjOS2q+PGp2PmSqAH+O7cuA7JeePCXE=
157157
github.com/pdok/ogc-specifications v1.0.0-beta7/go.mod h1:YDngwkwrWOfc5MYnEYseiv97K1Y9bZXlVzwi/8EaIl8=
158-
github.com/pdok/smooth-operator v0.1.0 h1:i1uZa3Niuh6ljl4UTGcC48sC01H3CaWHiIfK7pQTix8=
159-
github.com/pdok/smooth-operator v0.1.0/go.mod h1:przwM7mBGmNPqabyhImKVZ15WL4zbqLqH4ExbuWKhWE=
158+
github.com/pdok/smooth-operator v0.1.1 h1:rmsup4HmzJsxt4ZT9GWfj498dKLRfDhyuILeEkjju/A=
159+
github.com/pdok/smooth-operator v0.1.1/go.mod h1:przwM7mBGmNPqabyhImKVZ15WL4zbqLqH4ExbuWKhWE=
160160
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
161161
github.com/peterbourgon/ff v1.7.1 h1:xt1lxTG+Nr2+tFtysY7abFgPoH3Lug8CwYJMOmJRXhk=
162162
github.com/peterbourgon/ff v1.7.1/go.mod h1:fYI5YA+3RDqQRExmFbHnBjEeWzh9TrS8rnRpEq7XIg0=

internal/controller/mapserver/deployment.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func GetEnvVarsForDeployment[O pdoknlv3.WMSWFS](obj O, blobsSecretName string) [
216216
}
217217
}
218218

219-
// TODO fix linting (cyclop,gocritic,revive)
219+
// TODO fix linting (cyclop)
220220
// Resources for mapserver container
221221
func GetResourcesForDeployment[O pdoknlv3.WMSWFS](obj O) v1.ResourceRequirements {
222222
resources := v1.ResourceRequirements{
@@ -225,16 +225,18 @@ func GetResourcesForDeployment[O pdoknlv3.WMSWFS](obj O) v1.ResourceRequirements
225225
}
226226

227227
maxResourceVal := func(v1 *resource.Quantity, v2 *resource.Quantity) *resource.Quantity {
228-
if v1 != nil && v2 != nil {
228+
switch {
229+
case v1 != nil && v2 != nil:
229230
if v1.Value() > v2.Value() {
230231
return v1
231-
} else {
232-
return v2
233232
}
234-
} else if v1 != nil && v2 == nil {
233+
return v2
234+
case v1 != nil && v2 == nil:
235235
return v1
236-
} else if v1 == nil || v2 != nil {
236+
case v1 == nil || v2 != nil:
237237
return v2
238+
default:
239+
238240
}
239241

240242
return &resource.Quantity{}

0 commit comments

Comments
 (0)