python-api
1 TopicExploring Kubernetes API using Wireshark part 3: Python Client API
Quick Intro In this article, we continue our exploration of Kubernetes API but this time we're going to use Python along with Wireshark. I strongly advise you to go throughExploring Kubernetes API using Wireshark part 1: Creating, Listing and Deleting PodsandExploring Kubernetes API using Wireshark part 2: Namespacesfirst. We're not creating any app here. I'll just show you how you can explore Kubernetes API using Python's client API and Wireshark output retrieved fromkubectl get pods command: In case you want to follow along, I've set upgcloud toolas I'm using Google Cloud andGOOGLE_APPLICATION_CREDENTIALSenvironment variable on my Mac so I had no problem authenticating to Kubernetes API. I hope you understand that when I say Kubernetes API, I'm talking about the API on Kubernetes Master node on my Google Cloud's Kubernetes cluster. What I'm going to do here In this article, we're going to do the following (using Python): Authenticate to kube API Connect to /api/v1 and then move to/api/v1/namespaces/default/pods Add pod information to a variable named pods from variable pods, create a list of: pod names pod status At the end, we display the above info Authenticating/Authorising your code to contact Kube API Here we're loading auth and cluster info from kube-config file and storing into into our client API config: Connecting to /api/v1 We now store into v1 variable the API object. For the sake of simplicity, think of it as we were pointing to /api/v1/ folder and we haven't decided where to go yet: Connecting to /api/v1/namespaces/default/pods We now move to /api/v1/namespaces/default/pods which where we find information about pods that belong to default namespace (watch=False just means we will not be monitoring for changes, we're just retrieving it as one-off thing): Now, what we've stored in the above variable (ret) is the equivalent of moving to the root directory in our JSON tree (Object) in the output below: The output above was from kubectl get pods command which lists all pods from default namespace and that's equivalent to what we're doing here using Python's Kubernetes Client API. Python shows us similar options about where to go when I typeret.and hit tab: We've gotkind,apiVersion,metadataanditemsas options to move to butitemsis what we're looking for because it's supposed to store pod's information. So let's move to items and store it in another variable calledpodsto make it easier to remember that it actually holds all pods' information fromdefaultnamespace: Listing pods' names We're now in a comfortable place and ready to play! Let's keeppodsvariable as our placeholder! On Wireshark, pod's name is located inmetadata.name: \ So let's create another variable calledpods_namesto store all pods' names: Listing Pods' status (phase) What we're looking for is in status.phase: Let's store it instatusvariable in Python: Displaying the output We can now display the output using built-in zip() method: If you want something pretty you can use prettytable:3.3KViews1like0Comments