Пример #1
0
def get_ovdc(operation_context: ctx.OperationContext, ovdc_id: str) -> dict:
    """Get ovdc info for a particular ovdc.

    :param ctx.OperationContext operation_context: context for the request
    :param str ovdc_id: ID of the ovdc
    :return: dictionary containing the ovdc information
    :rtype: dict
    """
    # NOTE: For CSE 3.0, if `enable_tkg_plus` flag in config is set to false,
    # Prevent showing information about TKG+ by skipping TKG+ from the result.
    cse_params = {
        RequestKey.OVDC_ID:
        ovdc_id,
        PayloadKey.SOURCE_DESCRIPTION:
        thread_local_data.get_thread_local_data(
            ThreadLocalData.USER_AGENT)  # noqa: E501
    }
    telemetry_handler.record_user_action_details(
        cse_operation=CseOperation.OVDC_INFO,  # noqa: E501
        cse_params=cse_params)
    config = server_utils.get_server_runtime_config()
    log_wire = utils.str_to_bool(config.get('service', {}).get('log_wire'))
    result = asdict(
        get_ovdc_k8s_runtime_details(
            operation_context.sysadmin_client,  # noqa: E501
            ovdc_id=ovdc_id,
            log_wire=log_wire))
    # TODO: Find a better way to avoid sending remove_cp_from_vms_on_disable
    # flag
    if ClusterEntityKind.TKG_PLUS.value in result['k8s_runtime'] \
            and not server_utils.is_tkg_plus_enabled():
        result['k8s_runtime'].remove(ClusterEntityKind.TKG_PLUS.value)
    del result['remove_cp_from_vms_on_disable']
    return result
Пример #2
0
def _get_cse_ovdc_list(sysadmin_client: vcd_client.Client, ovdc_list: list):
    ovdcs = []
    config = server_utils.get_server_runtime_config()
    log_wire = utils.str_to_bool(config.get('service', {}).get('log_wire'))
    cpm = compute_policy_manager.ComputePolicyManager(sysadmin_client,
                                                      log_wire=log_wire)
    for ovdc in ovdc_list:
        ovdc_name = ovdc.get('name')
        ovdc_id = vcd_utils.extract_id(ovdc.get('id'))
        # obtain ovdc runtime details for the ovdc
        ovdc_details = asdict(
            get_ovdc_k8s_runtime_details(sysadmin_client,
                                         ovdc_id=ovdc_id,
                                         ovdc_name=ovdc_name,
                                         cpm=cpm,
                                         log_wire=log_wire))
        # NOTE: For CSE 3.0, if `enable_tkg_plus` flag in
        # config is set to false, Prevent showing information
        # about TKG+ by skipping TKG+ from the result.
        if ClusterEntityKind.TKG_PLUS.value in ovdc_details['k8s_runtime'] \
                and not server_utils.is_tkg_plus_enabled():  # noqa: E501
            ovdc_details['k8s_runtime'].remove(
                ClusterEntityKind.TKG_PLUS.value)  # noqa: E501
        # TODO: Find a better way to remove remove_cp_from_vms_on_disable
        del ovdc_details['remove_cp_from_vms_on_disable']
        ovdcs.append(ovdc_details)
    return ovdcs
def update_ovdc(operation_context: ctx.OperationContext,
                ovdc_id: str, ovdc_spec: common_models.Ovdc) -> dict: # noqa: 501
    """Update ovdc with the updated k8s runtimes list.

    :param ctx.OperationContext operation_context: context for the request
    :param common_models.Ovdc ovdc_spec: Ovdc object having the updated
        k8s runtime list
    :return: dictionary containing the task href for the update operation
    :rtype: dict
    """
    # NOTE: For CSE 3.0, if `enable_tkg_plus` flag in config is set to false,
    # Prevent enable/disable of OVDC for TKG+ k8s runtime by throwing an
    # exception
    msg = "Updating OVDC placement policies"
    task = vcd_task.Task(operation_context.sysadmin_client)
    org = vcd_utils.get_org(operation_context.client)
    user_href = org.get_user(operation_context.user.name).get('href')
    vdc = vcd_utils.get_vdc(operation_context.sysadmin_client, vdc_id=ovdc_id, # noqa: E501
                            is_admin_operation=True)
    logger.SERVER_LOGGER.debug(msg)
    task_resource = task.update(
        status=vcd_client.TaskStatus.RUNNING.value,
        namespace='vcloud.cse',
        operation=msg,
        operation_name='OVDC Update',
        details='',
        progress=None,
        owner_href=vdc.href,
        owner_name=vdc.name,
        owner_type=vcd_client.EntityType.VDC.value,
        user_href=user_href,
        user_name=operation_context.user.name,
        org_href=operation_context.user.org_href,
        task_href=None,
        error_message=None,
        stack_trace=None)
    task_href = task_resource.get('href')
    operation_context.is_async = True
    # NOTE: Telemetry is currently handled in the async function as it is not
    # possible to know the operation (enable/disable) without comparing it to
    # current k8s runtimes.
    if ClusterEntityKind.TKG_PLUS.value in ovdc_spec.k8s_runtime and \
            not server_utils.is_tkg_plus_enabled():
        msg = "TKG+ is not enabled on CSE server. Please enable TKG+ in the " \
              "server and try again."
        logger.SERVER_LOGGER.debug(msg)
        raise Exception(msg)
    policy_list = [RUNTIME_DISPLAY_NAME_TO_INTERNAL_NAME_MAP[p] for p in ovdc_spec.k8s_runtime]  # noqa: E501
    _update_ovdc_using_placement_policy_async(operation_context=operation_context, # noqa:E501
                                              task=task,
                                              task_href=task_href,
                                              user_href=user_href,
                                              policy_list=policy_list, # noqa:E501
                                              ovdc_id=ovdc_id,
                                              vdc=vdc,
                                              org_name=ovdc_spec.org_name,
                                              remove_cp_from_vms_on_disable=ovdc_spec.remove_cp_from_vms_on_disable) # noqa:E501
    return {'task_href': task_href}
def read_native_template_definition_from_catalog(
        config: ServerConfig,
        msg_update_callback=utils.NullPrinter()
):
    # NOTE: If `enable_tkg_plus` in the config file is set to false,
    # CSE server will skip loading the TKG+ template this will prevent
    # users from performing TKG+ related operations.
    msg = "Loading k8s template definition from catalog"
    logger.SERVER_LOGGER.info(msg)
    msg_update_callback.general_no_color(msg)

    client = None
    try:
        log_filename = None
        log_wire = \
            utils.str_to_bool(config.get_value_at('service.log_wire'))
        if log_wire:
            log_filename = logger.SERVER_DEBUG_WIRELOG_FILEPATH

        client = Client(
            uri=config.get_value_at('vcd.host'),
            api_version=config.get_value_at('service.default_api_version'),  # noqa: E501
            verify_ssl_certs=config.get_value_at('vcd.verify'),
            log_file=log_filename,
            log_requests=log_wire,
            log_headers=log_wire,
            log_bodies=log_wire
        )
        credentials = BasicLoginCredentials(
            config.get_value_at('vcd.username'),
            shared_constants.SYSTEM_ORG_NAME,
            config.get_value_at('vcd.password')
        )
        client.set_credentials(credentials)

        legacy_mode = config.get_value_at('service.legacy_mode')
        org_name = config.get_value_at('broker.org')
        catalog_name = config.get_value_at('broker.catalog')

        k8_templates = ltm.get_valid_k8s_local_template_definition(
            client=client, catalog_name=catalog_name, org_name=org_name,
            legacy_mode=legacy_mode,
            is_tkg_plus_enabled=server_utils.is_tkg_plus_enabled(config),
            logger_debug=logger.SERVER_LOGGER,
            msg_update_callback=msg_update_callback)

        return k8_templates
    finally:
        if client:
            client.logout()
Пример #5
0
    def _load_template_definition_from_catalog(
        self, msg_update_callback=utils.NullPrinter()):
        # NOTE: If `enable_tkg_plus` in the config file is set to false,
        # CSE server will skip loading the TKG+ template this will prevent
        # users from performing TKG+ related operations.
        msg = "Loading k8s template definition from catalog"
        logger.SERVER_LOGGER.info(msg)
        msg_update_callback.general_no_color(msg)

        client = None
        try:
            log_filename = None
            log_wire = \
                utils.str_to_bool(self.config['service'].get('log_wire'))
            if log_wire:
                log_filename = logger.SERVER_DEBUG_WIRELOG_FILEPATH

            # Since the config param has been read from file by
            # get_validated_config method, we can safely use the
            # default_api_version key, it will be set to the highest api
            # version supported by VCD and CSE.
            client = Client(
                self.config['vcd']['host'],
                api_version=self.config['service']['default_api_version'],
                verify_ssl_certs=self.config['vcd']['verify'],
                log_file=log_filename,
                log_requests=log_wire,
                log_headers=log_wire,
                log_bodies=log_wire)
            credentials = BasicLoginCredentials(
                self.config['vcd']['username'],
                shared_constants.SYSTEM_ORG_NAME,  # noqa: E501
                self.config['vcd']['password'])
            client.set_credentials(credentials)

            is_tkg_plus_enabled = server_utils.is_tkg_plus_enabled(self.config)
            legacy_mode = self.config['service']['legacy_mode']
            org_name = self.config['broker']['org']
            catalog_name = self.config['broker']['catalog']
            k8_templates = ltm.get_valid_k8s_local_template_definition(
                client=client,
                catalog_name=catalog_name,
                org_name=org_name,
                legacy_mode=legacy_mode,
                is_tkg_plus_enabled=is_tkg_plus_enabled,
                logger_debug=logger.SERVER_LOGGER,
                msg_update_callback=msg_update_callback)

            if not k8_templates:
                msg = "No valid K8 templates were found in catalog " \
                      f"'{catalog_name}'. Unable to start CSE server."
                msg_update_callback.error(msg)
                logger.SERVER_LOGGER.error(msg)
                sys.exit(1)

            # Check that default k8s template exists in vCD at the correct
            # revision
            default_template_name = \
                self.config['broker']['default_template_name']
            default_template_revision = \
                str(self.config['broker']['default_template_revision'])
            found_default_template = False
            for template in k8_templates:
                if str(template[server_constants.LocalTemplateKey.REVISION]) == default_template_revision and \
                        template[server_constants.LocalTemplateKey.NAME] == default_template_name:  # noqa: E501
                    found_default_template = True

            if not found_default_template:
                msg = f"Default template {default_template_name} with " \
                      f"revision {default_template_revision} not found." \
                      " Unable to start CSE server."
                msg_update_callback.error(msg)
                logger.SERVER_LOGGER.error(msg)
                sys.exit(1)

            self.config['broker']['templates'] = k8_templates
        finally:
            if client:
                client.logout()
Пример #6
0
    def _load_template_definition_from_catalog(
        self, msg_update_callback=utils.NullPrinter()):  # noqa: E501
        # NOTE: If `enable_tkg_plus` in the config file is set to false,
        # CSE server will skip loading the TKG+ template this will prevent
        # users from performing TKG+ related operations.
        msg = "Loading k8s template definition from catalog"
        logger.SERVER_LOGGER.info(msg)
        msg_update_callback.general_no_color(msg)

        client = None
        try:
            log_filename = None
            log_wire = \
                utils.str_to_bool(self.config['service'].get('log_wire'))
            if log_wire:
                log_filename = logger.SERVER_DEBUG_WIRELOG_FILEPATH

            client = Client(self.config['vcd']['host'],
                            api_version=self.config['vcd']['api_version'],
                            verify_ssl_certs=self.config['vcd']['verify'],
                            log_file=log_filename,
                            log_requests=log_wire,
                            log_headers=log_wire,
                            log_bodies=log_wire)
            credentials = BasicLoginCredentials(
                self.config['vcd']['username'],
                server_constants.SYSTEM_ORG_NAME,  # noqa: E501
                self.config['vcd']['password'])
            client.set_credentials(credentials)

            is_tkg_plus_enabled = server_utils.is_tkg_plus_enabled(self.config)
            org_name = self.config['broker']['org']
            catalog_name = self.config['broker']['catalog']
            k8_templates = ltm.get_all_k8s_local_template_definition(
                client=client,
                catalog_name=catalog_name,
                org_name=org_name,
                logger_debug=logger.SERVER_LOGGER)

            if not k8_templates:
                msg = "No valid K8 templates were found in catalog " \
                      f"'{catalog_name}'. Unable to start CSE server."
                msg_update_callback.error(msg)
                logger.SERVER_LOGGER.error(msg)
                sys.exit(1)

            # Check that default k8s template exists in vCD at the correct
            # revision
            default_template_name = \
                self.config['broker']['default_template_name']
            default_template_revision = \
                str(self.config['broker']['default_template_revision'])
            found_default_template = False
            for template in k8_templates:
                api_version = float(client.get_api_version())
                if api_version >= float(vCDApiVersion.VERSION_35.value) and \
                        template[server_constants.LocalTemplateKey.KIND] == \
                        shared_constants.ClusterEntityKind.TKG_PLUS.value and \
                        not is_tkg_plus_enabled:
                    # TKG+ is not enabled on CSE config. Skip the template and
                    # log the relevant information.
                    msg = "Skipping loading template data for " \
                          f"'{template[server_constants.LocalTemplateKey.NAME]}' as " \
                          "TKG+ is not enabled"  # noqa: E501
                    logger.SERVER_LOGGER.debug(msg)
                    k8_templates.remove(template)
                    continue
                if str(template[server_constants.LocalTemplateKey.REVISION]) == default_template_revision and \
                        template[server_constants.LocalTemplateKey.NAME] == default_template_name: # noqa: E501
                    found_default_template = True

                msg = f"Found K8 template '{template['name']}' at revision " \
                      f"{template['revision']} in catalog '{catalog_name}'"
                msg_update_callback.general(msg)
                logger.SERVER_LOGGER.info(msg)

            if not found_default_template:
                msg = f"Default template {default_template_name} with " \
                      f"revision {default_template_revision} not found." \
                      " Unable to start CSE server."
                msg_update_callback.error(msg)
                logger.SERVER_LOGGER.error(msg)
                sys.exit(1)

            self.config['broker']['templates'] = k8_templates
        finally:
            if client:
                client.logout()