mirror of
https://github.com/cloudnative-pg/plugin-barman-cloud.git
synced 2026-01-13 14:13:10 +01:00
chore: make ttl configurable
Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
This commit is contained in:
parent
38f50aabfe
commit
5a73c567c2
@ -18,18 +18,28 @@ type cachedSecret struct {
|
|||||||
type ExtendedClient struct {
|
type ExtendedClient struct {
|
||||||
client.Client
|
client.Client
|
||||||
cachedSecrets []*cachedSecret
|
cachedSecrets []*cachedSecret
|
||||||
// add a mux to lock the operations on the cache
|
mux *sync.Mutex
|
||||||
mux *sync.Mutex
|
ttl int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewExtendedClient returns an extended client capable of caching secrets on the 'Get' operation
|
// NewExtendedClient returns an extended client capable of caching secrets on the 'Get' operation
|
||||||
func NewExtendedClient(baseClient client.Client) client.Client {
|
func NewExtendedClient(baseClient client.Client, ttl int64) client.Client {
|
||||||
return &ExtendedClient{
|
return &ExtendedClient{
|
||||||
Client: baseClient,
|
Client: baseClient,
|
||||||
|
ttl: ttl,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ExtendedClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
|
func (e *ExtendedClient) Get(
|
||||||
|
ctx context.Context,
|
||||||
|
key client.ObjectKey,
|
||||||
|
obj client.Object,
|
||||||
|
opts ...client.GetOption,
|
||||||
|
) error {
|
||||||
|
if e.isCacheDisabled() {
|
||||||
|
return e.Client.Get(ctx, key, obj, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
if _, ok := obj.(*corev1.Secret); !ok {
|
if _, ok := obj.(*corev1.Secret); !ok {
|
||||||
return e.Client.Get(ctx, key, obj, opts...)
|
return e.Client.Get(ctx, key, obj, opts...)
|
||||||
}
|
}
|
||||||
@ -40,7 +50,7 @@ func (e *ExtendedClient) Get(ctx context.Context, key client.ObjectKey, obj clie
|
|||||||
// check if in cache
|
// check if in cache
|
||||||
for _, cache := range e.cachedSecrets {
|
for _, cache := range e.cachedSecrets {
|
||||||
if cache.secret.Namespace == key.Namespace && cache.secret.Name == key.Name {
|
if cache.secret.Namespace == key.Namespace && cache.secret.Name == key.Name {
|
||||||
if time.Now().Unix()-cache.fetchUnixTime < 180 {
|
if !e.isExpired(cache.fetchUnixTime) {
|
||||||
cache.secret.DeepCopyInto(obj.(*corev1.Secret))
|
cache.secret.DeepCopyInto(obj.(*corev1.Secret))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -52,27 +62,41 @@ func (e *ExtendedClient) Get(ctx context.Context, key client.ObjectKey, obj clie
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
secret := obj.(*corev1.Secret)
|
||||||
|
|
||||||
// check if the secret is already in cache if so replace it
|
// check if the secret is already in cache if so replace it
|
||||||
for _, cache := range e.cachedSecrets {
|
for _, cache := range e.cachedSecrets {
|
||||||
if cache.secret.Namespace == key.Namespace && cache.secret.Name == key.Name {
|
if cache.secret.Namespace == key.Namespace && cache.secret.Name == key.Name {
|
||||||
cache.secret = obj.(*corev1.Secret)
|
cache.secret = secret.DeepCopy()
|
||||||
cache.fetchUnixTime = time.Now().Unix()
|
cache.fetchUnixTime = time.Now().Unix()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if secret, ok := obj.(*corev1.Secret); ok {
|
// otherwise add it to the cache
|
||||||
e.cachedSecrets = append(e.cachedSecrets, &cachedSecret{
|
e.cachedSecrets = append(e.cachedSecrets, &cachedSecret{
|
||||||
secret: secret,
|
secret: secret.DeepCopy(),
|
||||||
fetchUnixTime: time.Now().Unix(),
|
fetchUnixTime: time.Now().Unix(),
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *ExtendedClient) isExpired(unixTime int64) bool {
|
||||||
|
return time.Now().Unix()-unixTime > e.ttl
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ExtendedClient) isCacheDisabled() bool {
|
||||||
|
const noCache = 0
|
||||||
|
return e.ttl == noCache
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveSecret ensures that a secret is not present in the cache
|
// RemoveSecret ensures that a secret is not present in the cache
|
||||||
func (e *ExtendedClient) RemoveSecret(key client.ObjectKey) {
|
func (e *ExtendedClient) RemoveSecret(key client.ObjectKey) {
|
||||||
|
if e.isCacheDisabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
e.mux.Lock()
|
e.mux.Lock()
|
||||||
defer e.mux.Unlock()
|
defer e.mux.Unlock()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user