Charlie Drage

Kubeshift: A Python library for Kubernetes and OpenShift

A Python library for OpenShift was needed after a search came up with only one library that’s no longer being developed.

With Kubernetes and OpenShift being such similar projects. It made sense to have a universal Python library that’s able to communicate with both.

Taking the code from a previous project the team at Project Atomic and I worked on, Kubeshift was created.

It’s super easy to use Kubeshift.

Installation

Installation is done either through Git clone or Pip.

git clone https://github.com/cdrage/kubeshift && cd kubeshift && sudo make install
# or
sudo pip install kubeshift

Use

After bringing your Kubernetes cluster / OpenShift cluster up, use one of the built-in methods to create a pod!

import kubeshift
import getpass

# Example k8s object
k8s_object = {"apiVersion": "v1", "kind": "Pod", "metadata": {"labels": {"app": "hellonginx"}, "name": "hellonginx"}, "spec": {
    "containers": [{"image": "nginx", "name": "hellonginx", "ports": [{"containerPort": 80, "hostPort": 80, "protocol": "TCP"}]}]}}

# Client configuration
user = getpass.getuser()
config = kubeshift.Config.from_file("/home/%s/.kube/config" % user)
client = kubeshift.KubernetesClient(config)
# client = kubeshift.OpenshiftClient(config)

# Create an object
client.create(k8s_object)  # Creates the k8s object

Main features

Kubeshift excels at configuration generation. Either specifying a configuration file (ex. /ghome/user/.kube/config) or leaving it blank will generate an appropriate config in order to communicate to the cluster.

Every API call is implemented as a function within each provider.

For more information on Kubeshift as well as further documentation on each method, check out the GitHub repo.

Comments