How Can I Use Kubernetes Python Api To Get Clusters Information?
Solution 1:
I was able to get the cluster name for the configmap used for clusterConfiguration. This config map exists if the cluster is a kubeadm one.
https://github.com/dguyhasnoname/k8s-cluster-checker/blob/master/objects/cluster.py#L17
below snippet fetches configmap from python client using module get-cm.py(in modules folder of above repo). It checks if clusetConfiguration configmap kubeadm-config
is present and if found, greps out the cluster name. You can put configMap of your cluster in below snippet and try running the script.
defget_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))
the grepping of cluster name is happening in below line:
re.search(r"clusterName: ([\s\S]+)controlPlaneEndpoint",
cluster name value is found between clusterName:
and controlPlaneEndpoint
strings. You can change these strings if needed, according to your env.
Solution 2:
This one maybe help you, at
from pick import pick # install pick using `pip install pick`
from kubernetes import client, config
from kubernetes.client import configuration
def main():
contexts, active_context = config.list_kube_config_contexts()
if not contexts:
print("Cannot find any context in kube-config file.")
return
contexts = [context['name'] for context in contexts]
active_index = contexts.index(active_context['name'])
cluster1, first_index = pick(contexts, title="Pick the first context",
default_index=active_index)
cluster2, _ = pick(contexts, title="Pick the second context",
default_index=first_index)
client1 = client.CoreV1Api(
api_client=config.new_client_from_config(context=cluster1))
client2 = client.CoreV1Api(
api_client=config.new_client_from_config(context=cluster2))
print("\nList of pods on %s:" % cluster1)
for i in client1.list_pod_for_all_namespaces().items:
print("%s\t%s\t%s" %
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
print("\n\nList of pods on %s:" % cluster2)
for i in client2.list_pod_for_all_namespaces().items:
print("%s\t%s\t%s" %
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
Solution 3:
Below is the code to get the cluster info (CRD):
clusters_info = []
d1 = {}
config.load_kube_config()
#config.load_incluster_config()
configuration = client.Configuration()
api_instance = client.AppsV1beta2Api(client.ApiClient(configuration))
try:
api_response = api_instance.list_namespaced_stateful_set(namespace)
for cluster in api_response.items:
d1['name']=cluster.metadata.labels['operator.io/cluster']
clusters_info.append(d1.copy())
return clusters_info
except ApiException as e:
return"Exception when calling AppsV1beta2Api->patch_namespaced_stateful_set_status: %s\n" % e
Post a Comment for "How Can I Use Kubernetes Python Api To Get Clusters Information?"