@@ -17,7 +17,7 @@ type instances struct {
1717 client Client
1818}
1919
20- func newInstances (client Client ) cloudprovider.Instances {
20+ func newInstances (client Client ) cloudprovider.InstancesV2 {
2121 return & instances {client }
2222}
2323
@@ -29,176 +29,89 @@ func (e instanceNoIPAddressesError) Error() string {
2929 return fmt .Sprintf ("instance %d has no IP addresses" , e .id )
3030}
3131
32- func (i * instances ) NodeAddresses (ctx context.Context , name types.NodeName ) ([]v1.NodeAddress , error ) {
33- ctx = sentry .SetHubOnContext (ctx )
34- sentry .SetTag (ctx , "node_name" , string (name ))
35-
36- linode , err := linodeByName (ctx , i .client , name )
37- if err != nil {
38- sentry .CaptureError (ctx , err )
39- return nil , err
40- }
41-
42- addresses , err := i .nodeAddresses (ctx , linode )
43- if err != nil {
44- sentry .CaptureError (ctx , err )
45- return nil , err
46- }
47-
48- return addresses , nil
49- }
32+ func (i * instances ) lookupLinode (ctx context.Context , node * v1.Node ) (* linodego.Instance , error ) {
33+ providerID := node .Spec .ProviderID
34+ nodeName := types .NodeName (node .Name )
5035
51- func (i * instances ) NodeAddressesByProviderID (ctx context.Context , providerID string ) ([]v1.NodeAddress , error ) {
52- ctx = sentry .SetHubOnContext (ctx )
5336 sentry .SetTag (ctx , "provider_id" , providerID )
37+ sentry .SetTag (ctx , "node_name" , node .Name )
5438
55- id , err := parseProviderID (providerID )
56- if err != nil {
57- sentry .CaptureError (ctx , err )
58- return nil , err
59- }
60-
61- linode , err := linodeByID (ctx , i .client , id )
62- if err != nil {
63- sentry .CaptureError (ctx , err )
64- return nil , err
65- }
66-
67- addresses , err := i .nodeAddresses (ctx , linode )
68- if err != nil {
69- sentry .CaptureError (ctx , err )
70- return nil , err
71- }
72-
73- return addresses , nil
74- }
75-
76- func (i * instances ) nodeAddresses (ctx context.Context , linode * linodego.Instance ) ([]v1.NodeAddress , error ) {
77- var addresses []v1.NodeAddress
78- addresses = append (addresses , v1.NodeAddress {Type : v1 .NodeHostName , Address : linode .Label })
79-
80- ips , err := i .client .GetInstanceIPAddresses (ctx , linode .ID )
81- if err != nil {
82- return nil , err
83- }
84-
85- if (len (ips .IPv4 .Public ) == 0 ) && (len (ips .IPv4 .Private ) == 0 ) {
86- return nil , instanceNoIPAddressesError {linode .ID }
87- }
88-
89- if len (ips .IPv4 .Public ) > 0 {
90- addresses = append (addresses , v1.NodeAddress {Type : v1 .NodeExternalIP , Address : ips .IPv4 .Public [0 ].Address })
91- }
92- // Allow Nodes to not have an ExternalIP, if this proves problematic this will be reverted
39+ if providerID != "" {
40+ id , err := parseProviderID (providerID )
41+ if err != nil {
42+ sentry .CaptureError (ctx , err )
43+ return nil , err
44+ }
45+ sentry .SetTag (ctx , "linode_id" , strconv .Itoa (id ))
9346
94- if len (ips .IPv4 .Private ) > 0 {
95- addresses = append (addresses , v1.NodeAddress {Type : v1 .NodeInternalIP , Address : ips .IPv4 .Private [0 ].Address })
47+ return linodeByID (ctx , i .client , id )
9648 }
97- // Allow Nodes to not have an InternalIP, if this proves problematic this will be reverted
9849
99- return addresses , nil
50+ return linodeByName ( ctx , i . client , nodeName )
10051}
10152
102- func (i * instances ) InstanceID (ctx context.Context , nodeName types. NodeName ) (string , error ) {
53+ func (i * instances ) InstanceExists (ctx context.Context , node * v1. Node ) (bool , error ) {
10354 ctx = sentry .SetHubOnContext (ctx )
104- sentry . SetTag (ctx , "node_name" , string ( nodeName ))
105-
106- linode , err := linodeByName ( ctx , i . client , nodeName )
107- if err != nil {
55+ if _ , err := i . lookupLinode (ctx , node ); err != nil {
56+ if apiError , ok := err .( * linodego. Error ); ok && apiError . Code == http . StatusNotFound {
57+ return false , nil
58+ }
10859 sentry .CaptureError (ctx , err )
109- return "" , err
60+ return false , err
11061 }
111- return strconv .Itoa (linode .ID ), nil
112- }
11362
114- func (i * instances ) InstanceType (ctx context.Context , nodeName types.NodeName ) (string , error ) {
115- ctx = sentry .SetHubOnContext (ctx )
116- sentry .SetTag (ctx , "node_name" , string (nodeName ))
117-
118- linode , err := linodeByName (ctx , i .client , nodeName )
119- if err != nil {
120- sentry .CaptureError (ctx , err )
121- return "" , err
122- }
123- return linode .Type , nil
63+ return true , nil
12464}
12565
126- func (i * instances ) InstanceTypeByProviderID (ctx context.Context , providerID string ) (string , error ) {
66+ func (i * instances ) InstanceShutdown (ctx context.Context , node * v1. Node ) (bool , error ) {
12767 ctx = sentry .SetHubOnContext (ctx )
128- sentry .SetTag (ctx , "provider_id" , providerID )
129-
130- id , err := parseProviderID (providerID )
68+ instance , err := i .lookupLinode (ctx , node )
13169 if err != nil {
13270 sentry .CaptureError (ctx , err )
133- return "" , err
71+ return false , err
13472 }
13573
136- sentry .SetTag (ctx , "linode_id" , strconv .Itoa (id ))
137-
138- linode , err := linodeByID (ctx , i .client , id )
139- if err != nil {
140- sentry .CaptureError (ctx , err )
141- return "" , err
74+ // An instance is considered to be "shutdown" when it is
75+ // in the process of shutting down, or already offline.
76+ if instance .Status == linodego .InstanceOffline ||
77+ instance .Status == linodego .InstanceShuttingDown {
78+ return true , nil
14279 }
143- return linode .Type , nil
144- }
145-
146- func (i * instances ) AddSSHKeyToAllInstances (_ context.Context , user string , keyData []byte ) error {
147- return cloudprovider .NotImplemented
148- }
14980
150- func (i * instances ) CurrentNodeName (_ context.Context , hostname string ) (types.NodeName , error ) {
151- return types .NodeName (hostname ), nil
81+ return false , nil
15282}
15383
154- func (i * instances ) InstanceExistsByProviderID (ctx context.Context , providerID string ) (bool , error ) {
84+ func (i * instances ) InstanceMetadata (ctx context.Context , node * v1. Node ) (* cloudprovider. InstanceMetadata , error ) {
15585 ctx = sentry .SetHubOnContext (ctx )
156- sentry .SetTag (ctx , "provider_id" , providerID )
157-
158- id , err := parseProviderID (providerID )
159- if err != nil {
160- sentry .CaptureError (ctx , err )
161- return false , err
162- }
163-
164- sentry .SetTag (ctx , "linode_id" , strconv .Itoa (id ))
165-
166- _ , err = linodeByID (ctx , i .client , id )
86+ linode , err := i .lookupLinode (ctx , node )
16787 if err != nil {
168- if apiError , ok := err .(* linodego.Error ); ok && apiError .Code == http .StatusNotFound {
169- return false , nil
170- }
17188 sentry .CaptureError (ctx , err )
172- return false , err
89+ return nil , err
17390 }
17491
175- return true , nil
176- }
177-
178- func (i * instances ) InstanceShutdownByProviderID (ctx context.Context , providerID string ) (bool , error ) {
179- ctx = sentry .SetHubOnContext (ctx )
180- sentry .SetTag (ctx , "provider_id" , providerID )
181-
182- id , err := parseProviderID (providerID )
183- if err != nil {
92+ if len (linode .IPv4 ) == 0 {
93+ err := instanceNoIPAddressesError {linode .ID }
18494 sentry .CaptureError (ctx , err )
185- return false , err
95+ return nil , err
18696 }
18797
188- sentry . SetTag ( ctx , "linode_id" , strconv . Itoa ( id ))
98+ addresses := []v1. NodeAddress {{ Type : v1 . NodeHostName , Address : linode . Label }}
18999
190- instance , err := linodeByID (ctx , i .client , id )
191- if err != nil {
192- sentry .CaptureError (ctx , err )
193- return false , err
100+ for _ , ip := range linode .IPv4 {
101+ ipType := v1 .NodeExternalIP
102+ if ip .IsPrivate () {
103+ ipType = v1 .NodeInternalIP
104+ }
105+ addresses = append (addresses , v1.NodeAddress {Type : ipType , Address : ip .String ()})
194106 }
195107
196- // An instance is considered to be "shutdown" when it is
197- // in the process of shutting down, or already offline.
198- if instance .Status == linodego .InstanceOffline ||
199- instance .Status == linodego .InstanceShuttingDown {
200- return true , nil
108+ // note that Zone is omitted as it's not a thing in Linode
109+ meta := & cloudprovider.InstanceMetadata {
110+ ProviderID : fmt .Sprintf ("%v%v" , providerIDPrefix , linode .ID ),
111+ NodeAddresses : addresses ,
112+ InstanceType : linode .Type ,
113+ Region : linode .Region ,
201114 }
202115
203- return false , nil
116+ return meta , nil
204117}
0 commit comments