def _find_cluster_in_org(self, cluster_name, is_org_admin_search=False): """Invoke set of all (vCD/PKS)brokers in the org to find the cluster. 'is_org_admin_search' is used here to prevent cluster creation with same cluster-name by users within org. If it is true, cluster list is filtered by the org name of the logged-in user. If cluster found: Return a tuple of (cluster and the broker instance used to find the cluster) Else: (None, None) if cluster not found. """ vcd_broker = VcdBroker(self.req_headers, self.req_spec) try: return vcd_broker.get_cluster_info(cluster_name), vcd_broker except Exception as err: LOGGER.debug(f"Get cluster info on {cluster_name} failed " f"on vCD with error: {err}") pks_ctx_list = self._create_pks_context_for_all_accounts_in_org() for pks_ctx in pks_ctx_list: pksbroker = PKSBroker(self.req_headers, self.req_spec, pks_ctx) try: return pksbroker.get_cluster_info( cluster_name=cluster_name, is_org_admin_search=is_org_admin_search), pksbroker except PksServerError as err: LOGGER.debug(f"Get cluster info on {cluster_name} failed " f"on {pks_ctx['host']} with error: {err}") return None, None
def _create_pks_compute_profile(self, pks_ctx): ovdc_id = self.req_spec.get('ovdc_id') org_name = self.req_spec.get('org_name') ovdc_name = self.req_spec.get('ovdc_name') # Compute profile creation pks_compute_profile_name = self.\ ovdc_cache.get_compute_profile_name(ovdc_id, ovdc_name) pks_compute_profile_description = f"{org_name}--{ovdc_name}" \ f"--{ovdc_id}" pks_az_name = f"az-{ovdc_name}" ovdc_rp_name = f"{ovdc_name} ({ovdc_id})" compute_profile_params = PksComputeProfileParams( pks_compute_profile_name, pks_az_name, pks_compute_profile_description, pks_ctx.get('cpi'), pks_ctx.get('datacenter'), pks_ctx.get('cluster'), ovdc_rp_name).to_dict() LOGGER.debug(f"Creating PKS Compute Profile with name:" f"{pks_compute_profile_name}") pksbroker = PKSBroker(self.req_headers, self.req_spec, pks_ctx) try: pksbroker.create_compute_profile(**compute_profile_params) except PksServerError as ex: if ex.status == HTTPStatus.CONFLICT.value: LOGGER.debug(f"Compute profile name {pks_compute_profile_name}" f" already exists\n{str(ex)}") else: raise ex
def _find_cluster_in_org(self, cluster_name): """Invoke set of all (vCD/PKS)brokers in the org to find the cluster. If cluster found: Return a tuple of (cluster and the broker instance used to find the cluster) Else: (None, None) if cluster not found. """ vcd_broker = VcdBroker(self.req_headers, self.req_spec) try: return vcd_broker.get_cluster_info(cluster_name), vcd_broker except Exception as err: LOGGER.debug(f"Get cluster info on {cluster_name} failed " f"on vCD with error: {err}") pks_ctx_list = self._create_pks_context_for_all_accounts_in_org() for pks_ctx in pks_ctx_list: pksbroker = PKSBroker(self.req_headers, self.req_spec, pks_ctx) try: return pksbroker.get_cluster_info(cluster_name=cluster_name),\ pksbroker except Exception as err: LOGGER.debug(f"Get cluster info on {cluster_name} failed " f"on {pks_ctx['host']} with error: {err}") return None, None
def create_pks_compute_profile(pks_ctx, tenant_auth_token, req_spec): ovdc_id = req_spec.get(RequestKey.OVDC_ID) org_name = req_spec.get(RequestKey.ORG_NAME) ovdc_name = req_spec.get(RequestKey.OVDC_NAME) # Compute profile creation pks_compute_profile_name = \ _construct_pks_compute_profile_name(ovdc_id) pks_compute_profile_description = f"{org_name}--{ovdc_name}" \ f"--{ovdc_id}" pks_az_name = f"az-{ovdc_name}" ovdc_rp_name = f"{ovdc_name} ({ovdc_id})" compute_profile_params = PksComputeProfileParams( pks_compute_profile_name, pks_az_name, pks_compute_profile_description, pks_ctx.get('cpi'), pks_ctx.get('datacenter'), pks_ctx.get('cluster'), ovdc_rp_name).to_dict() LOGGER.debug(f"Creating PKS Compute Profile with name:" f"{pks_compute_profile_name}") pksbroker = PKSBroker(tenant_auth_token, req_spec, pks_ctx) try: pksbroker.create_compute_profile(**compute_profile_params) except PksServerError as ex: if ex.status == requests.codes.conflict: LOGGER.debug(f"Compute profile name {pks_compute_profile_name}" f" already exists\n{str(ex)}") else: raise
def _construct_vc_to_pks_map(self): pks_vc_plans_map = {} pks_ctx_list = self._create_pks_context_for_all_accounts_in_org() for pks_ctx in pks_ctx_list: if pks_ctx['vc'] in pks_vc_plans_map: continue pks_broker = PKSBroker(self.req_headers, self.req_spec, pks_ctx) plans = pks_broker.list_plans() plan_names = [plan.get('name') for plan in plans] pks_vc_plans_map[pks_ctx['vc']] = [plan_names, pks_ctx['host']] return pks_vc_plans_map
def list_clusters(self): pks_clusters = [] pks_ctx_list = self.create_pks_context_for_all_accounts_in_org() for pks_ctx in pks_ctx_list: pks_broker = PKSBroker(self.tenant_auth_token, self.req_spec, pks_ctx) # Get all cluster information to get vdc name from # compute-profile-name for cluster in pks_broker.list_clusters(is_admin_request=True): pks_cluster = \ pks_broker.generate_cluster_subset_with_given_keys(cluster) pks_clusters.append(pks_cluster) return pks_clusters
def _construct_vc_to_pks_map(self): pks_vc_plans_map = {} pksbroker_manager = PksBrokerManager(self.tenant_auth_token, self.req_spec) pks_ctx_list = \ pksbroker_manager.create_pks_context_for_all_accounts_in_org() for pks_ctx in pks_ctx_list: if pks_ctx['vc'] in pks_vc_plans_map: continue pks_broker = PKSBroker(self.tenant_auth_token, self.req_spec, pks_ctx) plans = pks_broker.list_plans() plan_names = [plan.get('name') for plan in plans] pks_vc_plans_map[pks_ctx['vc']] = [plan_names, pks_ctx['host']] return pks_vc_plans_map
def _get_broker_based_on_ctr_prov_ctx(self, ctr_prov_ctx): if ctr_prov_ctx \ and ctr_prov_ctx.get(K8S_PROVIDER_KEY) == K8sProviders.PKS: return PKSBroker(self.req_headers, self.req_spec, pks_ctx=ctr_prov_ctx) else: # TODO() - This call should be based on a boolean flag # Specify flag in config file whether to have default # handling is required for missing ovdc or org. return VcdBroker(self.req_headers, self.req_spec)
def _list_clusters(self): """Logic of the method is as follows. If 'ovdc' is present in the body, choose the right broker (by identifying the container_provider (vcd|pks) defined for that ovdc) to do list_clusters operation. Else Invoke set of all (vCD/PKS)brokers in the org to do list_clusters. Post-process the result returned by each broker. Aggregate all the results into one. """ if self.is_ovdc_present_in_request: broker = self.get_broker_based_on_vdc() return broker.list_clusters() else: common_cluster_properties = ('name', 'vdc', 'status', 'org_name') vcd_broker = VcdBroker(self.req_headers, self.req_spec) vcd_clusters = [] for cluster in vcd_broker.list_clusters(): vcd_cluster = { k: cluster.get(k, None) for k in common_cluster_properties } vcd_cluster[K8S_PROVIDER_KEY] = K8sProviders.NATIVE vcd_clusters.append(vcd_cluster) pks_clusters = [] pks_ctx_list = self._create_pks_context_for_all_accounts_in_org() for pks_ctx in pks_ctx_list: pks_broker = PKSBroker(self.req_headers, self.req_spec, pks_ctx) # Get all cluster information to get vdc name from # compute-profile-name for cluster in pks_broker.list_clusters(is_admin_request=True): pks_cluster = \ PKSBroker.generate_cluster_subset_with_given_keys( cluster, common_cluster_properties) pks_cluster[K8S_PROVIDER_KEY] = K8sProviders.PKS pks_clusters.append(pks_cluster) return vcd_clusters + pks_clusters
def _list_clusters(self): """Logic of the method is as follows. If 'ovdc' is present in the body, choose the right broker (by identifying the container_provider (vcd|pks) defined for that ovdc) to do list_clusters operation. Else Invoke set of all (vCD/PKS)brokers in the org to do list_clusters. Post-process the result returned by each broker. Aggregate all the results into one. """ if self.is_ovdc_present_in_request: broker = self.get_broker_based_on_vdc() return broker.list_clusters() else: common_cluster_properties = ('name', 'vdc', 'status') vcd_broker = VcdBroker(self.req_headers, self.req_spec) vcd_clusters = [] for cluster in vcd_broker.list_clusters(): vcd_cluster = { k: cluster.get(k, None) for k in common_cluster_properties } vcd_cluster[CONTAINER_PROVIDER_KEY] = CtrProvType.VCD.value vcd_clusters.append(vcd_cluster) pks_clusters = [] pks_ctx_list = self._create_pks_context_for_all_accounts_in_org() for pks_ctx in pks_ctx_list: pks_broker = PKSBroker(self.req_headers, self.req_spec, pks_ctx) for cluster in pks_broker.list_clusters(): pks_cluster = self._get_truncated_cluster_info( cluster, pks_broker, common_cluster_properties) pks_cluster[CONTAINER_PROVIDER_KEY] = CtrProvType.PKS.value pks_clusters.append(pks_cluster) return vcd_clusters + pks_clusters
def find_cluster_in_org(self, cluster_name, is_org_admin_search=False): """Invoke all PKS brokers in the org to find the cluster. 'is_org_admin_search' is used here to prevent cluster creation with same cluster-name by users within org. If it is true, cluster list is filtered by the org name of the logged-in user. If cluster found: Return a tuple of (cluster and the broker instance used to find the cluster) Else: (None, None) if cluster not found. """ pks_ctx_list = \ self.create_pks_context_for_all_accounts_in_org() for pks_ctx in pks_ctx_list: pksbroker = PKSBroker(self.tenant_auth_token, self.req_spec, pks_ctx) try: return pksbroker.get_cluster_info( cluster_name=cluster_name, is_org_admin_search=is_org_admin_search), pksbroker except PksClusterNotFoundError as err: # If a cluster is not found, then broker_manager will # decide if it wants to raise an error or ignore it if was it # just scanning the broker to check if it can handle the # cluster request or not. LOGGER.debug(f"Get cluster info on {cluster_name}" f"on PKS failed with error: {err}") except PksDuplicateClusterError as err: LOGGER.debug(f"Get cluster info on {cluster_name}" f"on PKS failed with error: {err}") raise except PksServerError as err: LOGGER.debug(f"Get cluster info on {cluster_name} failed " f"on {pks_ctx['host']} with error: {err}") return None, None
def _get_broker_based_on_ctr_prov_ctx(self, ctr_prov_ctx): # If system is equipped with PKS, use the metadata on ovdc to determine # the correct broker, otherwise fallback to vCD for cluster deployment. # However if the system is enabled for PKS and has no metadata on odvc # or isn't enabled for container deployment raise appropriate # exception. if is_pks_enabled(): if ctr_prov_ctx: if ctr_prov_ctx.get(K8S_PROVIDER_KEY) == K8sProviders.PKS: return PKSBroker(self.tenant_auth_token, self.req_spec, pks_ctx=ctr_prov_ctx) elif ctr_prov_ctx.get(K8S_PROVIDER_KEY) == K8sProviders.NATIVE: return VcdBroker(self.tenant_auth_token, self.req_spec) else: return VcdBroker(self.tenant_auth_token, self.req_spec) raise CseServerError("Org VDC is not enabled for Kubernetes cluster " "deployment")
def get_broker_based_on_vdc(self): """Get the broker based on ovdc. :return: broker :rtype: container_service_extension.abstract_broker.AbstractBroker """ ovdc_name = self.req_spec.get('vdc') or \ self.req_qparams.get('vdc') org_name = self.req_spec.get('org') or \ self.req_qparams.get('org') or \ self.session.get('org') LOGGER.debug(f"org_name={org_name};vdc_name=\'{ovdc_name}\'") # Get the ovdc metadata using org and ovdc. # Create the right broker based on value of 'container_provider'. # Fall back to DefaultBroker for missing ovdc or org. if ovdc_name and org_name: ctr_prov_ctx = \ self.ovdc_cache.get_ovdc_container_provider_metadata( ovdc_name=ovdc_name, org_name=org_name, credentials_required=True, nsxt_info_required=True) LOGGER.debug( f"ovdc metadata for {ovdc_name}-{org_name}=>{ctr_prov_ctx}") if ctr_prov_ctx.get(CONTAINER_PROVIDER_KEY) == \ CtrProvType.PKS.value: return PKSBroker(self.req_headers, self.req_spec, pks_ctx=ctr_prov_ctx) elif ctr_prov_ctx.get(CONTAINER_PROVIDER_KEY) == \ CtrProvType.VCD.value: return VcdBroker(self.req_headers, self.req_spec) else: raise CseServerError(f"Vdc '{ovdc_name}' is not enabled for " "Kubernetes cluster deployment") else: # TODO() - This call should be based on a boolean flag # Specify flag in config file whether to have default # handling is required for missing ovdc or org. return VcdBroker(self.req_headers, self.req_spec)