def test_set_pod_annotation(self): with mock.patch('requests.patch') as req_mock: self.exp_headers['Content-Type'] = 'application/merge-patch+json' patch = '{"metadata": {"annotations": {"foo": "bar"}}}' req_mock.return_value = MockResponse(patch) ret_value = kubernetes.set_pod_annotation( 'http://meh:8080', 'meh_ns', 'meh_pod', 'foo', 'bar') self._verify_mock_call( req_mock, 'http://meh:8080/api/v1/namespaces/meh_ns/pods/meh_pod', data=patch, headers=self.exp_headers) self.assertEqual({'foo': 'bar'}, ret_value)
def create_logical_port(self, event): data = event.metadata logical_switch = data['spec']['nodeName'] pod_name = data['metadata']['name'] namespace = data['metadata']['namespace'] logical_port = "%s_%s" % (namespace, pod_name) if not logical_switch or not logical_port: vlog.err("absent node name or pod name in pod %s. " "Not creating logical port" % (data)) return (gateway_ip, mask) = self._get_switch_gateway_ip(logical_switch) if not gateway_ip or not mask: vlog.err("_create_logical_port: failed to get gateway_ip") return try: ovn_nbctl("--", "--if-exists", "lsp-del", logical_port, "--", "lsp-add", logical_switch, logical_port, "--", "lsp-set-addresses", logical_port, "dynamic") except Exception as e: vlog.err("_create_logical_port: lsp-add (%s)" % (str(e))) return # We wait for a maximum of 3 seconds to get the dynamic addresses in # intervals of 0.1 seconds. addresses = "" counter = 30 while counter != 0: try: ret = ovn_nbctl("get", "logical_switch_port", logical_port, "dynamic_addresses") addresses = ast.literal_eval(ret) if len(addresses): break except Exception as e: vlog.err("_create_logical_port: get dynamic_addresses (%s)" % (str(e))) time.sleep(0.1) counter = counter - 1 if not len(addresses): vlog.err("_create_logical_port: failed to get addresses after " "multiple retries.") return (mac_address, ip_address) = addresses.split() namespace = data['metadata']['namespace'] pod_name = data['metadata']['name'] ip_address_mask = "%s/%s" % (ip_address, mask) annotation = { 'ip_address': ip_address_mask, 'mac_address': mac_address, 'gateway_ip': gateway_ip } try: kubernetes.set_pod_annotation(variables.K8S_API_SERVER, namespace, pod_name, "ovn", str(annotation)) except Exception as e: vlog.err( "_create_logical_port: failed to annotate addresses (%s)" % (str(e))) return vlog.info("created logical port %s" % (logical_port))
def create_logical_port(self, event): data = event.metadata logical_switch = data['spec']['nodeName'] pod_name = data['metadata']['name'] namespace = data['metadata']['namespace'] logical_port = "%s_%s" % (namespace, pod_name) if not logical_switch or not pod_name: vlog.err("absent node name or pod name in pod %s. " "Not creating logical port" % (data)) return (gateway_ip, mask) = self._get_switch_gateway_ip(logical_switch) if not gateway_ip or not mask: vlog.err("_create_logical_port: failed to get gateway_ip") return try: ovn_nbctl("--wait=sb", "--", "--may-exist", "lsp-add", logical_switch, logical_port, "--", "lsp-set-addresses", logical_port, "dynamic", "--", "set", "logical_switch_port", logical_port, "external-ids:namespace=" + namespace, "external-ids:pod=true") except Exception as e: vlog.err("_create_logical_port: lsp-add (%s)" % (str(e))) return try: ret = ovn_nbctl("get", "logical_switch_port", logical_port, "dynamic_addresses") addresses = ast.literal_eval(ret) except Exception as e: vlog.err("_create_logical_port: get dynamic_addresses (%s)" % (str(e))) if not len(addresses): vlog.err("_create_logical_port: failed to get dynamic address") return (mac_address, ip_address) = addresses.split() namespace = data['metadata']['namespace'] pod_name = data['metadata']['name'] ip_address_mask = "%s/%s" % (ip_address, mask) annotation = { 'ip_address': ip_address_mask, 'mac_address': mac_address, 'gateway_ip': gateway_ip } try: kubernetes.set_pod_annotation(variables.K8S_API_SERVER, namespace, pod_name, "ovn", str(annotation)) except Exception as e: vlog.err( "_create_logical_port: failed to annotate addresses (%s)" % (str(e))) return vlog.info("created logical port %s" % (logical_port)) self._add_k8s_l4_port_name_cache(data, ip_address)
def create_logical_port(self, event): data = event.metadata logical_switch = data['spec']['nodeName'] pod_name = data['metadata']['name'] namespace = data['metadata']['namespace'] logical_port = "%s_%s" % (namespace, pod_name) if not logical_switch or not pod_name: vlog.err("absent node name or pod name in pod %s. " "Not creating logical port" % (data)) return (gateway_ip, mask) = self._get_switch_gateway_ip(logical_switch) if not gateway_ip or not mask: vlog.err("_create_logical_port: failed to get gateway_ip") return try: ovn_nbctl("--wait=sb", "--", "--may-exist", "lsp-add", logical_switch, logical_port, "--", "lsp-set-addresses", logical_port, "dynamic", "--", "set", "logical_switch_port", logical_port, "external-ids:namespace=" + namespace, "external-ids:pod=true") except Exception as e: vlog.err("_create_logical_port: lsp-add (%s)" % (str(e))) return try: ret = ovn_nbctl("get", "logical_switch_port", logical_port, "dynamic_addresses") addresses = ast.literal_eval(ret) except Exception as e: vlog.err("_create_logical_port: get dynamic_addresses (%s)" % (str(e))) if not len(addresses): vlog.err("_create_logical_port: failed to get dynamic address") return (mac_address, ip_address) = addresses.split() namespace = data['metadata']['namespace'] pod_name = data['metadata']['name'] ip_address_mask = "%s/%s" % (ip_address, mask) annotation = {'ip_address': str(ip_address_mask), 'mac_address': str(mac_address), 'gateway_ip': str(gateway_ip)} try: kubernetes.set_pod_annotation(variables.K8S_API_SERVER, namespace, pod_name, "ovn", json.dumps(annotation)) except Exception as e: vlog.err("_create_logical_port: failed to annotate addresses (%s)" % (str(e))) return vlog.info("created logical port %s" % (logical_port)) self._add_k8s_l4_port_name_cache(data, ip_address)
def create_logical_port(self, event): data = event.metadata logical_switch = data['spec']['nodeName'] pod_name = data['metadata']['name'] namespace = data['metadata']['namespace'] logical_port = "%s_%s" % (namespace, pod_name) if not logical_switch or not pod_name: vlog.err("absent node name or pod name in pod %s. " "Not creating logical port" % (data)) return (gateway_ip, mask) = self._get_switch_gateway_ip(logical_switch) if not gateway_ip or not mask: vlog.err("_create_logical_port: failed to get gateway_ip") return try: ovn_nbctl("--", "--may-exist", "lsp-add", logical_switch, logical_port, "--", "lsp-set-addresses", logical_port, "dynamic", "--", "set", "logical_switch_port", logical_port, "external-ids:namespace=" + namespace, "external-ids:pod=true") except Exception as e: vlog.err("_create_logical_port: lsp-add (%s)" % (str(e))) return # We wait for a maximum of 3 seconds to get the dynamic addresses in # intervals of 0.1 seconds. addresses = "" counter = 30 while counter != 0: try: ret = ovn_nbctl("get", "logical_switch_port", logical_port, "dynamic_addresses") addresses = ast.literal_eval(ret) if len(addresses): break except Exception as e: vlog.err("_create_logical_port: get dynamic_addresses (%s)" % (str(e))) time.sleep(0.1) counter = counter - 1 if not len(addresses): vlog.err("_create_logical_port: failed to get addresses after " "multiple retries.") return (mac_address, ip_address) = addresses.split() namespace = data['metadata']['namespace'] pod_name = data['metadata']['name'] ip_address_mask = "%s/%s" % (ip_address, mask) annotation = {'ip_address': ip_address_mask, 'mac_address': mac_address, 'gateway_ip': gateway_ip} try: kubernetes.set_pod_annotation(variables.K8S_API_SERVER, namespace, pod_name, "ovn", str(annotation)) except Exception as e: vlog.err("_create_logical_port: failed to annotate addresses (%s)" % (str(e))) return vlog.info("created logical port %s" % (logical_port))