def retrieve(self, request, project_id, *args, **kwargs): parameter = dict(request.GET.items()) serializer = ClusterKubeConfigSLZ(data=parameter) serializer.is_valid(raise_exception=True) cluster_id = serializer.data["cluster_id"] check_cluster_perm(user=request.user, project_id=project_id, cluster_id=cluster_id, request=request) if settings.HELM_HAS_ABILITY_SUPPLY_CHART_REPO_SERVICE: bcs_client = bcs_utils_client.get_bcs_client( project_id=project_id, cluster_id=cluster_id, access_token=self.access_token ) bcs_cluster_info = bcs_client.get_cluster() if bcs_cluster_info is None or not bcs_cluster_info.get("bcs_cluster_id"): result = {"code": 17602, "message": "cluster does not regist to bcs yet.", "initialized": False} return Response(result) serializer = self.serializer_class( { "public_repos": self.get_or_add_public_repos(project_id), "private_repos": self.get_or_add_private_repos(project_id, request.user), "initialized": True, } ) return Response(data=serializer.data)
def create(self, request, project_id, *args, **kwargs): serializer = ClusterImportSLZ(data=request.data) serializer.is_valid(raise_exception=True) cluster_id = serializer.data["cluster_id"] check_cluster_perm(user=request.user, project_id=project_id, cluster_id=cluster_id, request=request) bcs_client = bcs_utils_client.get_bcs_client( project_id=project_id, cluster_id=cluster_id, access_token=self.access_token) bcs_cluster_info = bcs_client.get_or_register_bcs_cluster() if not bcs_cluster_info["result"]: return Response(data=bcs_cluster_info) bcs_cluster_info = bcs_cluster_info["data"] content = render_bcs_agent_template( token=bcs_cluster_info["token"], bcs_cluster_id=bcs_cluster_info["bcs_cluster_id"], namespace=self.bcs_agent_namespace, access_token=self.access_token, project_id=project_id, cluster_id=cluster_id) response = HttpResponse(content=content, content_type='text/plain; charset=UTF-8') response[ 'Content-Disposition'] = 'attachment; filename="bcs-agent-%s.yaml"' % cluster_id return response
def create(self, validated_data): namespace_info = self.get_ns_info_by_id( validated_data["namespace_info"]) check_cluster_perm( user=self.context["request"].user, project_id=namespace_info["project_id"], cluster_id=namespace_info["cluster_id"], request=self.context["request"], ) # 检查集群已经成功注册到 bcs, 否则让用户先完成注册逻辑 bcs_client = get_bcs_client( project_id=namespace_info["project_id"], cluster_id=namespace_info["cluster_id"], access_token=self.context["request"].user.token.access_token, ) bcs_client.get_cluster_credential() sys_variables = collect_system_variable( access_token=self.context["request"].user.token.access_token, project_id=namespace_info["project_id"], namespace_id=namespace_info["id"], ) return App.objects.initialize_app( access_token=self.access_token, name=validated_data.get("name"), project_id=self.project_id, cluster_id=namespace_info["cluster_id"], namespace_id=namespace_info["id"], namespace=namespace_info["name"], chart_version=validated_data["chart_version"], answers=validated_data["get_answers"], customs=validated_data["get_customs"], valuefile=validated_data.get("get_valuefile"), creator=self.request_username, updator=self.request_username, sys_variables=sys_variables, valuefile_name=validated_data.get('get_valuefile_name'), cmd_flags=validated_data["cmd_flags"], )
def _polling_bke_status(pk, log_type="NodeUpdateLog"): """ """ model = models.log_factory(log_type) log = model.objects.filter(pk=pk).last() end_time = datetime.now() + BKE_POLLING_TIMEOUT status = models.CommonStatus.Normal message = "" while not log.is_finished and log.is_polling: if datetime.now() > end_time: break try: bke_client = get_bcs_client(log.project_id, log.cluster_id, log.token) bke_client.get_cluster_credential() status = models.CommonStatus.Normal break except Exception as err: status = models.NodeStatus.BkeFailed message = "%s" % err bke_log_save(log, log_type, status, message=message) return
def helm_init(access_token, project_id, cluster_id, bcs_agent_namespace): if not settings.HELM_NEED_REGIST_TO_BKE_WHEN_INIT: data = { "code": 0, "initialized": True, "detail": "HELM_NEED_REGIST_TO_BKE_WHEN_INIT set", "message": "ok", } return data # 1. do registering to bcs # need to be re-entrant bcs_client = bcs_utils_client.get_bcs_client(project_id=project_id, cluster_id=cluster_id, access_token=access_token) bcs_cluster_info = bcs_client.get_or_register_bcs_cluster() if not bcs_cluster_info.get("result"): data = { "code": 10601, "message": "failed to regist to bcs.", "data": bcs_cluster_info } return Response(data=data) bcs_cluster_info = bcs_cluster_info["data"] content = render_bcs_agent_template( token=bcs_cluster_info["token"], bcs_cluster_id=bcs_cluster_info["bcs_cluster_id"], namespace="kube-system", # namespace for bcs agent access_token=access_token, project_id=project_id, cluster_id=cluster_id) resources = parser.parse(content, bcs_agent_namespace).values() # 2. apply bcs agent deploy resource to target cluster # need to be re-entrant client = k8s.K8SClient(access_token, project_id, cluster_id, env=None) errors = [] for item in resources: if item.kind != "Secret": continue data = yaml.load(item.content) result = client.create_secret(bcs_agent_namespace, data) if result["code"] == 0: continue if not (result["code"] == 4001 and "exists" in result["message"]): errors.append("create_secret, %s" % json.dumps(result)) logger.error("ClusterHelmInitView client.create_secret, %s", json.dumps(result)) for item in resources: if item.kind != "ServiceAccount": continue data = yaml.load(item.content) result = client.create_serviceaccounts(bcs_agent_namespace, data) if result["code"] == 0: continue if not (result["code"] == 4001 and "exists" in result["message"]): errors.append("create_serviceaccounts, %s" % json.dumps(result)) logger.error( "ClusterHelmInitView client.create_serviceaccounts, %s", json.dumps(result)) for item in resources: if item.kind != "ClusterRoleBinding": continue data = yaml.load(item.content) result = client.create_clusterrolebindings(bcs_agent_namespace, data) if result["code"] == 0: continue if not (result["code"] == 4001 and "exists" in result["message"]): errors.append("create_clusterrolebindings, %s" % json.dumps(result)) logger.error( "ClusterHelmInitView client.create_clusterrolebindings, %s", json.dumps(result)) for item in resources: if item.kind != "Deployment": continue data = yaml.load(item.content) result = client.create_deployment(bcs_agent_namespace, data) if result["code"] == 0: continue if not (result["code"] == 4001 and "exists" in result["message"]): errors.append("create_deployment, %s" % json.dumps(result)) logger.error("ClusterHelmInitView client.create_deployment, %s", json.dumps(result)) # step3 and step4 has been moved to enable container service # 3. add plain public repo for project # public_repos = self.get_or_add_public_repos(project_id) # 4. add private repo for project # private_repos = self.get_or_add_private_repos(project_id, request.user) data = { "code": 0 if not bool(errors) else 400, "initialized": not bool(errors), "detail": errors, "message": "\n\n".join(errors), } return data