controller.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. StoreRefKind: esv1.ClusterProviderKindStr,
  117. }
  118. // For ClusterProvider validation, we need to be more lenient since:
  119. // - ManifestNamespace scope: we don't have a manifest namespace at validation time
  120. // - ProviderNamespace scope: validation namespace may not have RBAC set up yet
  121. // So we skip validation and rely on runtime errors instead
  122. // This is acceptable since the provider will be validated when actually used by an ExternalSecret
  123. // Get provider capabilities (using empty namespace for ClusterProvider)
  124. caps, err := client.Capabilities(ctx, providerRef, "")
  125. if err != nil {
  126. r.Log.Error(err, "failed to get capabilities")
  127. // Don't fail validation if capabilities check fails, just log and default to ReadOnly
  128. return esv1.ProviderReadOnly, nil
  129. }
  130. // Map gRPC capabilities to our API type
  131. var capabilities esv1.ProviderCapabilities
  132. switch caps {
  133. case pb.SecretStoreCapabilities_READ_ONLY:
  134. capabilities = esv1.ProviderReadOnly
  135. case pb.SecretStoreCapabilities_WRITE_ONLY:
  136. capabilities = esv1.ProviderWriteOnly
  137. case pb.SecretStoreCapabilities_READ_WRITE:
  138. capabilities = esv1.ProviderReadWrite
  139. default:
  140. capabilities = esv1.ProviderReadOnly
  141. }
  142. return capabilities, nil
  143. }
  144. // setReadyCondition sets the Ready condition to True.
  145. func (r *Reconciler) setReadyCondition(store *esv1.ClusterProvider) {
  146. condition := esv1.ProviderCondition{
  147. Type: esv1.ProviderReady,
  148. Status: metav1.ConditionTrue,
  149. LastTransitionTime: metav1.Now(),
  150. Reason: "Validated",
  151. Message: "ClusterProvider is ready",
  152. }
  153. r.setCondition(store, condition)
  154. }
  155. // setNotReadyCondition sets the Ready condition to False.
  156. func (r *Reconciler) setNotReadyCondition(store *esv1.ClusterProvider, reason, message string) {
  157. condition := esv1.ProviderCondition{
  158. Type: esv1.ProviderReady,
  159. Status: metav1.ConditionFalse,
  160. LastTransitionTime: metav1.Now(),
  161. Reason: reason,
  162. Message: message,
  163. }
  164. r.setCondition(store, condition)
  165. }
  166. // setCondition updates or adds a condition to the ClusterProvider status.
  167. func (r *Reconciler) setCondition(store *esv1.ClusterProvider, newCondition esv1.ProviderCondition) {
  168. // Find existing condition
  169. for i, condition := range store.Status.Conditions {
  170. if condition.Type == newCondition.Type {
  171. // Preserve LastTransitionTime unless the condition status actually changes.
  172. if condition.Status == newCondition.Status {
  173. newCondition.LastTransitionTime = condition.LastTransitionTime
  174. }
  175. if condition.Status != newCondition.Status ||
  176. condition.Reason != newCondition.Reason ||
  177. condition.Message != newCondition.Message {
  178. store.Status.Conditions[i] = newCondition
  179. }
  180. // Update metrics
  181. UpdateStatusCondition(store, newCondition)
  182. return
  183. }
  184. }
  185. // Add new condition
  186. store.Status.Conditions = append(store.Status.Conditions, newCondition)
  187. // Update metrics
  188. UpdateStatusCondition(store, newCondition)
  189. }
  190. func findClusterProvidersForKubernetesProvider(ctx context.Context, kubeClient client.Client, obj client.Object) []ctrlreconcile.Request {
  191. kubernetesProvider, ok := obj.(*k8sv2alpha1.Kubernetes)
  192. if !ok {
  193. return nil
  194. }
  195. var providers esv1.ClusterProviderList
  196. if err := kubeClient.List(ctx, &providers); err != nil {
  197. return nil
  198. }
  199. requests := make([]ctrlreconcile.Request, 0, len(providers.Items))
  200. for i := range providers.Items {
  201. provider := providers.Items[i]
  202. ref := provider.Spec.Config.ProviderRef
  203. if ref.APIVersion != k8sv2alpha1.GroupVersion.String() ||
  204. ref.Kind != k8sv2alpha1.Kind ||
  205. ref.Name != kubernetesProvider.Name ||
  206. ref.Namespace != kubernetesProvider.Namespace {
  207. continue
  208. }
  209. requests = append(requests, ctrlreconcile.Request{
  210. NamespacedName: client.ObjectKeyFromObject(&provider),
  211. })
  212. }
  213. return requests
  214. }
  215. // SetupWithManager sets up the controller with the Manager.
  216. func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
  217. return ctrl.NewControllerManagedBy(mgr).
  218. WithOptions(opts).
  219. For(&esv1.ClusterProvider{}).
  220. Watches(
  221. &k8sv2alpha1.Kubernetes{},
  222. handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []ctrlreconcile.Request {
  223. return findClusterProvidersForKubernetesProvider(ctx, r.Client, obj)
  224. }),
  225. ).
  226. Owns(&corev1.Secret{}). // Watch secrets that might be used for auth
  227. Complete(r)
  228. }