manager.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. Copyright © The ESO Authors
  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 clientmanager provides a Manager for provider clients
  14. package clientmanager
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "regexp"
  20. "strings"
  21. "sync"
  22. "github.com/go-logr/logr"
  23. v1 "k8s.io/api/core/v1"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/labels"
  26. "k8s.io/apimachinery/pkg/types"
  27. ctrl "sigs.k8s.io/controller-runtime"
  28. "sigs.k8s.io/controller-runtime/pkg/client"
  29. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  30. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  31. adapterstore "github.com/external-secrets/external-secrets/providers/v2/adapter/store"
  32. "github.com/external-secrets/external-secrets/providers/v2/common/grpc"
  33. )
  34. const (
  35. errGetClusterSecretStore = "could not get ClusterSecretStore %q, %w"
  36. errGetSecretStore = "could not get SecretStore %q, %w"
  37. errSecretStoreNotReady = "%s %q is not ready"
  38. errClusterStoreMismatch = "using cluster store %q is not allowed from namespace %q: denied by spec.condition"
  39. errClusterProviderStoreDenied = "using ClusterProviderStore %q is not allowed from namespace %q: denied by spec.conditions"
  40. providerMetricsLabel = "provider"
  41. clusterProviderMetricsLabel = "cluster-provider"
  42. cacheInvalidationGeneration = "generation_change"
  43. cacheInvalidationMismatch = "store_mismatch"
  44. v2ProviderStoreCacheKey = "v2-provider-store"
  45. v2ClusterProviderStoreCache = "v2-cluster-provider-store"
  46. runtimeRefCacheKeyType = "runtime-ref"
  47. )
  48. var (
  49. // globalV2ConnectionPool is a singleton connection pool for v2 gRPC providers.
  50. // It persists across all reconciles and Manager instances to enable connection reuse.
  51. // Initialized once on first use and shared globally.
  52. globalV2ConnectionPool *grpc.ConnectionPool
  53. globalV2ConnectionPoolOnce sync.Once
  54. globalV2ConnectionPoolLog logr.Logger
  55. )
  56. // initGlobalV2ConnectionPool initializes the global connection pool for v2 providers.
  57. // This is called once on first use via sync.Once.
  58. func initGlobalV2ConnectionPool() {
  59. globalV2ConnectionPoolLog = ctrl.Log.WithName("v2-connection-pool")
  60. poolConfig := grpc.DefaultPoolConfig()
  61. globalV2ConnectionPool = grpc.NewConnectionPool(poolConfig)
  62. globalV2ConnectionPoolLog.Info("global v2 connection pool initialized",
  63. "maxIdleTime", poolConfig.MaxIdleTime.String(),
  64. "maxLifetime", poolConfig.MaxLifetime.String(),
  65. "healthCheckInterval", poolConfig.HealthCheckInterval.String())
  66. }
  67. // getGlobalV2ConnectionPool returns the global connection pool, initializing it if needed.
  68. func getGlobalV2ConnectionPool() *grpc.ConnectionPool {
  69. globalV2ConnectionPoolOnce.Do(initGlobalV2ConnectionPool)
  70. return globalV2ConnectionPool
  71. }
  72. // v2PooledConnection tracks connection info needed to release connections back to the pool.
  73. type v2PooledConnection struct {
  74. address string
  75. tlsConfig *grpc.TLSConfig
  76. }
  77. // Manager stores instances of provider clients
  78. // At any given time we must have no more than one instance
  79. // of a client (due to limitations in GCP / see mutexlock there)
  80. // If the controller requests another instance of a given client
  81. // we will close the old client first and then construct a new one.
  82. type Manager struct {
  83. log logr.Logger
  84. client client.Client
  85. controllerClass string
  86. enableFloodgate bool
  87. // store clients by provider type
  88. clientMap map[clientKey]*clientVal
  89. // Track v2 provider connections for release back to pool
  90. v2PooledConnections []v2PooledConnection
  91. }
  92. type clientKey struct {
  93. providerType string
  94. // For v2 providers, store the provider name and namespace
  95. v2ProviderName string
  96. v2ProviderNamespace string
  97. runtimeSourceNamespace string
  98. }
  99. type clientVal struct {
  100. client esv1.SecretsClient
  101. store esv1.GenericStore
  102. // For v2 providers, store the generation for cache invalidation
  103. v2ProviderGeneration int64
  104. }
  105. func providerMetricsLabelForScope(isClusterScoped bool) string {
  106. if isClusterScoped {
  107. return clusterProviderMetricsLabel
  108. }
  109. return providerMetricsLabel
  110. }
  111. func providerMetricsLabelForKey(key clientKey) string {
  112. if key.v2ProviderName == "" {
  113. return "unknown"
  114. }
  115. if key.v2ProviderNamespace == "" {
  116. return clusterProviderMetricsLabel
  117. }
  118. return providerMetricsLabel
  119. }
  120. // NewManager constructs a new manager with defaults.
  121. func NewManager(ctrlClient client.Client, controllerClass string, enableFloodgate bool) *Manager {
  122. log := ctrl.Log.WithName("clientmanager")
  123. return &Manager{
  124. log: log,
  125. client: ctrlClient,
  126. controllerClass: controllerClass,
  127. enableFloodgate: enableFloodgate,
  128. clientMap: make(map[clientKey]*clientVal),
  129. }
  130. }
  131. // GetFromStore returns a provider client from the given store.
  132. // Do not close the client returned from this func, instead close
  133. // the manager once you're done with reconciling the external secret.
  134. func (m *Manager) GetFromStore(ctx context.Context, store esv1.GenericStore, namespace string) (esv1.SecretsClient, error) {
  135. if store.GetSpec().RuntimeRef != nil {
  136. return m.getRuntimeRefClient(ctx, store, namespace)
  137. }
  138. storeProvider, err := esv1.GetProvider(store)
  139. if err != nil {
  140. return nil, err
  141. }
  142. secretClient := m.getStoredClient(ctx, storeProvider, store)
  143. if secretClient != nil {
  144. return secretClient, nil
  145. }
  146. m.log.V(1).Info("creating new client",
  147. "provider", fmt.Sprintf("%T", storeProvider),
  148. "store", fmt.Sprintf("%s/%s", store.GetNamespace(), store.GetName()))
  149. // secret client is created only if we are going to refresh
  150. // this skip an unnecessary check/request in the case we are not going to do anything
  151. secretClient, err = storeProvider.NewClient(ctx, store, m.client, namespace)
  152. if err != nil {
  153. return nil, err
  154. }
  155. idx := storeKey(storeProvider)
  156. m.clientMap[idx] = &clientVal{
  157. client: secretClient,
  158. store: store,
  159. }
  160. return secretClient, nil
  161. }
  162. func (m *Manager) getRuntimeRefClient(ctx context.Context, store esv1.GenericStore, namespace string) (esv1.SecretsClient, error) {
  163. runtimeRef := store.GetSpec().RuntimeRef
  164. runtimeKind := runtimeRef.Kind
  165. if runtimeKind == "" {
  166. runtimeKind = runtimeRefKindClusterProviderClass
  167. }
  168. if runtimeKind != runtimeRefKindClusterProviderClass {
  169. return nil, fmt.Errorf("unsupported runtimeRef kind %q", runtimeKind)
  170. }
  171. cacheKey := runtimeRefStoreKey(store, namespace)
  172. if cached := m.getStoredRuntimeRefClient(ctx, cacheKey, store); cached != nil {
  173. return cached, nil
  174. }
  175. var runtimeClass esv1alpha1.ClusterProviderClass
  176. if err := m.client.Get(ctx, types.NamespacedName{Name: runtimeRef.Name}, &runtimeClass); err != nil {
  177. return nil, fmt.Errorf("failed to get %s %q: %w", runtimeRefKindClusterProviderClass, runtimeRef.Name, err)
  178. }
  179. compatStore, err := buildCompatibilityStore(store)
  180. if err != nil {
  181. return nil, fmt.Errorf("failed to build compatibility store for %s %q: %w", store.GetKind(), store.GetName(), err)
  182. }
  183. tlsSecretNamespace := grpc.ResolveTLSSecretNamespace(runtimeClass.Spec.Address, "", "", "")
  184. tlsConfig, err := grpc.LoadClientTLSConfig(ctx, m.client, runtimeClass.Spec.Address, tlsSecretNamespace)
  185. if err != nil {
  186. return nil, fmt.Errorf("failed to load TLS config for %s %q: %w", runtimeRefKindClusterProviderClass, runtimeRef.Name, err)
  187. }
  188. pool := getGlobalV2ConnectionPool()
  189. grpcClient, err := pool.Get(ctx, runtimeClass.Spec.Address, tlsConfig)
  190. if err != nil {
  191. return nil, fmt.Errorf("failed to get gRPC client from pool for %s %q: %w", runtimeRefKindClusterProviderClass, runtimeRef.Name, err)
  192. }
  193. compatibilityClient := adapterstore.NewCompatibilityClientWithCloser(grpcClient, compatStore, namespace, func(context.Context) error {
  194. pool.Release(runtimeClass.Spec.Address, tlsConfig)
  195. return nil
  196. })
  197. m.clientMap[cacheKey] = &clientVal{
  198. client: compatibilityClient,
  199. store: store,
  200. }
  201. return compatibilityClient, nil
  202. }
  203. func runtimeRefStoreKey(store esv1.GenericStore, sourceNamespace string) clientKey {
  204. return clientKey{
  205. providerType: runtimeRefCacheKeyType + ":" + store.GetKind(),
  206. v2ProviderName: store.GetName(),
  207. v2ProviderNamespace: store.GetNamespace(),
  208. runtimeSourceNamespace: sourceNamespace,
  209. }
  210. }
  211. func (m *Manager) getStoredRuntimeRefClient(ctx context.Context, key clientKey, store esv1.GenericStore) esv1.SecretsClient {
  212. val, ok := m.clientMap[key]
  213. if !ok {
  214. return nil
  215. }
  216. valGVK, err := m.client.GroupVersionKindFor(val.store)
  217. if err != nil {
  218. return nil
  219. }
  220. storeGVK, err := m.client.GroupVersionKindFor(store)
  221. if err != nil {
  222. return nil
  223. }
  224. if val.store.GetObjectMeta().Generation == store.GetGeneration() &&
  225. valGVK == storeGVK &&
  226. val.store.GetName() == store.GetName() &&
  227. val.store.GetNamespace() == store.GetNamespace() {
  228. clientManagerMetrics.RecordCacheHit(providerMetricsLabelForKey(key))
  229. return val.client
  230. }
  231. _ = val.client.Close(ctx)
  232. delete(m.clientMap, key)
  233. reason := cacheInvalidationMismatch
  234. if val.store.GetObjectMeta().Generation != store.GetGeneration() {
  235. reason = cacheInvalidationGeneration
  236. }
  237. clientManagerMetrics.RecordCacheInvalidation(providerMetricsLabelForKey(key), reason)
  238. return nil
  239. }
  240. // Get returns a provider client from the given storeRef or sourceRef.secretStoreRef
  241. // while sourceRef.SecretStoreRef takes precedence over storeRef.
  242. // Do not close the client returned from this func, instead close
  243. // the manager once you're done with recinciling the external secret.
  244. func (m *Manager) Get(ctx context.Context, storeRef esv1.SecretStoreRef, namespace string, sourceRef *esv1.StoreGeneratorSourceRef) (esv1.SecretsClient, error) {
  245. if sourceRef != nil && sourceRef.SecretStoreRef != nil {
  246. storeRef = *sourceRef.SecretStoreRef
  247. }
  248. if storeRef.Kind == esv1.ProviderStoreKindStr {
  249. return m.getV2ProviderStoreClient(ctx, storeRef.Name, namespace)
  250. }
  251. if storeRef.Kind == esv1.ClusterProviderStoreKindStr {
  252. return m.getV2ClusterProviderStoreClient(ctx, storeRef.Name, namespace)
  253. }
  254. store, err := m.getStore(ctx, &storeRef, namespace)
  255. if err != nil {
  256. return nil, err
  257. }
  258. // check if store should be handled by this controller instance
  259. if !ShouldProcessStore(store, m.controllerClass) {
  260. return nil, errors.New("can not reference unmanaged store")
  261. }
  262. // when using ClusterSecretStore, validate the ClusterSecretStore namespace conditions
  263. shouldProcess, err := m.shouldProcessSecret(store, namespace)
  264. if err != nil {
  265. return nil, err
  266. }
  267. if !shouldProcess {
  268. return nil, fmt.Errorf(errClusterStoreMismatch, store.GetName(), namespace)
  269. }
  270. if m.enableFloodgate {
  271. err := assertStoreIsUsable(store)
  272. if err != nil {
  273. return nil, err
  274. }
  275. }
  276. return m.GetFromStore(ctx, store, namespace)
  277. }
  278. // returns a previously stored client from the cache if store and store-version match
  279. // if a client exists for the same provider which points to a different store or store version
  280. // it will be cleaned up.
  281. func (m *Manager) getStoredClient(ctx context.Context, storeProvider esv1.ProviderInterface, store esv1.GenericStore) esv1.SecretsClient {
  282. idx := storeKey(storeProvider)
  283. val, ok := m.clientMap[idx]
  284. if !ok {
  285. return nil
  286. }
  287. valGVK, err := m.client.GroupVersionKindFor(val.store)
  288. if err != nil {
  289. return nil
  290. }
  291. storeGVK, err := m.client.GroupVersionKindFor(store)
  292. if err != nil {
  293. return nil
  294. }
  295. storeName := fmt.Sprintf("%s/%s", store.GetNamespace(), store.GetName())
  296. // return client if it points to the very same store
  297. if val.store.GetObjectMeta().Generation == store.GetGeneration() &&
  298. valGVK == storeGVK &&
  299. val.store.GetName() == store.GetName() &&
  300. val.store.GetNamespace() == store.GetNamespace() {
  301. m.log.V(1).Info("reusing stored client",
  302. "provider", fmt.Sprintf("%T", storeProvider),
  303. "store", storeName)
  304. // Record cache hit
  305. clientManagerMetrics.RecordCacheHit(providerMetricsLabelForKey(idx))
  306. return val.client
  307. }
  308. m.log.V(1).Info("cleaning up client",
  309. "provider", fmt.Sprintf("%T", storeProvider),
  310. "store", storeName)
  311. // if we have a client, but it points to a different store
  312. // we must clean it up
  313. _ = val.client.Close(ctx)
  314. delete(m.clientMap, idx)
  315. // Record cache invalidation
  316. providerType := providerMetricsLabelForKey(idx)
  317. reason := cacheInvalidationMismatch
  318. if idx.v2ProviderName != "" {
  319. if val.store.GetObjectMeta().Generation != store.GetGeneration() {
  320. reason = cacheInvalidationGeneration
  321. }
  322. }
  323. clientManagerMetrics.RecordCacheInvalidation(providerType, reason)
  324. return nil
  325. }
  326. func storeKey(storeProvider esv1.ProviderInterface) clientKey {
  327. return clientKey{
  328. providerType: fmt.Sprintf("%T", storeProvider),
  329. }
  330. }
  331. // getStore fetches the (Cluster)SecretStore from the kube-apiserver
  332. // and returns a GenericStore representing it.
  333. func (m *Manager) getStore(ctx context.Context, storeRef *esv1.SecretStoreRef, namespace string) (esv1.GenericStore, error) {
  334. ref := types.NamespacedName{
  335. Name: storeRef.Name,
  336. }
  337. if storeRef.Kind == esv1.ClusterSecretStoreKind {
  338. var store esv1.ClusterSecretStore
  339. err := m.client.Get(ctx, ref, &store)
  340. if err != nil {
  341. return nil, fmt.Errorf(errGetClusterSecretStore, ref.Name, err)
  342. }
  343. return &store, nil
  344. }
  345. ref.Namespace = namespace
  346. var store esv1.SecretStore
  347. err := m.client.Get(ctx, ref, &store)
  348. if err != nil {
  349. return nil, fmt.Errorf(errGetSecretStore, ref.Name, err)
  350. }
  351. return &store, nil
  352. }
  353. // Close cleans up all clients.
  354. // For v1 providers, it closes the clients directly.
  355. // For v2 providers, it releases connections back to the pool for reuse.
  356. func (m *Manager) Close(ctx context.Context) error {
  357. var errs []string
  358. // Release v2 pooled connections back to the pool
  359. pool := getGlobalV2ConnectionPool()
  360. for _, pooledConn := range m.v2PooledConnections {
  361. pool.Release(pooledConn.address, pooledConn.tlsConfig)
  362. m.log.V(1).Info("released v2 connection back to pool",
  363. "address", pooledConn.address)
  364. }
  365. m.v2PooledConnections = nil
  366. // Close v1 provider clients (they don't use the pool)
  367. for key, val := range m.clientMap {
  368. // Only close v1 clients; v2 clients are managed by the pool
  369. if key.providerType != v2ProviderStoreCacheKey &&
  370. key.providerType != v2ClusterProviderStoreCache {
  371. err := val.client.Close(ctx)
  372. if err != nil {
  373. errs = append(errs, err.Error())
  374. }
  375. }
  376. delete(m.clientMap, key)
  377. }
  378. if len(errs) != 0 {
  379. return fmt.Errorf("errors while closing clients: %s", strings.Join(errs, ", "))
  380. }
  381. return nil
  382. }
  383. // validateNamespaceConditions checks if a namespace matches the given conditions.
  384. // Returns true if the namespace is allowed, false if denied.
  385. func (m *Manager) validateNamespaceConditions(conditions []esv1.ClusterSecretStoreCondition, ns string) (bool, error) {
  386. if len(conditions) == 0 {
  387. return true, nil
  388. }
  389. namespace := v1.Namespace{}
  390. if err := m.client.Get(context.Background(), client.ObjectKey{Name: ns}, &namespace); err != nil {
  391. return false, fmt.Errorf("failed to get a namespace %q: %w", ns, err)
  392. }
  393. nsLabels := labels.Set(namespace.GetLabels())
  394. for _, condition := range conditions {
  395. var labelSelectors []*metav1.LabelSelector
  396. if condition.NamespaceSelector != nil {
  397. labelSelectors = append(labelSelectors, condition.NamespaceSelector)
  398. }
  399. for _, n := range condition.Namespaces {
  400. labelSelectors = append(labelSelectors, &metav1.LabelSelector{
  401. MatchLabels: map[string]string{
  402. "kubernetes.io/metadata.name": n,
  403. },
  404. })
  405. }
  406. for _, ls := range labelSelectors {
  407. selector, err := metav1.LabelSelectorAsSelector(ls)
  408. if err != nil {
  409. return false, fmt.Errorf("failed to convert label selector into selector %v: %w", ls, err)
  410. }
  411. if selector.Matches(nsLabels) {
  412. return true, nil
  413. }
  414. }
  415. for _, reg := range condition.NamespaceRegexes {
  416. match, err := regexp.MatchString(reg, ns)
  417. if err != nil {
  418. // Should not happen since store validation already verified the regexes.
  419. return false, fmt.Errorf("failed to compile regex %v: %w", reg, err)
  420. }
  421. if match {
  422. return true, nil
  423. }
  424. }
  425. }
  426. return false, nil
  427. }
  428. // shouldProcessSecret validates if a secret should be processed based on namespace conditions.
  429. // This is a wrapper around validateNamespaceConditions for backward compatibility with GenericStore.
  430. func (m *Manager) shouldProcessSecret(store esv1.GenericStore, ns string) (bool, error) {
  431. // Only check conditions for cluster-scoped resources.
  432. if store.GetKind() != esv1.ClusterSecretStoreKind {
  433. return true, nil
  434. }
  435. return m.validateNamespaceConditions(store.GetSpec().Conditions, ns)
  436. }
  437. // assertStoreIsUsable asserts that the store is ready to use.
  438. func assertStoreIsUsable(store esv1.GenericStore) error {
  439. if store == nil {
  440. return nil
  441. }
  442. condition := GetSecretStoreCondition(store.GetStatus(), esv1.SecretStoreReady)
  443. if condition == nil || condition.Status != v1.ConditionTrue {
  444. return fmt.Errorf(errSecretStoreNotReady, store.GetKind(), store.GetName())
  445. }
  446. return nil
  447. }
  448. // ShouldProcessStore returns true if the store should be processed.
  449. func ShouldProcessStore(store esv1.GenericStore, class string) bool {
  450. if store == nil || store.GetSpec().Controller == "" || store.GetSpec().Controller == class {
  451. return true
  452. }
  453. return false
  454. }
  455. // GetSecretStoreCondition returns the condition with the provided type.
  456. func GetSecretStoreCondition(status esv1.SecretStoreStatus, condType esv1.SecretStoreConditionType) *esv1.SecretStoreStatusCondition {
  457. for i := range status.Conditions {
  458. c := status.Conditions[i]
  459. if c.Type == condType {
  460. return &c
  461. }
  462. }
  463. return nil
  464. }