@@ -17,29 +17,20 @@ limitations under the License.
17
17
package ionoscloud
18
18
19
19
import (
20
+ "maps"
20
21
"sync"
21
- "time"
22
22
23
23
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
24
24
"k8s.io/klog/v2"
25
25
)
26
26
27
- const nodeGroupCacheEntryTimeout = 2 * time .Minute
28
-
29
- type nodeGroupCacheEntry struct {
30
- data cloudprovider.NodeGroup
31
- ts time.Time
32
- }
33
-
34
- var timeNow = time .Now
35
-
36
27
// IonosCache caches resources to reduce API calls.
37
28
// Cached state includes autoscaling limits, sizes and target sizes, a mapping of instances to node
38
29
// groups, and a simple lock mechanism to prevent invalid node group writes.
39
30
type IonosCache struct {
40
31
mutex sync.Mutex
41
32
42
- nodeGroups map [string ]nodeGroupCacheEntry
33
+ nodeGroups map [string ]cloudprovider. NodeGroup
43
34
nodesToNodeGroups map [string ]string
44
35
nodeGroupSizes map [string ]int
45
36
nodeGroupTargetSizes map [string ]int
@@ -49,11 +40,11 @@ type IonosCache struct {
49
40
// NewIonosCache initializes a new IonosCache.
50
41
func NewIonosCache () * IonosCache {
51
42
return & IonosCache {
52
- nodeGroups : map [string ]nodeGroupCacheEntry {} ,
53
- nodesToNodeGroups : map [string ]string {} ,
54
- nodeGroupSizes : map [string ]int {} ,
55
- nodeGroupTargetSizes : map [string ]int {} ,
56
- nodeGroupLockTable : map [string ]bool {} ,
43
+ nodeGroups : make ( map [string ]cloudprovider. NodeGroup ) ,
44
+ nodesToNodeGroups : make ( map [string ]string ) ,
45
+ nodeGroupSizes : make ( map [string ]int ) ,
46
+ nodeGroupTargetSizes : make ( map [string ]int ) ,
47
+ nodeGroupLockTable : make ( map [string ]bool ) ,
57
48
}
58
49
}
59
50
@@ -62,15 +53,7 @@ func (cache *IonosCache) AddNodeGroup(newPool cloudprovider.NodeGroup) {
62
53
cache .mutex .Lock ()
63
54
defer cache .mutex .Unlock ()
64
55
65
- cache .nodeGroups [newPool .Id ()] = nodeGroupCacheEntry {data : newPool }
66
- }
67
-
68
- func (cache * IonosCache ) removeNodesForNodeGroupNoLock (id string ) {
69
- for nodeId , nodeGroupId := range cache .nodesToNodeGroups {
70
- if nodeGroupId == id {
71
- delete (cache .nodesToNodeGroups , nodeId )
72
- }
73
- }
56
+ cache .nodeGroups [newPool .Id ()] = newPool
74
57
}
75
58
76
59
// RemoveInstanceFromCache deletes an instance and its respective mapping to the node group from
@@ -79,38 +62,39 @@ func (cache *IonosCache) RemoveInstanceFromCache(id string) {
79
62
cache .mutex .Lock ()
80
63
defer cache .mutex .Unlock ()
81
64
82
- klog . V ( 5 ). Infof ( "Removed instance %s from cache" , id )
83
- nodeGroupId := cache .nodesToNodeGroups [ id ]
84
- delete ( cache . nodesToNodeGroups , id )
85
- cache . updateNodeGroupTimestampNoLock ( nodeGroupId )
65
+ if _ , ok := cache . nodesToNodeGroups [ id ]; ok {
66
+ delete ( cache .nodesToNodeGroups , id )
67
+ klog . V ( 5 ). Infof ( "Removed instance %s from cache" , id )
68
+ }
86
69
}
87
70
88
71
// SetInstancesCacheForNodeGroup overwrites cached instances and mappings for a node group.
89
72
func (cache * IonosCache ) SetInstancesCacheForNodeGroup (id string , instances []cloudprovider.Instance ) {
90
73
cache .mutex .Lock ()
91
74
defer cache .mutex .Unlock ()
92
75
93
- cache .removeNodesForNodeGroupNoLock (id )
76
+ maps .DeleteFunc (cache .nodesToNodeGroups , func (_ , nodeGroupID string ) bool {
77
+ return nodeGroupID == id
78
+ })
94
79
cache .setInstancesCacheForNodeGroupNoLock (id , instances )
95
80
}
96
81
97
82
func (cache * IonosCache ) setInstancesCacheForNodeGroupNoLock (id string , instances []cloudprovider.Instance ) {
98
83
for _ , instance := range instances {
99
- nodeId := convertToNodeId (instance .Id )
100
- cache .nodesToNodeGroups [nodeId ] = id
84
+ nodeID := convertToNodeID (instance .Id )
85
+ cache .nodesToNodeGroups [nodeID ] = id
101
86
}
102
- cache .updateNodeGroupTimestampNoLock (id )
103
87
}
104
88
105
- // GetNodeGroupIds returns an unsorted list of cached node group ids.
106
- func (cache * IonosCache ) GetNodeGroupIds () []string {
89
+ // GetNodeGroupIDs returns an unsorted list of cached node group ids.
90
+ func (cache * IonosCache ) GetNodeGroupIDs () []string {
107
91
cache .mutex .Lock ()
108
92
defer cache .mutex .Unlock ()
109
93
110
- return cache .getNodeGroupIds ()
94
+ return cache .getNodeGroupIDs ()
111
95
}
112
96
113
- func (cache * IonosCache ) getNodeGroupIds () []string {
97
+ func (cache * IonosCache ) getNodeGroupIDs () []string {
114
98
ids := make ([]string , 0 , len (cache .nodeGroups ))
115
99
for id := range cache .nodeGroups {
116
100
ids = append (ids , id )
@@ -125,26 +109,26 @@ func (cache *IonosCache) GetNodeGroups() []cloudprovider.NodeGroup {
125
109
126
110
nodeGroups := make ([]cloudprovider.NodeGroup , 0 , len (cache .nodeGroups ))
127
111
for id := range cache .nodeGroups {
128
- nodeGroups = append (nodeGroups , cache .nodeGroups [id ]. data )
112
+ nodeGroups = append (nodeGroups , cache .nodeGroups [id ])
129
113
}
130
114
return nodeGroups
131
115
}
132
116
133
117
// GetNodeGroupForNode returns the node group for the given node.
134
118
// Returns nil if either the mapping or the node group is not cached.
135
- func (cache * IonosCache ) GetNodeGroupForNode (nodeId string ) cloudprovider.NodeGroup {
119
+ func (cache * IonosCache ) GetNodeGroupForNode (nodeID string ) cloudprovider.NodeGroup {
136
120
cache .mutex .Lock ()
137
121
defer cache .mutex .Unlock ()
138
122
139
- id , found := cache .nodesToNodeGroups [nodeId ]
123
+ nodeGroupID , found := cache .nodesToNodeGroups [nodeID ]
140
124
if ! found {
141
125
return nil
142
126
}
143
- entry , found := cache .nodeGroups [id ]
127
+ nodeGroup , found := cache .nodeGroups [nodeGroupID ]
144
128
if ! found {
145
129
return nil
146
130
}
147
- return entry . data
131
+ return nodeGroup
148
132
}
149
133
150
134
// TryLockNodeGroup tries to write a node group lock entry.
@@ -219,19 +203,3 @@ func (cache *IonosCache) InvalidateNodeGroupTargetSize(id string) {
219
203
220
204
delete (cache .nodeGroupTargetSizes , id )
221
205
}
222
-
223
- // NodeGroupNeedsRefresh returns true when the instances for the given node group have not been
224
- // updated for more than 2 minutes.
225
- func (cache * IonosCache ) NodeGroupNeedsRefresh (id string ) bool {
226
- cache .mutex .Lock ()
227
- defer cache .mutex .Unlock ()
228
-
229
- return timeNow ().Sub (cache .nodeGroups [id ].ts ) > nodeGroupCacheEntryTimeout
230
- }
231
-
232
- func (cache * IonosCache ) updateNodeGroupTimestampNoLock (id string ) {
233
- if entry , ok := cache .nodeGroups [id ]; ok {
234
- entry .ts = timeNow ()
235
- cache .nodeGroups [id ] = entry
236
- }
237
- }
0 commit comments