示例#1
0
    def create_jforg_secret(self, client, access_token, project_id,
                            project_code, data):
        try:
            domain_list = paas_cc.get_jfrog_domain_list(
                access_token, project_id, data['cluster_id'])
            domain_list = set(domain_list)

            # 获取项目的用户信息
            jfrog_account = get_jfrog_account(access_token, project_code,
                                              project_id)
            user_pwd = "%s:%s" % (jfrog_account.get('user'),
                                  jfrog_account.get('password'))
            user_auth = {
                "auth":
                base64.b64encode(user_pwd.encode(encoding="utf-8")).decode()
            }

            auth_dict = {}
            for _d in domain_list:
                if _d.startswith(settings.BK_JFROG_ACCOUNT_DOMAIN):
                    _bk_auth = get_bk_jfrog_auth(access_token, project_code,
                                                 project_id)
                    auth_dict[_d] = _bk_auth
                else:
                    auth_dict[_d] = user_auth

            jfrog_auth = {"auths": auth_dict}

            jfrog_auth_bytes = bytes(json.dumps(jfrog_auth), "utf-8")
            jfrog_config = {
                "apiVersion": "v1",
                "kind": "Secret",
                "metadata": {
                    "name": "%s%s" % (K8S_IMAGE_SECRET_PRFIX, data['name']),
                    "namespace": data['name']
                },
                "data": {
                    ".dockerconfigjson":
                    base64.b64encode(jfrog_auth_bytes).decode()
                },
                "type": "kubernetes.io/dockerconfigjson"
            }
            result = client.create_secret(data['name'], jfrog_config)
        except Exception as e:
            self.delete_ns_by_bcs(client, data['name'])
            logger.exception(u"获取项目仓库账号信息失败:%s" % e)
            raise ValidationError(_("获取项目仓库账号信息失败,请联系管理员解决"))

        # 通过错误消息判断 包含仓库信息的secret 是否已经存在,已经存在则直接进行下一步
        res_msg = result.get('message') or ''
        is_already_exists = res_msg.endswith("already exists")

        if result.get('code') != 0 and not is_already_exists:
            self.delete_ns_by_bcs(client, data['name'])
            raise error_codes.ComponentError.f(
                _("创建registry secret失败,{}, 请联系管理员解决").format(
                    result.get('message')))
示例#2
0
文件: k8s.py 项目: aikjjl/bk-bcs-saas
def create_dept_account(access_token, project_id, project_code, cluster_id):
    domain_list = paas_cc.get_jfrog_domain_list(access_token, project_id,
                                                cluster_id)
    if not domain_list:
        raise error_codes.APIError('get dept domain error, domain is empty')
    domain_list = set(domain_list)
    # get user auth by project
    dept_account = get_jfrog_account(access_token, project_code, project_id)
    user_pwd = f'{dept_account.get("user")}:{dept_account.get("password")}'
    user_auth = {
        'auth': base64.b64encode(user_pwd.encode(encoding='utf-8')).decode()
    }
    # compose many dept account auth
    auth_dict = {}
    for _d in domain_list:
        if _d.startswith(settings.BK_JFROG_ACCOUNT_DOMAIN):
            _bk_auth = get_bk_jfrog_auth(access_token, project_code,
                                         project_id)
            auth_dict[_d] = _bk_auth
        else:
            auth_dict[_d] = user_auth

    return auth_dict