def _remove_unused_members(self, loadbalancer_crd): lb_crd_name = loadbalancer_crd['metadata']['name'] spec_ports = {} pools = loadbalancer_crd['status'].get('pools', []) for pool in pools: port = self._get_port_in_pool(pool, loadbalancer_crd) if port: if not port.get('name'): port['name'] = None spec_ports[port['name']] = pool['id'] ep_slices = loadbalancer_crd['spec'].get('endpointSlices', []) current_targets = [utils.get_current_endpoints_target( ep, p, spec_ports, lb_crd_name) for ep_slice in ep_slices for ep in ep_slice['endpoints'] for p in ep_slice['ports'] if p.get('name') in spec_ports] removed_ids = set() for member in loadbalancer_crd['status'].get('members', []): member_name = member.get('name', '') try: # NOTE: The member name is compose of: # NAMESPACE_NAME/POD_NAME:PROTOCOL_PORT pod_name = member_name.split('/')[1].split(':')[0] except AttributeError: pod_name = "" if ((str(member['ip']), pod_name, member['port'], member[ 'pool_id']) in current_targets): continue self._drv_lbaas.release_member(loadbalancer_crd['status'][ 'loadbalancer'], member) removed_ids.add(member['id']) if removed_ids: loadbalancer_crd['status']['members'] = [m for m in loadbalancer_crd[ 'status'][ 'members'] if m['id'] not in removed_ids] kubernetes = clients.get_kubernetes_client() try: kubernetes.patch_crd('status', utils.get_res_link(loadbalancer_crd), loadbalancer_crd['status']) except k_exc.K8sResourceNotFound: LOG.debug('KuryrLoadbalancer CRD not found %s', loadbalancer_crd) except k_exc.K8sClientException: LOG.exception('Error updating KuryLoadbalancer CRD %s', loadbalancer_crd) raise return bool(removed_ids)
def test_get_current_endpoints_target_without_target_ref(self): ep = {'addresses': ['10.0.1.208'], 'conditions': {'ready': True}} port = {'port': 8080, 'protocol': 'TCP'} spec_ports = {None: '4472fab1-f01c-46a7-b197-5cba4f2d7135'} ep_name = 'test' target = utils.get_current_endpoints_target(ep, port, spec_ports, ep_name) self.assertEqual(target, ('10.0.1.208', 'test', 8080, '4472fab1-f01c-46a7-b197-5cba4f2d7135'))
def _remove_unused_members(self, loadbalancer_crd): lb_crd_name = loadbalancer_crd['metadata']['name'] spec_ports = {} pools = loadbalancer_crd['status'].get('pools', []) for pool in pools: port = self._get_port_in_pool(pool, loadbalancer_crd) if port: if not port.get('name'): port['name'] = None spec_ports[port['name']] = pool['id'] ep_slices = loadbalancer_crd['spec'].get('endpointSlices', []) current_targets = [ utils.get_current_endpoints_target(ep, p, spec_ports, lb_crd_name) for ep_slice in ep_slices for ep in ep_slice['endpoints'] for p in ep_slice['ports'] if p.get('name') in spec_ports ] removed_ids = set() for member in loadbalancer_crd['status'].get('members', []): member_name = member.get('name', '') try: # NOTE: The member name is compose of: # NAMESPACE_NAME/POD_NAME:PROTOCOL_PORT pod_name = member_name.split('/')[1].split(':')[0] except AttributeError: pod_name = "" if ((str(member['ip']), pod_name, member['port'], member['pool_id']) in current_targets): continue # NOTE(maysams): The Endpoints object of kubernetes service is # unstable during installation, so to minimize # effect of this we're only removing node once # we're sure it's gone from the deployment. That's # done by looking for host-networking pods with # that IP. if ('kubernetes' == lb_crd_name and loadbalancer_crd['spec']['provider'] != 'ovn' and utils.get_pod_by_ip(member['ip'])): continue self._drv_lbaas.release_member( loadbalancer_crd['status']['loadbalancer'], member) removed_ids.add(member['id']) if removed_ids: members = [ m for m in loadbalancer_crd['status'].get('members', []) if m['id'] not in removed_ids ] loadbalancer_crd['status']['members'] = members if not self._patch_status(loadbalancer_crd): return False return bool(removed_ids)
def _remove_unused_members(self, loadbalancer_crd): lb_crd_name = loadbalancer_crd['metadata']['name'] spec_ports = {} pools = loadbalancer_crd['status'].get('pools', []) for pool in pools: port = self._get_port_in_pool(pool, loadbalancer_crd) if port: if not port.get('name'): port['name'] = None spec_ports[port['name']] = pool['id'] ep_slices = loadbalancer_crd['spec'].get('endpointSlices', []) current_targets = [ utils.get_current_endpoints_target(ep, p, spec_ports, lb_crd_name) for ep_slice in ep_slices for ep in ep_slice['endpoints'] for p in ep_slice['ports'] if p.get('name') in spec_ports ] removed_ids = set() for member in loadbalancer_crd['status'].get('members', []): member_name = member.get('name', '') try: # NOTE: The member name is compose of: # NAMESPACE_NAME/POD_NAME:PROTOCOL_PORT pod_name = member_name.split('/')[1].split(':')[0] except AttributeError: pod_name = "" if ((str(member['ip']), pod_name, member['port'], member['pool_id']) in current_targets): continue self._drv_lbaas.release_member( loadbalancer_crd['status']['loadbalancer'], member) removed_ids.add(member['id']) if removed_ids: members = [ m for m in loadbalancer_crd['status'].get('members', []) if m['id'] not in removed_ids ] loadbalancer_crd['status']['members'] = members if not self._patch_status(loadbalancer_crd): return False return bool(removed_ids)
def test_get_current_endpoints_target_with_target_ref(self): ep = { 'addresses': ['10.0.2.107'], 'conditions': { 'ready': True }, 'targetRef': { 'kind': 'Pod', 'name': 'test-868d9cbd68-xq2fl', 'namespace': 'test2' } } port = {'port': 8080, 'protocol': 'TCP'} spec_ports = {None: '31d59e41-05db-4a39-8aca-6a9a572c83cd'} ep_name = 'test' target = utils.get_current_endpoints_target(ep, port, spec_ports, ep_name) self.assertEqual(target, ('10.0.2.107', 'test-868d9cbd68-xq2fl', 8080, '31d59e41-05db-4a39-8aca-6a9a572c83cd'))