provider.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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 v1
  14. import (
  15. "context"
  16. corev1 "k8s.io/api/core/v1"
  17. "sigs.k8s.io/controller-runtime/pkg/client"
  18. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  19. )
  20. const (
  21. // Ready indicates that the client is configured correctly
  22. // and can be used.
  23. ValidationResultReady ValidationResult = iota
  24. // Unknown indicates that the client can be used
  25. // but information is missing and it can not be validated.
  26. ValidationResultUnknown
  27. // Error indicates that there is a misconfiguration.
  28. ValidationResultError
  29. )
  30. type ValidationResult uint8
  31. func (v ValidationResult) String() string {
  32. return [...]string{"Ready", "Unknown", "Error"}[v]
  33. }
  34. // +kubebuilder:object:root=false
  35. // +kubebuilder:object:generate:false
  36. // +k8s:deepcopy-gen:interfaces=nil
  37. // +k8s:deepcopy-gen=nil
  38. // Provider is a common interface for interacting with secret backends.
  39. type Provider interface {
  40. // NewClient constructs a SecretsManager Provider
  41. NewClient(ctx context.Context, store GenericStore, kube client.Client, namespace string) (SecretsClient, error)
  42. // ValidateStore checks if the provided store is valid
  43. // The provider may return a warning and an error.
  44. // The intended use of the warning to indicate a deprecation of behavior
  45. // or other type of message that is NOT a validation failure but should be noticed by the user.
  46. ValidateStore(store GenericStore) (admission.Warnings, error)
  47. // Capabilities returns the provider Capabilities (Read, Write, ReadWrite)
  48. Capabilities() SecretStoreCapabilities
  49. }
  50. // +kubebuilder:object:root=false
  51. // +kubebuilder:object:generate:false
  52. // +k8s:deepcopy-gen:interfaces=nil
  53. // +k8s:deepcopy-gen=nil
  54. // SecretsClient provides access to secrets.
  55. type SecretsClient interface {
  56. // GetSecret returns a single secret from the provider
  57. // if GetSecret returns an error with type NoSecretError
  58. // then the secret entry will be deleted depending on the deletionPolicy.
  59. GetSecret(ctx context.Context, ref ExternalSecretDataRemoteRef) ([]byte, error)
  60. // PushSecret will write a single secret into the provider
  61. PushSecret(ctx context.Context, secret *corev1.Secret, data PushSecretData) error
  62. // DeleteSecret will delete the secret from a provider
  63. DeleteSecret(ctx context.Context, remoteRef PushSecretRemoteRef) error
  64. // SecretExists checks if a secret is already present in the provider at the given location.
  65. SecretExists(ctx context.Context, remoteRef PushSecretRemoteRef) (bool, error)
  66. // Validate checks if the client is configured correctly
  67. // and is able to retrieve secrets from the provider.
  68. // If the validation result is unknown it will be ignored.
  69. Validate() (ValidationResult, error)
  70. // GetSecretMap returns multiple k/v pairs from the provider
  71. GetSecretMap(ctx context.Context, ref ExternalSecretDataRemoteRef) (map[string][]byte, error)
  72. // GetAllSecrets returns multiple k/v pairs from the provider
  73. GetAllSecrets(ctx context.Context, ref ExternalSecretFind) (map[string][]byte, error)
  74. Close(ctx context.Context) error
  75. }
  76. var NoSecretErr = NoSecretError{}
  77. // NoSecretError shall be returned when a GetSecret can not find the
  78. // desired secret. This is used for deletionPolicy.
  79. type NoSecretError struct{}
  80. func (NoSecretError) Error() string {
  81. return "Secret does not exist"
  82. }
  83. var NotModifiedErr = NotModifiedError{}
  84. // NotModifiedError to signal that the webhook received no changes,
  85. // and it should just return without doing anything.
  86. type NotModifiedError struct{}
  87. func (NotModifiedError) Error() string {
  88. return "not modified"
  89. }