This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
cached.go
69 lines (55 loc) · 2.04 KB
/
cached.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package tenantcluster
import (
"context"
"time"
"github.com/giantswarm/k8sclient/v7/pkg/k8sclient"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
gocache "github.com/patrickmn/go-cache"
capi "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-operator/v5/service/controller/key"
)
const (
expiration = 5 * time.Minute
)
type cachedTenantClientFactory struct {
cache *gocache.Cache
logger micrologger.Logger
factory Factory
}
func NewCachedFactory(tenantClientFactory Factory, logger micrologger.Logger) (Factory, error) {
if tenantClientFactory == nil {
return nil, microerror.Maskf(invalidConfigError, "tenantClientFactory must not be empty")
}
if logger == nil {
return nil, microerror.Maskf(invalidConfigError, "logger must not be empty")
}
return &cachedTenantClientFactory{
cache: gocache.New(expiration, expiration/2),
logger: logger,
factory: tenantClientFactory,
}, nil
}
func (ctcf *cachedTenantClientFactory) GetAllClients(ctx context.Context, cr *capi.Cluster) (k8sclient.Interface, error) {
ctcf.logger.Debugf(ctx, "trying to fetch tenant cluster %#q k8s client from cache before creating it", key.ClusterID(cr))
tenantClusterClient, inCache := ctcf.cache.Get(key.ClusterID(cr))
if inCache {
ctcf.logger.Debugf(ctx, "tenant cluster k8s client for cluster %#q found in cache", key.ClusterID(cr))
return tenantClusterClient.(k8sclient.Interface), nil
}
tenantClusterClient, err := ctcf.factory.GetAllClients(ctx, cr)
if err != nil {
return nil, microerror.Mask(err)
}
ctcf.cache.SetDefault(key.ClusterID(cr), tenantClusterClient)
ctcf.logger.Debugf(ctx, "saved tenant cluster k8s client for cluster %#q in cache", key.ClusterID(cr))
return tenantClusterClient.(k8sclient.Interface), nil
}
func (ctcf *cachedTenantClientFactory) GetClient(ctx context.Context, cr *capi.Cluster) (client.Client, error) {
all, err := ctcf.GetAllClients(ctx, cr)
if err != nil {
return nil, microerror.Mask(err)
}
return all.CtrlClient(), nil
}