Esempio n. 1
0
    def _template_with_ytt_renderer(self,
                                    username,
                                    access_token,
                                    ignore_empty_access_token=False,
                                    extra_inject_source=None):

        try:
            # 组装注入的参数
            bcs_inject_data = bcs_helm_utils.BCSInjectData(
                source_type="helm",
                creator=self.creator,
                updator=username,
                version=self.release.chartVersionSnapshot.version,
                project_id=self.project_id,
                app_id=get_cc_app_id(access_token, self.project_id),
                cluster_id=self.cluster_id,
                namespace=self.namespace,
                stdlog_data_id=bcs_helm_utils.get_stdlog_data_id(
                    self.project_id),
                image_pull_secret=bcs_helm_utils.provide_image_pull_secrets(
                    self.namespace),
            )
            content, notes = self.release.render(
                namespace=self.namespace, bcs_inject_data=bcs_inject_data)
            content = str(content, encoding="utf-8")
        except helm_exceptions.HelmBaseException as e:
            logger.error("helm render failed, %s", str(e))
            return None, None
        except Exception as e:
            logger.exception("render app failed, %s", str(e))
            return None, None
        return content, notes
Esempio n. 2
0
    def _run_with_helm(self, client, name, namespace, operation):
        transitioning_result = True
        try:
            if operation in [
                    ChartOperations.INSTALL.value,
                    ChartOperations.UPGRADE.value
            ]:
                project_id = self.app.project_id
                namespace = self.app.namespace
                bcs_inject_data = bcs_helm_utils.BCSInjectData(
                    source_type="helm",
                    creator=self.app.creator,
                    updator=self.app.updator,
                    version=self.app.release.chartVersionSnapshot.version,
                    project_id=project_id,
                    app_id=get_cc_app_id(self.access_token, project_id),
                    cluster_id=self.app.cluster_id,
                    namespace=namespace,
                    stdlog_data_id=bcs_helm_utils.get_stdlog_data_id(
                        project_id),
                    image_pull_secret=bcs_helm_utils.
                    provide_image_pull_secrets(namespace))
                # 追加系统和用户渲染的变量
                values_with_bcs_variables = get_valuefile_with_bcs_variable_injected(
                    access_token=self.access_token,
                    project_id=project_id,
                    namespace_id=self.app.namespace_id,
                    valuefile=self.app.release.valuefile,
                    cluster_id=self.app.cluster_id,
                )
                # 获取执行的操作命令
                cmd_out = getattr(client, operation)(
                    name=name,
                    namespace=namespace,
                    files=self.app.release.chartVersionSnapshot.files,
                    chart_values=values_with_bcs_variables,
                    bcs_inject_data=bcs_inject_data,
                    cmd_flags=json.loads(self.app.cmd_flags))[0]
                self.app.release.revision = self.get_release_revision(cmd_out)
                self.app.release.save()
            elif operation == ChartOperations.UNINSTALL.value:
                client.uninstall(name, namespace)
            elif operation == ChartOperations.ROLLBACK.value:
                client.rollback(name, namespace, self.app.release.revision)
        except HelmExecutionError as e:
            transitioning_result = False
            transitioning_message = (
                "helm command execute failed.\n"
                "Error code: {error_no}\nOutput:\n{output}").format(
                    error_no=e.error_no, output=e.output)
            logger.warn(transitioning_message)
        except HelmError as e:
            err_msg = str(e)
            logger.warn(err_msg)
            # TODO: 现阶段针对删除release找不到的情况,认为是正常的
            if "not found" in err_msg and operation == ChartOperations.UNINSTALL.value:
                transitioning_result = True
                transitioning_message = "app success %s" % operation
            else:
                transitioning_result = False
                transitioning_message = err_msg
        except Exception as e:
            err_msg = str(e)
            transitioning_result = False
            logger.warning(err_msg)
            transitioning_message = self.collect_transitioning_error_message(e)
        else:
            transitioning_result = True
            transitioning_message = "app success %s" % operation

        self.app.set_transitioning(transitioning_result, transitioning_message)