Пример #1
0
    def _add_kube_agent_config(self, cluster_id, params):
        if params.get("cluster_state") == constants.ClusterState.BCSNew.value:
            return
        # get bcs agent info
        bcs_client = BCSClusterClient(host=BCS_SERVER_HOST,
                                      access_token=params["access_token"],
                                      project_id=params["project_id"],
                                      cluster_id=cluster_id)
        bcs_cluster_info = bcs_client.get_or_register_bcs_cluster()
        if not bcs_cluster_info.get("result"):
            err_msg = bcs_cluster_info.get("message",
                                           "request bcs agent api error")
            raise error_codes.APIError(err_msg)
        bcs_cluster_data = bcs_cluster_info.get("data", {})
        if not bcs_cluster_data:
            raise error_codes.APIError("bcs agent api response is null")

        # 通过api server domain, 解析到host ip
        raw_url = urlparse(BCS_SERVER_HOST)
        host, port = raw_url.netloc.split(":")
        # 使用getaddrinfo,是因为后续支持多个ip
        # getaddrinfo 返回: [(2, 1, 6, '', ('127.0.0.1', 80))]
        addr_info = socket.getaddrinfo(host, port)
        api_server_domain = f"{raw_url.scheme}://{addr_info[0][4][0]}:{port}"

        self.k8s_config["bcs.kube_agent"].update({
            "register_token":
            bcs_cluster_data["token"],
            "bcs_api_server":
            api_server_domain,
            "register_cluster_id":
            bcs_cluster_data["bcs_cluster_id"]
        })
Пример #2
0
    def _get_node_vars(self, node_legals, kubeapps_master_legal_host, access_token, project_id, cluster_id):
        legal_hosts = list(node_legals.keys())
        self.k8s_config['kubernetes.node'].update({
            'legal_hosts': legal_hosts,
            'is_kube_master': False,
            'standalone_kubelet': False
        })
        self.k8s_config['docker'].update({'legal_hosts': legal_hosts})
        self.k8s_config['agent.cadvisorbeat'].update({'legal_hosts': legal_hosts})
        self.k8s_config['agent.logbeat'].update({'legal_hosts': legal_hosts})
        if self.op_type == constants.OpType.ADD_NODE.value:
            # get bcs agent info
            bcs_client = BCSClusterClient(
                host=BCS_SERVER_HOST, access_token=access_token, project_id=project_id, cluster_id=cluster_id)
            bcs_cluster_info = bcs_client.get_or_register_bcs_cluster()
            if not bcs_cluster_info.get('result'):
                err_msg = bcs_cluster_info.get('message', 'request bcs agent api error')
                raise error_codes.APIError(err_msg)
            bcs_cluster_data = bcs_cluster_info.get('data', {})
            if not bcs_cluster_data:
                raise error_codes.APIError('bcs agent api response is null')

            self.k8s_config['bcs.kube_agent'].update({
                'legal_hosts': kubeapps_master_legal_host,
                'register_token': bcs_cluster_data['token'],
                'bcs_api_server': BCS_SERVER_HOST,
                'register_cluster_id': bcs_cluster_data['bcs_cluster_id']
            })
            self.k8s_config['kubeapps.kube_agent'].update({'legal_hosts': kubeapps_master_legal_host})
        # 根据操作
        if self.op_type == constants.OpType.DELETE_NODE.value:
            self.k8s_config['kubeapps.node'].update({
                'legal_hosts': kubeapps_master_legal_host,
                'nodes': node_legals
            })
Пример #3
0
    def _add_kube_agent_config(self, cluster_id, params):
        """针对纳管集群,需要在创建集群时,传递kube client组件需要的配置信息
        """
        if params.get("cluster_state") == constants.ClusterState.BCSNew.value:
            return
        # get bcs agent info
        bcs_client = BCSClusterClient(host=BCS_SERVER_HOST,
                                      access_token=params["access_token"],
                                      project_id=params["project_id"],
                                      cluster_id=cluster_id)
        bcs_cluster_info = bcs_client.get_or_register_bcs_cluster()
        if not bcs_cluster_info.get("result"):
            err_msg = bcs_cluster_info.get("message",
                                           "request bcs agent api error")
            raise error_codes.APIError(err_msg)
        bcs_cluster_data = bcs_cluster_info.get("data", {})
        if not bcs_cluster_data:
            raise error_codes.APIError("bcs agent api response is null")

        self.k8s_config["bcs.kube_agent"].update({
            "register_token":
            bcs_cluster_data["token"],
            "bcs_api_server":
            BCS_SERVER_HOST,
            "register_cluster_id":
            bcs_cluster_data["bcs_cluster_id"]
        })
Пример #4
0
def get_bcs_client(project_id, cluster_id, access_token):
    target_cluster_id = getattr(settings, "FORCE_APPLY_CLUSTER_ID", None)
    if not target_cluster_id:
        target_cluster_id = cluster_id

    host = get_bcs_host(access_token, project_id, cluster_id)
    if not host:
        raise ValueError(host)

    bcs_client = BCSClusterClient(
        host=host,
        access_token=access_token,
        project_id=project_id,
        cluster_id=target_cluster_id,
    )
    return bcs_client