Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Kubernetes Python API获取集群信息?_Python_Api_Kubernetes - Fatal编程技术网

如何使用Kubernetes Python API获取集群信息?

如何使用Kubernetes Python API获取集群信息?,python,api,kubernetes,Python,Api,Kubernetes,我正在寻找关于如何使用Kubernetes Python API获取集群信息的答案(kubectl get clusters) ~$kubectl-n获取群集 姓名年龄 簇-1 6d17h 簇-2 6d17h 以下是获取群集信息(CRD)的代码: 我能够获取用于clusterConfiguration的configmap的集群名称。如果集群是kubeadm集群,则存在此配置映射 下面的代码段使用模块get cm.py(在上面repo的modules文件夹中)从python客户端获取config

我正在寻找关于如何使用Kubernetes Python API获取集群信息的答案(
kubectl get clusters

~$kubectl-n获取群集
姓名年龄
簇-1 6d17h
簇-2 6d17h

以下是获取群集信息(CRD)的代码:


我能够获取用于clusterConfiguration的configmap的集群名称。如果集群是kubeadm集群,则存在此配置映射

下面的代码段使用模块get cm.py(在上面repo的modules文件夹中)从python客户端获取configmap。它检查clusetConfiguration configmap
kubeadm config
是否存在,如果找到,则灰显集群名称。您可以将集群的configMap放在下面的代码段中,然后尝试运行脚本

    def get_cluster_name():
        cm = K8sConfigMap.get_cm('kube-system')
        for item in cm.items:
            if 'kubeadm-config' in item.metadata.name:
                if 'clusterName' in item.data['ClusterConfiguration']:
                    cluster_name = re.search(r"clusterName: ([\s\S]+)controlPlaneEndpoint", \
                    item.data['ClusterConfiguration']).group(1)
                    print ( "\nCluster name: {}".format(cluster_name))

群集名称的灰色映射发生在以下行中:

re.search(r"clusterName: ([\s\S]+)controlPlaneEndpoint",

集群名称值位于
clusterName:
controlPlaneEndpoint
字符串之间。如果需要,可以根据您的环境更改这些字符串。

我知道这不是一种核心资源类型,它是CRD吗?似乎与CRD有关,请检查,因为这是CRD不会有多大帮助的。configuration=client.configuration()api_instance=client.AppsV1beta2Api(client.ApiClient(configuration))try:api_response=api_instance.list_namespaced_stateful_set(命名空间)用于api_response中的群集。items:clusters['name']=cluster.metadata.labels['operator.io/cluster']clusters_info.append(clusters.copy())返回群集信息,ApiException除外,如e:return“调用AppsV1beta2Api->patch_namespaced_stateful_set_status:%s\n”%t您的代码毫无意义。
返回集群信息
?集群信息在哪里?您正在列出
statefulset
。api_response=api_实例。列出api_response中集群的名称空间_stateful_set(名称空间)。项:d1['name']=cluster.metadata.labels['operator.io/cluster']clusters\u info.append(d1.copy())
    def get_cluster_name():
        cm = K8sConfigMap.get_cm('kube-system')
        for item in cm.items:
            if 'kubeadm-config' in item.metadata.name:
                if 'clusterName' in item.data['ClusterConfiguration']:
                    cluster_name = re.search(r"clusterName: ([\s\S]+)controlPlaneEndpoint", \
                    item.data['ClusterConfiguration']).group(1)
                    print ( "\nCluster name: {}".format(cluster_name))

re.search(r"clusterName: ([\s\S]+)controlPlaneEndpoint",