Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disk objects aren't deleted when the VM is deleted #927

Closed
gabipetrovay opened this issue Nov 9, 2015 · 11 comments
Closed

Disk objects aren't deleted when the VM is deleted #927

gabipetrovay opened this issue Nov 9, 2015 · 11 comments
Assignees
Labels
api: compute Issues related to the Compute Engine API. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@gabipetrovay
Copy link

There seems to be an issue with the VM instances when recycling the names. In the below example I create, delete and create again an instance using the same name. The error is shown in the output snippet below.

var BUILDER_MACHINE_NAME = "image2";                                                                          
var BUILDER_MACHINE_CONFIG = {                                                                                
    os: "ubuntu",                                                                                             
    machineType: "g1-small"                                                                                   
};                                                                                                            

var gce = gcloud.compute();                                                                                   
var zone = gce.zone(zone);                                                                                    

zone.createVM(BUILDER_MACHINE_NAME, BUILDER_MACHINE_CONFIG, function(err, vm, operation, apiResponse) {       

    if (err) { return callback(err); }                                                                        

    console.dir(apiResponse);                                                                                 
    console.log("1 --------------");                                                                          

    operation.onComplete(function(err, metadata) {                                                            

        if (err) { return callback(err); }                                                                    

        console.dir(metadata);                                                                                
        console.log("2 --------------");                                                                      

        vm.delete(function(err, operation, apiResponse) {                                                     

            if (err) { return callback(err); }                                                                

            console.dir(apiResponse);                                                                         
            console.log("3 --------------");                                                                  

            operation.onComplete(function(err, metadata) {                                                    

                if (err) { return callback(err); }                                                            

                console.dir(metadata);                                                                        
                console.log("4 --------------");                                                              

                zone.createVM(BUILDER_MACHINE_NAME, BUILDER_MACHINE_CONFIG, function(err, vm, operation, apiResponse) {

                    if (err) { return callback(err); }                                                        

                    console.dir(apiResponse);                                                                 
                    console.log("5 --------------");                                                          

                    operation.onComplete(function(err, metadata) {                                            

                        if (err) { return callback(err); }                                                    

                        console.dir(metadata);                                                                
                    }); 
                }); 
            }); 
        }); 
    }); 
}); 

The operation after the 2nd createVM call will issue the error:

...
4 --------------
{ kind: 'compute#operation',
  id: '1947681473087096016',
  name: 'operation-1447083184520-5241d4f2c7b41-6718dbc3-790a19cf',
  zone: 'https://www.googleapis.com/compute/v1/projects/sigma-cairn-99810/zones/europe-west1-c',
  operationType: 'insert',
  targetLink: 'https://www.googleapis.com/compute/v1/projects/sigma-cairn-99810/zones/europe-west1-c/instances/image2',
  status: 'PENDING',
  user: '262451203973-qh79osjjiu4p1m535ctcu8easjdqsqq3@developer.gserviceaccount.com',
  progress: 0,
  insertTime: '2015-11-09T07:33:05.005-08:00',
  startTime: '2015-11-09T07:33:05.367-08:00',
  selfLink: 'https://www.googleapis.com/compute/v1/projects/sigma-cairn-99810/zones/europe-west1-c/operations/operation-1447083184520-5241d4f2c7b41-6718dbc3-790a19cf' }
5 --------------
{ errors: 
   [ { code: 'RESOURCE_ALREADY_EXISTS',
       message: 'The resource \'projects/sigma-cairn-99810/zones/europe-west1-c/disks/image2\' already exists' } ] }

Can the VM instance names be reused? (Can you please point me to the documentation, if there is smth like this specified)

Thanks!

@stephenplusplus
Copy link
Contributor

Found the issue! Our API allows you to provide the os config property, which we then turn into a full object the API call to create the VM actually wants:

{
  name: 'image2',
  machineType: 'zones/us-central1-a/machineTypes/g1-small',
  // ...
  disks: [
    {
      boot: true,
      initializeParams: {
        sourceImage: 'https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1504-vivid-v20150616a'
      }
    }
  ]
}

The problem is that a disk is automatically created for your VM. When you delete the VM, by default, it thinks you won't want the disk to be deleted as well. Here's what we need to do to change that:

  disks: [
    {
+     autoDelete: true,
      boot: true,

I'll put a PR that defaults autoDelete to true, but if you need this behavior now, you'll need to manually form the configuration object with the help of gce-images:

var config = { keyFilename: '', projectId: '' } // must be provided to gceImages
var gcloud = require('gcloud')(config)
var gceImages = require('gce-images')(config)

gceImages.getLatest('ubuntu', function (err, image) {
  if (err) // ...

  var cfg = {
    machineType: 'g1-small',
    disks: [
      {
        autoDelete: true,
        boot: true,
        initializeParams: {
          sourceImage: image.selfLink
        }
      }
    ]
  }

  zone.createVM('vm-name', cfg, function () {})
})

Good catch, by the way. I had quite a few lingering disks in my test project :)

@stephenplusplus stephenplusplus added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. api: compute Issues related to the Compute Engine API. labels Nov 9, 2015
@stephenplusplus
Copy link
Contributor

Sorry, my last post had some bugs I just fixed. Refresh if you have already read it to get the latest.

@gabipetrovay
Copy link
Author

I am trying it out now.

@gabipetrovay
Copy link
Author

😜 👍 🎉

Same here with the disks. :)

@gabipetrovay
Copy link
Author

Stumbling upon a new error when building the images object:

{ [Error: Scopes are required for this request.] code: 'MISSING_SCOPE' }

I assume this has to do with the authentication. I am initialising gce-images with:

var gceImages = require("gce-images");
var images = gceImages({ keyFile:  "/Users/gabriel/Work/gcloud/keyfile.json"});

This last line is missing from your example above (gceImages has no getLatest method).

@stephenplusplus
Copy link
Contributor

Sorry, try un- and re-installing gce-images (so that you get 0.2.1). Pushed a fix!

@gabipetrovay
Copy link
Author

Ok, I am further. 😄

Next one:

{ [ApiError: Invalid value for field 'resource.disks': ''.  At most one persistent disk can be marked as a boot device.]
  errors: 
   [ { domain: 'global',
       reason: 'invalid',
       message: 'Invalid value for field \'resource.disks\': \'\'.  At most one persistent disk can be marked as a boot device.' } ],
  code: 400,
  message: 'Invalid value for field \'resource.disks\': \'\'.  At most one persistent disk can be marked as a boot device.',
  response: undefined }

I try to build the VM instance using this config:

var BUILDER_MACHINE_CONFIG = {
    os: "ubuntu",
    machineType: "g1-small",
    disks: [{
        autoDelete: true,
        boot: true,                                                                                         
        initializeParams: {
            sourceImage: sourceImage
        }
    }]
};  

@stephenplusplus
Copy link
Contributor

Thanks for being patient with me :) Remove the os from the BUILDER_MACHINE_CONFIG object.

@gabipetrovay
Copy link
Author

Makes sense because the sourceImage URL has the info ...

@gabipetrovay
Copy link
Author

Ok, we're flying! 🚀

Thanks also for help. I'll bug you later. No worries! 😝

You can close this issue is you don't have anything to track further.

@stephenplusplus stephenplusplus changed the title Reusing VM instance names: RESOURCE_ALREADY_EXISTS Disk objects aren't deleted when the VM is deleted Nov 9, 2015
@stephenplusplus
Copy link
Contributor

Thanks again for catching this! I'll keep this open to make sure we don't forget to set autoDelete to true.

sofisl pushed a commit that referenced this issue Nov 11, 2022
- [ ] Regenerate this pull request now.

Committer: @summer-ji-eng
PiperOrigin-RevId: 424244721

Source-Link: googleapis/googleapis@4b6b01f

Source-Link: googleapis/googleapis-gen@8ac83fb
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiOGFjODNmYmE2MDZkMDA4YzdlOGE0MmU3ZDU1YjY1OTZlYzRiZTM1ZiJ9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: compute Issues related to the Compute Engine API. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants