@@ -243,7 +243,8 @@ func (cp *CloudProvider) InstanceExistsByProviderID(ctx context.Context, provide
243
243
}
244
244
instance , err := cp .client .Compute ().GetInstance (ctx , instanceID )
245
245
if client .IsNotFound (err ) {
246
- return false , nil
246
+ return cp .checkForAuthorizationError (ctx , providerID )
247
+
247
248
}
248
249
if err != nil {
249
250
return false , err
@@ -252,6 +253,34 @@ func (cp *CloudProvider) InstanceExistsByProviderID(ctx context.Context, provide
252
253
return ! client .IsInstanceInTerminalState (instance ), nil
253
254
}
254
255
256
+ func (cp * CloudProvider ) checkForAuthorizationError (ctx context.Context , instanceId string ) (bool , error ) {
257
+ cp .logger .With ("instanceId" , instanceId ).Info ("Received 404 for an instance, listing instances to check for authorization errors" )
258
+ compartmentId , err := cp .getCompartmentIDByInstanceID (instanceId )
259
+ if err != nil {
260
+ return false , err
261
+ }
262
+ // to eliminate AD specific issues, list all ADs and make AD specific requests
263
+ availabilityDomains , err := cp .client .Identity ().ListAvailabilityDomains (ctx , compartmentId )
264
+ for _ , availabilityDomain := range availabilityDomains {
265
+ instances , err := cp .client .Compute ().ListInstancesByCompartmentAndAD (ctx , compartmentId , * availabilityDomain .Name )
266
+ // if we are getting errors for ListInstances the issue can be authorization or other issues
267
+ // so to be safe we return the error back as we can't list instances in the compartment
268
+ if err != nil {
269
+ cp .logger .With ("instanceId" , instanceId ).Info ("Received error when listing instances to check for authorization errors" )
270
+ return false , err
271
+ }
272
+
273
+ for _ , instance := range instances {
274
+ if * instance .Id == instanceId {
275
+ // Can only happen if changes are done in policy in-between requests
276
+ return true , nil
277
+ }
278
+ }
279
+ }
280
+
281
+ return false , nil
282
+ }
283
+
255
284
// InstanceShutdownByProviderID returns true if the instance is shutdown in cloudprovider.
256
285
func (cp * CloudProvider ) InstanceShutdownByProviderID (ctx context.Context , providerID string ) (bool , error ) {
257
286
//Please do not try to optimise it by using InstanceCache because we prefer correctness over efficiency here
0 commit comments