def get_k8s_context(self, request, project_id, cluster_id): """获取docker监控信息 """ client = K8SClient(request.user.token.access_token, project_id, cluster_id, None) slz = K8SWebConsoleSLZ(data=request.query_params, context={'client': client}) slz.is_valid(raise_exception=True) try: bcs_context = utils.get_k8s_cluster_context(client, project_id, cluster_id) except Exception as error: logger.exception("get access cluster context failed: %s", error) message = f"获取集群{self.cluster_name}【{cluster_id}】WebConsole 信息失败" # 记录操作日志 utils.activity_log(project_id, self.cluster_name, request.user.username, False, message) # 返回前端消息 raise error_codes.APIError( f"{message},请检查 Deployment【kube-system/bcs-agent】是否正常{settings.COMMON_EXCEPTION_MSG}") # kubectl版本区别 _, version = get_cluster_proper_kubectl(request.user.token.access_token, project_id, cluster_id) kubectld_version = get_kubectld_version(version) container_id = slz.validated_data.get('container_id') if container_id: bcs_context['mode'] = k8s.ContainerDirectClient.MODE bcs_context['user_pod_name'] = slz.validated_data['pod_name'] bcs_context.update(slz.validated_data) else: bcs_context = utils.get_k8s_admin_context(client, bcs_context, settings.WEB_CONSOLE_MODE) ctx = {'username': self.request.user.username, 'settings': settings, 'kubectld_version': kubectld_version, 'namespace': constants.NAMESPACE, 'pod_spec': utils.get_k8s_pod_spec(client), 'username_slug': utils.get_username_slug(self.request.user.username), # 缓存ctx, 清理使用 'should_cache_ctx': True} ctx.update(bcs_context) try: pod_life_cycle.ensure_namespace(ctx) configmap = pod_life_cycle.ensure_configmap(ctx) logger.debug('get configmap %s', configmap) pod = pod_life_cycle.ensure_pod(ctx) logger.debug('get pod %s', pod) except pod_life_cycle.PodLifeError as error: logger.error("kubetctl apply error: %s", error) utils.activity_log(project_id, self.cluster_name, request.user.username, False, '%s' % error) raise error_codes.APIError('%s' % error) except Exception as error: logger.exception("kubetctl apply error: %s", error) utils.activity_log(project_id, self.cluster_name, request.user.username, False, "申请pod资源失败") raise error_codes.APIError(f"申请pod资源失败,请稍后再试{settings.COMMON_EXCEPTION_MSG}") bcs_context['user_pod_name'] = pod.metadata.name return bcs_context
def get_k8s_cluster_context(self, request, project_id, cluster_id, client, bcs_context): """获取集群模式(kubectl)上下文 """ # kubectl版本区别 kubectld_version = get_kubectld_version(client.version) bcs_context = utils.get_k8s_admin_context(client, bcs_context, settings.WEB_CONSOLE_MODE) ctx = { "username": self.request.user.username, "settings": settings, "kubectld_version": kubectld_version, "namespace": constants.NAMESPACE, "pod_spec": utils.get_k8s_pod_spec(client), "username_slug": utils.get_username_slug(self.request.user.username), # 缓存ctx, 清理使用 "should_cache_ctx": True, } ctx.update(bcs_context) try: pod_life_cycle.ensure_namespace(ctx) configmap = pod_life_cycle.ensure_configmap(ctx) logger.debug("get configmap %s", configmap) pod = pod_life_cycle.ensure_pod(ctx) logger.debug("get pod %s", pod) except pod_life_cycle.PodLifeError as error: logger.error("kubetctl apply error: %s", error) utils.activity_log(project_id, cluster_id, self.cluster_name, request.user.username, False, "%s" % error) raise error_codes.APIError("%s" % error) except Exception as error: logger.exception("kubetctl apply error: %s", error) utils.activity_log(project_id, cluster_id, self.cluster_name, request.user.username, False, "申请pod资源失败") raise error_codes.APIError( _("申请pod资源失败,请稍后再试{}").format(settings.COMMON_EXCEPTION_MSG)) bcs_context["user_pod_name"] = pod.metadata.name return bcs_context