cache.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 vault
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. "sync"
  18. lru "github.com/hashicorp/golang-lru"
  19. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  20. )
  21. type clientCache struct {
  22. cache *lru.Cache
  23. Size int
  24. initialized bool
  25. mu sync.Mutex
  26. }
  27. type clientCacheKey struct {
  28. Name string
  29. Namespace string
  30. Kind string
  31. }
  32. type clientCacheValue struct {
  33. ResourceVersion string
  34. Client Client
  35. }
  36. func (c *clientCache) initialize() error {
  37. if !c.initialized {
  38. var err error
  39. c.cache, err = lru.New(c.Size)
  40. if err != nil {
  41. return fmt.Errorf(errVaultCacheCreate, err)
  42. }
  43. c.initialized = true
  44. }
  45. return nil
  46. }
  47. func (c *clientCache) get(ctx context.Context, store esv1beta1.GenericStore, key clientCacheKey) (Client, bool, error) {
  48. value, ok := c.cache.Get(key)
  49. if ok {
  50. cachedClient := value.(clientCacheValue)
  51. if cachedClient.ResourceVersion == store.GetObjectMeta().ResourceVersion {
  52. return cachedClient.Client, true, nil
  53. }
  54. // revoke token and clear old item from cache if resource has been updated
  55. err := revokeTokenIfValid(ctx, cachedClient.Client)
  56. if err != nil {
  57. return nil, false, err
  58. }
  59. c.cache.Remove(key)
  60. }
  61. return nil, false, nil
  62. }
  63. func (c *clientCache) add(ctx context.Context, store esv1beta1.GenericStore, key clientCacheKey, client Client) error {
  64. // don't let the LRU cache evict items
  65. // remove the oldest item manually when needed so we can do some cleanup
  66. for c.cache.Len() >= c.Size {
  67. _, value, ok := c.cache.RemoveOldest()
  68. if !ok {
  69. return errors.New(errVaultCacheRemove)
  70. }
  71. cachedClient := value.(clientCacheValue)
  72. err := revokeTokenIfValid(ctx, cachedClient.Client)
  73. if err != nil {
  74. return fmt.Errorf(errVaultRevokeToken, err)
  75. }
  76. }
  77. evicted := c.cache.Add(key, clientCacheValue{ResourceVersion: store.GetObjectMeta().ResourceVersion, Client: client})
  78. if evicted {
  79. return errors.New(errVaultCacheEviction)
  80. }
  81. return nil
  82. }
  83. func (c *clientCache) contains(key clientCacheKey) bool {
  84. return c.cache.Contains(key)
  85. }
  86. func (c *clientCache) lock() {
  87. c.mu.Lock()
  88. }
  89. func (c *clientCache) unlock() {
  90. c.mu.Unlock()
  91. }