OpenShift python client
Python client for the OpenShift API.
Installation
From source:
git clone https://github.com/openshift/openshift-restclient-python.git
cd openshift-restclient-python
python setup.py install
From PyPi directly:
pip install openshift
Using Dockerfile:
docker build -t openshift-restclient-python -f Dockerfile .
Usage and examples
The OpenShift client depends on the Kubernetes Python client, and as part of the installation process, the Kubernetes (K8s) client is automatically installed.
In the case you are using Docker, you will likely need to share your .kube/config
with the openshift-restclient-python
container:
docker run -it -v $HOME/.kube/config:/root/.kube/config:z openshift-restclient-python python
To work with a K8s object, use the K8s client, and to work with an OpenShift specific object, use the OpenShift client. For example, the following uses the K8s client to create a new Service object:
import yaml
from kubernetes import client, config
config.load_kube_config()
api = client.CoreV1Api()
service = """
kind: Service
apiVersion: v1
metadata:
name: my-service
spec:
selector:
app: MyApp
ports:
- protocol: TCP
port: 8080
targetPort: 9376
"""
service_data = yaml.load(service)
resp = api.create_namespaced_service(body=service_data, namespace='default')
# resp is a V1Service object
print resp.metadata.self_link
Now in the following example, we use the OpenShift client to create a Route object, and associate it with the new Service:
import yaml
from openshift import client, config
config.load_kube_config()
api = client.OapiApi()
route = """
apiVersion: v1
kind: Route
metadata:
name: frontend
spec:
host: www.example.com
to:
kind: Service
name: my-service
"""
route_data = yaml.load(route)
resp = api.create_namespaced_route(body=route_data, namespace='default')
# resp is a V1Route object
print resp.metadata.self_link
And finally, the following uses the OpenShift client to list Projects the user can access:
from openshift import client, config
config.load_kube_config()
oapi = client.OapiApi()
project_list = oapi.list_project()
for project in project_list.items:
print project.metadata.name
Documentation
All OpenShift API and Model documentation can be found in the Generated client's README file
Community, Support, Discussion
If you have any problem with the package or any suggestions, please file an issue.
Code of Conduct
Participation in the Kubernetes community is governed by the CNCF Code of Conduct.
Update generated client
Updating the generated client requires the following tools:
- tox
- maven3
To apply the updates:
- Incorporate new changes to update scripts
- scripts/constants.py, scripts/pom.xml, scripts/preprocess_spec.py, and update-client.sh are the most important
- Run tox -e update_client
Ansible Modules
This repo is home to the tools used to generate the K8s modules for Ansible.
Using the modules
The modules are currently in pre-release. For convenience there is an Ansible role available at ansible/ansible-kubernetes-modules, which if referenced in a playbook, will provide full access to the latest.
Requirements
- Ansible installed from source
- OpenShift Rest Client installed on the host where the modules will execute
Installation and use
Using the Galaxy client, download and install the role as follows:
$ ansible-galaxy install ansible.kubernetes-modules
Include the role in your playbook, and the modules will be available, allowing tasks from any other play or role to reference them. Here's an example:
- hosts: localhost
connection: local
gather_facts: no
roles:
- role: ansible.kubernetes-modules
- role: hello-world
The hello-world
role deploys an application to a locally running OpenShift instance by executing tasks with the modules. It's able to access them because ansible.ansible-kubernetes-modules
is referenced.
You'll find the modules in the library folder of the role. Each contains documented parameters, and the returned data structure. Not every module contains examples, only those where we have added test data.
If you find a bug, or have a suggestion, related to the modules, please file an issue here
Generating the modules
After installing the OpenShift client, the modules can be generated by running the following:
$ openshift-ansible-gen modules --output-path /path/to/modules/dir
If --output-path
is not provided, modules will be written to ./_modules
.
Common module
Individual modules are generated using the OpenShift Rest Client. However, there is a shared or utility module in the Ansible repo called, k8s_common.py, which imports the client, and performs most of the work. This is currently in a pre-release state as well, and is only available in the devel
branch of Ansible. For this reason, you'll need to run Ansible from source. For assistnace, see Running from source.