diff --git a/.golangci.yml b/.golangci.yml index a9f9bdf..331b6f2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,7 +4,7 @@ run: timeout: 5m # Modules download mode (do not modify go.mod) - module-download-mode: readonly + modules-download-mode: readonly # Include test files (see below to exclude certain linters) tests: true @@ -21,7 +21,9 @@ issues: - gosec output: - formats: colored-line-number + formats: + - format: colored-line-number + path: stdout print-issued-lines: true print-linter-name: true diff --git a/api/v3/shared_types.go b/api/v3/shared_types.go index b6de5ab..0722aa6 100644 --- a/api/v3/shared_types.go +++ b/api/v3/shared_types.go @@ -8,6 +8,8 @@ import ( "net/url" "strings" + "k8s.io/apimachinery/pkg/runtime/schema" + autoscalingv2 "k8s.io/api/autoscaling/v2" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -37,6 +39,8 @@ type WMSWFS interface { *WFS | *WMS metav1.Object + GroupKind() schema.GroupKind + Inspire() *Inspire Mapfile() *Mapfile PodSpecPatch() *corev1.PodSpec HorizontalPodAutoscalerPatch() *HorizontalPodAutoscalerPatch diff --git a/api/v3/shared_validation.go b/api/v3/shared_validation.go new file mode 100644 index 0000000..ba027f5 --- /dev/null +++ b/api/v3/shared_validation.go @@ -0,0 +1,44 @@ +package v3 + +import ( + "github.com/pdok/smooth-operator/model" + sharedValidation "github.com/pdok/smooth-operator/pkg/validation" + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/util/validation/field" +) + +func ValidateUpdate[W WMSWFS](newW, oldW W, validate func(W, *[]string, *field.ErrorList)) ([]string, error) { + warnings := []string{} + allErrs := field.ErrorList{} + + sharedValidation.ValidateLabelsOnUpdate(oldW.GetLabels(), newW.GetLabels(), &allErrs) + + path := field.NewPath("spec").Child("service").Child("url") + oldURL, err := model.ParseURL(oldW.URLPath()) + if err != nil { + allErrs = append(allErrs, field.InternalError(path, err)) + } + newURL, err := model.ParseURL(oldW.URLPath()) + if err != nil { + allErrs = append(allErrs, field.InternalError(path, err)) + } + sharedValidation.CheckUrlImmutability( + model.URL{URL: oldURL}, + model.URL{URL: newURL}, + &allErrs, + path, + ) + + if (newW.Inspire() == nil && oldW.Inspire() != nil) || (newW.Inspire() != nil && oldW.Inspire() == nil) { + allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Child("service").Child("inspire"), "cannot change from inspire to not inspire or the other way around")) + } + + validate(newW, &warnings, &allErrs) + + if len(allErrs) == 0 { + return warnings, nil + } + return warnings, apierrors.NewInvalid( + newW.GroupKind(), + newW.GetName(), allErrs) +} diff --git a/api/v3/wfs_types.go b/api/v3/wfs_types.go index 2941be6..959bd02 100644 --- a/api/v3/wfs_types.go +++ b/api/v3/wfs_types.go @@ -25,10 +25,12 @@ SOFTWARE. package v3 import ( + "slices" + shared_model "github.com/pdok/smooth-operator/model" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "slices" + "k8s.io/apimachinery/pkg/runtime/schema" ) // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! @@ -215,6 +217,14 @@ func (wfs *WFS) HasPostgisData() bool { return false } +func (wfs *WFS) GroupKind() schema.GroupKind { + return schema.GroupKind{Group: GroupVersion.Group, Kind: wfs.Kind} +} + +func (wfs *WFS) Inspire() *Inspire { + return wfs.Spec.Service.Inspire +} + func (wfs *WFS) Mapfile() *Mapfile { return wfs.Spec.Service.Mapfile } @@ -254,8 +264,3 @@ func (wfs *WFS) GeoPackages() []*Gpkg { return gpkgs } - -//nolint:revive -func (wfs *WFS) GetBaseUrl() string { - return wfs.Spec.Service.URL -} diff --git a/api/v3/wfs_validation.go b/api/v3/wfs_validation.go index 9139849..a348ba5 100644 --- a/api/v3/wfs_validation.go +++ b/api/v3/wfs_validation.go @@ -30,28 +30,8 @@ func (wfs *WFS) ValidateCreate() ([]string, error) { wfs.Name, allErrs) } -// TODO fix linting (dupl) func (wfs *WFS) ValidateUpdate(wfsOld *WFS) ([]string, error) { - warnings := []string{} - allErrs := field.ErrorList{} - - sharedValidation.ValidateLabelsOnUpdate(wfsOld.Labels, wfs.Labels, &allErrs) - - sharedValidation.CheckBaseUrlImmutability(wfsOld, wfs, &allErrs) - - if (wfs.Spec.Service.Inspire == nil && wfsOld.Spec.Service.Inspire != nil) || (wfs.Spec.Service.Inspire != nil && wfsOld.Spec.Service.Inspire == nil) { - allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Child("service").Child("inspire"), "cannot change from inspire to not inspire or the other way around")) - } - - ValidateWFS(wfs, &warnings, &allErrs) - - if len(allErrs) == 0 { - return warnings, nil - } - - return warnings, apierrors.NewInvalid( - schema.GroupKind{Group: "pdok.nl", Kind: "WFS"}, - wfs.Name, allErrs) + return ValidateUpdate(wfs, wfsOld, ValidateWFS) } func ValidateWFS(wfs *WFS, warnings *[]string, allErrs *field.ErrorList) { diff --git a/api/v3/wms_types.go b/api/v3/wms_types.go index 4e63b8d..d772205 100644 --- a/api/v3/wms_types.go +++ b/api/v3/wms_types.go @@ -29,6 +29,8 @@ import ( "slices" "sort" + "k8s.io/apimachinery/pkg/runtime/schema" + shared_model "github.com/pdok/smooth-operator/model" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -118,9 +120,9 @@ type WMSService struct { Layer Layer `json:"layer"` } -func (s WMSService) KeywordsIncludingInspireKeyword() []string { - keywords := s.Keywords - if s.Inspire != nil && !slices.Contains(keywords, "infoMapAccessService") { +func (wmsService WMSService) KeywordsIncludingInspireKeyword() []string { + keywords := wmsService.Keywords + if wmsService.Inspire != nil && !slices.Contains(keywords, "infoMapAccessService") { keywords = append(keywords, "infoMapAccessService") } @@ -542,6 +544,14 @@ func (wms *WMS) HasPostgisData() bool { return false } +func (wms *WMS) GroupKind() schema.GroupKind { + return schema.GroupKind{Group: GroupVersion.Group, Kind: wms.Kind} +} + +func (wms *WMS) Inspire() *Inspire { + return wms.Spec.Service.Inspire +} + func (wms *WMS) Mapfile() *Mapfile { return wms.Spec.Service.Mapfile } @@ -573,18 +583,15 @@ func (wms *WMS) URLPath() string { func (wms *WMS) GeoPackages() []*Gpkg { gpkgs := make([]*Gpkg, 0) - // TODO fix linting (nestif) - if wms.Spec.Service.Layer.Layers != nil { - for _, layer := range wms.Spec.Service.Layer.Layers { - if layer.Data != nil { - if layer.Data.Gpkg != nil { - gpkgs = append(gpkgs, layer.Data.Gpkg) - } - } else if layer.Layers != nil { - for _, childLayer := range layer.Layers { - if childLayer.Data != nil && childLayer.Data.Gpkg != nil { - gpkgs = append(gpkgs, childLayer.Data.Gpkg) - } + for _, layer := range wms.Spec.Service.Layer.Layers { + if layer.Data != nil { + if layer.Data.Gpkg != nil { + gpkgs = append(gpkgs, layer.Data.Gpkg) + } + } else if layer.Layers != nil { + for _, childLayer := range layer.Layers { + if childLayer.Data != nil && childLayer.Data.Gpkg != nil { + gpkgs = append(gpkgs, childLayer.Data.Gpkg) } } } @@ -592,8 +599,3 @@ func (wms *WMS) GeoPackages() []*Gpkg { return gpkgs } - -//nolint:revive -func (wms *WMS) GetBaseUrl() string { - return wms.Spec.Service.URL -} diff --git a/api/v3/wms_validation.go b/api/v3/wms_validation.go index 14e9cec..89d8c93 100644 --- a/api/v3/wms_validation.go +++ b/api/v3/wms_validation.go @@ -32,28 +32,8 @@ func (wms *WMS) ValidateCreate() ([]string, error) { wms.Name, allErrs) } -// TODO fix linting (dupl) func (wms *WMS) ValidateUpdate(wmsOld *WMS) ([]string, error) { - warnings := []string{} - allErrs := field.ErrorList{} - - sharedValidation.ValidateLabelsOnUpdate(wmsOld.Labels, wms.Labels, &allErrs) - - sharedValidation.CheckBaseUrlImmutability(wmsOld, wms, &allErrs) - - if (wms.Spec.Service.Inspire == nil && wmsOld.Spec.Service.Inspire != nil) || (wms.Spec.Service.Inspire != nil && wmsOld.Spec.Service.Inspire == nil) { - allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Child("service").Child("inspire"), "cannot change from inspire to not inspire or the other way around")) - } - - ValidateWMS(wms, &warnings, &allErrs) - - if len(allErrs) == 0 { - return warnings, nil - } - - return warnings, apierrors.NewInvalid( - schema.GroupKind{Group: "pdok.nl", Kind: "WFS"}, - wms.Name, allErrs) + return ValidateUpdate(wms, wmsOld, ValidateWMS) } // TODO fix linting (cyclop) diff --git a/go.mod b/go.mod index df9c2f0..f445791 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/pdok/featureinfo-generator v1.4.0-beta1 github.com/pdok/ogc-capabilities-generator v1.0.0-beta7 github.com/pdok/ogc-specifications v1.0.0-beta7 - github.com/pdok/smooth-operator v0.1.0 + github.com/pdok/smooth-operator v0.1.1 github.com/peterbourgon/ff v1.7.1 github.com/stretchr/testify v1.10.0 github.com/traefik/traefik/v3 v3.3.4 diff --git a/go.sum b/go.sum index 8a4aed0..1010034 100644 --- a/go.sum +++ b/go.sum @@ -155,8 +155,8 @@ github.com/pdok/ogc-capabilities-generator v1.0.0-beta7 h1:w0dP2RQX0KEEovrq57NCM github.com/pdok/ogc-capabilities-generator v1.0.0-beta7/go.mod h1:slk89sAgmWU5NCIKwGyciQWnm0RHmwhJYQ431E2CwSk= github.com/pdok/ogc-specifications v1.0.0-beta7 h1:AFSO8iCYbD1MrjOS2q+PGp2PmSqAH+O7cuA7JeePCXE= github.com/pdok/ogc-specifications v1.0.0-beta7/go.mod h1:YDngwkwrWOfc5MYnEYseiv97K1Y9bZXlVzwi/8EaIl8= -github.com/pdok/smooth-operator v0.1.0 h1:i1uZa3Niuh6ljl4UTGcC48sC01H3CaWHiIfK7pQTix8= -github.com/pdok/smooth-operator v0.1.0/go.mod h1:przwM7mBGmNPqabyhImKVZ15WL4zbqLqH4ExbuWKhWE= +github.com/pdok/smooth-operator v0.1.1 h1:rmsup4HmzJsxt4ZT9GWfj498dKLRfDhyuILeEkjju/A= +github.com/pdok/smooth-operator v0.1.1/go.mod h1:przwM7mBGmNPqabyhImKVZ15WL4zbqLqH4ExbuWKhWE= github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= github.com/peterbourgon/ff v1.7.1 h1:xt1lxTG+Nr2+tFtysY7abFgPoH3Lug8CwYJMOmJRXhk= github.com/peterbourgon/ff v1.7.1/go.mod h1:fYI5YA+3RDqQRExmFbHnBjEeWzh9TrS8rnRpEq7XIg0= diff --git a/internal/controller/mapserver/deployment.go b/internal/controller/mapserver/deployment.go index d2d1015..8170f2f 100644 --- a/internal/controller/mapserver/deployment.go +++ b/internal/controller/mapserver/deployment.go @@ -216,7 +216,7 @@ func GetEnvVarsForDeployment[O pdoknlv3.WMSWFS](obj O, blobsSecretName string) [ } } -// TODO fix linting (cyclop,gocritic,revive) +// TODO fix linting (cyclop) // Resources for mapserver container func GetResourcesForDeployment[O pdoknlv3.WMSWFS](obj O) v1.ResourceRequirements { resources := v1.ResourceRequirements{ @@ -225,16 +225,18 @@ func GetResourcesForDeployment[O pdoknlv3.WMSWFS](obj O) v1.ResourceRequirements } maxResourceVal := func(v1 *resource.Quantity, v2 *resource.Quantity) *resource.Quantity { - if v1 != nil && v2 != nil { + switch { + case v1 != nil && v2 != nil: if v1.Value() > v2.Value() { return v1 - } else { - return v2 } - } else if v1 != nil && v2 == nil { + return v2 + case v1 != nil && v2 == nil: return v1 - } else if v1 == nil || v2 != nil { + case v1 == nil || v2 != nil: return v2 + default: + } return &resource.Quantity{}