Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 additions & 23 deletions api/v3/shared_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,88 @@ func ValidateCreate[W WMSWFS](c client.Client, obj W, validate func(W, *[]string
warnings := []string{}
allErrs := field.ErrorList{}

validateCreateWMSWFS(c, obj, &warnings, &allErrs, validate)

if len(allErrs) == 0 {
return warnings, nil
}

return warnings, apierrors.NewInvalid(
obj.GroupKind(),
obj.GetName(), allErrs)
}

func validateCreateWMSWFS[W WMSWFS](c client.Client, obj W, warnings *[]string, allErrs *field.ErrorList, validate func(W, *[]string, *field.ErrorList)) {
err := sharedValidation.ValidateLabelsOnCreate(obj.GetLabels())
if err != nil {
allErrs = append(allErrs, err)
*allErrs = append(*allErrs, err)
}

err = sharedValidation.ValidateIngressRouteURLsContainsBaseURL(obj.IngressRouteURLs(false), obj.URL(), nil)
if err != nil {
allErrs = append(allErrs, err)
*allErrs = append(*allErrs, err)
}

validate(obj, &warnings, &allErrs)
ValidateOwnerInfo(c, obj, &allErrs)
validate(obj, warnings, allErrs)

if len(allErrs) == 0 {
return warnings, nil
// Only validate owner info if k8s client is available
if c != nil {
ValidateOwnerInfo(c, obj, allErrs)
}

return warnings, apierrors.NewInvalid(
obj.GroupKind(),
obj.GetName(), allErrs)
}

func ValidateUpdate[W WMSWFS](c client.Client, newW, oldW W, validate func(W, *[]string, *field.ErrorList)) ([]string, error) {
warnings := []string{}
allErrs := field.ErrorList{}

validateUpdateWMSWFS(c, newW, oldW, &warnings, &allErrs, validate)

if len(allErrs) == 0 {
return warnings, nil
}
return warnings, apierrors.NewInvalid(
newW.GroupKind(),
newW.GetName(), allErrs)
}

func validateUpdateWMSWFS[W WMSWFS](c client.Client, newW, oldW W, warnings *[]string, allErrs *field.ErrorList, validate func(W, *[]string, *field.ErrorList)) {
// Make sure no ingressRouteURLs have been removed
sharedValidation.ValidateIngressRouteURLsNotRemoved(oldW.IngressRouteURLs(false), newW.IngressRouteURLs(true), &allErrs, nil)
sharedValidation.ValidateIngressRouteURLsNotRemoved(oldW.IngressRouteURLs(false), newW.IngressRouteURLs(true), allErrs, nil)

if len(newW.IngressRouteURLs(false)) == 0 {
// There are no ingressRouteURLs given, spec.service.url is immutable is that case.
path := field.NewPath("spec").Child("service").Child("url")
sharedValidation.CheckURLImmutability(
oldW.URL(),
newW.URL(),
&allErrs,
allErrs,
path,
)
} else if oldW.URL().String() != newW.URL().String() {
// Make sure both the old spec.service.url and the new one are included in the ingressRouteURLs list.
err := sharedValidation.ValidateIngressRouteURLsContainsBaseURL(newW.IngressRouteURLs(true), oldW.URL(), nil)
if err != nil {
allErrs = append(allErrs, err)
*allErrs = append(*allErrs, err)
}

err = sharedValidation.ValidateIngressRouteURLsContainsBaseURL(newW.IngressRouteURLs(true), newW.URL(), nil)
if err != nil {
allErrs = append(allErrs, err)
*allErrs = append(*allErrs, err)
}
}

sharedValidation.ValidateLabelsOnUpdate(oldW.GetLabels(), newW.GetLabels(), &allErrs)
sharedValidation.ValidateLabelsOnUpdate(oldW.GetLabels(), newW.GetLabels(), allErrs)

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"))
*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)
ValidateOwnerInfo(c, newW, &allErrs)
validate(newW, warnings, allErrs)

if len(allErrs) == 0 {
return warnings, nil
// Only validate owner info if k8s client is available
if c != nil {
ValidateOwnerInfo(c, newW, allErrs)
}
return warnings, apierrors.NewInvalid(
newW.GroupKind(),
newW.GetName(), allErrs)
}

func ValidateHorizontalPodAutoscalerPatch(patch HorizontalPodAutoscalerPatch, allErrs *field.ErrorList) {
Expand Down
10 changes: 10 additions & 0 deletions api/v3/wfs_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ func (wfs *WFS) ValidateUpdate(c client.Client, wfsOld *WFS) ([]string, error) {
return ValidateUpdate(c, wfs, wfsOld, ValidateWFS)
}

// ValidateCreateWFS validates WFS creation without k8s client
func ValidateCreateWFS(wfs *WFS, warnings *[]string, allErrs *field.ErrorList) {
validateCreateWMSWFS(nil, wfs, warnings, allErrs, ValidateWFS)
}

// ValidateUpdateWFS validates WFS update without k8s client
func ValidateUpdateWFS(wfs *WFS, wfsOld *WFS, warnings *[]string, allErrs *field.ErrorList) {
validateUpdateWMSWFS(nil, wfs, wfsOld, warnings, allErrs, ValidateWFS)
}

func ValidateWFS(wfs *WFS, warnings *[]string, allErrs *field.ErrorList) {
if strings.Contains(wfs.GetName(), "wfs") {
sharedValidation.AddWarning(
Expand Down
10 changes: 10 additions & 0 deletions api/v3/wms_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ func (wms *WMS) ValidateUpdate(c client.Client, wmsOld *WMS) ([]string, error) {
return ValidateUpdate(c, wms, wmsOld, ValidateWMS)
}

// ValidateCreateWMS validates WMS creation without k8s client
func ValidateCreateWMS(wms *WMS, warnings *[]string, allErrs *field.ErrorList) {
validateCreateWMSWFS(nil, wms, warnings, allErrs, ValidateWMS)
}

// ValidateUpdateWMS validates WMS update without k8s client
func ValidateUpdateWMS(wms *WMS, wmsOld *WMS, warnings *[]string, allErrs *field.ErrorList) {
validateUpdateWMSWFS(nil, wms, wmsOld, warnings, allErrs, ValidateWMS)
}

func ValidateWMS(wms *WMS, warnings *[]string, allErrs *field.ErrorList) {
if strings.Contains(wms.GetName(), "wms") {
sharedValidation.AddWarning(
Expand Down