|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/cloudnative-pg/machinery/pkg/log" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + |
| 13 | + v1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" |
| 14 | +) |
| 15 | + |
| 16 | +type cachedSecret struct { |
| 17 | + secret *corev1.Secret |
| 18 | + fetchUnixTime int64 |
| 19 | +} |
| 20 | + |
| 21 | +// ExtendedClient is an extended client that is capable of caching multiple secrets without relying on informers |
| 22 | +type ExtendedClient struct { |
| 23 | + client.Client |
| 24 | + barmanObjectKey client.ObjectKey |
| 25 | + cachedSecrets []*cachedSecret |
| 26 | + mux *sync.Mutex |
| 27 | + ttl int |
| 28 | +} |
| 29 | + |
| 30 | +// NewExtendedClient returns an extended client capable of caching secrets on the 'Get' operation |
| 31 | +func NewExtendedClient( |
| 32 | + baseClient client.Client, |
| 33 | + objectStoreKey client.ObjectKey, |
| 34 | +) client.Client { |
| 35 | + return &ExtendedClient{ |
| 36 | + Client: baseClient, |
| 37 | + barmanObjectKey: objectStoreKey, |
| 38 | + mux: &sync.Mutex{}, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (e *ExtendedClient) refreshTTL(ctx context.Context) error { |
| 43 | + var object v1.ObjectStore |
| 44 | + if err := e.Get(ctx, e.barmanObjectKey, &object); err != nil { |
| 45 | + return fmt.Errorf("failed to get the object store while refreshing the TTL parameter: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + e.ttl = object.Spec.InstanceSidecarConfiguration.GetCacheTTL() |
| 49 | + |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +// Get behaves like the original Get method, but uses a cache for secrets |
| 54 | +func (e *ExtendedClient) Get( |
| 55 | + ctx context.Context, |
| 56 | + key client.ObjectKey, |
| 57 | + obj client.Object, |
| 58 | + opts ...client.GetOption, |
| 59 | +) error { |
| 60 | + contextLogger := log.FromContext(ctx). |
| 61 | + WithName("extended_client"). |
| 62 | + WithValues("name", key.Name, "namespace", key.Namespace) |
| 63 | + |
| 64 | + if _, ok := obj.(*corev1.Secret); !ok { |
| 65 | + contextLogger.Trace("not a secret, skipping") |
| 66 | + return e.Client.Get(ctx, key, obj, opts...) |
| 67 | + } |
| 68 | + |
| 69 | + if err := e.refreshTTL(ctx); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + if e.isCacheDisabled() { |
| 74 | + contextLogger.Trace("cache is disabled") |
| 75 | + return e.Client.Get(ctx, key, obj, opts...) |
| 76 | + } |
| 77 | + |
| 78 | + contextLogger.Trace("locking the cache") |
| 79 | + e.mux.Lock() |
| 80 | + defer e.mux.Unlock() |
| 81 | + |
| 82 | + expiredSecretIndex := -1 |
| 83 | + // check if in cache |
| 84 | + for idx, cache := range e.cachedSecrets { |
| 85 | + if cache.secret.Namespace != key.Namespace || cache.secret.Name != key.Name { |
| 86 | + continue |
| 87 | + } |
| 88 | + if e.isExpired(cache.fetchUnixTime) { |
| 89 | + contextLogger.Trace("secret found, but it is expired") |
| 90 | + expiredSecretIndex = idx |
| 91 | + break |
| 92 | + } |
| 93 | + contextLogger.Debug("secret found, loading it from cache") |
| 94 | + cache.secret.DeepCopyInto(obj.(*corev1.Secret)) |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + if err := e.Client.Get(ctx, key, obj); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + cs := &cachedSecret{ |
| 103 | + secret: obj.(*corev1.Secret).DeepCopy(), |
| 104 | + fetchUnixTime: time.Now().Unix(), |
| 105 | + } |
| 106 | + |
| 107 | + contextLogger.Debug("setting secret in the cache") |
| 108 | + if expiredSecretIndex != -1 { |
| 109 | + e.cachedSecrets[expiredSecretIndex] = cs |
| 110 | + } else { |
| 111 | + e.cachedSecrets = append(e.cachedSecrets, cs) |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func (e *ExtendedClient) isExpired(unixTime int64) bool { |
| 118 | + return time.Now().Unix()-unixTime > int64(e.ttl) |
| 119 | +} |
| 120 | + |
| 121 | +func (e *ExtendedClient) isCacheDisabled() bool { |
| 122 | + const noCache = 0 |
| 123 | + return e.ttl == noCache |
| 124 | +} |
| 125 | + |
| 126 | +// RemoveSecret ensures that a secret is not present in the cache |
| 127 | +func (e *ExtendedClient) RemoveSecret(key client.ObjectKey) { |
| 128 | + if e.isCacheDisabled() { |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + e.mux.Lock() |
| 133 | + defer e.mux.Unlock() |
| 134 | + |
| 135 | + for i, cache := range e.cachedSecrets { |
| 136 | + if cache.secret.Namespace == key.Namespace && cache.secret.Name == key.Name { |
| 137 | + e.cachedSecrets = append(e.cachedSecrets[:i], e.cachedSecrets[i+1:]...) |
| 138 | + return |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// Update behaves like the original Update method, but on secrets it removes the secret from the cache |
| 144 | +func (e *ExtendedClient) Update( |
| 145 | + ctx context.Context, |
| 146 | + obj client.Object, |
| 147 | + opts ...client.UpdateOption, |
| 148 | +) error { |
| 149 | + if e.isCacheDisabled() { |
| 150 | + return e.Client.Update(ctx, obj, opts...) |
| 151 | + } |
| 152 | + |
| 153 | + if _, ok := obj.(*corev1.Secret); !ok { |
| 154 | + return e.Client.Update(ctx, obj, opts...) |
| 155 | + } |
| 156 | + |
| 157 | + e.RemoveSecret(client.ObjectKeyFromObject(obj)) |
| 158 | + |
| 159 | + return e.Client.Update(ctx, obj, opts...) |
| 160 | +} |
| 161 | + |
| 162 | +// Delete behaves like the original Delete method, but on secrets it removes the secret from the cache |
| 163 | +func (e *ExtendedClient) Delete( |
| 164 | + ctx context.Context, |
| 165 | + obj client.Object, |
| 166 | + opts ...client.DeleteOption, |
| 167 | +) error { |
| 168 | + if e.isCacheDisabled() { |
| 169 | + return e.Client.Delete(ctx, obj, opts...) |
| 170 | + } |
| 171 | + |
| 172 | + if _, ok := obj.(*corev1.Secret); !ok { |
| 173 | + return e.Client.Delete(ctx, obj, opts...) |
| 174 | + } |
| 175 | + |
| 176 | + e.RemoveSecret(client.ObjectKeyFromObject(obj)) |
| 177 | + |
| 178 | + return e.Client.Delete(ctx, obj, opts...) |
| 179 | +} |
| 180 | + |
| 181 | +// Patch behaves like the original Patch method, but on secrets it removes the secret from the cache |
| 182 | +func (e *ExtendedClient) Patch( |
| 183 | + ctx context.Context, |
| 184 | + obj client.Object, |
| 185 | + patch client.Patch, |
| 186 | + opts ...client.PatchOption, |
| 187 | +) error { |
| 188 | + if e.isCacheDisabled() { |
| 189 | + return e.Client.Patch(ctx, obj, patch, opts...) |
| 190 | + } |
| 191 | + |
| 192 | + if _, ok := obj.(*corev1.Secret); !ok { |
| 193 | + return e.Client.Patch(ctx, obj, patch, opts...) |
| 194 | + } |
| 195 | + |
| 196 | + e.RemoveSecret(client.ObjectKeyFromObject(obj)) |
| 197 | + |
| 198 | + return e.Client.Patch(ctx, obj, patch, opts...) |
| 199 | +} |
0 commit comments