def vnc_port_delete(self, vmi_id, pod_id): self._unset_tags_on_pod_vmi(pod_id, vmi_id=vmi_id) vmi = VirtualMachineInterfaceKM.get(vmi_id) if not vmi: return for iip_id in list(vmi.instance_ips): try: self._vnc_lib.instance_ip_delete(id=iip_id) except NoIdError: pass # Cleanup floating ip's on this interface. for fip_id in list(vmi.floating_ips): try: self._vnc_lib.ref_update('floating-ip', fip_id, 'virtual-machine-interface', vmi_id, None, 'DELETE') FloatingIpKM.update(fip_id) except NoIdError: pass try: self._vnc_lib.virtual_machine_interface_delete(id=vmi_id) except NoIdError: pass VirtualMachineInterfaceKM.delete(vmi_id)
def _get_floating_ip(self, name, ns_name, proj_obj, external_ip=None, vmi_obj=None, specified_fip_pool_fq_name_str=None): fip_pool_fq_name = None if specified_fip_pool_fq_name_str is not None: fip_pool_fq_name = get_fip_pool_fq_name_from_dict_string( specified_fip_pool_fq_name_str) if fip_pool_fq_name is None: ns = self._get_namespace(ns_name) fip_pool_fq_name = ns.get_annotated_ns_fip_pool_fq_name() if fip_pool_fq_name is None: if not vnc_kube_config.is_public_fip_pool_configured(): return None try: fip_pool_fq_name = get_fip_pool_fq_name_from_dict_string( self._args.public_fip_pool) except Exception: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self._logger.error("%s - %s" % (self._name, err_msg)) return None if vmi_obj: fip_refs = vmi_obj.get_floating_ip_back_refs() for ref in fip_refs or []: fip = FloatingIpKM.get(ref['uuid']) if fip and fip.fq_name[:-1] == fip_pool_fq_name: return fip else: break fip_pool = self._get_fip_pool_obj(fip_pool_fq_name) if fip_pool is None: return None fip_uuid = str(uuid.uuid4()) fip_name = VncCommon.make_name(name, fip_uuid) fip_obj = FloatingIp(fip_name, fip_pool) fip_obj.uuid = fip_uuid fip_obj.set_project(proj_obj) if vmi_obj: fip_obj.set_virtual_machine_interface(vmi_obj) if external_ip: fip_obj.floating_ip_address = external_ip try: self._vnc_lib.floating_ip_create(fip_obj) fip = FloatingIpKM.locate(fip_obj.uuid) except Exception: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self._logger.error("%s - %s" % (self._name, err_msg)) return None return fip
def _deallocate_floating_ip(self, lb): vmi_id = list(lb.virtual_machine_interfaces)[0] vmi = VirtualMachineInterfaceKM.get(vmi_id) if vmi is None: self._logger.error("%s - %s Vmi %s Not Found" % (self._name, lb.name, vmi_id)) return fip_list = vmi.floating_ips.copy() for fip_id in fip_list or []: fip_obj = self._vnc_lib.floating_ip_read(id=fip_id) fip_obj.set_virtual_machine_interface_list([]) self._vnc_lib.floating_ip_update(fip_obj) self._vnc_lib.floating_ip_delete(id=fip_obj.uuid) FloatingIpKM.delete(fip_obj.uuid)
def _allocate_floating_ip(lb, vmi, fip_pool, external_ip=None): fip_obj = FloatingIp(lb.name + str(external_ip) + "-externalIP", fip_pool) fip_obj.set_virtual_machine_interface(vmi_obj) if external_ip: if not (_check_ip_with_fip_pool(external_ip, fip_pool)): err_str = "external_ip " + external_ip + " not in fip_pool subnet" self.logger.error(err_str) return None fip_obj.set_floating_ip_address(external_ip) project = self._vnc_lib.project_read(id=lb.parent_uuid) fip_obj.set_project(project) try: self._vnc_lib.floating_ip_create(fip_obj) except RefsExistError: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self.logger.error("%s" % (err_msg)) except Exception: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self.logger.error("%s" % (err_msg)) fip = FloatingIpKM.locate(fip_obj.uuid) self.logger.notice("floating ip allocated : %s for Service (%s)" % (fip.address, service_id)) return (fip.address)
def _read_allocated_floating_ips(self, service_id): floating_ips = set() lb = LoadbalancerKM.get(service_id) if not lb: return vmi_ids = lb.virtual_machine_interfaces if vmi_ids is None: return None interface_found = False for vmi_id in vmi_ids: vmi = VirtualMachineInterfaceKM.get(vmi_id) if vmi is not None: interface_found = True break if not interface_found: return fip_ids = vmi.floating_ips if fip_ids is None: return None for fip_id in list(fip_ids): fip = FloatingIpKM.get(fip_id) if fip is not None: floating_ips.add(fip.address) return floating_ips
def _create_cluster_service_fip(self, pod_name, pod_namespace, vmi_uuid): """ Isolated Pods in the cluster will be allocated a floating ip from the cluster service network, so that the pods can talk to cluster services. """ if not self._service_fip_pool: return # Construct parent ref. fip_pool_obj = FloatingIpPool() fip_pool_obj.uuid = self._service_fip_pool.uuid fip_pool_obj.fq_name = self._service_fip_pool.fq_name fip_pool_obj.name = self._service_fip_pool.name # Create Floating-Ip object. obj_uuid = str(uuid.uuid1()) display_name = VncCommon.make_display_name(pod_namespace, pod_name) name = VncCommon.make_name(pod_name, obj_uuid) fip_obj = FloatingIp(name="cluster-svc-fip-%s"% (name), parent_obj=fip_pool_obj, floating_ip_traffic_direction='egress', display_name=display_name) fip_obj.uuid = obj_uuid # Creation of fip requires the vmi vnc object. vmi_obj = self._vnc_lib.virtual_machine_interface_read(id=vmi_uuid) fip_obj.set_virtual_machine_interface(vmi_obj) FloatingIpKM.add_annotations(self, fip_obj, pod_namespace, pod_name) try: fip_uuid = self._vnc_lib.floating_ip_create(fip_obj) except RefsExistError: fip_uuid = self._vnc_lib.floating_ip_update(fip_obj) # Cached service floating ip. FloatingIpKM.locate(fip_uuid) return
def _create_cluster_service_fip(self, pod_name, pod_namespace, vmi_uuid): """ Isolated Pods in the cluster will be allocated a floating ip from the cluster service network, so that the pods can talk to cluster services. """ if not self._service_fip_pool: return # Construct parent ref. fip_pool_obj = FloatingIpPool() fip_pool_obj.uuid = self._service_fip_pool.uuid fip_pool_obj.fq_name = self._service_fip_pool.fq_name fip_pool_obj.name = self._service_fip_pool.name # Create Floating-Ip object. obj_uuid = str(uuid.uuid1()) display_name = VncCommon.make_display_name(pod_namespace, pod_name) name = VncCommon.make_name(pod_name, obj_uuid) fip_obj = FloatingIp(name="cluster-svc-fip-%s" % (name), parent_obj=fip_pool_obj, floating_ip_traffic_direction='egress', display_name=display_name) fip_obj.uuid = obj_uuid # Creation of fip requires the vmi vnc object. vmi_obj = self._vnc_lib.virtual_machine_interface_read(id=vmi_uuid) fip_obj.set_virtual_machine_interface(vmi_obj) FloatingIpKM.add_annotations(self, fip_obj, pod_namespace, pod_name) try: fip_uuid = self._vnc_lib.floating_ip_create(fip_obj) except RefsExistError: fip_uuid = self._vnc_lib.floating_ip_update(fip_obj) # Cached service floating ip. FloatingIpKM.locate(fip_uuid) return