@@ -210,6 +210,11 @@ func (g *Cloud) InstanceShutdownByProviderID(ctx context.Context, providerID str
210
210
return false , cloudprovider .NotImplemented
211
211
}
212
212
213
+ // InstanceShutdown returns true if the instance is in safe state to detach volumes
214
+ func (g * Cloud ) InstanceShutdown (ctx context.Context , node * v1.Node ) (bool , error ) {
215
+ return false , cloudprovider .NotImplemented
216
+ }
217
+
213
218
func nodeAddressesFromInstance (instance * compute.Instance ) ([]v1.NodeAddress , error ) {
214
219
if len (instance .NetworkInterfaces ) < 1 {
215
220
return nil , fmt .Errorf ("could not find network interfaces for instanceID %q" , instance .Id )
@@ -253,6 +258,61 @@ func (g *Cloud) InstanceExistsByProviderID(ctx context.Context, providerID strin
253
258
return true , nil
254
259
}
255
260
261
+ // InstanceExists returns true if the instance with the given provider id still exists and is running.
262
+ // If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
263
+ func (g * Cloud ) InstanceExists (ctx context.Context , node * v1.Node ) (bool , error ) {
264
+ providerID := node .Spec .ProviderID
265
+ if providerID == "" {
266
+ var err error
267
+ if providerID , err = g .InstanceID (ctx , types .NodeName (node .Name )); err != nil {
268
+ return false , err
269
+ }
270
+ }
271
+ return g .InstanceExistsByProviderID (ctx , providerID )
272
+ }
273
+
274
+ // InstanceMetadata returns metadata of the specified instance.
275
+ func (g * Cloud ) InstanceMetadata (ctx context.Context , node * v1.Node ) (* cloudprovider.InstanceMetadata , error ) {
276
+ timeoutCtx , cancel := context .WithTimeout (ctx , 1 * time .Hour )
277
+ defer cancel ()
278
+
279
+ providerID := node .Spec .ProviderID
280
+ if providerID == "" {
281
+ var err error
282
+ if providerID , err = g .InstanceID (ctx , types .NodeName (node .Name )); err != nil {
283
+ return nil , err
284
+ }
285
+ }
286
+
287
+ _ , zone , name , err := splitProviderID (providerID )
288
+ if err != nil {
289
+ return nil , err
290
+ }
291
+
292
+ region , err := GetGCERegion (zone )
293
+ if err != nil {
294
+ return nil , err
295
+ }
296
+
297
+ instance , err := g .c .Instances ().Get (timeoutCtx , meta .ZonalKey (canonicalizeInstanceName (name ), zone ))
298
+ if err != nil {
299
+ return nil , fmt .Errorf ("error while querying for providerID %q: %v" , providerID , err )
300
+ }
301
+
302
+ addresses , err := nodeAddressesFromInstance (instance )
303
+ if err != nil {
304
+ return nil , err
305
+ }
306
+
307
+ return & cloudprovider.InstanceMetadata {
308
+ ProviderID : providerID ,
309
+ InstanceType : lastComponent (instance .MachineType ),
310
+ NodeAddresses : addresses ,
311
+ Zone : zone ,
312
+ Region : region ,
313
+ }, nil
314
+ }
315
+
256
316
// InstanceID returns the cloud provider ID of the node with the specified NodeName.
257
317
func (g * Cloud ) InstanceID (ctx context.Context , nodeName types.NodeName ) (string , error ) {
258
318
instanceName := mapNodeNameToInstanceName (nodeName )
0 commit comments