Skip to content
284 changes: 216 additions & 68 deletions docs/index.html

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions src/KubeLibrary/KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def reload_config(self, kube_config=None, context=None, api_url=None, bearer_tok
self._add_api('appsv1', client.AppsV1Api)
self._add_api('batchv1_beta1', client.BatchV1beta1Api)
self._add_api('custom_object', client.CustomObjectsApi)
self._add_api('custom_definition', client.ApiextensionsV1Api)
self._add_api('rbac_authv1_api', client.RbacAuthorizationV1Api)
self._add_api('autoscalingv1', client.AutoscalingV1Api)
self._add_api('dynamic', DynamicClient)
Expand Down Expand Up @@ -1451,6 +1452,30 @@ def get_role_bindings_in_namespace(self, namespace):
ret = self.rbac_authv1_api.list_namespaced_role_binding(namespace, watch=False)
return [item.metadata.name for item in ret.items]

def list_cluster_custom_definition(self, label_selector="", field_selector=""):
"""Lists Custom Resource Definitions.

Can be optionally filtered by label. e.g. label_selector=label_key=label_value
Can be optionally filtered by field. e.g. field_selector=label_key=label_value

Returns list of CRD's.

https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md
"""
return self.custom_definition.list_custom_resource_definition(label_selector=label_selector, field_selector=field_selector).items

def read_cluster_custom_definition(self, name):
"""Reads the specified CustomResourceDefinition.

Returns an object.

- ``name``:
Custom Resource Definition name, e.g. 'repos.configmanagement.gke.io'

https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md
"""
return self.custom_definition.read_custom_resource_definition(name)

def list_cluster_custom_object(self, group, version, plural):
"""Lists cluster level custom objects.

Expand Down
10 changes: 9 additions & 1 deletion test/test_KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def mock_list_namespaced_role_bindings(namespace, watch=False):
class TestKubeLibrary(unittest.TestCase):

apis = ('v1', 'networkingv1api', 'batchv1', 'appsv1', 'batchv1_beta1',
'custom_object', 'rbac_authv1_api', 'autoscalingv1', 'dynamic')
'custom_object', 'custom_definition', 'rbac_authv1_api', 'autoscalingv1', 'dynamic')

@responses.activate
def test_KubeLibrary_inits_from_kubeconfig(self):
Expand Down Expand Up @@ -783,3 +783,11 @@ def test_read_namespaced_cron_job(self, mock_lnp):
cron_job_details2 = kl.get_cron_job_details_in_namespace('hello', 'default')
self.assertEqual(cron_job_details.items.metadata.labels.TestLabel, cron_job_details2.items.metadata.labels.TestLabel)
self.assertEqual('mytestlabel', cron_job_details.items.metadata.labels.TestLabel)

def test_read_cluster_custom_definition(self):
pass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add mocks and perform actual check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your reply! Really appreciate it. Will fix it later on.


def test_list_cluster_custom_definition(self):
kl = KubeLibrary(kube_config='test/resources/k3d')
crd = kl.list_cluster_custom_definition()

15 changes: 15 additions & 0 deletions testcases/custom_definition/custom_definitions.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*** Settings ***
Resource ./custom_definitions_kw.robot

*** Variables ***
${crds} repos.configmanagement.gke.io syncs.configmanagement.gke.io
${crd_name} repos.configmanagement.gke.io

*** Test Cases ***
Check All The Custom Resource Definitions Exist
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are missing

[Tags]    other

as a result they are not executed in CI

[Documentation] Test all the given Custom Resource Definitions exist.
CRD contains: ${crds}

Check The Given Custom Resource Definition Exist
[Documentation] Test the given Custom Resource Definition exists.
CRD ${crd_name} exists
26 changes: 26 additions & 0 deletions testcases/custom_definition/custom_definitions_kw.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
*** Settings ***
Library String
# For regular execution
Library KubeLibrary
# For incluster execution
#Library KubeLibrary None True False
# For development
#Library ../../src/KubeLibrary/KubeLibrary.py ~/.kube/k3d

*** Keywords ***
CRD contains: ${crd_names}
[Documentation] Check the given CRD's exist!
@{crd_name_list}= Split String ${crd_names} ${SPACE}
@{all_crds}= List Cluster Custom Definition
@{all_crd_names}= Filter Names ${all_crds}
${crd_num}= Get Length ${all_crd_names}
Log \nTotally ${crd_num} CRD(s) have been found. console=True
FOR ${name} IN @{crd_name_list}
Should Contain ${all_crd_names} ${name}
END

CRD ${crd_name} exists
[Documentation] Check the given CRD exit!
${crd_detail}= Read Cluster Custom Definition ${crd_name}
Should Be Equal ${crd_detail.kind} CustomResourceDefinition
Should Be Equal ${crd_detail.metadata.name} ${crd_name}