Use the Kubernetes Python client from your running Red Hat OpenShift pods
Red Hat OpenShift is part of the Cloud Native Computing Foundation (CNCF) Certified Program, ensuring portability and interoperability for your container workloads. This also allows you to use Kubernetes tools to interact with an OpenShift cluster, like
kubectl, and you can rest assured that all the APIs you know and love are right there at your fingertips.
The Kubernetes Python client is another great tool for interacting with an OpenShift cluster, allowing you to perform actions on Kubernetes resources with Python code. It also has applications within a cluster. We can configure a Python application running on OpenShift to consume the OpenShift API, and list and create resources. We could then create containerized batch jobs from the running application, or a custom service monitor, for example. It sounds a bit like “OpenShift inception,” using the OpenShift API from services created using the OpenShift API.
In this article, we’ll create a Flask application running on OpenShift. This application will use the Kubernetes Python client to interact with the OpenShift API, list other pods in the project, and display them back to the user.
You’ll need a couple of things to follow along:
- An OpenShift cluster
- A working knowledge of Python
Let’s get started!
I’ve created a template to alleviate a lot of the boilerplate, so let’s clone it down:
git clone https://github.com/shaneboulden/openshift-client-demo cd openshift-client-demo
You can create a new app on your OpenShift cluster using the provided template and see the application running:
oc new-app openshift_deploy/ocd.yaml
If you do an
oc get routes, you’ll be able to see the route that’s been created. For now, if you select the Pods menu item you’ll just get some placeholder text. We’ll fix this shortly 🙂
Everything you need to grow your career.
With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.SIGN UP
Configure the Kubernetes Python client
Listing pods is trivial once we have our client configured, and, fortunately, we can use a little Kubernetes Python client magic to configure this easily with the correct service account token.
Usually, we’d configure a Kubernetes client using a kubeconfig file, which has the required token and hostname to create API requests. The Kubernetes Python client also provides a method
load_incluster_config(), which replaces the kubeconfig file in a running pod, instead using the available environment variables and mount points to find the service account token and build API URLs from the information available within the pod.
There’s another huge benefit to using
load_incluster_config()—our code is now portable. We can take this same application to any Kubernetes cluster, assume nothing about hostnames or network addresses, and easily construct API requests using this awesome little method.
Let’s configure our application to use the
load_incluster_config() method. First, we need to import the client and config objects, you can verify this in the
from kubernetes import client, config
We can now use that magic method to configure the client:
config.load_incluster_config() v1 = client.CoreV1Api()
That’s it! This is all of the code we need to be able to interact with the OpenShift API from running pods.
Use the Kubernetes Downward API
I’m going to introduce something new here, and yes, it’s another “OpenShift-inception” concept. We’re going to use the
list_namespaced_pod method to list pod details; you can find all of the methods available in the documentation. To use this method, we need to pass the current namespace (project) to the Kubernetes client object. But wait, how do we get the namespace for our pod, from inside the running pod?
This is where another awesome Kubernetes API comes into play. It’s called the Downward API and allows us to access metadata about our pod from inside the running pod. To expose information from the Downward API to our pod, we can use environment variables. If you look at the template, you’ll see the following in the ‘env’ section:
- name: POD_NAMESPACE valueFrom: fieldRef: apiVersion: v1 fieldPath: metadata.namespace
Bring it all together
Now let’s get back to our
/pods route in the
ocd.py file. The last thing we need to do is to pass the namespace of the app to the Kubernetes client. We have our environment variable configured to use the downward API already, so let’s pass it in:
pods = v1.list_namespaced_pod(namespace=os.environ["POD_NAMESPACE"])
Ensure you’re in the top-level project directory (i.e., you can see the README) and start a build from the local directory:
oc start-build openshift-client-demo --from-dir=.
When you next visit the route and select the Pods menu, you’ll be able to see all of the pods for the current namespace:
I hope you’ve enjoyed this short introduction to the Kubernetes Python client. If you want to explore a little deeper, you can look at creating resources. There’s an example here that looks at creating containerized batch jobs from API POSTs.
Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets, books, and product downloads.