Bootstrap FreeKB - OpenShift - Create project using the Python Kubernetes client
OpenShift - Create project using the Python Kubernetes client

Updated:   |  OpenShift articles

The openshift package can be used to interact with OpenShift in Python. pip install can be used to install the OpenShift package.

pip install openshift

 

You may want to first check out my article FreeKB - OpenShift - Login into OpenShift using Python.

And here is an example of how you could log into OpenShift with a username and password and create a new project.

from kubernetes import client
from openshift.dynamic import DynamicClient
from openshift.helper.userpassauth import OCPLoginConfiguration

apihost  = 'https://api.openshift.example.com:6443'
username = 'john.doe'
password = 'itsasecret'

kubeConfig = OCPLoginConfiguration(ocp_username=username, ocp_password=password)
kubeConfig.host = apihost
kubeConfig.verify_ssl = False
kubeConfig.get_token()

k8s_client = client.ApiClient(kubeConfig)
dyn_client = DynamicClient(k8s_client)

project = dyn_client.resources.get(api_version='project.openshift.io/v1', kind='Project')

project_manifest = {
  "apiVersion": "project.openshift.io/v1",
  "kind": "Project",
  "metadata": {
    "name": "my-project"
  }
}

project.create(body=project_manifest)

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 7b84f5 in the box below so that we can be sure you are a human.