externalsecret_validator.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 v1beta1
  13. import (
  14. "context"
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  18. )
  19. type ExternalSecretValidator struct{}
  20. func (esv *ExternalSecretValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
  21. return validateExternalSecret(obj)
  22. }
  23. func (esv *ExternalSecretValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
  24. return validateExternalSecret(newObj)
  25. }
  26. func (esv *ExternalSecretValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
  27. return nil, nil
  28. }
  29. func validateExternalSecret(obj runtime.Object) (admission.Warnings, error) {
  30. es, ok := obj.(*ExternalSecret)
  31. if !ok {
  32. return nil, fmt.Errorf("unexpected type")
  33. }
  34. if (es.Spec.Target.DeletionPolicy == DeletionPolicyDelete && es.Spec.Target.CreationPolicy == CreatePolicyMerge) ||
  35. (es.Spec.Target.DeletionPolicy == DeletionPolicyDelete && es.Spec.Target.CreationPolicy == CreatePolicyNone) {
  36. return nil, fmt.Errorf("deletionPolicy=Delete must not be used when the controller doesn't own the secret. Please set creationPolcy=Owner")
  37. }
  38. if es.Spec.Target.DeletionPolicy == DeletionPolicyMerge && es.Spec.Target.CreationPolicy == CreatePolicyNone {
  39. return nil, fmt.Errorf("deletionPolicy=Merge must not be used with creationPolcy=None. There is no Secret to merge with")
  40. }
  41. for _, ref := range es.Spec.DataFrom {
  42. findOrExtract := ref.Find != nil || ref.Extract != nil
  43. if findOrExtract && ref.SourceRef != nil && ref.SourceRef.GeneratorRef != nil {
  44. return nil, fmt.Errorf("generator can not be used with find or extract")
  45. }
  46. }
  47. return nil, nil
  48. }