Bootstrap FreeKB - OpenShift - List Role Bindings using Python
OpenShift - List Role Bindings using Python

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 list all Role Bindings in all namespaces.

#!/usr/bin/python
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)

v1_role_bindings = dyn_client.resources.get(api_version='rbac.authorization.k8s.io/v1', kind='RoleBinding')
role_bindings = v1_role_bindings.get()

print(role_bindings)

 

Here is a bit more of a practical example with try / except / else error handling.

#!/usr/bin/python
import sys
from kubernetes import client
from kubernetes.client.rest import ApiException
from openshift.dynamic import DynamicClient
from openshift.helper.userpassauth import OCPLoginConfiguration

kubeConfig = OCPLoginConfiguration(ocp_username='john.doe', ocp_password='itsasecret')
kubeConfig.host = 'https://api.openshift.example.com:6443'
kubeConfig.verify_ssl = False
kubeConfig.get_token()

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

try:
  v1_role_bindings = dyn_client.resources.get(api_version='rbac.authorization.k8s.io/v1', kind='RoleBinding')
except ApiException as exception:
  print(f"got the following exception - {exception}")
  sys.exit(1)

role_bindings = v1_role_bindings.get()

print(role_bindings)

 

And here is now you could list all Role Bindings in a specific namespace.

role_bindings = v1_role_bindings.get(namespace='my-project')

 

And here is now you could list a specific Role Binding in a specific namespace.

role_binding = v1_role_bindings.get(namespace='my-project', name='my-role-binding')

 

If your request returns two or more results, here is how you can loop through the results.

role_bindings = v1_role_bindings.get()

for role_binding in role_bindings.items:
  print(role_binding)

 

And here is how you can print a specific key in the response using dot notation.

role_bindings = v1_role_bindings.get()

for role_binding in role_bindings.items:
  print(role_binding.metadata.name)

 

Or like this.

role_bindings = v1_role_bindings.get()

for role_binding in role_bindings.items:
  print(role_binding['metadata']['name'])

 




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 983452 in the box below so that we can be sure you are a human.