Following code snipet uses kubeconfig python module to switch context and Python client for the kubernetes API to get cluster node details. It takes the default kubeconfig file, and switch to the required context, and get node info of the respective cluster.
kubectl commands:
kubectl config get-contexts
kubectl config current-context
kubectl config use-context <context_name>
kubectl get nodes -o json
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# prerequisite: pip3 install kubernetes, pip3 install kubeconfig | |
# this sample code snipet can be used to get basic node details of a given list of clusters/ contexts | |
from kubernetes import client, config | |
from kubeconfig import KubeConfig | |
import argparse | |
import json | |
def get_nodes(): | |
config.load_kube_config() | |
v1 = client.CoreV1Api() | |
return v1.list_node(_preload_content=False) | |
def switch_context(cluster_name): | |
conf = KubeConfig() | |
conf.use_context(f"{cluster_name}") | |
conf_doc = conf.view() | |
return conf.current_context() | |
def main(): | |
""" | |
This function will use the default kubeconfig file, and prints basic node details of a given context. | |
""" | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"-c", "--cluster", required=True, help="K8s cluster context name" | |
) | |
args = parser.parse_args() | |
all_clusters = args.cluster | |
cluster_list = all_clusters.split(",") | |
for cluster_name in cluster_list: | |
current_context = switch_context(cluster_name) | |
print(f"Current context: {current_context}") | |
get_nodes_info = get_nodes() | |
get_nodes_info_dict = json.loads(get_nodes_info.data) | |
for each_node in get_nodes_info_dict["items"]: | |
if ( | |
"node-role.kubernetes.io/control-plane" | |
in each_node["metadata"]["labels"] | |
): | |
role = "Control Plane Node" | |
elif "node-role.kubernetes.io/agent" in each_node["metadata"]["labels"]: | |
role = "Worker Node" | |
print( | |
f'{each_node["metadata"]["name"]} - {each_node["status"]["nodeInfo"]["operatingSystem"]} - {role}' | |
) | |
print("------------------------------------------") | |
if __name__ == "__main__": | |
main() |
https://kubeconfig-python.readthedocs.io/en/latest/
https://github.com/kubernetes-client/python