@@ -14,7 +14,8 @@ type cachedSecret struct {
1414 fetchUnixTime int64
1515}
1616
17- type extendedClient struct {
17+ // ExtendedClient is an extended client that is capable of caching multiple secrets without relying on 'list and watch'
18+ type ExtendedClient struct {
1819 client.Client
1920 cachedSecrets []* cachedSecret
2021 // add a mux to lock the operations on the cache
@@ -23,12 +24,12 @@ type extendedClient struct {
2324
2425// NewExtendedClient returns an extended client capable of caching secrets on the 'Get' operation
2526func NewExtendedClient (baseClient client.Client ) client.Client {
26- return & extendedClient {
27+ return & ExtendedClient {
2728 Client : baseClient ,
2829 }
2930}
3031
31- func (e * extendedClient ) Get (ctx context.Context , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
32+ func (e * ExtendedClient ) Get (ctx context.Context , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
3233 if _ , ok := obj .(* corev1.Secret ); ! ok {
3334 return e .Client .Get (ctx , key , obj , opts ... )
3435 }
@@ -69,3 +70,16 @@ func (e *extendedClient) Get(ctx context.Context, key client.ObjectKey, obj clie
6970
7071 return nil
7172}
73+
74+ // RemoveSecret ensures that a secret is not present in the cache
75+ func (e * ExtendedClient ) RemoveSecret (key client.ObjectKey ) {
76+ e .mux .Lock ()
77+ defer e .mux .Unlock ()
78+
79+ for i , cache := range e .cachedSecrets {
80+ if cache .secret .Namespace == key .Namespace && cache .secret .Name == key .Name {
81+ e .cachedSecrets = append (e .cachedSecrets [:i ], e .cachedSecrets [i + 1 :]... )
82+ return
83+ }
84+ }
85+ }
0 commit comments