diff --git a/src/vsphere_cpi/lib/cloud/vsphere/vm_config.rb b/src/vsphere_cpi/lib/cloud/vsphere/vm_config.rb index 40645fe22..2c07685ab 100644 --- a/src/vsphere_cpi/lib/cloud/vsphere/vm_config.rb +++ b/src/vsphere_cpi/lib/cloud/vsphere/vm_config.rb @@ -98,6 +98,10 @@ def bosh_group end end + def vmx_options + vm_type['vmx_options'] || {} + end + private def validate_drs_rules diff --git a/src/vsphere_cpi/lib/cloud/vsphere/vm_creator.rb b/src/vsphere_cpi/lib/cloud/vsphere/vm_creator.rb index 1ac3e29fb..2b68473f8 100644 --- a/src/vsphere_cpi/lib/cloud/vsphere/vm_creator.rb +++ b/src/vsphere_cpi/lib/cloud/vsphere/vm_creator.rb @@ -73,9 +73,16 @@ def create(vm_config) replicated_stemcell_vm.fix_device_unit_numbers(config_spec.device_change) - @logger.info("Cloning vm: #{replicated_stemcell_vm} to #{vm_config.name}") + if vm_config.vmx_options.is_a?(Hash) + config_spec.extra_config = vm_config.vmx_options.keys.map { |key| + VimSdk::Vim::Option::OptionValue.new(key: key, value: vm_config.vmx_options[key]) + } + else + raise "Unable to parse vmx options: 'vmx_options' is not a Hash" + end # Clone VM + @logger.info("Cloning vm: #{replicated_stemcell_vm} to #{vm_config.name}") created_vm_mob = @client.wait_for_task do @cpi.clone_vm(replicated_stemcell_vm.mob, vm_config.name, diff --git a/src/vsphere_cpi/spec/integration/vmx_options_spec.rb b/src/vsphere_cpi/spec/integration/vmx_options_spec.rb new file mode 100644 index 000000000..3be4be876 --- /dev/null +++ b/src/vsphere_cpi/spec/integration/vmx_options_spec.rb @@ -0,0 +1,73 @@ +require 'integration/spec_helper' + +describe 'vmx_options' do + let(:vmx_option_key) { 'some_key' } + + let(:network_spec) do + { + 'static' => { + 'ip' => "169.254.#{rand(1..254)}.#{rand(4..254)}", + 'netmask' => '255.255.254.0', + 'cloud_properties' => {'name' => @vlan}, + 'default' => ['dns', 'gateway'], + 'dns' => ['169.254.1.2'], + 'gateway' => '169.254.1.3' + } + } + end + + context 'when vmx_options are provided as a Hash' do + let(:vm_type) do + { + 'ram' => 512, + 'disk' => 2048, + 'cpu' => 1, + 'vmx_options' => { + vmx_option_key => 'some_value' + } + } + end + + it 'can set the defined vmx_options as configuration properties' do + begin + vm_id = @cpi.create_vm( + 'agent-007', + @stemcell_id, + vm_type, + network_spec + ) + vm = @cpi.vm_provider.find(vm_id) + + found_vmx_option = vm.mob.config.extra_config.find {|c| c.key == vmx_option_key} + expect(found_vmx_option).not_to be_nil + expect(found_vmx_option.value).to eq('some_value') + ensure + delete_vm(@cpi, vm_id) + end + end + end + + context 'when vmx_options are NOT a Hash' do + let(:vm_type) do + { + 'ram' => 512, + 'disk' => 2048, + 'cpu' => 1, + 'vmx_options' => [ + vmx_option_key => 'some_value' + ] + } + end + + it 'can set the defined vmx_options as configuration properties' do + expect { + vm_id = @cpi.create_vm( + 'agent-007', + @stemcell_id, + vm_type, + network_spec + ) + }.to raise_error(/Unable to parse vmx options/) + end + end +end diff --git a/src/vsphere_cpi/spec/unit/cloud/vsphere/vm_config_spec.rb b/src/vsphere_cpi/spec/unit/cloud/vsphere/vm_config_spec.rb index fefaa6378..8fd4e3aee 100644 --- a/src/vsphere_cpi/spec/unit/cloud/vsphere/vm_config_spec.rb +++ b/src/vsphere_cpi/spec/unit/cloud/vsphere/vm_config_spec.rb @@ -686,5 +686,36 @@ module VSphereCloud end end end + + describe '#vmx_options' do + context 'vmx_options are specified' do + let(:input) do + { + vm_type: { + 'vmx_options' => { + 'sched.mem.maxmemctl' => '0' + } + } + } + end + + it 'should return the vmx_options' do + expect(vm_config.vmx_options).to eq({ 'sched.mem.maxmemctl' => '0' }) + end + end + + context 'vmx_options are NOT specified' do + let(:input) do + { + vm_type: {} + } + end + + it 'should return empty hash' do + expect(vm_config.vmx_options).to eq({}) + end + end + + end end end