Beispiel #1
0
    def render(self, namespace="default", bcs_inject_data=None):
        client = KubeHelmClient(helm_bin=settings.HELM_BIN)
        # 针对rollback的diff,不比对平台注入的信息
        if not bcs_inject_data:
            content, notes = client.template(
                files=self.chartVersionSnapshot.files,
                name=self.app.name,
                namespace=namespace,
                parameters=self.parameters,
                valuefile=self.generate_valuesyaml(self.app.project_id,
                                                   self.app.namespace_id,
                                                   self.app.cluster_id),
                cluster_id=self.app.cluster_id,
            )
        else:
            content, notes = client.template_with_ytt_renderer(
                files=self.chartVersionSnapshot.files,
                name=self.app.name,
                namespace=namespace,
                parameters=self.parameters,
                valuefile=self.generate_valuesyaml(self.app.project_id,
                                                   self.app.namespace_id,
                                                   self.app.cluster_id),
                cluster_id=self.app.cluster_id,
                bcs_inject_data=bcs_inject_data,
                cmd_flags=json.loads(self.app.cmd_flags),
            )

        return content, notes
Beispiel #2
0
    def create(self, validated_data):
        """ 生成应用的预览数据,这个时候应用没有创建,release也没有创建 """
        namespace_info = self.get_ns_info_by_id(validated_data["namespace_info"])

        cluster_id = namespace_info["cluster_id"]
        check_cluster_perm(
            user=self.context["request"].user,
            project_id=namespace_info["project_id"],
            cluster_id=cluster_id,
            request=self.context["request"],
        )

        # prepare parameters
        parameters = merge_rancher_answers(validated_data["get_answers"], validated_data["get_customs"])

        valuefile = get_valuefile_with_bcs_variable_injected(
            access_token=self.context["request"].user.token.access_token,
            project_id=namespace_info["project_id"],
            namespace_id=namespace_info["id"],
            valuefile=validated_data["valuefile"],
            cluster_id=cluster_id,
        )

        # inject bcs info
        now = datetime.datetime.now()
        username = self.context["request"].user.username

        # 组装注入的参数
        bcs_inject_data = bcs_helm_utils.BCSInjectData(
            source_type="helm",
            creator=username,
            updator=username,
            version=validated_data["chart_version"].version,
            project_id=self.project_id,
            app_id=self.context["request"].project.cc_app_id,
            cluster_id=cluster_id,
            namespace=namespace_info["name"],
            stdlog_data_id=bcs_helm_utils.get_stdlog_data_id(self.project_id),
            image_pull_secret=bcs_helm_utils.provide_image_pull_secrets(namespace_info["name"]),
        )
        client = KubeHelmClient(helm_bin=settings.HELM3_BIN)
        try:
            extra_params = {"cmd_flags": validated_data["cmd_flags"]}
            content, notes = client.template_with_ytt_renderer(
                files=validated_data["chart_version"].files,
                namespace=namespace_info["name"],
                name=validated_data.get("name"),
                parameters=parameters,
                valuefile=valuefile,
                cluster_id=cluster_id,
                bcs_inject_data=bcs_inject_data,
                **extra_params
            )
        except helm_exceptions.HelmBaseException:
            # raise ParseError(str(e))
            # NOTE: 现阶段为防止出现未测试到的情况,允许出错时,按照先前流程渲染;后续删除
            content, notes = _template_with_bcs_renderer(
                client,
                validated_data["chart_version"].files,
                validated_data.get("name"),
                namespace_info["name"],
                namespace_info["id"],
                parameters,
                valuefile,
                cluster_id,
                username,
                now,
                validated_data["chart_version"].version,
                self.access_token,
                self.project_id,
            )

        return {"content": preview_parse(content, namespace_info["name"]), "notes": notes}
Beispiel #3
0
    def create(self, validated_data):
        """ 应用更新时的预览数据,这个时候目标release还没有创建 """
        instance = App.objects.get(id=self.app_id)

        check_cluster_perm(
            user=self.context["request"].user,
            project_id=instance.project_id,
            cluster_id=instance.cluster_id,
            request=self.context["request"],
        )

        # 标记Chart中的values.yaml是否发生变化,用于提醒用户
        chart_version_changed = False

        # prepare parameters
        parameters = merge_rancher_answers(validated_data["get_answers"], validated_data["get_customs"])

        chart_version_id = validated_data["upgrade_verion"]
        chart_version_id = int(chart_version_id)
        if chart_version_id == KEEP_TEMPLATE_UNCHANGED:
            files = instance.release.chartVersionSnapshot.files
        else:
            chart_version_changed = True
            chart_version = ChartVersion.objects.get(id=chart_version_id)
            files = chart_version.files

        valuefile = get_valuefile_with_bcs_variable_injected(
            access_token=self.context["request"].user.token.access_token,
            project_id=instance.project_id,
            namespace_id=instance.namespace_id,
            valuefile=validated_data["valuefile"],
            cluster_id=instance.cluster_id,
        )

        now = datetime.datetime.now()
        username = self.context["request"].user.username
        # 组装注入的参数
        bcs_inject_data = bcs_helm_utils.BCSInjectData(
            source_type="helm",
            creator=username,
            updator=username,
            version=instance.release.chartVersionSnapshot.version,
            project_id=self.project_id,
            app_id=self.context["request"].project.cc_app_id,
            cluster_id=instance.cluster_id,
            namespace=instance.namespace,
            stdlog_data_id=bcs_helm_utils.get_stdlog_data_id(self.project_id),
            image_pull_secret=bcs_helm_utils.provide_image_pull_secrets(instance.namespace),
        )
        # 默认为使用helm3 client
        client = KubeHelmClient(helm_bin=settings.HELM3_BIN)
        try:
            content, notes = client.template_with_ytt_renderer(
                files=files,
                namespace=instance.namespace,
                name=instance.name,
                parameters=parameters,
                valuefile=valuefile,
                cluster_id=instance.cluster_id,
                bcs_inject_data=bcs_inject_data,
            )
        except helm_exceptions.HelmBaseException:
            # raise ParseError(str(e))
            # NOTE: 现阶段为防止出现未测试到的情况,允许出错时,按照先前流程渲染;后续删除
            content, notes = _template_with_bcs_renderer(
                client,
                files,
                instance.name,
                instance.namespace,
                instance.namespace_id,
                parameters,
                valuefile,
                instance.cluster_id,
                username,
                now,
                instance.release.chartVersionSnapshot.version,
                self.access_token,
                instance.project_id,
            )

        # compute diff
        old_content = instance.release.content
        if not old_content:
            old_content, _ = instance.render_app(
                username=self.context["request"].user.username, access_token=self.access_token
            )
        difference = simple_diff(old_content, content, instance.namespace)
        return {
            "content": preview_parse(content, instance.namespace),
            "notes": notes,
            "difference": difference,
            "chart_version_changed": chart_version_changed,
            "old_content": old_content,
            "new_content": content,
        }