5
5
"errors"
6
6
"fmt"
7
7
"strings"
8
- "time"
9
8
10
- "github.com/jellydator/ttlcache/v3"
11
9
kappsv1 "k8s.io/api/apps/v1"
12
10
kcorev1 "k8s.io/api/core/v1"
13
11
kapiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -41,7 +39,6 @@ type Client interface {
41
39
}
42
40
43
41
const (
44
- nodesZoneTTL = 10 * time .Hour
45
42
nodeZoneLabelKey = "topology.kubernetes.io/zone"
46
43
)
47
44
@@ -51,21 +48,15 @@ type KubeClient struct {
51
48
client client.Client
52
49
clientset kapiextclientset.Interface
53
50
fieldManager string
54
- nodesZoneCache * ttlcache. Cache [string , string ]
51
+ nodesZoneCache map [string ] string
55
52
}
56
53
57
54
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
-
64
55
return & KubeClient {
65
56
client : client ,
66
57
clientset : clientset ,
67
58
fieldManager : fieldManager ,
68
- nodesZoneCache : nodesZoneCache ,
59
+ nodesZoneCache : make ( map [ string ] string ) ,
69
60
}
70
61
}
71
62
@@ -163,15 +154,10 @@ func (c *KubeClient) GetNode(ctx context.Context, name string) (*kcorev1.Node, e
163
154
// GetNodeZone returns the zone information of the node.
164
155
// It caches the zone information of the node for a certain duration.
165
156
func (c * KubeClient ) GetNodeZone (ctx context.Context , name string ) (string , error ) {
166
- // delete expired entries from the cache.
167
- c .nodesZoneCache .DeleteExpired ()
168
-
169
157
// 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
175
161
}
176
162
177
163
// get the node from kubernetes.
@@ -187,7 +173,7 @@ func (c *KubeClient) GetNodeZone(ctx context.Context, name string) (string, erro
187
173
}
188
174
189
175
// set the zone information in the cache.
190
- c .nodesZoneCache . Set ( name , zone , nodesZoneTTL )
176
+ c .nodesZoneCache [ name ] = zone
191
177
192
178
// return the zone information.
193
179
return zone , nil
0 commit comments