def construct_k8s_metadata_from_pks_cache(sysadmin_client: vcd_client.Client, ovdc_id, org_name, pks_plans, pks_cluster_domain, k8s_provider): vcd_utils.raise_error_if_not_sysadmin(sysadmin_client) ctr_prov_context = { K8S_PROVIDER_KEY: k8s_provider, } if k8s_provider == K8sProvider.PKS: if not utils.is_pks_enabled(): raise e.CseServerError('CSE is not configured to work with PKS.') ovdc = vcd_utils.get_vdc(client=sysadmin_client, vdc_id=ovdc_id, is_admin_operation=True) pks_cache = utils.get_pks_cache() pvdc_id = vcd_utils.get_pvdc_id(sysadmin_client, ovdc) pvdc_info = pks_cache.get_pvdc_info(pvdc_id) if not pvdc_info: LOGGER.debug(f"pvdc '{pvdc_id}' is not backed " f"by PKS-managed-vSphere resources") raise e.CseServerError(f"VDC '{ovdc.get_resource().get('name')}'" " is not eligible to provide resources" " for PKS clusters.") pks_account_info = pks_cache.get_pks_account_info( org_name, pvdc_info.vc) nsxt_info = pks_cache.get_nsxt_info(pvdc_info.vc) pks_compute_profile_name = _construct_pks_compute_profile_name( sysadmin_client, ovdc_id) ctr_prov_context = construct_pks_context( pks_account_info=pks_account_info, pvdc_info=pvdc_info, nsxt_info=nsxt_info, pks_compute_profile_name=pks_compute_profile_name, pks_plans=pks_plans, pks_cluster_domain=pks_cluster_domain, credentials_required=True) return ctr_prov_context
def update_status(self, server_action: ServerAction): def graceful_shutdown(): message = 'Shutting down CSE' n = self.active_requests_count() if n > 0: message += f" CSE will finish processing {n} requests." self._state = ServerState.STOPPING return message if self._state == ServerState.RUNNING: if server_action == ServerAction.ENABLE: return 'CSE is already enabled and running.' if server_action == ServerAction.DISABLE: self._state = ServerState.DISABLED return 'CSE has been disabled.' if server_action == ServerAction.STOP: raise cse_exception.BadRequestError( error_message='CSE must be disabled before ' 'it can be stopped.') raise cse_exception.BadRequestError( error_message=f"Invalid server action: '{server_action}'") if self._state == ServerState.DISABLED: if server_action == ServerAction.ENABLE: self._state = ServerState.RUNNING return 'CSE has been enabled and is running.' if server_action == ServerAction.DISABLE: return 'CSE is already disabled.' if server_action == ServerAction.STOP: return graceful_shutdown() if self._state == ServerState.STOPPING: if server_action == ServerAction.ENABLE: raise cse_exception.BadRequestError( error_message='Cannot enable CSE while it is being' 'stopped.') if server_action == ServerAction.DISABLE: raise cse_exception.BadRequestError( error_message='Cannot disable CSE while it is being' ' stopped.') if server_action == ServerAction.STOP: return graceful_shutdown() raise cse_exception.CseServerError( f"Invalid server state: '{self._state}'") # noqa: E501
def ovdc_update(request_data, op_ctx: ctx.OperationContext): """Request handler for ovdc enable, disable operations. Required data: org_name, ovdc_name, k8s_provider Conditional data: if k8s_provider is 'ent-pks': pks_plan_name, pks_cluster_domain :return: Dictionary with org VDC update task href. """ # TODO the data flow here should be better understood. # org_name and ovdc_name seem redundant if we already have ovdc_id required = [ RequestKey.ORG_NAME, RequestKey.OVDC_NAME, RequestKey.K8S_PROVIDER, RequestKey.OVDC_ID ] validated_data = request_data req_utils.validate_payload(validated_data, required) k8s_provider = validated_data[RequestKey.K8S_PROVIDER] k8s_provider_info = {K8S_PROVIDER_KEY: k8s_provider} # Record the telemetry data cse_params = copy.deepcopy(validated_data) cse_operation = CseOperation.OVDC_DISABLE if k8s_provider == K8sProvider.NONE else CseOperation.OVDC_ENABLE # noqa: E501 record_user_action_details(cse_operation=cse_operation, cse_params=cse_params) # noqa: E501 try: if k8s_provider == K8sProvider.PKS: if not utils.is_pks_enabled(): raise e.CseServerError('CSE server is not ' 'configured to work with PKS.') required = [ RequestKey.PKS_PLAN_NAME, RequestKey.PKS_CLUSTER_DOMAIN ] req_utils.validate_payload(validated_data, required) # Check if target ovdc is not already enabled for other non PKS k8 providers # noqa: E501 ovdc_metadata = ovdc_utils.get_ovdc_k8s_provider_metadata( op_ctx.sysadmin_client, ovdc_id=validated_data[RequestKey.OVDC_ID]) ovdc_k8_provider = ovdc_metadata.get(K8S_PROVIDER_KEY) if ovdc_k8_provider != K8sProvider.NONE and \ ovdc_k8_provider != k8s_provider: raise e.CseServerError( "Ovdc already enabled for different K8 provider" ) # noqa: E501 k8s_provider_info = ovdc_utils.construct_k8s_metadata_from_pks_cache( # noqa: E501 op_ctx.sysadmin_client, ovdc_id=validated_data[RequestKey.OVDC_ID], org_name=validated_data[RequestKey.ORG_NAME], pks_plans=validated_data[RequestKey.PKS_PLAN_NAME], pks_cluster_domain=validated_data[ RequestKey.PKS_CLUSTER_DOMAIN], # noqa: E501 k8s_provider=k8s_provider) ovdc_utils.create_pks_compute_profile(validated_data, op_ctx, k8s_provider_info) task = ovdc_utils.update_ovdc_k8s_provider_metadata( op_ctx.sysadmin_client, validated_data[RequestKey.OVDC_ID], k8s_provider_data=k8s_provider_info, k8s_provider=k8s_provider) # Telemetry - Record successful enabling/disabling of ovdc record_user_action(cse_operation, status=OperationStatus.SUCCESS) return {'task_href': task.get('href')} except Exception as err: # Telemetry - Record failed enabling/disabling of ovdc record_user_action(cse_operation, status=OperationStatus.FAILED) raise err