def test_vm_port_not_added_to_lr(self): project = self._vnc_lib.project_read( ['default-domain', 'default-project']) ipam = self._vnc_lib.network_ipam_read( ['default-domain', 'default-project', 'default-network-ipam']) # Create subnets ipam_sn_v4_vn = IpamSubnetType(subnet=SubnetType('11.1.1.0', 24)) # Create VN my-vn vn = VirtualNetwork('%s-vn' % self.id(), project) vn.add_network_ipam(ipam, VnSubnetsType([ipam_sn_v4_vn])) self._vnc_lib.virtual_network_create(vn) net_obj = self._vnc_lib.virtual_network_read(id=vn.uuid) # Create v4 Ip object ip_obj = InstanceIp(name=str(uuid.uuid4()), instance_ip_family='v4') ip_obj.uuid = ip_obj.name # Create VM vm_inst_obj = VirtualMachine(str(uuid.uuid4())) vm_inst_obj.uuid = vm_inst_obj.name self._vnc_lib.virtual_machine_create(vm_inst_obj) id_perms = IdPermsType(enable=True) vm_port_obj = VirtualMachineInterface(str(uuid.uuid4()), vm_inst_obj, id_perms=id_perms) vm_port_obj.uuid = vm_port_obj.name vm_port_obj.set_virtual_network(vn) ip_obj.set_virtual_machine_interface(vm_port_obj) ip_obj.set_virtual_network(net_obj) self._vnc_lib.virtual_machine_interface_create(vm_port_obj) self._vnc_lib.instance_ip_create(ip_obj) # Create Logical Router lr = LogicalRouter('router-test-v4-%s' % self.id(), project) self._vnc_lib.logical_router_create(lr) # Add Router Interface lr.add_virtual_machine_interface(vm_port_obj) with ExpectedException(RefsExistError): self._vnc_lib.logical_router_update(lr) lr.del_virtual_machine_interface(vm_port_obj) # Create Port port_obj = self.create_port(project, net_obj) lr.add_virtual_machine_interface(port_obj) self._vnc_lib.logical_router_update(lr) with ExpectedException(BadRequest): port_obj.add_virtual_machine(vm_inst_obj) self._vnc_lib.virtual_machine_interface_update(port_obj) self._vnc_lib.logical_router_delete(id=lr.uuid)
def _create_vmi(self, pod_name, pod_namespace, pod_id, vm_obj, vn_obj, proj_obj, parent_vmi, idx, network=None): if network and 'namespace' in network: network.pop('namespace') vmi_prop = None if self._is_pod_nested() and parent_vmi: # Pod is nested. # Allocate a vlan-id for this pod from the vlan space managed # in the VMI of the underlay VM. parent_vmi = VirtualMachineInterfaceKM.get(parent_vmi.uuid) vlan_id = parent_vmi.alloc_vlan() vmi_prop = VirtualMachineInterfacePropertiesType( sub_interface_vlan_tag=vlan_id) obj_uuid = str(uuid.uuid1()) name = VncCommon.make_name(pod_name, obj_uuid) vmi_obj = VirtualMachineInterface( name=name, parent_obj=proj_obj, virtual_machine_interface_properties=vmi_prop, display_name=name) vmi_obj.uuid = obj_uuid vmi_obj.set_virtual_network(vn_obj) vmi_obj.set_virtual_machine(vm_obj) self._associate_security_groups(vmi_obj, proj_obj, pod_namespace) vmi_obj.port_security_enabled = True VirtualMachineInterfaceKM.add_annotations(self, vmi_obj, pod_namespace, pod_name, index=idx, **network) try: vmi_uuid = self._vnc_lib.virtual_machine_interface_create(vmi_obj) except RefsExistError: vmi_uuid = self._vnc_lib.virtual_machine_interface_update(vmi_obj) VirtualMachineInterfaceKM.locate(vmi_uuid) return vmi_uuid
def create_port(self, project, vn): # Create v4 Ip object ip_obj = InstanceIp(name=str(uuid.uuid4()), instance_ip_family='v4') ip_obj.uuid = ip_obj.name # Create Port id_perms = IdPermsType(enable=True) port_obj = VirtualMachineInterface(str(uuid.uuid4()), parent_obj=project, id_perms=id_perms) port_obj.uuid = port_obj.name port_obj.set_virtual_network(vn) ip_obj.set_virtual_machine_interface(port_obj) ip_obj.set_virtual_network(vn) self._vnc_lib.virtual_machine_interface_create(port_obj) self._vnc_lib.instance_ip_create(ip_obj) return port_obj
def _create_virtual_interface(self, proj_obj, vn_obj, service_ns, service_name, service_id, k8s_event_type, vip_address=None, subnet_uuid=None, tags=None): vmi_uuid = str(uuid.uuid4()) cluster_name = vnc_kube_config.cluster_name() vmi_name = VncCommon.make_name(cluster_name, k8s_event_type, service_name, service_id) vmi_display_name = VncCommon.make_display_name(service_ns, service_name) # Check if VMI exists, if yes, delete it. vmi_obj = VirtualMachineInterface(name=vmi_name, parent_obj=proj_obj, display_name=vmi_display_name) try: vmi_id = self._vnc_lib.fq_name_to_id('virtual-machine-interface', vmi_obj.get_fq_name()) if vmi_id: self.logger.error("Duplicate LB Interface %s, delete it" % vmi_obj.get_fq_name()) vmi = VirtualMachineInterfaceKM.get(vmi_id) iip_ids = vmi.instance_ips for iip_id in list(iip_ids): iip_obj = self._vnc_lib.instance_ip_read(id=iip_id) fip_refs = iip_obj.get_floating_ips() for fip_ref in fip_refs or []: fip = self._vnc_lib.floating_ip_read( id=fip_ref['uuid']) fip.set_virtual_machine_interface_list([]) self._vnc_lib.floating_ip_update(fip) self._vnc_lib.floating_ip_delete(id=fip_ref['uuid']) self._vnc_lib.instance_ip_delete(id=iip_obj.uuid) self._vnc_lib.virtual_machine_interface_delete(id=vmi_id) except NoIdError: pass # Create LB VMI vmi_obj.name = vmi_name vmi_obj.uuid = vmi_uuid vmi_obj.set_virtual_network(vn_obj) vmi_obj.set_virtual_machine_interface_device_owner("K8S:LOADBALANCER") sg_name = "-".join( [vnc_kube_config.cluster_name(), service_ns, 'default-sg']) sg_obj = SecurityGroup(sg_name, proj_obj) vmi_obj.add_security_group(sg_obj) vmi_obj.port_security_enabled = True try: self.logger.debug("Create LB Interface %s " % vmi_obj.get_fq_name()) self._vnc_lib.virtual_machine_interface_create(vmi_obj) VirtualMachineInterfaceKM.locate(vmi_obj.uuid) except BadRequest as e: self.logger.warning("LB (%s) Interface create failed %s " % (service_name, str(e))) return None, None try: vmi_obj = self._vnc_lib.virtual_machine_interface_read( id=vmi_obj.uuid) except NoIdError: self.logger.warning("Read Service VMI failed for" " service (" + service_name + ")" + " with NoIdError for vmi(" + vmi_id + ")") return None, None # Attach tags on this VMI. if tags: self._vnc_lib.set_tags(vmi_obj, tags) # Create InstanceIP <--- LB VMI iip_uuid = str(uuid.uuid4()) iip_name = VncCommon.make_name(service_name, iip_uuid) iip_display_name = VncCommon.make_display_name(service_ns, service_name) perms2 = PermType2() perms2.owner = proj_obj.uuid perms2.owner_access = cfgm_common.PERMS_RWX iip_obj = InstanceIp(name=iip_name, perms2=perms2, display_name=iip_display_name) iip_obj.uuid = iip_uuid iip_obj.set_virtual_network(vn_obj) if subnet_uuid: iip_obj.set_subnet_uuid(subnet_uuid) iip_obj.set_virtual_machine_interface(vmi_obj) iip_obj.set_display_name(service_name) if vip_address: iip_obj.set_instance_ip_address(vip_address) try: self.logger.debug("Create LB VMI InstanceIp %s " % iip_obj.get_fq_name()) self._vnc_lib.instance_ip_create(iip_obj) except RefsExistError: self._vnc_lib.instance_ip_update(iip_obj) InstanceIpKM.locate(iip_obj.uuid) iip_obj = self._vnc_lib.instance_ip_read(id=iip_obj.uuid) vip_address = iip_obj.get_instance_ip_address() self.logger.debug("Created LB VMI InstanceIp %s with VIP %s" % (iip_obj.get_fq_name(), vip_address)) return vmi_obj, vip_address
def test_lr_v4_subnets(self): # Create Domain domain = Domain('my-lr-domain') self._vnc_lib.domain_create(domain) # Create Project project = Project('my-lr-proj', domain) self._vnc_lib.project_create(project) # Create NetworkIpam ipam = NetworkIpam('default-network-ipam', project, IpamType("dhcp")) self._vnc_lib.network_ipam_create(ipam) ipam = self._vnc_lib.network_ipam_read( ['my-lr-domain', 'my-lr-proj', 'default-network-ipam']) # Create subnets ipam_sn_v4_vn1 = IpamSubnetType(subnet=SubnetType('11.1.1.0', 24)) ipam_sn_v6_vn1 = IpamSubnetType(subnet=SubnetType('fd11::', 120)) ipam_sn_v4_vn2 = IpamSubnetType(subnet=SubnetType('11.1.2.0', 24)) ipam_sn_v6_vn2 = IpamSubnetType(subnet=SubnetType('fd12::', 120)) # Create VN my-vn-1 vn1 = VirtualNetwork('my-vn-1', project) vn1.add_network_ipam(ipam, VnSubnetsType([ipam_sn_v4_vn1, ipam_sn_v6_vn1])) self._vnc_lib.virtual_network_create(vn1) net_obj1 = self._vnc_lib.virtual_network_read(id=vn1.uuid) # Create VN my-vn-2 vn2 = VirtualNetwork('my-vn-2', project) vn2.add_network_ipam(ipam, VnSubnetsType([ipam_sn_v4_vn2, ipam_sn_v6_vn2])) self._vnc_lib.virtual_network_create(vn2) net_obj2 = self._vnc_lib.virtual_network_read(id=vn2.uuid) # Create Logical Router lr = LogicalRouter('router-test-v4-%s' % self.id(), project) lr_uuid = self._vnc_lib.logical_router_create(lr) # Create a Virtual Machine Interface belonging to my-vn-1 id_perms = IdPermsType(enable=True) port_obj1 = VirtualMachineInterface(str(uuid.uuid4()), parent_obj=project, id_perms=id_perms) port_obj1.uuid = port_obj1.name port_obj1.set_virtual_network(vn1) port_obj1.set_virtual_machine_interface_device_owner( 'DEVICE_OWNER_ROUTER_INTF') # Assign gateway ip ipam_refs = net_obj1.get_network_ipam_refs() for ipam_ref in ipam_refs: subnets = ipam_ref['attr'].get_ipam_subnets() for subnet in subnets: cidr = '%s/%s' % (subnet.subnet.get_ip_prefix(), subnet.subnet.get_ip_prefix_len()) if IPNetwork(cidr).version == 4: gateway_ip = subnet.get_default_gateway() self._vnc_lib.virtual_machine_interface_create(port_obj1) # Create v4 Ip object ip_obj1 = InstanceIp(name=str(uuid.uuid4()), instance_ip_address=gateway_ip, instance_ip_family='v4') ip_obj1.uuid = ip_obj1.name ip_obj1.set_virtual_machine_interface(port_obj1) ip_obj1.set_virtual_network(net_obj1) ip_id1 = self._vnc_lib.instance_ip_create(ip_obj1) # Add Router Interface (test being subnet) lr.add_virtual_machine_interface(port_obj1) self._vnc_lib.logical_router_update(lr) # Create a Virtual Machine Interface belonging to my-vn-2 port_obj2 = VirtualMachineInterface(str(uuid.uuid4()), parent_obj=project, id_perms=id_perms) port_obj2.uuid = port_obj2.name port_obj2.set_virtual_network(vn2) port_obj2.set_virtual_machine_interface_device_owner( 'DEVICE_OWNER_ROUTER_INTF') # Assign gateway ip ipam_refs = net_obj2.get_network_ipam_refs() for ipam_ref in ipam_refs: subnets = ipam_ref['attr'].get_ipam_subnets() for subnet in subnets: cidr = '%s/%s' % (subnet.subnet.get_ip_prefix(), subnet.subnet.get_ip_prefix_len()) if IPNetwork(cidr).version == 4: gateway_ip = subnet.get_default_gateway() self._vnc_lib.virtual_machine_interface_create(port_obj2) # Create v4 Ip object ip_obj2 = InstanceIp(name=str(uuid.uuid4()), instance_ip_address=gateway_ip, instance_ip_family='v4') ip_obj2.uuid = ip_obj2.name ip_obj2.set_virtual_machine_interface(port_obj2) ip_obj2.set_virtual_network(net_obj2) ip_id2 = self._vnc_lib.instance_ip_create(ip_obj2) # Add Router Interface (test being subnet) lr.add_virtual_machine_interface(port_obj2) self._vnc_lib.logical_router_update(lr) # TODO: Schema transformer not integrated in the tests, # hence route-target refs not set yet # Verify Route Target Creation rt_refs = lr.get_route_target_refs() for rt_ref in rt_refs or []: rt_obj = self._vnc_lib.route_target_read(id=rt_ref['uuid']) ri_refs = rt_obj.get_routing_instance_back_refs() for ri_ref in ri_refs: ri_obj = self.vnc_lib.routing_instance_read(id=ri_ref['uuid']) ri_name = ri_obj.get_display_name() if ri_name != 'my-vn-1' and ri_name != 'my-vn-2': pass # cleanup self._vnc_lib.instance_ip_delete(id=ip_id1) self._vnc_lib.instance_ip_delete(id=ip_id2) self._vnc_lib.logical_router_delete(id=lr_uuid) self._vnc_lib.virtual_machine_interface_delete(id=port_obj1.uuid) self._vnc_lib.virtual_machine_interface_delete(id=port_obj2.uuid) self._vnc_lib.virtual_network_delete(id=vn1.uuid) self._vnc_lib.virtual_network_delete(id=vn2.uuid) self._vnc_lib.network_ipam_delete(id=ipam.uuid) self._vnc_lib.project_delete(id=project.uuid) self._vnc_lib.domain_delete(id=domain.uuid)