Skip to content

Commit 992b5bb

Browse files
committed
replaced cache
1 parent be96b35 commit 992b5bb

File tree

4 files changed

+10
-27
lines changed

4 files changed

+10
-27
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/avast/retry-go/v3 v3.1.1
88
github.com/dustin/go-humanize v1.0.1
99
github.com/go-logr/logr v1.4.1
10-
github.com/jellydator/ttlcache/v3 v3.2.0
1110
github.com/kelseyhightower/envconfig v1.4.0
1211
github.com/nats-io/nats-server/v2 v2.10.11
1312
github.com/nats-io/nats.go v1.33.1

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,6 @@ github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
317317
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
318318
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
319319
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
320-
github.com/jellydator/ttlcache/v3 v3.2.0 h1:6lqVJ8X3ZaUwvzENqPAobDsXNExfUJd61u++uW8a3LE=
321-
github.com/jellydator/ttlcache/v3 v3.2.0/go.mod h1:hi7MGFdMAwZna5n2tuvh63DvFLzVKySzCVW6+0gA2n4=
322320
github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
323321
github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
324322
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=

pkg/k8s/client.go

+6-20
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"strings"
8-
"time"
98

10-
"github.com/jellydator/ttlcache/v3"
119
kappsv1 "k8s.io/api/apps/v1"
1210
kcorev1 "k8s.io/api/core/v1"
1311
kapiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -41,7 +39,6 @@ type Client interface {
4139
}
4240

4341
const (
44-
nodesZoneTTL = 10 * time.Hour
4542
nodeZoneLabelKey = "topology.kubernetes.io/zone"
4643
)
4744

@@ -51,21 +48,15 @@ type KubeClient struct {
5148
client client.Client
5249
clientset kapiextclientset.Interface
5350
fieldManager string
54-
nodesZoneCache *ttlcache.Cache[string, string]
51+
nodesZoneCache map[string]string
5552
}
5653

5754
func NewKubeClient(client client.Client, clientset kapiextclientset.Interface, fieldManager string) Client {
58-
// initialize the cache for the nodes zone information.
59-
nodesZoneCache := ttlcache.New[string, string](
60-
ttlcache.WithTTL[string, string](nodesZoneTTL),
61-
ttlcache.WithDisableTouchOnHit[string, string](),
62-
)
63-
6455
return &KubeClient{
6556
client: client,
6657
clientset: clientset,
6758
fieldManager: fieldManager,
68-
nodesZoneCache: nodesZoneCache,
59+
nodesZoneCache: make(map[string]string),
6960
}
7061
}
7162

@@ -163,15 +154,10 @@ func (c *KubeClient) GetNode(ctx context.Context, name string) (*kcorev1.Node, e
163154
// GetNodeZone returns the zone information of the node.
164155
// It caches the zone information of the node for a certain duration.
165156
func (c *KubeClient) GetNodeZone(ctx context.Context, name string) (string, error) {
166-
// delete expired entries from the cache.
167-
c.nodesZoneCache.DeleteExpired()
168-
169157
// check if the zone information is already in the cache.
170-
if c.nodesZoneCache.Has(name) {
171-
item := c.nodesZoneCache.Get(name)
172-
if item.Value() != "" {
173-
return item.Value(), nil
174-
}
158+
cacheValue, ok := c.nodesZoneCache[name]
159+
if ok && cacheValue != "" {
160+
return cacheValue, nil
175161
}
176162

177163
// get the node from kubernetes.
@@ -187,7 +173,7 @@ func (c *KubeClient) GetNodeZone(ctx context.Context, name string) (string, erro
187173
}
188174

189175
// set the zone information in the cache.
190-
c.nodesZoneCache.Set(name, zone, nodesZoneTTL)
176+
c.nodesZoneCache[name] = zone
191177

192178
// return the zone information.
193179
return zone, nil

pkg/k8s/client_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ func Test_GetNodeZone(t *testing.T) {
528528
if tc.givenExistsInCache {
529529
kcStruct, ok := kubeClient.(*KubeClient)
530530
require.True(t, ok)
531-
kcStruct.nodesZoneCache.Set(tc.givenNode.GetName(), givenLabels[nodeZoneLabelKey], nodesZoneTTL)
531+
kcStruct.nodesZoneCache[tc.givenNode.GetName()] = givenLabels[nodeZoneLabelKey]
532532
}
533533

534534
// when
@@ -547,9 +547,9 @@ func Test_GetNodeZone(t *testing.T) {
547547
// check cache entry.
548548
kcStruct, ok := kubeClient.(*KubeClient)
549549
require.True(t, ok)
550-
require.True(t, kcStruct.nodesZoneCache.Has(tc.givenNode.GetName()))
551-
item := kcStruct.nodesZoneCache.Get(tc.givenNode.GetName())
552-
require.Equal(t, tc.wantZone, item.Value())
550+
gotValue, ok := kcStruct.nodesZoneCache[tc.givenNode.GetName()]
551+
require.True(t, ok)
552+
require.Equal(t, tc.wantZone, gotValue)
553553
})
554554
}
555555
}

0 commit comments

Comments
 (0)