secretstore_validator.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package v1
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. "regexp"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  20. )
  21. var _ admission.CustomValidator = &GenericStoreValidator{}
  22. const (
  23. errInvalidStore = "invalid store"
  24. warnStoreUnmaintained = "store %s isn't currently maintained. Please plan and prepare accordingly."
  25. )
  26. type GenericStoreValidator struct{}
  27. // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
  28. func (r *GenericStoreValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
  29. st, ok := obj.(GenericStore)
  30. if !ok {
  31. return nil, errors.New(errInvalidStore)
  32. }
  33. return validateStore(st)
  34. }
  35. // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
  36. func (r *GenericStoreValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
  37. st, ok := newObj.(GenericStore)
  38. if !ok {
  39. return nil, errors.New(errInvalidStore)
  40. }
  41. return validateStore(st)
  42. }
  43. // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
  44. func (r *GenericStoreValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
  45. return nil, nil
  46. }
  47. func validateStore(store GenericStore) (admission.Warnings, error) {
  48. if err := validateConditions(store); err != nil {
  49. return nil, err
  50. }
  51. provider, err := GetProvider(store)
  52. if err != nil {
  53. return nil, err
  54. }
  55. isMaintained, err := GetMaintenanceStatus(store)
  56. if err != nil {
  57. return nil, err
  58. }
  59. warns, err := provider.ValidateStore(store)
  60. if !isMaintained {
  61. warns = append(warns, fmt.Sprintf(warnStoreUnmaintained, store.GetName()))
  62. }
  63. return warns, err
  64. }
  65. func validateConditions(store GenericStore) error {
  66. var errs error
  67. for ci, condition := range store.GetSpec().Conditions {
  68. for ri, r := range condition.NamespaceRegexes {
  69. if _, err := regexp.Compile(r); err != nil {
  70. errs = errors.Join(errs, fmt.Errorf("failed to compile %dth namespace regex in %dth condition: %w", ri, ci, err))
  71. }
  72. }
  73. }
  74. return errs
  75. }