1
- package linode
1
+ package cache
2
2
3
3
import (
4
4
"context"
@@ -18,6 +18,8 @@ import (
18
18
"k8s.io/klog/v2"
19
19
20
20
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
21
+ "github.com/linode/linode-cloud-controller-manager/cloud/linode/options"
22
+ ccmUtils "github.com/linode/linode-cloud-controller-manager/cloud/linode/utils"
21
23
"github.com/linode/linode-cloud-controller-manager/sentry"
22
24
)
23
25
@@ -60,7 +62,7 @@ func (nc *nodeCache) getInstanceAddresses(instance linodego.Instance, vpcips []s
60
62
61
63
for _ , ip := range instance .IPv4 {
62
64
ipType := v1 .NodeExternalIP
63
- if isPrivate (ip ) {
65
+ if ccmUtils . IsPrivate (ip , options . Options . LinodeExternalNetwork ) {
64
66
ipType = v1 .NodeInternalIP
65
67
}
66
68
ips = append (ips , nodeIP {ip : ip .String (), ipType : ipType })
@@ -94,7 +96,7 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
94
96
// If running within VPC, find instances and store their ips
95
97
vpcNodes := map [int ][]string {}
96
98
vpcIPv6AddrTypes := map [string ]v1.NodeAddressType {}
97
- for _ , name := range Options .VPCNames {
99
+ for _ , name := range options . Options .VPCNames {
98
100
vpcName := strings .TrimSpace (name )
99
101
if vpcName == "" {
100
102
continue
@@ -134,7 +136,7 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
134
136
newNodes := make (map [int ]linodeInstance , len (instances ))
135
137
for index , instance := range instances {
136
138
// if running within VPC, only store instances in cache which are part of VPC
137
- if len (Options .VPCNames ) > 0 && len (vpcNodes [instance .ID ]) == 0 {
139
+ if len (options . Options .VPCNames ) > 0 && len (vpcNodes [instance .ID ]) == 0 {
138
140
continue
139
141
}
140
142
node := linodeInstance {
@@ -149,13 +151,14 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
149
151
return nil
150
152
}
151
153
152
- type instances struct {
154
+ type Instances struct {
153
155
client client.Client
154
156
155
157
nodeCache * nodeCache
156
158
}
157
159
158
- func newInstances (client client.Client ) * instances {
160
+ // NewInstances creates a new Instances cache with a specified TTL for the nodeCache.
161
+ func NewInstances (client client.Client ) * Instances {
159
162
timeout := 15
160
163
if raw , ok := os .LookupEnv ("LINODE_INSTANCE_CACHE_TTL" ); ok {
161
164
if t , err := strconv .Atoi (raw ); t > 0 && err == nil {
@@ -164,7 +167,7 @@ func newInstances(client client.Client) *instances {
164
167
}
165
168
klog .V (3 ).Infof ("TTL for nodeCache set to %d" , timeout )
166
169
167
- return & instances {client , & nodeCache {
170
+ return & Instances {client , & nodeCache {
168
171
nodes : make (map [int ]linodeInstance , 0 ),
169
172
ttl : time .Duration (timeout ) * time .Second ,
170
173
}}
@@ -178,7 +181,7 @@ func (e instanceNoIPAddressesError) Error() string {
178
181
return fmt .Sprintf ("instance %d has no IP addresses" , e .id )
179
182
}
180
183
181
- func (i * instances ) linodeByIP (kNode * v1.Node ) (* linodego.Instance , error ) {
184
+ func (i * Instances ) linodeByIP (kNode * v1.Node ) (* linodego.Instance , error ) {
182
185
i .nodeCache .RLock ()
183
186
defer i .nodeCache .RUnlock ()
184
187
var kNodeAddresses []string
@@ -192,7 +195,7 @@ func (i *instances) linodeByIP(kNode *v1.Node) (*linodego.Instance, error) {
192
195
}
193
196
for _ , node := range i .nodeCache .nodes {
194
197
for _ , nodeIP := range node .instance .IPv4 {
195
- if ! isPrivate (nodeIP ) && slices .Contains (kNodeAddresses , nodeIP .String ()) {
198
+ if ! ccmUtils . IsPrivate (nodeIP , options . Options . LinodeExternalNetwork ) && slices .Contains (kNodeAddresses , nodeIP .String ()) {
196
199
return node .instance , nil
197
200
}
198
201
}
@@ -201,7 +204,7 @@ func (i *instances) linodeByIP(kNode *v1.Node) (*linodego.Instance, error) {
201
204
return nil , cloudprovider .InstanceNotFound
202
205
}
203
206
204
- func (i * instances ) linodeByName (nodeName types.NodeName ) * linodego.Instance {
207
+ func (i * Instances ) linodeByName (nodeName types.NodeName ) * linodego.Instance {
205
208
i .nodeCache .RLock ()
206
209
defer i .nodeCache .RUnlock ()
207
210
for _ , node := range i .nodeCache .nodes {
@@ -213,7 +216,7 @@ func (i *instances) linodeByName(nodeName types.NodeName) *linodego.Instance {
213
216
return nil
214
217
}
215
218
216
- func (i * instances ) linodeByID (id int ) (* linodego.Instance , error ) {
219
+ func (i * Instances ) linodeByID (id int ) (* linodego.Instance , error ) {
217
220
i .nodeCache .RLock ()
218
221
defer i .nodeCache .RUnlock ()
219
222
linodeInstance , ok := i .nodeCache .nodes [id ]
@@ -223,8 +226,8 @@ func (i *instances) linodeByID(id int) (*linodego.Instance, error) {
223
226
return linodeInstance .instance , nil
224
227
}
225
228
226
- // listAllInstances returns all instances in nodeCache
227
- func (i * instances ) listAllInstances (ctx context.Context ) ([]linodego.Instance , error ) {
229
+ // ListAllInstances returns all instances in nodeCache
230
+ func (i * Instances ) ListAllInstances (ctx context.Context ) ([]linodego.Instance , error ) {
228
231
if err := i .nodeCache .refreshInstances (ctx , i .client ); err != nil {
229
232
return nil , err
230
233
}
@@ -236,7 +239,8 @@ func (i *instances) listAllInstances(ctx context.Context) ([]linodego.Instance,
236
239
return instances , nil
237
240
}
238
241
239
- func (i * instances ) lookupLinode (ctx context.Context , node * v1.Node ) (* linodego.Instance , error ) {
242
+ // LookupLinode looks up a Linode instance by its ProviderID or NodeName.
243
+ func (i * Instances ) LookupLinode (ctx context.Context , node * v1.Node ) (* linodego.Instance , error ) {
240
244
if err := i .nodeCache .refreshInstances (ctx , i .client ); err != nil {
241
245
return nil , err
242
246
}
@@ -247,8 +251,8 @@ func (i *instances) lookupLinode(ctx context.Context, node *v1.Node) (*linodego.
247
251
sentry .SetTag (ctx , "provider_id" , providerID )
248
252
sentry .SetTag (ctx , "node_name" , node .Name )
249
253
250
- if providerID != "" && isLinodeProviderID (providerID ) {
251
- id , err := parseProviderID (providerID )
254
+ if providerID != "" && ccmUtils . IsLinodeProviderID (providerID ) {
255
+ id , err := ccmUtils . ParseProviderID (providerID )
252
256
if err != nil {
253
257
sentry .CaptureError (ctx , err )
254
258
return nil , err
@@ -265,9 +269,9 @@ func (i *instances) lookupLinode(ctx context.Context, node *v1.Node) (*linodego.
265
269
return i .linodeByIP (node )
266
270
}
267
271
268
- func (i * instances ) InstanceExists (ctx context.Context , node * v1.Node ) (bool , error ) {
272
+ func (i * Instances ) InstanceExists (ctx context.Context , node * v1.Node ) (bool , error ) {
269
273
ctx = sentry .SetHubOnContext (ctx )
270
- if _ , err := i .lookupLinode (ctx , node ); err != nil {
274
+ if _ , err := i .LookupLinode (ctx , node ); err != nil {
271
275
if errors .Is (err , cloudprovider .InstanceNotFound ) {
272
276
return false , nil
273
277
}
@@ -278,9 +282,9 @@ func (i *instances) InstanceExists(ctx context.Context, node *v1.Node) (bool, er
278
282
return true , nil
279
283
}
280
284
281
- func (i * instances ) InstanceShutdown (ctx context.Context , node * v1.Node ) (bool , error ) {
285
+ func (i * Instances ) InstanceShutdown (ctx context.Context , node * v1.Node ) (bool , error ) {
282
286
ctx = sentry .SetHubOnContext (ctx )
283
- instance , err := i .lookupLinode (ctx , node )
287
+ instance , err := i .LookupLinode (ctx , node )
284
288
if err != nil {
285
289
sentry .CaptureError (ctx , err )
286
290
return false , err
@@ -296,9 +300,9 @@ func (i *instances) InstanceShutdown(ctx context.Context, node *v1.Node) (bool,
296
300
return false , nil
297
301
}
298
302
299
- func (i * instances ) InstanceMetadata (ctx context.Context , node * v1.Node ) (* cloudprovider.InstanceMetadata , error ) {
303
+ func (i * Instances ) InstanceMetadata (ctx context.Context , node * v1.Node ) (* cloudprovider.InstanceMetadata , error ) {
300
304
ctx = sentry .SetHubOnContext (ctx )
301
- linode , err := i .lookupLinode (ctx , node )
305
+ linode , err := i .LookupLinode (ctx , node )
302
306
if err != nil {
303
307
sentry .CaptureError (ctx , err )
304
308
return nil , err
@@ -344,7 +348,7 @@ func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
344
348
klog .Infof ("Instance %s, assembled IP addresses: %v" , node .Name , addresses )
345
349
// note that Zone is omitted as it's not a thing in Linode
346
350
meta := & cloudprovider.InstanceMetadata {
347
- ProviderID : fmt .Sprintf ("%v%v" , providerIDPrefix , linode .ID ),
351
+ ProviderID : fmt .Sprintf ("%v%v" , ccmUtils . ProviderIDPrefix , linode .ID ),
348
352
NodeAddresses : addresses ,
349
353
InstanceType : linode .Type ,
350
354
Region : linode .Region ,
@@ -353,9 +357,9 @@ func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
353
357
return meta , nil
354
358
}
355
359
356
- func (i * instances ) getLinodeAddresses (ctx context.Context , node * v1.Node ) ([]nodeIP , error ) {
360
+ func (i * Instances ) getLinodeAddresses (ctx context.Context , node * v1.Node ) ([]nodeIP , error ) {
357
361
ctx = sentry .SetHubOnContext (ctx )
358
- instance , err := i .lookupLinode (ctx , node )
362
+ instance , err := i .LookupLinode (ctx , node )
359
363
if err != nil {
360
364
sentry .CaptureError (ctx , err )
361
365
return nil , err
0 commit comments