Kubernetes and OpenShift Python library
Python Shell Makefile
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
docs
examples
kubeshift
script
test
.coveragerc
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
MANIFEST.in
Makefile
README.md
requirements.txt
setup.cfg
setup.py
test-requirements.txt
tox.ini

README.md

Kubeshift

Build Status Coverage Status

Introduction

Kubeshift is a multi-provider Python library for Kubernetes (kube) and Openshift (shift). We connect and communicate with each container orchestator 100% through their TLS (if available) HTTP API.

Features:

  • 100% HTTP API
  • Auto-parsing of ~/.kube/config
  • .kube/config generation
  • TLS authentication
  • 100% test coverage with functional and integration tests

Library installation

Pip

sudo pip install kubeshift

Manual / development

git clone https://github.com/cdrage/kubeshift && cd kubeshift
make install

Python requirements

▶ cat requirements.txt
PyYAML
requests

Configuration import

The configuration file used with the provider must be an object. Currently we support the import and generation of Kubernetes and OpenShift configuration files .

import kubeshift

# Import the configuration, this can be either from a file
config = kubeshift.Config.from_file("/home/user/.kube/config")

# Or generated via a set of parameters
config_params = kubeshift.Config.from_params(context_name="default", username="default", api="https://localhost:8080", auth="foobar", ca="/home/user/.kube/ca.cert", verify=True, filepath=None)

# Client connection
k8s_client = kubeshift.KubernetesClient(config)
oc_client = kubeshift.OpenshiftClient(config)

Named Query methods

API calls are also available via their corresponding method. Each call returns a Query object used to retrieve and filter.

Methods Sourced through discovery

  • http://localhost:8080/apis
  • http://localhost:8080/oapi

Full example:

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)

# Main methods
client.create(k8s_object)  # Creates the k8s object
# client.scale(k8s_object, replicas=3) # Scales the k8s object (if it's a service)
client.delete(k8s_object)  # Deletes the k8s object

# API calls

# Namespaces
client.namespaces().all()

# Pods
client.pods().all()
client.pods().filter(namespace="default", status="Running")
client.pods().metadata()
client.pods().items()