def get_hbs_network(self, vn_uuid, vn_k8s_str, namespace): hbs_nw = { 'apiVersion': 'k8s.cni.cncf.io/v1', 'kind': 'NetworkAttachmentDefinition', 'metadata': { 'annotations': { 'opencontrail.org/network': '' }, 'name': 'hbf-left', 'namespace': 'hp101' }, 'spec': { 'config': '{"cniVersion":"0.3.0", "type": ' '"contrail-k8s-cni" }' } } vn_class = self.server.get_resource_class('virtual_network') ok, result = vn_class.locate(uuid=vn_uuid, fields=['network_ipam_refs'], create_it=False) if not ok: raise HttpError(result[0], result[1]) vn_fq_name = result['fq_name'] hbs_nw['metadata']['annotations'][ 'opencontrail.org/network'] = self._get_network(vn_fq_name) hbs_nw['metadata']['name'] = vn_k8s_str hbs_nw['metadata']['namespace'] = namespace return hbs_nw
def __enter__(self): try: acquired_lock = self.lock.acquire(timeout=self.timeout) except LockTimeout: contenders = self.lock.contenders() action_in_progress = '<unknown action>' if len(contenders) > 0 and contenders[0]: _, _, action_in_progress = contenders[0].partition(' ') msg = ("%s ZK Lock Creation Failed for lock name (%s)" "Server Busy, Try again Later" % (action_in_progress, name)) raise HttpError(408, msg) return acquired_lock
def get_hbs_info(self, hbs_fq_name, hbs_uuid): hbs_info = { 'cluster': None, 'namespace': None, 'vnleft': { 'fq_name': None, 'uuid': None, 'k8s_str': None }, 'vnright': { 'fq_name': None, 'uuid': None, 'k8s_str': None }, 'vnmgmt': { 'fq_name': None, 'uuid': None, 'k8s_str': None }, 'service': { 'image': None, 'imagePullSecrets': None } } # get hbs object ok, result = self.locate(fq_name=hbs_fq_name, uuid=hbs_uuid, create_it=False) if not ok: raise HttpError(result[0], result[1]) hbs_dict = result # get project and check for k8s, if not return an error project_uuid = hbs_dict['parent_uuid'] project_class = self.server.get_resource_class('project') ok, result = project_class.locate(uuid=project_uuid, fields=['annotations'], create_it=False) if not ok: raise HttpError(result[0], result[1]) project_dict = result project_fq_name = project_dict['fq_name'] # Get namespace information from project annotations kvs = project_dict.get('annotations', {}).get('key_value_pair', []) namespace = None cluster = None for kv in kvs: if kv['key'] == 'namespace': namespace = kv['value'] if kv['key'] == 'cluster': cluster = kv['value'] if not namespace: raise HttpError( 404, 'Project ' + pformat(project_fq_name) + ' is not k8s project, namespace is not present') if not cluster: raise HttpError( 404, 'Project ' + pformat(project_fq_name) + ' is not k8s project, clsuter is not present') hbs_info['namespace'] = namespace hbs_info['cluster'] = cluster # Get vn refs, get both left and right VNs vn_refs = hbs_dict.get('virtual_network_refs', []) vn_left = vn_right = vn_mgmt = None for vn in vn_refs: attr = vn.get('attr', {}) l_type = attr.get('virtual_network_type', None) if l_type == 'left': hbs_info['vnleft']['fq_name'] = vn['to'] hbs_info['vnleft']['uuid'] = vn['uuid'] vn_left = vn['to'][-1:][0] hbs_info['vnleft']['k8s_str'] = vn_left if l_type == 'right': hbs_info['vnright']['fq_name'] = vn['to'] hbs_info['vnright']['uuid'] = vn['uuid'] vn_right = vn['to'][-1:][0] hbs_info['vnright']['k8s_str'] = vn_right if l_type == 'management': hbs_info['vnmgmt']['fq_name'] = vn['to'] hbs_info['vnmgmt']['uuid'] = vn['uuid'] vn_mgmt = vn['to'][-1:][0] hbs_info['vnmgmt']['k8s_str'] = vn_mgmt # both left and right vn should be present if vn_left is None or vn_right is None: raise HttpError(404, 'Left and right HBF networks should be present') # get the map and look for image name and secrect kvs = hbs_dict.get('annotations', {}).get('key_value_pair', []) for kv in kvs: if kv['key'] == 'spec.template.spec.containers[].image' or \ kv['key'] == 'image': hbs_info['service']['image'] = kv['value'] if kv['key'] == 'spec.template.spec.imagePullSecrets[].name' or \ kv['key'] == 'imagePullSecrets': hbs_info['service']['imagePullSecrets'] = kv['value'] return hbs_info