providerstore_validator.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package v2alpha1
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "regexp"
  19. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  20. )
  21. // Ensures validators implement the admission.CustomValidator interface correctly.
  22. var _ admission.Validator[*ProviderStore] = &ProviderStoreValidator{}
  23. var _ admission.Validator[*ClusterProviderStore] = &ClusterProviderStoreValidator{}
  24. // ProviderStoreValidator implements webhook validation for ProviderStore resources.
  25. type ProviderStoreValidator struct{}
  26. // ClusterProviderStoreValidator implements webhook validation for ClusterProviderStore resources.
  27. type ClusterProviderStoreValidator struct{}
  28. // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
  29. func (r *ProviderStoreValidator) ValidateCreate(_ context.Context, obj *ProviderStore) (admission.Warnings, error) {
  30. return nil, validateProviderStoreSpec(obj.Namespace, obj.Spec.BackendRef, nil)
  31. }
  32. // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
  33. func (r *ProviderStoreValidator) ValidateUpdate(_ context.Context, _, newObj *ProviderStore) (admission.Warnings, error) {
  34. return nil, validateProviderStoreSpec(newObj.Namespace, newObj.Spec.BackendRef, nil)
  35. }
  36. // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
  37. func (r *ProviderStoreValidator) ValidateDelete(_ context.Context, _ *ProviderStore) (admission.Warnings, error) {
  38. return nil, nil
  39. }
  40. // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
  41. func (r *ClusterProviderStoreValidator) ValidateCreate(_ context.Context, obj *ClusterProviderStore) (admission.Warnings, error) {
  42. return nil, validateProviderStoreSpec("", obj.Spec.BackendRef, obj.Spec.Conditions)
  43. }
  44. // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
  45. func (r *ClusterProviderStoreValidator) ValidateUpdate(_ context.Context, _, newObj *ClusterProviderStore) (admission.Warnings, error) {
  46. return nil, validateProviderStoreSpec("", newObj.Spec.BackendRef, newObj.Spec.Conditions)
  47. }
  48. // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
  49. func (r *ClusterProviderStoreValidator) ValidateDelete(_ context.Context, _ *ClusterProviderStore) (admission.Warnings, error) {
  50. return nil, nil
  51. }
  52. func validateProviderStoreSpec(namespace string, backendRef BackendObjectReference, conditions []StoreNamespaceCondition) error {
  53. var errs error
  54. if namespace != "" && backendRef.Namespace != "" && backendRef.Namespace != namespace {
  55. errs = errors.Join(errs, fmt.Errorf("backendRef.namespace %q must match metadata.namespace %q", backendRef.Namespace, namespace))
  56. }
  57. for ci, condition := range conditions {
  58. for ri, expr := range condition.NamespaceRegexes {
  59. if _, err := regexp.Compile(expr); err != nil {
  60. errs = errors.Join(errs, fmt.Errorf("failed to compile %dth namespace regex in %dth condition: %w", ri, ci, err))
  61. }
  62. }
  63. }
  64. return errs
  65. }