controller.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 provider implements the controller for Provider resources.
  13. package provider
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "github.com/go-logr/logr"
  19. corev1 "k8s.io/api/core/v1"
  20. apierrors "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. ctrl "sigs.k8s.io/controller-runtime"
  24. "sigs.k8s.io/controller-runtime/pkg/client"
  25. "sigs.k8s.io/controller-runtime/pkg/controller"
  26. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  27. pb "github.com/external-secrets/external-secrets/proto/provider"
  28. "github.com/external-secrets/external-secrets/providers/v2/common/grpc"
  29. )
  30. // Reconciler reconciles a Provider object.
  31. type Reconciler struct {
  32. client.Client
  33. Log logr.Logger
  34. Scheme *runtime.Scheme
  35. RequeueInterval time.Duration
  36. }
  37. // Reconcile validates the Provider and updates its status.
  38. func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  39. log := r.Log.WithValues("Provider", req.NamespacedName)
  40. start := time.Now()
  41. defer func() {
  42. duration := time.Since(start)
  43. if gaugeVec := GetGaugeVec(ProviderReconcileDurationKey); gaugeVec != nil {
  44. gaugeVec.WithLabelValues(req.Name, req.Namespace).Set(duration.Seconds())
  45. }
  46. }()
  47. log.Info("reconciling Provider")
  48. var store esv1.Provider
  49. if err := r.Get(ctx, req.NamespacedName, &store); err != nil {
  50. if apierrors.IsNotFound(err) {
  51. RemoveMetrics(req.Namespace, req.Name)
  52. return ctrl.Result{}, nil
  53. }
  54. log.Error(err, "unable to get Provider")
  55. return ctrl.Result{}, err
  56. }
  57. // Validate provider config and get capabilities
  58. capabilities, err := r.validateStoreAndGetCapabilities(ctx, &store)
  59. if err != nil {
  60. log.Error(err, "validation failed")
  61. r.setNotReadyCondition(&store, "ValidationFailed", err.Error())
  62. if updateErr := r.Status().Update(ctx, &store); updateErr != nil {
  63. log.Error(updateErr, "failed to update status")
  64. return ctrl.Result{}, updateErr
  65. }
  66. // Requeue after interval to retry
  67. return ctrl.Result{RequeueAfter: r.RequeueInterval}, nil
  68. }
  69. // Set ready condition and capabilities
  70. r.setReadyCondition(&store)
  71. store.Status.Capabilities = capabilities
  72. if err := r.Status().Update(ctx, &store); err != nil {
  73. log.Error(err, "failed to update status")
  74. return ctrl.Result{}, err
  75. }
  76. log.Info("Provider is ready", "capabilities", capabilities)
  77. return ctrl.Result{RequeueAfter: r.RequeueInterval}, nil
  78. }
  79. // validateStoreAndGetCapabilities validates the Provider configuration and retrieves capabilities by:
  80. // 1. Creating a gRPC client to the provider
  81. // 2. Calling Validate() on the provider with the ProviderReference
  82. // 3. Calling Capabilities() to get the provider's capabilities.
  83. func (r *Reconciler) validateStoreAndGetCapabilities(ctx context.Context, store *esv1.Provider) (esv1.ProviderCapabilities, error) {
  84. // Get provider address
  85. address := store.Spec.Config.Address
  86. if address == "" {
  87. return "", fmt.Errorf("provider address is required")
  88. }
  89. tlsSecretNamespace := grpc.NamespaceFromAddress(store.Spec.Config.Address, store.Namespace)
  90. // Load TLS configuration
  91. tlsConfig, err := grpc.LoadClientTLSConfig(ctx, r.Client, store.Spec.Config.Address, tlsSecretNamespace)
  92. if err != nil {
  93. return "", fmt.Errorf("failed to load TLS config: %w", err)
  94. }
  95. // Create gRPC client with TLS
  96. client, err := grpc.NewClient(address, tlsConfig)
  97. if err != nil {
  98. return "", fmt.Errorf("failed to create gRPC client: %w", err)
  99. }
  100. defer func() { _ = client.Close(ctx) }()
  101. // Convert ProviderReference to protobuf format
  102. providerRef := &pb.ProviderReference{
  103. ApiVersion: store.Spec.Config.ProviderRef.APIVersion,
  104. Kind: store.Spec.Config.ProviderRef.Kind,
  105. Name: store.Spec.Config.ProviderRef.Name,
  106. Namespace: store.Spec.Config.ProviderRef.Namespace,
  107. }
  108. // Validate the provider configuration
  109. if err := client.Validate(ctx, providerRef, store.Namespace); err != nil {
  110. r.Log.Error(err, "provider validation failed")
  111. return "", fmt.Errorf("provider validation failed: %w", err)
  112. }
  113. // Get provider capabilities
  114. caps, err := client.Capabilities(ctx, providerRef, store.Namespace)
  115. if err != nil {
  116. r.Log.Error(err, "failed to get capabilities")
  117. // Don't fail validation if capabilities check fails, just log and default to ReadOnly
  118. return esv1.ProviderReadOnly, nil
  119. }
  120. // Map gRPC capabilities to our API type
  121. var capabilities esv1.ProviderCapabilities
  122. switch caps {
  123. case pb.SecretStoreCapabilities_READ_ONLY:
  124. capabilities = esv1.ProviderReadOnly
  125. case pb.SecretStoreCapabilities_WRITE_ONLY:
  126. capabilities = esv1.ProviderWriteOnly
  127. case pb.SecretStoreCapabilities_READ_WRITE:
  128. capabilities = esv1.ProviderReadWrite
  129. default:
  130. capabilities = esv1.ProviderReadOnly
  131. }
  132. return capabilities, nil
  133. }
  134. // setReadyCondition sets the Ready condition to True.
  135. func (r *Reconciler) setReadyCondition(store *esv1.Provider) {
  136. condition := esv1.ProviderCondition{
  137. Type: esv1.ProviderReady,
  138. Status: metav1.ConditionTrue,
  139. LastTransitionTime: metav1.Now(),
  140. Reason: "Validated",
  141. Message: "Provider is ready",
  142. }
  143. r.setCondition(store, condition)
  144. }
  145. // setNotReadyCondition sets the Ready condition to False.
  146. func (r *Reconciler) setNotReadyCondition(store *esv1.Provider, reason, message string) {
  147. condition := esv1.ProviderCondition{
  148. Type: esv1.ProviderReady,
  149. Status: metav1.ConditionFalse,
  150. LastTransitionTime: metav1.Now(),
  151. Reason: reason,
  152. Message: message,
  153. }
  154. r.setCondition(store, condition)
  155. }
  156. // setCondition updates or adds a condition to the Provider status.
  157. func (r *Reconciler) setCondition(store *esv1.Provider, newCondition esv1.ProviderCondition) {
  158. // Find existing condition
  159. for i, condition := range store.Status.Conditions {
  160. if condition.Type == newCondition.Type {
  161. // Only update if status changed
  162. if condition.Status != newCondition.Status {
  163. store.Status.Conditions[i] = newCondition
  164. }
  165. // Update metrics
  166. UpdateStatusCondition(store, newCondition)
  167. return
  168. }
  169. }
  170. // Add new condition
  171. store.Status.Conditions = append(store.Status.Conditions, newCondition)
  172. // Update metrics
  173. UpdateStatusCondition(store, newCondition)
  174. }
  175. // SetupWithManager sets up the controller with the Manager.
  176. func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
  177. return ctrl.NewControllerManagedBy(mgr).
  178. WithOptions(opts).
  179. For(&esv1.Provider{}).
  180. Owns(&corev1.Secret{}). // Watch secrets that might be used for auth
  181. Complete(r)
  182. }