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

Fix the issue where rancher-machine does not clean up vSphere VMs that are in failed status #254

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions drivers/vmwarevsphere/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ func (d *Driver) createLegacy() error {
if err != nil {
return err
}
log.Info("Fetching MachineID ...")
// save the machine id as soon as the VM is created
if _, err = d.GetMachineId(); err != nil {
// no need to return the error, it is not a blocker for creating the machine,
// we will fetch the machineID again after starting the VM
log.Warnf("[createLegacy] failed to fetch MachineID for %s: %v", d.MachineName, err)
}

log.Infof("Uploading Boot2docker ISO ...")
vm := object.NewVirtualMachine(c.Client, info.Result.(types.ManagedObjectReference))
Expand Down Expand Up @@ -274,6 +281,14 @@ func (d *Driver) createFromVmName() error {
return err
}

log.Info("Fetching MachineID ...")
// save the machine id as soon as the VM is created
if _, err = d.GetMachineId(); err != nil {
// no need to return the error, it is not a blocker for creating the machine,
// we will fetch the machineID again after starting the VM
log.Warnf("[createFromVmName] failed to fetch MachineID for %s: %v", d.MachineName, err)
}

// Retrieve the new VM
vm := object.NewVirtualMachine(c.Client, info.Result.(types.ManagedObjectReference))
if err := d.addNetworks(vm, d.networks); err != nil {
Expand Down Expand Up @@ -357,6 +372,14 @@ func (d *Driver) createFromLibraryName() error {
return err
}

log.Info("Fetching MachineID ...")
// save the machine id as soon as the VM is created
if _, err = d.GetMachineId(); err != nil {
// no need to return the error, it is not a blocker for creating the machine,
// we will fetch the machineID again after starting the VM
log.Warnf("[createFromLibraryName] failed to fetch MachineID for %s: %v", d.MachineName, err)
}

// At this point, the VM is deployed from content library with defaults from template
// Reconfiguration of the VM based on driver inputs follows

Expand Down
1 change: 1 addition & 0 deletions drivers/vmwarevsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ func (d *Driver) Kill() error {
func (d *Driver) Remove() error {
if d.MachineId == "" {
// no guid from config, nothing in vsphere to delete
log.Info("MachineID is empty, skipping removing VM")
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions libmachine/libmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ func (api *Client) Create(h *host.Host) error {
log.Info("Creating machine...")

if err := api.performCreate(h); err != nil {
if h.Driver.DriverName() == "vmwarevsphere" {
// it is possible that the VM is instantiated but fails to bootstrap,
// save the machine to the store, so the VM and associated resources can be found and destroyed later
if err := api.Save(h); err != nil {
log.Warnf("Error saving host to store after creation fails: %s", err)
}
}
return fmt.Errorf("Error creating machine: %s", err)
}

Expand Down
Loading