Skip to content

Commit

Permalink
add a customizable disk_size field to vm post-provision, including re…
Browse files Browse the repository at this point in the history
…factoring to separate between container reconfiguration (memory, CPU) and disk resize
  • Loading branch information
OrGur1987 committed Nov 27, 2022
1 parent 84e6290 commit 54a80f4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 13 deletions.
Expand Up @@ -5,11 +5,28 @@ module ManageIQ::Providers::Vmware::InfraManager::Provision::Configuration
include_concern 'Network'
include_concern 'Disk'

def reconfigure_hardware_on_destination?
def reconfigure_container_on_destination?
# Do we need to perform a post-clone hardware reconfigure on the new VM?
[:cpu_limit, :memory_limit, :cpu_reserve, :memory_reserve].any? do |k|
return false unless options.key?(k)
destination.send(k) != options[k]
end
end

def reconfigure_disk_on_destination?(destination)
default_size = destination.hardware.disks.find_by(:device_type => "disk").size / 1024**3
get_option(:disk_size) > default_size
end

def reconfigure_hardware(destination)
config_spec = VimHash.new("VirtualMachineConfigSpec") do |vmcs|
set_cpu_and_memory_allocation(vmcs) if reconfigure_container_on_destination?
set_disk_allocation(destination, vmcs) if reconfigure_disk_on_destination?(destination)
end
unless config_spec.empty?
_log.info("Calling VM reconfiguration")
dump_obj(config_spec, "#{_log.prefix} Post-create Config spec: ", $log, :info)
vm.spec_reconfigure(config_spec)
end
end
end
Expand Up @@ -107,8 +107,7 @@ def get_next_device_idx
@new_device_idx -= 1
end

def set_cpu_and_memory_allocation(vm)
config_spec = VimHash.new("VirtualMachineConfigSpec") do |vmcs|
def set_cpu_and_memory_allocation(vmcs)
vmcs.cpuAllocation = VimHash.new("ResourceAllocationInfo") do |rai|
set_spec_option(rai, :limit, :cpu_limit, nil, :to_i)
set_spec_option(rai, :reservation, :cpu_reserve, nil, :to_i)
Expand All @@ -122,10 +121,5 @@ def set_cpu_and_memory_allocation(vm)
# Only explicitly disable #MemoryReservationLockedToMax if the memory reserve
# is less than the total vm memory
vmcs.memoryReservationLockedToMax = false if get_option(:memory_reserve).to_i < get_option(:vm_memory).to_i
end

_log.info("Calling VM reconfiguration")
dump_obj(config_spec, "#{_log.prefix} Post-create Config spec: ", $log, :info)
vm.spec_reconfigure(config_spec)
end
end
Expand Up @@ -105,6 +105,23 @@ def add_disk(vmcs, disk, controller, new_dev_key)
end
end

def set_disk_allocation(vm, vmcs)
disk = vm.hardware.disks.find_by(:device_type => "disk")
vm.with_provider_object do |vim_obj|
device = vim_obj.getDeviceByLabel(disk.device_name)
new_capacity_in_kb = get_option(:disk_size).to_i * 1024**2
add_device_config_spec(vmcs, VirtualDeviceConfigSpecOperation::Edit) do |vdcs|
vdcs.device = VimHash.new("VirtualDisk") do |dev|
dev.key = device.key
dev.capacityInKB = new_capacity_in_kb
dev.controllerKey = device.controllerKey
dev.unitNumber = device.unitNumber
dev.backing = device.backing
end
end
end
end

private

def devices
Expand Down
Expand Up @@ -72,7 +72,7 @@ def customize_destination
_log.info("Post-processing #{destination_type} id: [#{destination.id}], name: [#{dest_name}]")
update_and_notify_parent(:message => "Starting New #{destination_type} Customization")

set_cpu_and_memory_allocation(destination) if reconfigure_hardware_on_destination?
reconfigure_hardware(destination)
signal :autostart_destination
end

Expand Down
Expand Up @@ -3,8 +3,7 @@ def customize_destination
_log.info("Post-processing #{destination_type} id: [#{destination.id}], name: [#{dest_name}]")
update_and_notify_parent(:message => "Starting New #{destination_type} Customization")

set_cpu_and_memory_allocation(destination) if reconfigure_hardware_on_destination?

reconfigure_hardware(destination)
signal :create_pxe_configuration_file
end

Expand Down
Expand Up @@ -693,6 +693,13 @@
:display: :edit
:default: unchanged
:data_type: :string
:disk_size:
:description: Disk Size (GB)
:required: false
:display: :edit
:data_type: :integer
:notes: (default size taken from template, custom can only be higher)
:notes_display: :show
:cpu_limit:
:description: CPU (MHz)
:required: false
Expand Down
Expand Up @@ -79,9 +79,9 @@
it "should detect when a reconfigure_hardware_on_destination call is required" do
target_vm = FactoryBot.create(:vm_vmware, :name => "target_vm1", :location => "abc/def.vmx", :cpu_limit => @vm_prov.options[:cpu_limit])
@vm_prov.destination = target_vm
expect(@vm_prov.reconfigure_hardware_on_destination?).to eq(false)
expect(@vm_prov.reconfigure_container_on_destination?).to eq(false)
@vm_prov.options[:cpu_limit] = 100
expect(@vm_prov.reconfigure_hardware_on_destination?).to eq(true)
expect(@vm_prov.reconfigure_container_on_destination?).to eq(true)
end

it "should delete unneeded network cards" do
Expand Down

0 comments on commit 54a80f4

Please sign in to comment.