|
| 1 | +package cloudmap |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/model" |
| 7 | + "github.com/go-logr/logr" |
| 8 | + "k8s.io/apimachinery/pkg/util/cache" |
| 9 | + ctrl "sigs.k8s.io/controller-runtime" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + nsKeyPrefix = "ns" |
| 15 | + svcKeyPrefix = "svc" |
| 16 | + endptKeyPrefix = "endpt" |
| 17 | + |
| 18 | + defaultCacheSize = 1024 |
| 19 | + defaultNsTTL = 2 * time.Minute |
| 20 | + defaultSvcTTL = 2 * time.Minute |
| 21 | + defaultEndptTTL = 5 * time.Second |
| 22 | +) |
| 23 | + |
| 24 | +type ServiceDiscoveryClientCache interface { |
| 25 | + GetNamespace(namespaceName string) (namespace *model.Namespace, found bool) |
| 26 | + CacheNamespace(namespace *model.Namespace) |
| 27 | + CacheNilNamespace(namespaceName string) |
| 28 | + GetServiceId(namespaceName string, serviceName string) (serviceId string, found bool) |
| 29 | + CacheServiceId(namespaceName string, serviceName string, serviceId string) |
| 30 | + GetEndpoints(serviceId string) (endpoints []*model.Endpoint, found bool) |
| 31 | + CacheEndpoints(serviceId string, endpoints []*model.Endpoint) |
| 32 | + EvictEndpoints(serviceId string) |
| 33 | +} |
| 34 | + |
| 35 | +type sdCache struct { |
| 36 | + log logr.Logger |
| 37 | + cache *cache.LRUExpireCache |
| 38 | + config sdCacheConfig |
| 39 | +} |
| 40 | + |
| 41 | +type sdCacheConfig struct { |
| 42 | + nsTTL time.Duration |
| 43 | + svcTTL time.Duration |
| 44 | + endptTTL time.Duration |
| 45 | +} |
| 46 | + |
| 47 | +func NewDefaultServiceDiscoveryClientCache() ServiceDiscoveryClientCache { |
| 48 | + return &sdCache{ |
| 49 | + log: ctrl.Log.WithName("cloudmap"), |
| 50 | + cache: cache.NewLRUExpireCache(defaultCacheSize), |
| 51 | + config: sdCacheConfig{ |
| 52 | + nsTTL: defaultNsTTL, |
| 53 | + svcTTL: defaultSvcTTL, |
| 54 | + endptTTL: defaultEndptTTL, |
| 55 | + }} |
| 56 | +} |
| 57 | + |
| 58 | +func (sdCache *sdCache) GetNamespace(nsName string) (ns *model.Namespace, found bool) { |
| 59 | + key := sdCache.buildNsKey(nsName) |
| 60 | + entry, exists := sdCache.cache.Get(key) |
| 61 | + if !exists { |
| 62 | + return nil, false |
| 63 | + } |
| 64 | + |
| 65 | + if entry == nil { |
| 66 | + return nil, true |
| 67 | + } |
| 68 | + |
| 69 | + nsEntry, ok := entry.(model.Namespace) |
| 70 | + if !ok { |
| 71 | + sdCache.log.Error(errors.New("failed to retrieve namespace from cache"), "", "nsName", nsName) |
| 72 | + sdCache.cache.Remove(key) |
| 73 | + return nil, false |
| 74 | + } |
| 75 | + |
| 76 | + return &nsEntry, true |
| 77 | +} |
| 78 | + |
| 79 | +func (sdCache *sdCache) CacheNamespace(namespace *model.Namespace) { |
| 80 | + key := sdCache.buildNsKey(namespace.Name) |
| 81 | + sdCache.cache.Add(key, *namespace, sdCache.config.nsTTL) |
| 82 | +} |
| 83 | + |
| 84 | +func (sdCache *sdCache) CacheNilNamespace(nsName string) { |
| 85 | + key := sdCache.buildNsKey(nsName) |
| 86 | + sdCache.cache.Add(key, nil, sdCache.config.nsTTL) |
| 87 | +} |
| 88 | + |
| 89 | +func (sdCache *sdCache) GetServiceId(nsName string, svcName string) (svcId string, found bool) { |
| 90 | + key := sdCache.buildSvcKey(nsName, svcName) |
| 91 | + entry, exists := sdCache.cache.Get(key) |
| 92 | + if !exists { |
| 93 | + return "", false |
| 94 | + } |
| 95 | + |
| 96 | + svcId, ok := entry.(string) |
| 97 | + if !ok { |
| 98 | + sdCache.log.Error(errors.New("failed to retrieve service ID from cache"), "", |
| 99 | + "nsName", nsName, "svcName", svcName) |
| 100 | + sdCache.cache.Remove(key) |
| 101 | + return "", false |
| 102 | + } |
| 103 | + |
| 104 | + return svcId, true |
| 105 | +} |
| 106 | + |
| 107 | +func (sdCache *sdCache) CacheServiceId(nsName string, svcName string, svcId string) { |
| 108 | + key := sdCache.buildSvcKey(nsName, svcName) |
| 109 | + sdCache.cache.Add(key, svcId, sdCache.config.svcTTL) |
| 110 | +} |
| 111 | + |
| 112 | +func (sdCache *sdCache) GetEndpoints(svcId string) (endpts []*model.Endpoint, found bool) { |
| 113 | + key := sdCache.buildEndptsKey(svcId) |
| 114 | + entry, exists := sdCache.cache.Get(key) |
| 115 | + if !exists { |
| 116 | + return nil, false |
| 117 | + } |
| 118 | + |
| 119 | + endpts, ok := entry.([]*model.Endpoint) |
| 120 | + if !ok { |
| 121 | + sdCache.log.Error(errors.New("failed to retrieve endpoints from cache"), "", "svcId", svcId) |
| 122 | + sdCache.cache.Remove(key) |
| 123 | + return nil, false |
| 124 | + } |
| 125 | + |
| 126 | + return endpts, true |
| 127 | +} |
| 128 | + |
| 129 | +func (sdCache *sdCache) CacheEndpoints(svcId string, endpts []*model.Endpoint) { |
| 130 | + key := sdCache.buildEndptsKey(svcId) |
| 131 | + sdCache.cache.Add(key, endpts, sdCache.config.endptTTL) |
| 132 | +} |
| 133 | + |
| 134 | +func (sdCache *sdCache) EvictEndpoints(svcId string) { |
| 135 | + key := sdCache.buildEndptsKey(svcId) |
| 136 | + sdCache.cache.Remove(key) |
| 137 | +} |
| 138 | + |
| 139 | +func (sdCache *sdCache) buildNsKey(nsName string) (cacheKey string) { |
| 140 | + return fmt.Sprintf("%s:%s", nsKeyPrefix, nsName) |
| 141 | +} |
| 142 | + |
| 143 | +func (sdCache *sdCache) buildSvcKey(nsName string, svcName string) (cacheKey string) { |
| 144 | + return fmt.Sprintf("%s:%s:%s", svcKeyPrefix, nsName, svcName) |
| 145 | +} |
| 146 | + |
| 147 | +func (sdCache *sdCache) buildEndptsKey(svcId string) string { |
| 148 | + return fmt.Sprintf("%s:%s", endptKeyPrefix, svcId) |
| 149 | +} |
0 commit comments