@@ -18,18 +18,28 @@ type cachedSecret struct {
1818type ExtendedClient struct {
1919 client.Client
2020 cachedSecrets []* cachedSecret
21- // add a mux to lock the operations on the cache
22- mux * sync. Mutex
21+ mux * sync. Mutex
22+ ttl int64
2323}
2424
2525// NewExtendedClient returns an extended client capable of caching secrets on the 'Get' operation
26- func NewExtendedClient (baseClient client.Client ) client.Client {
26+ func NewExtendedClient (baseClient client.Client , ttl int64 ) client.Client {
2727 return & ExtendedClient {
2828 Client : baseClient ,
29+ ttl : ttl ,
2930 }
3031}
3132
32- func (e * ExtendedClient ) Get (ctx context.Context , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
33+ func (e * ExtendedClient ) Get (
34+ ctx context.Context ,
35+ key client.ObjectKey ,
36+ obj client.Object ,
37+ opts ... client.GetOption ,
38+ ) error {
39+ if e .isCacheDisabled () {
40+ return e .Client .Get (ctx , key , obj , opts ... )
41+ }
42+
3343 if _ , ok := obj .(* corev1.Secret ); ! ok {
3444 return e .Client .Get (ctx , key , obj , opts ... )
3545 }
@@ -40,7 +50,7 @@ func (e *ExtendedClient) Get(ctx context.Context, key client.ObjectKey, obj clie
4050 // check if in cache
4151 for _ , cache := range e .cachedSecrets {
4252 if cache .secret .Namespace == key .Namespace && cache .secret .Name == key .Name {
43- if time . Now (). Unix () - cache .fetchUnixTime < 180 {
53+ if ! e . isExpired ( cache .fetchUnixTime ) {
4454 cache .secret .DeepCopyInto (obj .(* corev1.Secret ))
4555 return nil
4656 }
@@ -52,27 +62,41 @@ func (e *ExtendedClient) Get(ctx context.Context, key client.ObjectKey, obj clie
5262 return err
5363 }
5464
65+ secret := obj .(* corev1.Secret )
66+
5567 // check if the secret is already in cache if so replace it
5668 for _ , cache := range e .cachedSecrets {
5769 if cache .secret .Namespace == key .Namespace && cache .secret .Name == key .Name {
58- cache .secret = obj .( * corev1. Secret )
70+ cache .secret = secret . DeepCopy ( )
5971 cache .fetchUnixTime = time .Now ().Unix ()
6072 return nil
6173 }
6274 }
6375
64- if secret , ok := obj .(* corev1.Secret ); ok {
65- e .cachedSecrets = append (e .cachedSecrets , & cachedSecret {
66- secret : secret ,
67- fetchUnixTime : time .Now ().Unix (),
68- })
69- }
76+ // otherwise add it to the cache
77+ e .cachedSecrets = append (e .cachedSecrets , & cachedSecret {
78+ secret : secret .DeepCopy (),
79+ fetchUnixTime : time .Now ().Unix (),
80+ })
7081
7182 return nil
7283}
7384
85+ func (e * ExtendedClient ) isExpired (unixTime int64 ) bool {
86+ return time .Now ().Unix ()- unixTime > e .ttl
87+ }
88+
89+ func (e * ExtendedClient ) isCacheDisabled () bool {
90+ const noCache = 0
91+ return e .ttl == noCache
92+ }
93+
7494// RemoveSecret ensures that a secret is not present in the cache
7595func (e * ExtendedClient ) RemoveSecret (key client.ObjectKey ) {
96+ if e .isCacheDisabled () {
97+ return
98+ }
99+
76100 e .mux .Lock ()
77101 defer e .mux .Unlock ()
78102
0 commit comments