Skip to content

Commit

Permalink
Accommodate non-NSX-T Distributed Virtual Switches
Browse files Browse the repository at this point in the history
Previously the vSphere CPI NSX-T Policy API assumed _all_ Distributed
Virtual Switches (DVSes) were NSX-T Segments, but that assummption was
false; DVSes may be managed by vSphere and not NSX-T.

We now check whether the network interface controller (NIC) is
NSX-T-managed before attempting to tag the NIC's port with NSX-T
metadata.

fixes:
```
unable to create bosh vm:CPI 'set_vm_metadata' method responded with error: CmdError{"type":"Unknown","message":"Invalid Query","ok_to_retry":false}
```

[fixes #302]
[#179334136](https://www.pivotaltracker.com/story/show/179334136)

Signed-off-by: Brian Cunnie <bcunnie@vmware.com>
  • Loading branch information
julian-hj authored and Brian Cunnie committed Aug 26, 2021
1 parent 38514b7 commit e421942
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/vsphere_cpi/lib/cloud/vsphere/resources/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,20 @@ def system_disk
end

def get_nsxt_segment_vif_list
nsxt_nics = get_nsxt_nics
if nsxt_nics.empty?
potential_nsxt_nics = get_potential_nsxt_nics
if potential_nsxt_nics.empty?
logger.info("No NSXT network/nic present on the VM")
return nil
end
nsxt_networks_id_name_map = get_nsxt_networks_id_name_map
nsxt_nics.reduce([]) do |list, nic|
potential_nsxt_nics.reduce([]) do |list, nic|
if nic.backing.is_a?(VimSdk::Vim::Vm::Device::VirtualEthernetCard::DistributedVirtualPortBackingInfo)
segment_name = nsxt_networks_id_name_map[nic.backing.port.portgroup_key]
else
segment_name = nsxt_networks_id_name_map[nic.backing.opaque_network_id]
end
port_id = nic.external_id
list << [segment_name, port_id]
list << [segment_name, port_id] unless segment_name.nil?
end
end

Expand Down Expand Up @@ -419,7 +419,7 @@ def self.create_edit_device_spec(device)

private

def get_nsxt_nics
def get_potential_nsxt_nics
nsxt_nics = nics.select do |nic|
nic.backing.is_a?(VimSdk::Vim::Vm::Device::VirtualEthernetCard::DistributedVirtualPortBackingInfo) ||
( nic.backing.is_a?(VimSdk::Vim::Vm::Device::VirtualEthernetCard::OpaqueNetworkBackingInfo) &&
Expand Down
35 changes: 35 additions & 0 deletions src/vsphere_cpi/spec/integration/nsxt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,41 @@ def initialize(name: "BOSH-CPI-test-#{SecureRandom.uuid}", id: SecureRandom.uuid
expect(server_pool_2.members).to be_nil
end
end

context 'with non-nsxt distributed virtual switches' do
let(:nsxt_spec) { {} }
let(:dvpg_name) { ENV.fetch('BOSH_VSPHERE_CPI_FOLDER_PORTGROUP_ONE') }
let(:policy_network_spec) do
{
'static-bridged' => {
'ip' => "169.254.#{rand(1..254)}.#{rand(4..254)}",
'netmask' => '255.255.254.0',
'cloud_properties' => { 'name' => segment_1.name },
'default' => ['dns', 'gateway'],
'dns' => ['169.254.1.2'],
'gateway' => '169.254.1.3'
},
'static' => {
'ip' => "169.254.#{rand(1..254)}.#{rand(4..254)}",
'netmask' => '255.255.254.0',
'cloud_properties' => { 'name' => dvpg_name },
'default' => ['dns', 'gateway'],
'dns' => ['169.254.1.2'],
'gateway' => '169.254.1.3'
}
}
end
it 'creates a VM without errors' do
simple_vm_lifecycle(cpi, '', vm_type, policy_network_spec) do |vm_id|
cpi.set_vm_metadata(vm_id, {'id' => 'foo'})

vm = @cpi.vm_provider.find(vm_id)
segment_names = vm.get_nsxt_segment_vif_list.map { |x| x[0] }
expect(segment_names.length).to eq(1)
expect(segment_names).to include(segment_1.name)
end
end
end
end
end

Expand Down
28 changes: 28 additions & 0 deletions src/vsphere_cpi/spec/unit/cloud/vsphere/resources/vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -651,4 +651,32 @@
end
end
end

describe '#get_nsxt_segment_vif_list' do
let (:nic0) { instance_double(VimSdk::Vim::Vm::Device::VirtualEthernetCard) }
before(:each) do
expect(subject).to receive(:get_potential_nsxt_nics).and_return([nic0])
allow(nic0).to receive_message_chain(:backing, :is_a?).and_return(true)
expect(nic0).to receive(:external_id).and_return('nic-external-id')
allow(nic0).to receive_message_chain(:backing, :port, :portgroup_key).and_return('nic-portgroup-key')
end

context "when the NIC is an NSX-T DVS NIC" do
before do
allow(subject).to receive(:get_nsxt_networks_id_name_map).and_return({'nic-portgroup-key' => 'some-segment-name'})
end
it "is not returned in the list of NSX-T vifs" do
expect(subject.get_nsxt_segment_vif_list).to match_array([["some-segment-name", "nic-external-id"]])
end
end

context "when the NIC is a DVS NIC but NOT an NSX-T DVS nic" do
before do
allow(subject).to receive(:get_nsxt_networks_id_name_map).and_return({})
end
it "is not returned in the list of NSX-T vifs" do
expect(subject.get_nsxt_segment_vif_list).to be_nil
end
end
end
end

0 comments on commit e421942

Please sign in to comment.