@@ -152,7 +152,7 @@ func (c *CloudNodeLifecycleController) MonitorNodes(ctx context.Context) {
152
152
153
153
// At this point the node has NotReady status, we need to check if the node has been removed
154
154
// from the cloud provider. If node cannot be found in cloudprovider, then delete the node
155
- exists , err := ensureNodeExistsByProviderID (ctx , c . cloud , node )
155
+ exists , err := c . ensureNodeExistsByProviderID (ctx , node )
156
156
if err != nil {
157
157
klog .Errorf ("error checking if node %s exists: %v" , node .Name , err )
158
158
continue
@@ -180,7 +180,7 @@ func (c *CloudNodeLifecycleController) MonitorNodes(ctx context.Context) {
180
180
// Node exists. We need to check this to get taint working in similar in all cloudproviders
181
181
// current problem is that shutdown nodes are not working in similar way ie. all cloudproviders
182
182
// does not delete node from kubernetes cluster when instance it is shutdown see issue #46442
183
- shutdown , err := shutdownInCloudProvider (ctx , c . cloud , node )
183
+ shutdown , err := c . shutdownInCloudProvider (ctx , node )
184
184
if err != nil {
185
185
klog .Errorf ("error checking if node %s is shutdown: %v" , node .Name , err )
186
186
}
@@ -196,18 +196,49 @@ func (c *CloudNodeLifecycleController) MonitorNodes(ctx context.Context) {
196
196
}
197
197
}
198
198
199
+ // getProviderID returns the provider ID for the node. If Node CR has no provider ID,
200
+ // it will be the one from the cloud provider.
201
+ func (c * CloudNodeLifecycleController ) getProviderID (ctx context.Context , node * v1.Node ) (string , error ) {
202
+ if node .Spec .ProviderID != "" {
203
+ return node .Spec .ProviderID , nil
204
+ }
205
+
206
+ if instanceV2 , ok := c .cloud .InstancesV2 (); ok {
207
+ metadata , err := instanceV2 .InstanceMetadata (ctx , node )
208
+ if err != nil {
209
+ return "" , err
210
+ }
211
+ return metadata .ProviderID , nil
212
+ }
213
+
214
+ providerID , err := cloudprovider .GetInstanceProviderID (ctx , c .cloud , types .NodeName (node .Name ))
215
+ if err != nil {
216
+ return "" , err
217
+ }
218
+
219
+ return providerID , nil
220
+ }
221
+
199
222
// shutdownInCloudProvider returns true if the node is shutdown on the cloud provider
200
- func shutdownInCloudProvider (ctx context.Context , cloud cloudprovider. Interface , node * v1.Node ) (bool , error ) {
201
- if instanceV2 , ok := cloud .InstancesV2 (); ok {
223
+ func ( c * CloudNodeLifecycleController ) shutdownInCloudProvider (ctx context.Context , node * v1.Node ) (bool , error ) {
224
+ if instanceV2 , ok := c . cloud .InstancesV2 (); ok {
202
225
return instanceV2 .InstanceShutdown (ctx , node )
203
226
}
204
227
205
- instances , ok := cloud .Instances ()
228
+ instances , ok := c . cloud .Instances ()
206
229
if ! ok {
207
230
return false , errors .New ("cloud provider does not support instances" )
208
231
}
209
232
210
- shutdown , err := instances .InstanceShutdownByProviderID (ctx , node .Spec .ProviderID )
233
+ providerID , err := c .getProviderID (ctx , node )
234
+ if err != nil {
235
+ if err == cloudprovider .InstanceNotFound {
236
+ return false , nil
237
+ }
238
+ return false , err
239
+ }
240
+
241
+ shutdown , err := instances .InstanceShutdownByProviderID (ctx , providerID )
211
242
if err == cloudprovider .NotImplemented {
212
243
return false , nil
213
244
}
@@ -216,32 +247,22 @@ func shutdownInCloudProvider(ctx context.Context, cloud cloudprovider.Interface,
216
247
}
217
248
218
249
// ensureNodeExistsByProviderID checks if the instance exists by the provider id,
219
- // If provider id in spec is empty it calls instanceId with node name to get provider id
220
- func ensureNodeExistsByProviderID (ctx context.Context , cloud cloudprovider.Interface , node * v1.Node ) (bool , error ) {
221
- if instanceV2 , ok := cloud .InstancesV2 (); ok {
250
+ func (c * CloudNodeLifecycleController ) ensureNodeExistsByProviderID (ctx context.Context , node * v1.Node ) (bool , error ) {
251
+ if instanceV2 , ok := c .cloud .InstancesV2 (); ok {
222
252
return instanceV2 .InstanceExists (ctx , node )
223
253
}
224
254
225
- instances , ok := cloud .Instances ()
255
+ instances , ok := c . cloud .Instances ()
226
256
if ! ok {
227
257
return false , errors .New ("instances interface not supported in the cloud provider" )
228
258
}
229
259
230
- providerID := node .Spec .ProviderID
231
- if providerID == "" {
232
- var err error
233
- providerID , err = instances .InstanceID (ctx , types .NodeName (node .Name ))
234
- if err != nil {
235
- if err == cloudprovider .InstanceNotFound {
236
- return false , nil
237
- }
238
- return false , err
239
- }
240
-
241
- if providerID == "" {
242
- klog .Warningf ("Cannot find valid providerID for node name %q, assuming non existence" , node .Name )
260
+ providerID , err := c .getProviderID (ctx , node )
261
+ if err != nil {
262
+ if err == cloudprovider .InstanceNotFound {
243
263
return false , nil
244
264
}
265
+ return false , err
245
266
}
246
267
247
268
return instances .InstanceExistsByProviderID (ctx , providerID )
0 commit comments