From d3cd981326c98984117ae6fd768d63c39257de69 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Mon, 3 Jul 2017 16:33:30 -0400 Subject: Adding storageclass support to lib_openshift. --- roles/lib_openshift/src/class/oc_storageclass.py | 147 +++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 roles/lib_openshift/src/class/oc_storageclass.py (limited to 'roles/lib_openshift/src/class') diff --git a/roles/lib_openshift/src/class/oc_storageclass.py b/roles/lib_openshift/src/class/oc_storageclass.py new file mode 100644 index 000000000..6c62c7bd8 --- /dev/null +++ b/roles/lib_openshift/src/class/oc_storageclass.py @@ -0,0 +1,147 @@ +# pylint: skip-file +# flake8: noqa + +# pylint: disable=too-many-instance-attributes +class OCStorageClass(OpenShiftCLI): + ''' Class to wrap the oc command line tools ''' + kind = 'sc' + + # pylint allows 5 + # pylint: disable=too-many-arguments + def __init__(self, + config, + verbose=False): + ''' Constructor for OCStorageClass ''' + super(OCStorageClass, self).__init__(None, kubeconfig=config.kubeconfig, verbose=verbose) + self.config = config + self.storage_class = None + + def exists(self): + ''' return whether a storageclass exists''' + if self.storage_class: + return True + + return False + + def get(self): + '''return storageclass ''' + result = self._get(self.kind, self.config.name) + if result['returncode'] == 0: + self.storage_class = StorageClass(content=result['results'][0]) + elif '\"%s\" not found' % self.config.name in result['stderr']: + result['returncode'] = 0 + result['results'] = [{}] + + return result + + def delete(self): + '''delete the object''' + return self._delete(self.kind, self.config.name) + + def create(self): + '''create the object''' + return self._create_from_content(self.config.name, self.config.data) + + def update(self): + '''update the object''' + # parameters are currently unable to be updated. need to delete and recreate + self.delete() + return self.create() + + def needs_update(self): + ''' verify an update is needed ''' + # check if params have updated + if self.storage_class.get_parameters() == self.config.parameters: + return False + + return True + + @staticmethod + # pylint: disable=too-many-return-statements,too-many-branches + # TODO: This function should be refactored into its individual parts. + def run_ansible(params, check_mode): + '''run the ansible idempotent code''' + + rconfig = StorageClassConfig(params['name'], + provisioner="kubernetes.io/{}".format(params['provisioner']), + parameters=params['parameters'], + annotations=params['annotations'], + api_version="storage.k8s.io/{}".format(params['api_version']), + default_sc=params['default_storage_class'], + kubeconfig=params['kubeconfig'], + ) + + oc_sc = OCStorageClass(rconfig, verbose=params['debug']) + + state = params['state'] + + api_rval = oc_sc.get() + + ##### + # Get + ##### + if state == 'list': + return {'changed': False, 'results': api_rval['results'], 'state': 'list'} + + ######## + # Delete + ######## + if state == 'absent': + if oc_sc.exists(): + + if check_mode: + return {'changed': True, 'msg': 'Would have performed a delete.'} + + api_rval = oc_sc.delete() + + return {'changed': True, 'results': api_rval, 'state': 'absent'} + + return {'changed': False, 'state': 'absent'} + + if state == 'present': + ######## + # Create + ######## + if not oc_sc.exists(): + + if check_mode: + return {'changed': True, 'msg': 'Would have performed a create.'} + + # Create it here + api_rval = oc_sc.create() + + if api_rval['returncode'] != 0: + return {'failed': True, 'msg': api_rval} + + # return the created object + api_rval = oc_sc.get() + + if api_rval['returncode'] != 0: + return {'failed': True, 'msg': api_rval} + + return {'changed': True, 'results': api_rval, 'state': 'present'} + + ######## + # Update + ######## + if oc_sc.needs_update(): + api_rval = oc_sc.update() + + if api_rval['returncode'] != 0: + return {'failed': True, 'msg': api_rval} + + # return the created object + api_rval = oc_sc.get() + + if api_rval['returncode'] != 0: + return {'failed': True, 'msg': api_rval} + + return {'changed': True, 'results': api_rval, 'state': 'present'} + + return {'changed': False, 'results': api_rval, 'state': 'present'} + + + return {'failed': True, + 'changed': False, + 'msg': 'Unknown state passed. %s' % state, + 'state': 'unknown'} -- cgit v1.2.3 From f51e0082fe17f14aec5e14facc143986f47cf260 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Fri, 14 Jul 2017 08:59:02 -0400 Subject: Fixing needs_update comparison. Added a small pause for race conditions. Fixed doc. Fix kind to storageclass --- roles/lib_openshift/src/class/oc_storageclass.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'roles/lib_openshift/src/class') diff --git a/roles/lib_openshift/src/class/oc_storageclass.py b/roles/lib_openshift/src/class/oc_storageclass.py index 6c62c7bd8..ece684c56 100644 --- a/roles/lib_openshift/src/class/oc_storageclass.py +++ b/roles/lib_openshift/src/class/oc_storageclass.py @@ -4,7 +4,7 @@ # pylint: disable=too-many-instance-attributes class OCStorageClass(OpenShiftCLI): ''' Class to wrap the oc command line tools ''' - kind = 'sc' + kind = 'storageclass' # pylint allows 5 # pylint: disable=too-many-arguments @@ -46,15 +46,22 @@ class OCStorageClass(OpenShiftCLI): '''update the object''' # parameters are currently unable to be updated. need to delete and recreate self.delete() + # pause here and attempt to wait for delete. + # Better option would be to poll + time.sleep(5) return self.create() def needs_update(self): ''' verify an update is needed ''' # check if params have updated - if self.storage_class.get_parameters() == self.config.parameters: - return False + if self.storage_class.get_parameters() != self.config.parameters: + return True + + for anno_key, anno_value in self.storage_class.get_annotations().items(): + if 'is-default-class' in anno_key and anno_value != self.config.default_storage_class: + return True - return True + return False @staticmethod # pylint: disable=too-many-return-statements,too-many-branches @@ -67,7 +74,7 @@ class OCStorageClass(OpenShiftCLI): parameters=params['parameters'], annotations=params['annotations'], api_version="storage.k8s.io/{}".format(params['api_version']), - default_sc=params['default_storage_class'], + default_storage_class=params.get('default_storage_class', 'false'), kubeconfig=params['kubeconfig'], ) -- cgit v1.2.3 From 199ce71ff2dfb2c32710c1bf39b72255f8427deb Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Fri, 14 Jul 2017 09:09:01 -0400 Subject: Fixed tests and added sleep for update. --- roles/lib_openshift/src/class/oc_storageclass.py | 1 + 1 file changed, 1 insertion(+) (limited to 'roles/lib_openshift/src/class') diff --git a/roles/lib_openshift/src/class/oc_storageclass.py b/roles/lib_openshift/src/class/oc_storageclass.py index ece684c56..34ebf7c41 100644 --- a/roles/lib_openshift/src/class/oc_storageclass.py +++ b/roles/lib_openshift/src/class/oc_storageclass.py @@ -48,6 +48,7 @@ class OCStorageClass(OpenShiftCLI): self.delete() # pause here and attempt to wait for delete. # Better option would be to poll + import time time.sleep(5) return self.create() -- cgit v1.2.3 From 94923be737328a2f0227b70a8dc12bda7e731f1d Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Mon, 17 Jul 2017 15:14:32 -0400 Subject: Fixed spacing and lint errors. --- roles/lib_openshift/src/class/oc_storageclass.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'roles/lib_openshift/src/class') diff --git a/roles/lib_openshift/src/class/oc_storageclass.py b/roles/lib_openshift/src/class/oc_storageclass.py index 34ebf7c41..aced586ae 100644 --- a/roles/lib_openshift/src/class/oc_storageclass.py +++ b/roles/lib_openshift/src/class/oc_storageclass.py @@ -46,8 +46,8 @@ class OCStorageClass(OpenShiftCLI): '''update the object''' # parameters are currently unable to be updated. need to delete and recreate self.delete() - # pause here and attempt to wait for delete. - # Better option would be to poll + # pause here and attempt to wait for delete. + # Better option would be to poll import time time.sleep(5) return self.create() -- cgit v1.2.3