controller.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 clusterprovider implements the controller for ClusterProvider resources.
  13. package clusterprovider
  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. "sigs.k8s.io/controller-runtime/pkg/handler"
  27. ctrlreconcile "sigs.k8s.io/controller-runtime/pkg/reconcile"
  28. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  29. k8sv2alpha1 "github.com/external-secrets/external-secrets/apis/provider/kubernetes/v2alpha1"
  30. pb "github.com/external-secrets/external-secrets/proto/provider"
  31. "github.com/external-secrets/external-secrets/providers/v2/common/grpc"
  32. )
  33. // Reconciler reconciles a ClusterProvider object.
  34. type Reconciler struct {
  35. client.Client
  36. Log logr.Logger
  37. Scheme *runtime.Scheme
  38. RequeueInterval time.Duration
  39. }
  40. // Reconcile validates the ClusterProvider and updates its status.
  41. func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  42. log := r.Log.WithValues("ClusterProvider", req.NamespacedName)
  43. start := time.Now()
  44. defer func() {
  45. duration := time.Since(start)
  46. if gaugeVec := GetGaugeVec(ClusterProviderReconcileDurationKey); gaugeVec != nil {
  47. gaugeVec.WithLabelValues(req.Name).Set(duration.Seconds())
  48. }
  49. }()
  50. log.Info("reconciling ClusterProvider")
  51. var store esv1.ClusterProvider
  52. if err := r.Get(ctx, req.NamespacedName, &store); err != nil {
  53. if apierrors.IsNotFound(err) {
  54. RemoveMetrics(req.Name)
  55. return ctrl.Result{}, nil
  56. }
  57. log.Error(err, "unable to get ClusterProvider")
  58. return ctrl.Result{}, err
  59. }
  60. // Validate provider config and get capabilities
  61. capabilities, err := r.validateStoreAndGetCapabilities(ctx, &store)
  62. if err != nil {
  63. log.Error(err, "validation failed")
  64. r.setNotReadyCondition(&store, "ValidationFailed", err.Error())
  65. store.Status.Capabilities = ""
  66. if updateErr := r.Status().Update(ctx, &store); updateErr != nil {
  67. log.Error(updateErr, "failed to update status")
  68. return ctrl.Result{}, updateErr
  69. }
  70. // Requeue after interval to retry
  71. return ctrl.Result{RequeueAfter: r.RequeueInterval}, nil
  72. }
  73. // Set ready condition and capabilities
  74. r.setReadyCondition(&store)
  75. store.Status.Capabilities = capabilities
  76. if err := r.Status().Update(ctx, &store); err != nil {
  77. log.Error(err, "failed to update status")
  78. return ctrl.Result{}, err
  79. }
  80. log.Info("ClusterProvider is ready", "capabilities", capabilities)
  81. return ctrl.Result{RequeueAfter: r.RequeueInterval}, nil
  82. }
  83. // validateStoreAndGetCapabilities validates the ClusterProvider configuration and retrieves capabilities by:
  84. // 1. Creating a gRPC client to the provider
  85. // 2. Calling Validate() on the provider with the ProviderReference
  86. // 3. Calling Capabilities() to get the provider's capabilities.
  87. func (r *Reconciler) validateStoreAndGetCapabilities(ctx context.Context, store *esv1.ClusterProvider) (esv1.ProviderCapabilities, error) {
  88. // Get provider address
  89. address := store.Spec.Config.Address
  90. if address == "" {
  91. return "", fmt.Errorf("provider address is required")
  92. }
  93. tlsSecretNamespace := grpc.ResolveTLSSecretNamespace(
  94. store.Spec.Config.Address,
  95. "",
  96. "",
  97. store.Spec.Config.ProviderRef.Namespace,
  98. )
  99. // Load TLS configuration
  100. tlsConfig, err := grpc.LoadClientTLSConfig(ctx, r.Client, store.Spec.Config.Address, tlsSecretNamespace)
  101. if err != nil {
  102. return "", fmt.Errorf("failed to load TLS config: %w", err)
  103. }
  104. // Create gRPC client with TLS
  105. client, err := grpc.NewClient(address, tlsConfig)
  106. if err != nil {
  107. return "", fmt.Errorf("failed to create gRPC client: %w", err)
  108. }
  109. defer func() { _ = client.Close(ctx) }()
  110. // Convert ProviderReference to protobuf format
  111. providerRef := &pb.ProviderReference{
  112. ApiVersion: store.Spec.Config.ProviderRef.APIVersion,
  113. Kind: store.Spec.Config.ProviderRef.Kind,
  114. Name: store.Spec.Config.ProviderRef.Name,
  115. Namespace: store.Spec.Config.ProviderRef.Namespace,
  116. }
  117. // For ClusterProvider validation, we need to be more lenient since:
  118. // - ManifestNamespace scope: we don't have a manifest namespace at validation time
  119. // - ProviderNamespace scope: validation namespace may not have RBAC set up yet
  120. // So we skip validation and rely on runtime errors instead
  121. // This is acceptable since the provider will be validated when actually used by an ExternalSecret
  122. // Get provider capabilities (using empty namespace for ClusterProvider)
  123. caps, err := client.Capabilities(ctx, providerRef, "")
  124. if err != nil {
  125. r.Log.Error(err, "failed to get capabilities")
  126. // Don't fail validation if capabilities check fails, just log and default to ReadOnly
  127. return esv1.ProviderReadOnly, nil
  128. }
  129. // Map gRPC capabilities to our API type
  130. var capabilities esv1.ProviderCapabilities
  131. switch caps {
  132. case pb.SecretStoreCapabilities_READ_ONLY:
  133. capabilities = esv1.ProviderReadOnly
  134. case pb.SecretStoreCapabilities_WRITE_ONLY:
  135. capabilities = esv1.ProviderWriteOnly
  136. case pb.SecretStoreCapabilities_READ_WRITE:
  137. capabilities = esv1.ProviderReadWrite
  138. default:
  139. capabilities = esv1.ProviderReadOnly
  140. }
  141. return capabilities, nil
  142. }
  143. // setReadyCondition sets the Ready condition to True.
  144. func (r *Reconciler) setReadyCondition(store *esv1.ClusterProvider) {
  145. condition := esv1.ProviderCondition{
  146. Type: esv1.ProviderReady,
  147. Status: metav1.ConditionTrue,
  148. LastTransitionTime: metav1.Now(),
  149. Reason: "Validated",
  150. Message: "ClusterProvider is ready",
  151. }
  152. r.setCondition(store, condition)
  153. }
  154. // setNotReadyCondition sets the Ready condition to False.
  155. func (r *Reconciler) setNotReadyCondition(store *esv1.ClusterProvider, reason, message string) {
  156. condition := esv1.ProviderCondition{
  157. Type: esv1.ProviderReady,
  158. Status: metav1.ConditionFalse,
  159. LastTransitionTime: metav1.Now(),
  160. Reason: reason,
  161. Message: message,
  162. }
  163. r.setCondition(store, condition)
  164. }
  165. // setCondition updates or adds a condition to the ClusterProvider status.
  166. func (r *Reconciler) setCondition(store *esv1.ClusterProvider, newCondition esv1.ProviderCondition) {
  167. // Find existing condition
  168. for i, condition := range store.Status.Conditions {
  169. if condition.Type == newCondition.Type {
  170. // Preserve LastTransitionTime unless the condition status actually changes.
  171. if condition.Status == newCondition.Status {
  172. newCondition.LastTransitionTime = condition.LastTransitionTime
  173. }
  174. if condition.Status != newCondition.Status ||
  175. condition.Reason != newCondition.Reason ||
  176. condition.Message != newCondition.Message {
  177. store.Status.Conditions[i] = newCondition
  178. }
  179. // Update metrics
  180. UpdateStatusCondition(store, newCondition)
  181. return
  182. }
  183. }
  184. // Add new condition
  185. store.Status.Conditions = append(store.Status.Conditions, newCondition)
  186. // Update metrics
  187. UpdateStatusCondition(store, newCondition)
  188. }
  189. func findClusterProvidersForKubernetesProvider(ctx context.Context, kubeClient client.Client, obj client.Object) []ctrlreconcile.Request {
  190. kubernetesProvider, ok := obj.(*k8sv2alpha1.Kubernetes)
  191. if !ok {
  192. return nil
  193. }
  194. var providers esv1.ClusterProviderList
  195. if err := kubeClient.List(ctx, &providers); err != nil {
  196. return nil
  197. }
  198. requests := make([]ctrlreconcile.Request, 0, len(providers.Items))
  199. for i := range providers.Items {
  200. provider := providers.Items[i]
  201. ref := provider.Spec.Config.ProviderRef
  202. if ref.APIVersion != k8sv2alpha1.GroupVersion.String() ||
  203. ref.Kind != k8sv2alpha1.Kind ||
  204. ref.Name != kubernetesProvider.Name ||
  205. ref.Namespace != kubernetesProvider.Namespace {
  206. continue
  207. }
  208. requests = append(requests, ctrlreconcile.Request{
  209. NamespacedName: client.ObjectKeyFromObject(&provider),
  210. })
  211. }
  212. return requests
  213. }
  214. // SetupWithManager sets up the controller with the Manager.
  215. func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
  216. return ctrl.NewControllerManagedBy(mgr).
  217. WithOptions(opts).
  218. For(&esv1.ClusterProvider{}).
  219. Watches(
  220. &k8sv2alpha1.Kubernetes{},
  221. handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []ctrlreconcile.Request {
  222. return findClusterProvidersForKubernetesProvider(ctx, r.Client, obj)
  223. }),
  224. ).
  225. Owns(&corev1.Secret{}). // Watch secrets that might be used for auth
  226. Complete(r)
  227. }