@@ -9,72 +9,21 @@ import (
99 "time"
1010
1111 "github.com/linode/linodego"
12- "golang.org/x/sync/errgroup"
1312 v1 "k8s.io/api/core/v1"
1413 "k8s.io/apimachinery/pkg/types"
1514 cloudprovider "k8s.io/cloud-provider"
16- "k8s.io/klog/v2"
1715
1816 "github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
1917 "github.com/linode/linode-cloud-controller-manager/sentry"
2018)
2119
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-
3220type nodeCache struct {
3321 sync.RWMutex
34- nodes map [int ]linodeInstance
22+ nodes map [int ]* linodego. Instance
3523 lastUpdate time.Time
3624 ttl time.Duration
3725}
3826
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-
7827// refreshInstances conditionally loads all instances from the Linode API and caches them.
7928// It does not refresh if the last update happened less than `nodeCache.ttl` ago.
8029func (nc * nodeCache ) refreshInstances (ctx context.Context , client client.Client ) error {
@@ -90,29 +39,11 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
9039 return err
9140 }
9241
93- nc .nodes = make (map [int ]linodeInstance , len ( instances ) )
42+ nc .nodes = make (map [int ]* linodego. Instance )
9443
95- mtx := sync.Mutex {}
96- g := new (errgroup.Group )
9744 for _ , instance := range instances {
9845 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
11647 }
11748
11849 nc .lastUpdate = time .Now ()
@@ -133,10 +64,9 @@ func newInstances(client client.Client) *instances {
13364 timeout = t
13465 }
13566 }
136- klog .V (3 ).Infof ("TTL for nodeCache set to %d" , timeout )
13767
13868 return & instances {client , & nodeCache {
139- nodes : make (map [int ]linodeInstance , 0 ),
69+ nodes : make (map [int ]* linodego. Instance ),
14070 ttl : time .Duration (timeout ) * time .Second ,
14171 }}
14272}
@@ -153,8 +83,8 @@ func (i *instances) linodeByName(nodeName types.NodeName) (*linodego.Instance, e
15383 i .nodeCache .RLock ()
15484 defer i .nodeCache .RUnlock ()
15585 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
15888 }
15989 }
16090
@@ -164,24 +94,11 @@ func (i *instances) linodeByName(nodeName types.NodeName) (*linodego.Instance, e
16494func (i * instances ) linodeByID (id int ) (* linodego.Instance , error ) {
16595 i .nodeCache .RLock ()
16696 defer i .nodeCache .RUnlock ()
167- linodeInstance , ok := i .nodeCache .nodes [id ]
97+ instance , ok := i .nodeCache .nodes [id ]
16898 if ! ok {
16999 return nil , cloudprovider .InstanceNotFound
170100 }
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
185102}
186103
187104func (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
248165 return nil , err
249166 }
250167
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 {
258169 err := instanceNoIPAddressesError {linode .ID }
259170 sentry .CaptureError (ctx , err )
260171 return nil , err
261172 }
262173
263174 addresses := []v1.NodeAddress {{Type : v1 .NodeHostName , Address : linode .Label }}
264175
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 ()})
267182 }
268183
269184 // 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
276191
277192 return meta , nil
278193}
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