|
1 | 1 | package v3 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "slices" |
6 | 7 |
|
| 8 | + smoothoperatorv1 "github.com/pdok/smooth-operator/api/v1" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 10 | + |
7 | 11 | sharedValidation "github.com/pdok/smooth-operator/pkg/validation" |
8 | 12 | v1 "k8s.io/api/core/v1" |
9 | 13 |
|
10 | 14 | apierrors "k8s.io/apimachinery/pkg/api/errors" |
11 | 15 | "k8s.io/apimachinery/pkg/util/validation/field" |
12 | 16 | ) |
13 | 17 |
|
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) { |
15 | 45 | warnings := []string{} |
16 | 46 | allErrs := field.ErrorList{} |
17 | 47 |
|
@@ -47,6 +77,7 @@ func ValidateUpdate[W WMSWFS](newW, oldW W, validate func(W, *[]string, *field.E |
47 | 77 | } |
48 | 78 |
|
49 | 79 | validate(newW, &warnings, &allErrs) |
| 80 | + ValidateOwnerInfo(c, newW, &allErrs) |
50 | 81 |
|
51 | 82 | if len(allErrs) == 0 { |
52 | 83 | return warnings, nil |
@@ -137,3 +168,43 @@ func ValidateInspire[O WMSWFS](obj O, allErrs *field.ErrorList) { |
137 | 168 | } |
138 | 169 |
|
139 | 170 | } |
| 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 | +} |
0 commit comments