def sandesh_handle_db_list_request(cls, req): """ Reply to Network Policy DB lookup/introspect request. """ np_resp = introspect.NetworkPolicyDatabaseListResp(network_policies=[]) # Iterate through all elements of Network Policy DB. for np in NetworkPolicyKM.values(): # If the request is for a specific entry, then locate the entry. if req.network_policy_uuid and req.network_policy_uuid != np.uuid: continue # Parse "ingress" attribute. np_ingress_list = [] if np.spec.get('ingress'): for ingress in np.spec.get('ingress'): # Parse "from" attribute. from_list = [] if ingress.get('from'): for each_from in ingress.get('from'): np_pod_selector = None if each_from.get('podSelector'): np_pod_selector = np.get_pod_selector( each_from.get('podSelector')) from_list.append(introspect.NetworkPolicyFromRules( podSelector=np_pod_selector)) # Parse "ports" attribute. np_port_list = [] if ingress.get('ports'): for port in ingress.get('ports'): np_port = introspect.NetworkPolicyPort( port=port.get('port').__str__(), protocol=port.get('protocol')) np_port_list.append(np_port) np_ingress_list.append(\ introspect.NetworkPolicyIngressPolicy(\ fromPolicy=from_list, ports=np_port_list)) # Parse "pod selector" attribute. np_pod_selector = None if np.spec.get('podSelector'): pod_selector = np.spec.get('podSelector') np_pod_selector = introspect.NetworkPolicyPodSelectors( matchLabels=pod_selector.get('matchLabels')) np_spec = introspect.NetworkPolicySpec(ingress=np_ingress_list, podSelector=np_pod_selector) np_instance = introspect.NetworkPolicyInstance(uuid=np.uuid, name=np.name, name_space=np.namespace, spec_string=np.spec.__str__(), spec=np_spec) # Append the constructed element info to the response. np_resp.network_policies.append(np_instance) # Send the reply out. np_resp.response(req.context())
def get_pod_selector(self, pod_selector): labels = pod_selector.get('matchLabels') \ if pod_selector.get('matchLabels') else {} return introspect.NetworkPolicyPodSelectors(matchLabels=labels)