@@ -9,72 +9,21 @@ import (
9
9
"time"
10
10
11
11
"github.com/linode/linodego"
12
- "golang.org/x/sync/errgroup"
13
12
v1 "k8s.io/api/core/v1"
14
13
"k8s.io/apimachinery/pkg/types"
15
14
cloudprovider "k8s.io/cloud-provider"
16
- "k8s.io/klog/v2"
17
15
18
16
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
19
17
"github.com/linode/linode-cloud-controller-manager/sentry"
20
18
)
21
19
22
- type nodeIP struct {
23
- ip string
24
- ipType v1.NodeAddressType
25
- }
26
-
27
- type linodeInstance struct {
28
- instance * linodego.Instance
29
- ips []nodeIP
30
- }
31
-
32
20
type nodeCache struct {
33
21
sync.RWMutex
34
- nodes map [int ]linodeInstance
22
+ nodes map [int ]* linodego. Instance
35
23
lastUpdate time.Time
36
24
ttl time.Duration
37
25
}
38
26
39
- // getInstanceIPv4Addresses returns all ipv4 addresses configured on a linode.
40
- func (nc * nodeCache ) getInstanceIPv4Addresses (ctx context.Context , id int , client client.Client ) ([]nodeIP , error ) {
41
- // Retrieve ipaddresses for the linode
42
- addresses , err := client .GetInstanceIPAddresses (ctx , id )
43
- if err != nil {
44
- return nil , err
45
- }
46
-
47
- var ips []nodeIP
48
- if len (addresses .IPv4 .Public ) != 0 {
49
- for _ , ip := range addresses .IPv4 .Public {
50
- ips = append (ips , nodeIP {ip : ip .Address , ipType : v1 .NodeExternalIP })
51
- }
52
- }
53
-
54
- // Retrieve instance configs for the linode
55
- configs , err := client .ListInstanceConfigs (ctx , id , & linodego.ListOptions {})
56
- if err != nil || len (configs ) == 0 {
57
- return nil , err
58
- }
59
-
60
- // Iterate over interfaces in config and find VPC specific ips
61
- for _ , iface := range configs [0 ].Interfaces {
62
- if iface .VPCID != nil && iface .IPv4 .VPC != "" {
63
- ips = append (ips , nodeIP {ip : iface .IPv4 .VPC , ipType : v1 .NodeInternalIP })
64
- }
65
- }
66
-
67
- // NOTE: We specifically store VPC ips first so that if they exist, they are
68
- // used as internal ip for the nodes than the private ip
69
- if len (addresses .IPv4 .Private ) != 0 {
70
- for _ , ip := range addresses .IPv4 .Private {
71
- ips = append (ips , nodeIP {ip : ip .Address , ipType : v1 .NodeInternalIP })
72
- }
73
- }
74
-
75
- return ips , nil
76
- }
77
-
78
27
// refreshInstances conditionally loads all instances from the Linode API and caches them.
79
28
// It does not refresh if the last update happened less than `nodeCache.ttl` ago.
80
29
func (nc * nodeCache ) refreshInstances (ctx context.Context , client client.Client ) error {
@@ -90,29 +39,11 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
90
39
return err
91
40
}
92
41
93
- nc .nodes = make (map [int ]linodeInstance , len ( instances ) )
42
+ nc .nodes = make (map [int ]* linodego. Instance )
94
43
95
- mtx := sync.Mutex {}
96
- g := new (errgroup.Group )
97
44
for _ , instance := range instances {
98
45
instance := instance
99
- g .Go (func () error {
100
- addresses , err := nc .getInstanceIPv4Addresses (ctx , instance .ID , client )
101
- if err != nil {
102
- klog .Errorf ("Failed fetching ip addresses for instance id %d. Error: %s" , instance .ID , err .Error ())
103
- return err
104
- }
105
- // take lock on map so that concurrent writes are safe
106
- mtx .Lock ()
107
- defer mtx .Unlock ()
108
- node := linodeInstance {instance : & instance , ips : addresses }
109
- nc .nodes [instance .ID ] = node
110
- return nil
111
- })
112
- }
113
-
114
- if err := g .Wait (); err != nil {
115
- return err
46
+ nc .nodes [instance .ID ] = & instance
116
47
}
117
48
118
49
nc .lastUpdate = time .Now ()
@@ -133,10 +64,9 @@ func newInstances(client client.Client) *instances {
133
64
timeout = t
134
65
}
135
66
}
136
- klog .V (3 ).Infof ("TTL for nodeCache set to %d" , timeout )
137
67
138
68
return & instances {client , & nodeCache {
139
- nodes : make (map [int ]linodeInstance , 0 ),
69
+ nodes : make (map [int ]* linodego. Instance ),
140
70
ttl : time .Duration (timeout ) * time .Second ,
141
71
}}
142
72
}
@@ -153,8 +83,8 @@ func (i *instances) linodeByName(nodeName types.NodeName) (*linodego.Instance, e
153
83
i .nodeCache .RLock ()
154
84
defer i .nodeCache .RUnlock ()
155
85
for _ , node := range i .nodeCache .nodes {
156
- if node .instance . Label == string (nodeName ) {
157
- return node . instance , nil
86
+ if node .Label == string (nodeName ) {
87
+ return node , nil
158
88
}
159
89
}
160
90
@@ -164,24 +94,11 @@ func (i *instances) linodeByName(nodeName types.NodeName) (*linodego.Instance, e
164
94
func (i * instances ) linodeByID (id int ) (* linodego.Instance , error ) {
165
95
i .nodeCache .RLock ()
166
96
defer i .nodeCache .RUnlock ()
167
- linodeInstance , ok := i .nodeCache .nodes [id ]
97
+ instance , ok := i .nodeCache .nodes [id ]
168
98
if ! ok {
169
99
return nil , cloudprovider .InstanceNotFound
170
100
}
171
- return linodeInstance .instance , nil
172
- }
173
-
174
- // listAllInstances returns all instances in nodeCache
175
- func (i * instances ) listAllInstances (ctx context.Context ) ([]linodego.Instance , error ) {
176
- if err := i .nodeCache .refreshInstances (ctx , i .client ); err != nil {
177
- return nil , err
178
- }
179
-
180
- instances := []linodego.Instance {}
181
- for _ , linodeInstance := range i .nodeCache .nodes {
182
- instances = append (instances , * linodeInstance .instance )
183
- }
184
- return instances , nil
101
+ return instance , nil
185
102
}
186
103
187
104
func (i * instances ) lookupLinode (ctx context.Context , node * v1.Node ) (* linodego.Instance , error ) {
@@ -248,22 +165,20 @@ func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
248
165
return nil , err
249
166
}
250
167
251
- ips , err := i .getLinodeIPv4Addresses (ctx , node )
252
- if err != nil {
253
- sentry .CaptureError (ctx , err )
254
- return nil , err
255
- }
256
-
257
- if len (ips ) == 0 {
168
+ if len (linode .IPv4 ) == 0 {
258
169
err := instanceNoIPAddressesError {linode .ID }
259
170
sentry .CaptureError (ctx , err )
260
171
return nil , err
261
172
}
262
173
263
174
addresses := []v1.NodeAddress {{Type : v1 .NodeHostName , Address : linode .Label }}
264
175
265
- for _ , ip := range ips {
266
- addresses = append (addresses , v1.NodeAddress {Type : ip .ipType , Address : ip .ip })
176
+ for _ , ip := range linode .IPv4 {
177
+ ipType := v1 .NodeExternalIP
178
+ if ip .IsPrivate () {
179
+ ipType = v1 .NodeInternalIP
180
+ }
181
+ addresses = append (addresses , v1.NodeAddress {Type : ipType , Address : ip .String ()})
267
182
}
268
183
269
184
// note that Zone is omitted as it's not a thing in Linode
@@ -276,23 +191,3 @@ func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
276
191
277
192
return meta , nil
278
193
}
279
-
280
- func (i * instances ) getLinodeIPv4Addresses (ctx context.Context , node * v1.Node ) ([]nodeIP , error ) {
281
- ctx = sentry .SetHubOnContext (ctx )
282
- instance , err := i .lookupLinode (ctx , node )
283
- if err != nil {
284
- sentry .CaptureError (ctx , err )
285
- return nil , err
286
- }
287
-
288
- i .nodeCache .RLock ()
289
- defer i .nodeCache .RUnlock ()
290
- linodeInstance , ok := i .nodeCache .nodes [instance .ID ]
291
- if ! ok || len (linodeInstance .ips ) == 0 {
292
- err := instanceNoIPAddressesError {instance .ID }
293
- sentry .CaptureError (ctx , err )
294
- return nil , err
295
- }
296
-
297
- return linodeInstance .ips , nil
298
- }
0 commit comments