def _create_iip(self, pod_name, pod_namespace, vn_obj, vmi): # Instance-ip for pods are ALWAYS allocated from pod ipam on this # VN. Get the subnet uuid of the pod ipam on this VN, so we can request # an IP from it. vn = VirtualNetworkKM.find_by_name_or_uuid(vn_obj.get_uuid()) if not vn: # It is possible our cache may not have the VN yet. Locate it. vn = VirtualNetworkKM.locate(vn_obj.get_uuid()) pod_ipam_subnet_uuid = vn.get_ipam_subnet_uuid( vnc_kube_config.pod_ipam_fq_name()) # Create instance-ip. display_name = VncCommon.make_display_name(pod_namespace, pod_name) iip_uuid = str(uuid.uuid1()) iip_name = VncCommon.make_name(pod_name, iip_uuid) iip_obj = InstanceIp(name=iip_name, subnet_uuid=pod_ipam_subnet_uuid, display_name=display_name) iip_obj.uuid = iip_uuid iip_obj.add_virtual_network(vn_obj) # Creation of iip requires the vmi vnc object. vmi_obj = self._vnc_lib.virtual_machine_interface_read( fq_name=vmi.fq_name) iip_obj.add_virtual_machine_interface(vmi_obj) InstanceIpKM.add_annotations(self, iip_obj, pod_namespace, pod_name) try: self._vnc_lib.instance_ip_create(iip_obj) except RefsExistError: self._vnc_lib.instance_ip_update(iip_obj) InstanceIpKM.locate(iip_obj.uuid) return iip_obj
def _create_iip(self, pod_name, pod_namespace, vn_obj, vmi): # Instance-ip for pods are ALWAYS allocated from pod ipam on this # VN. Get the subnet uuid of the pod ipam on this VN, so we can request # an IP from it. vn = VirtualNetworkKM.find_by_name_or_uuid(vn_obj.get_uuid()) if not vn: # It is possible our cache may not have the VN yet. Locate it. vn = VirtualNetworkKM.locate(vn_obj.get_uuid()) pod_ipam_subnet_uuid = vn.get_ipam_subnet_uuid( vnc_kube_config.pod_ipam_fq_name()) # Create instance-ip. display_name = VncCommon.make_display_name(pod_namespace, pod_name) iip_uuid = str(uuid.uuid1()) iip_name = VncCommon.make_name(pod_name, iip_uuid) iip_obj = InstanceIp(name=iip_name, subnet_uuid=pod_ipam_subnet_uuid, display_name=display_name) iip_obj.uuid = iip_uuid iip_obj.add_virtual_network(vn_obj) # Creation of iip requires the vmi vnc object. vmi_obj = self._vnc_lib.virtual_machine_interface_read( fq_name=vmi.fq_name) iip_obj.add_virtual_machine_interface(vmi_obj) InstanceIpKM.add_annotations(self, iip_obj, pod_namespace, pod_name) try: self._vnc_lib.instance_ip_create(iip_obj) except RefsExistError: self._vnc_lib.instance_ip_update(iip_obj) InstanceIpKM.locate(iip_obj.uuid) return iip_obj
def _create_iip(self, pod_name, pod_namespace, proj_uuid, vn_obj, vmi, vmi_ip): # Instance-ip for pods are ALWAYS allocated from pod ipam on this # VN. Get the subnet uuid of the pod ipam on this VN, so we can request # an IP from it. vn = VirtualNetworkKM.find_by_name_or_uuid(vn_obj.get_uuid()) if not vn: # It is possible our cache may not have the VN yet. Locate it. vn = VirtualNetworkKM.locate(vn_obj.get_uuid()) if self._is_pod_network_isolated(pod_namespace): vn_namespace = pod_namespace else: vn_namespace = 'default' if self._is_ip_fabric_forwarding_enabled(vn_namespace): ipam_fq_name = vnc_kube_config.ip_fabric_ipam_fq_name() else: ipam_fq_name = vnc_kube_config.pod_ipam_fq_name() pod_ipam_subnet_uuid = vn.get_ipam_subnet_uuid(ipam_fq_name) # Create instance-ip. iip_uuid = str(uuid.uuid1()) iip_name = VncCommon.make_name(pod_name, iip_uuid) perms2 = PermType2() perms2.owner = proj_uuid perms2.owner_access = cfgm_common.PERMS_RWX iip_obj = InstanceIp(name=iip_name, instance_ip_address=vmi_ip, subnet_uuid=pod_ipam_subnet_uuid, display_name=iip_name, perms2=perms2) iip_obj.uuid = iip_uuid iip_obj.add_virtual_network(vn_obj) # Creation of iip requires the vmi vnc object. vmi_obj = self._vnc_lib.virtual_machine_interface_read( fq_name=vmi.fq_name) iip_obj.add_virtual_machine_interface(vmi_obj) InstanceIpKM.add_annotations(self, iip_obj, pod_namespace, pod_name) self._logger.debug("%s: Create IIP from ipam_fq_name [%s]" " pod_ipam_subnet_uuid [%s]" " vn [%s] vmi_fq_name [%s]" % (self._name, ipam_fq_name, pod_ipam_subnet_uuid, vn.name, vmi.fq_name)) try: self._vnc_lib.instance_ip_create(iip_obj) except RefsExistError: self._vnc_lib.instance_ip_update(iip_obj) InstanceIpKM.locate(iip_obj.uuid) return iip_obj
def _delete_virtual_interface(self, vmi_ids): for vmi_id in vmi_ids: vmi = VirtualMachineInterfaceKM.get(vmi_id) if vmi: # Delete vmi-->floating-ip fip_ids = vmi.floating_ips.copy() for fip_id in fip_ids: try: self._vnc_lib.floating_ip_delete(id=fip_id) except NoIdError: # already deleted and not updated in the local-cache continue ip_ids = vmi.instance_ips.copy() for ip_id in ip_ids: ip = InstanceIpKM.get(ip_id) if ip: fip_ids = ip.floating_ips.copy() for fip_id in fip_ids: # Delete vmi-->instance-ip-->floating-ip try: self._vnc_lib.floating_ip_delete(id=fip_id) except NoIdError: # deleted by svc-monitor pass # Delete vmi-->instance-ip self._vnc_lib.instance_ip_delete(id=ip_id) # Delete vmi self.logger.debug("Deleting LB Interface %s" % vmi.name) self._vnc_lib.virtual_machine_interface_delete(id=vmi_id)
def _create_iip(self, pod_name, pod_namespace, vn_obj, vmi): # Instance-ip for pods are ALWAYS allocated from pod ipam on this # VN. Get the subnet uuid of the pod ipam on this VN, so we can request # an IP from it. vn = VirtualNetworkKM.find_by_name_or_uuid(vn_obj.get_uuid()) if not vn: # It is possible our cache may not have the VN yet. Locate it. vn = VirtualNetworkKM.locate(vn_obj.get_uuid()) if self._is_pod_network_isolated(pod_namespace): vn_namespace = pod_namespace else: vn_namespace = 'default' if self._is_ip_fabric_forwarding_enabled(vn_namespace): ipam_fq_name = vnc_kube_config.ip_fabric_ipam_fq_name() else: ipam_fq_name = vnc_kube_config.pod_ipam_fq_name() pod_ipam_subnet_uuid = vn.get_ipam_subnet_uuid(ipam_fq_name) # Create instance-ip. iip_uuid = str(uuid.uuid1()) iip_name = VncCommon.make_name(pod_name, iip_uuid) iip_obj = InstanceIp(name=iip_name, subnet_uuid=pod_ipam_subnet_uuid, display_name=iip_name) iip_obj.uuid = iip_uuid iip_obj.add_virtual_network(vn_obj) # Creation of iip requires the vmi vnc object. vmi_obj = self._vnc_lib.virtual_machine_interface_read( fq_name=vmi.fq_name) iip_obj.add_virtual_machine_interface(vmi_obj) InstanceIpKM.add_annotations(self, iip_obj, pod_namespace, pod_name) self._logger.debug("%s: Create IIP from ipam_fq_name [%s]" " pod_ipam_subnet_uuid [%s]" " vn [%s] vmi_fq_name [%s]" %\ (self._name, ipam_fq_name, pod_ipam_subnet_uuid, vn.name, vmi.fq_name)) try: self._vnc_lib.instance_ip_create(iip_obj) except RefsExistError: self._vnc_lib.instance_ip_update(iip_obj) InstanceIpKM.locate(iip_obj.uuid) return iip_obj
def _get_host_vm(host_ip): iip = InstanceIpKM.get_object( host_ip, vnc_kube_config.cluster_default_network_fq_name()) if iip: for vmi_id in iip.virtual_machine_interfaces: vm_vmi = VirtualMachineInterfaceKM.get(vmi_id) if vm_vmi and vm_vmi.virtual_machine: return vm_vmi.virtual_machine return None
def _get_host_vm(host_ip): iip = InstanceIpKM.get_object( host_ip, vnc_kube_config.cluster_default_network_fq_name()) if iip: for vmi_id in iip.virtual_machine_interfaces: vm_vmi = VirtualMachineInterfaceKM.get(vmi_id) if vm_vmi and vm_vmi.virtual_machine: return vm_vmi.virtual_machine return None
def create_virtual_machine(self, name, vn, ipaddress): vm = VirtualMachine(name) self._vnc_lib.virtual_machine_create(vm) VirtualMachineKM.locate(vm.uuid) vmi = VirtualMachineInterface( parent_type='virtual-machine', fq_name=[name, '0']) vmi.set_virtual_machine(vm) vmi.set_virtual_network(vn) self._vnc_lib.virtual_machine_interface_create(vmi) VirtualMachineInterfaceKM.locate(vmi.uuid) ip = InstanceIp(vm.name + '.0') ip.set_virtual_machine_interface(vmi) ip.set_virtual_network(vn) ip.set_instance_ip_address(ipaddress) self._vnc_lib.instance_ip_create(ip) InstanceIpKM.locate(ip.uuid) return vm, vmi, ip
def create_virtual_machine(self, name, vn, ipaddress): vm = VirtualMachine(name) self._vnc_lib.virtual_machine_create(vm) VirtualMachineKM.locate(vm.uuid) vmi = VirtualMachineInterface( parent_type='virtual-machine', fq_name=[name, '0']) vmi.set_virtual_machine(vm) vmi.set_virtual_network(vn) self._vnc_lib.virtual_machine_interface_create(vmi) VirtualMachineInterfaceKM.locate(vmi.uuid) ip = InstanceIp(vm.name + '.0') ip.set_virtual_machine_interface(vmi) ip.set_virtual_network(vn) ip.set_instance_ip_address(ipaddress) self._vnc_lib.instance_ip_create(ip) InstanceIpKM.locate(ip.uuid) return vm, vmi, ip
def _get_host_vmi(self, pod_name): host_ip = self._get_host_ip(pod_name) if host_ip: net_fq_name = vnc_kube_config.cluster_default_network_fq_name() iip = InstanceIpKM.get_object(host_ip, net_fq_name) if iip: for vmi_id in iip.virtual_machine_interfaces: vm_vmi = VirtualMachineInterfaceKM.get(vmi_id) if vm_vmi and vm_vmi.host_id: return vm_vmi return None
def _get_host_vmi(self, pod_name): host_ip = self._get_host_ip(pod_name) if host_ip: net_fq_name = vnc_kube_config.cluster_default_network_fq_name() iip = InstanceIpKM.get_object(host_ip, net_fq_name) if iip: for vmi_id in iip.virtual_machine_interfaces: vm_vmi = VirtualMachineInterfaceKM.get(vmi_id) if vm_vmi and vm_vmi.host_id: return vm_vmi return None
def create_virtual_machine(self, name, vn, ipaddress): vm = VirtualMachine(name) self._vnc_lib.virtual_machine_create(vm) VirtualMachineKM.locate(vm.uuid) vmi = VirtualMachineInterface( parent_type='virtual-machine', fq_name=[name, '0']) vmi.set_virtual_machine(vm) vmi.set_virtual_network(vn) if DBBaseKM.is_nested(): vmi.set_virtual_machine_interface_bindings( KeyValuePairs([KeyValuePair('host_id', 'WHATEVER')])) self._vnc_lib.virtual_machine_interface_create(vmi) VirtualMachineInterfaceKM.locate(vmi.uuid) ip = InstanceIp(vm.name + '.0') ip.set_virtual_machine_interface(vmi) ip.set_virtual_network(vn) ip.set_instance_ip_address(ipaddress) self._vnc_lib.instance_ip_create(ip) InstanceIpKM.locate(ip.uuid) return vm, vmi, ip
def create_virtual_machine(self, name, vn, ipaddress): vm = VirtualMachine(name) self._vnc_lib.virtual_machine_create(vm) VirtualMachineKM.locate(vm.uuid) vmi = VirtualMachineInterface(parent_type='virtual-machine', fq_name=[name, '0']) vmi.set_virtual_machine(vm) vmi.set_virtual_network(vn) if DBBaseKM.is_nested(): vmi.set_virtual_machine_interface_bindings( KeyValuePairs([KeyValuePair('host_id', 'WHATEVER')])) self._vnc_lib.virtual_machine_interface_create(vmi) VirtualMachineInterfaceKM.locate(vmi.uuid) ip = InstanceIp(vm.name + '.0') ip.set_virtual_machine_interface(vmi) ip.set_virtual_network(vn) ip.set_instance_ip_address(ipaddress) self._vnc_lib.instance_ip_create(ip) InstanceIpKM.locate(ip.uuid) return vm, vmi, ip
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 _add_pod_to_service(self, service_id, pod_id, port=None, address=None): lb = LoadbalancerKM.get(service_id) if not lb: return vm = VirtualMachineKM.get(pod_id) host_vmi = None if not vm: if not self._args.host_network_service: return host_vmi = self._get_vmi_from_ip(address) if host_vmi is None: return else: vm = VirtualMachine(name="host", display_name="host") vm.virtual_machine_interfaces = [host_vmi] for lb_listener_id in lb.loadbalancer_listeners: pool = self._get_loadbalancer_pool(lb_listener_id, port) if not pool: continue for vmi_id in vm.virtual_machine_interfaces: vmi = VirtualMachineInterfaceKM.get(vmi_id) if not vmi: continue if host_vmi is None: # Add VMI only if it matches the default address for endpoint, # ignore other interfaces for pod ip_found = False for iip_uuid in vmi.instance_ips: iip = InstanceIpKM.get(iip_uuid) if iip and iip.address == address: ip_found = True break if not ip_found: continue for member_id in pool.members: member = LoadbalancerMemberKM.get(member_id) if member and member.vmi == vmi_id: break else: self.logger.debug( "Creating LB member for Pod/VM: %s in LB: %s with " "target-port: %d" % (vm.fq_name, lb.name, port['port'])) member_obj = self._vnc_create_member( pool, pod_id, vmi_id, port['port']) vmi_obj = self._vnc_lib.virtual_machine_interface_read( id=vmi_id) # Attach the service label to underlying pod vmi. self._labels.append( vmi_id, self._labels.get_service_label(lb.service_name)) # Set tags on the vmi. self._vnc_lib.set_tags( vmi_obj, self._labels.get_labels_dict(vmi_id)) LoadbalancerMemberKM.locate(member_obj.uuid)
def _add_pod_to_service(self, service_id, pod_id, port=None, address=None): lb = LoadbalancerKM.get(service_id) if not lb: return vm = VirtualMachineKM.get(pod_id) host_vmi = None if not vm: if not self._args.host_network_service: return host_vmi = self._get_vmi_from_ip(address) if host_vmi == None: return else: vm = VirtualMachine(name="host", display_name="host") vm.virtual_machine_interfaces = [host_vmi] for lb_listener_id in lb.loadbalancer_listeners: pool = self._get_loadbalancer_pool(lb_listener_id, port) if not pool: continue for vmi_id in vm.virtual_machine_interfaces: if host_vmi == None: vmi = VirtualMachineInterfaceKM.get(vmi_id) else: vmi = self._vnc_lib.virtual_machine_interface_read(id=vmi_id) if not vmi: continue # Add VMI only if it matches the default address for endpoint, # ignore other interfaces for pod ip_found = False for iip_uuid in vmi.instance_ips: iip = InstanceIpKM.get(iip_uuid) if iip and iip.address == address: ip_found = True break if ip_found == False: continue for member_id in pool.members: member = LoadbalancerMemberKM.get(member_id) if member and member.vmi == vmi_id: break else: self.logger.debug( "Creating LB member for Pod/VM: %s in LB: %s with " "target-port: %d" % (vm.fq_name, lb.name, port['port'])) member_obj = self._vnc_create_member( pool, pod_id, vmi_id, port['port']) try: vmi_obj = self._vnc_lib.virtual_machine_interface_read( id = vmi_id) except: raise # Attach the service label to underlying pod vmi. self._labels.append(vmi_id, self._labels.get_service_label(lb.service_name)) # Set tags on the vmi. self._vnc_lib.set_tags(vmi_obj, self._labels.get_labels_dict(vmi_id)) LoadbalancerMemberKM.locate(member_obj.uuid)