From b5de5357042c146a930097d52c3920808af89c42 Mon Sep 17 00:00:00 2001 From: Joel Diaz Date: Fri, 27 Jan 2017 20:39:06 +0000 Subject: oc_label ansible module used for adding/removing labels on various OpenShift objects --- roles/lib_openshift/src/doc/label | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 roles/lib_openshift/src/doc/label (limited to 'roles/lib_openshift/src/doc/label') diff --git a/roles/lib_openshift/src/doc/label b/roles/lib_openshift/src/doc/label new file mode 100644 index 000000000..8122bd4b0 --- /dev/null +++ b/roles/lib_openshift/src/doc/label @@ -0,0 +1,65 @@ +# flake8: noqa +# pylint: skip-file + +DOCUMENTATION = ''' +--- +module: oc_label +short_description: Create, modify, and idempotently manage openshift object labels. +description: + - Modify openshift labels programmatically. +options: + state: + description: + - State represents whether to create, modify, delete, or list + required: true + default: present + choices: ["present", "absent", "list", "add"] + aliases: [] + kubeconfig: + description: + - The path for the kubeconfig file to use for authentication + required: false + default: /etc/origin/master/admin.kubeconfig + aliases: [] + debug: + description: + - Turn on debug output. + required: false + default: False + aliases: [] + kind: + description: + - The kind of object that can be managed. + required: false + default: None + choices: + - node + - pod + aliases: [] + labels: + description: + - The labels to add to the resource. + required: false + default: None + aliases: [] + selector: + description: + - The selector to apply to the query + required: false + default: None + aliases: [] +author: +- "Joel Diaz " +extends_documentation_fragment: [] +''' + +EXAMPLES = ''' +- name: Add label to node + oc_label: + name: ip-172-31-5-23.ec2.internal + state: add + kind: node + labels: + - key: logging-infra-fluentd + value: 'true' +''' -- cgit v1.2.3 From 4806fe365e11a9a0266c06d052537836397a26c6 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 31 Jan 2017 09:36:23 -0500 Subject: Fixing doc and generating new label with updated base. --- roles/lib_openshift/library/oc_label.py | 93 ++++++++++++++++++----------- roles/lib_openshift/src/ansible/oc_label.py | 2 +- roles/lib_openshift/src/doc/label | 9 +-- 3 files changed, 63 insertions(+), 41 deletions(-) (limited to 'roles/lib_openshift/src/doc/label') diff --git a/roles/lib_openshift/library/oc_label.py b/roles/lib_openshift/library/oc_label.py index 9fc0a15b5..a876346f4 100644 --- a/roles/lib_openshift/library/oc_label.py +++ b/roles/lib_openshift/library/oc_label.py @@ -24,6 +24,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # + +# -*- -*- -*- Begin included fragment: lib/import.py -*- -*- -*- ''' OpenShiftCLI class that wraps the oc commands in a subprocess ''' @@ -40,12 +42,16 @@ import subprocess import ruamel.yaml as yaml from ansible.module_utils.basic import AnsibleModule +# -*- -*- -*- End included fragment: lib/import.py -*- -*- -*- + +# -*- -*- -*- Begin included fragment: doc/label -*- -*- -*- + DOCUMENTATION = ''' --- module: oc_label -short_description: Create, modify, and idempotently manage openshift object labels. +short_description: Create, modify, and idempotently manage openshift labels. description: - - Manage openshift object labels programmatically. + - Modify openshift labels programmatically. options: state: description: @@ -66,52 +72,47 @@ options: required: false default: False aliases: [] - name: - description: - - Name of the object that is being queried. - required: false - default: None - aliases: [] - namespace: - description: - - The namespace where the object lives. - required: false - default: None - aliases: [] kind: description: - - The kind of OpenShift object being queried (node or pod). - required: true + - The kind of object that can be managed. + required: True default: None - choices: ["node", "pod"] + choices: + - node + - pod + - namespace aliases: [] labels: description: - - List of label_name=label_value items. + - A list of labels to for the resource. required: false default: None aliases: [] selector: description: - - XXX + - The selector to apply to the resource query required: false default: None aliases: [] author: -- "Kenny Woodson " +- "Joel Diaz " extends_documentation_fragment: [] ''' EXAMPLES = ''' - name: Add label to node oc_label: + name: ip-172-31-5-23.ec2.internal state: add kind: node - name: ip-172-31-5-23.ec2.internal labels: - - key: provision_date - value: "2017-01-01" + - key: logging-infra-fluentd + value: 'true' ''' + +# -*- -*- -*- End included fragment: doc/label -*- -*- -*- + +# -*- -*- -*- Begin included fragment: ../../lib_utils/src/class/yedit.py -*- -*- -*- # noqa: E301,E302 @@ -676,6 +677,10 @@ class Yedit(object): 'state': "present"} return {'failed': True, 'msg': 'Unkown state passed'} + +# -*- -*- -*- End included fragment: ../../lib_utils/src/class/yedit.py -*- -*- -*- + +# -*- -*- -*- Begin included fragment: lib/base.py -*- -*- -*- # pylint: disable=too-many-lines # noqa: E301,E302,E303,T001 @@ -787,12 +792,11 @@ class OpenShiftCLI(object): cmd = ['get', resource] if selector: cmd.append('--selector=%s' % selector) + elif rname: + cmd.append(rname) cmd.extend(['-o', 'json']) - if rname: - cmd.append(rname) - rval = self.openshift_cmd(cmd, output=True) # Ensure results are retuned in an array @@ -882,6 +886,18 @@ class OpenShiftCLI(object): cmd.append('--confirm') return self.openshift_cmd(cmd) + def _run(self, cmds, input_data): + ''' Actually executes the command. This makes mocking easier. ''' + proc = subprocess.Popen(cmds, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env={'KUBECONFIG': self.kubeconfig}) + + stdout, stderr = proc.communicate(input_data) + + return proc.returncode, stdout, stderr + # pylint: disable=too-many-arguments,too-many-branches def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None): '''Base command for oc ''' @@ -893,7 +909,7 @@ class OpenShiftCLI(object): if self.all_namespaces: cmds.extend(['--all-namespaces']) - elif self.namespace: + elif self.namespace is not None and self.namespace.lower() not in ['none', 'emtpy']: # E501 cmds.extend(['-n', self.namespace]) cmds.extend(cmd) @@ -905,18 +921,13 @@ class OpenShiftCLI(object): if self.verbose: print(' '.join(cmds)) - proc = subprocess.Popen(cmds, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - env={'KUBECONFIG': self.kubeconfig}) + returncode, stdout, stderr = self._run(cmds, input_data) - stdout, stderr = proc.communicate(input_data) - rval = {"returncode": proc.returncode, + rval = {"returncode": returncode, "results": results, "cmd": ' '.join(cmds)} - if proc.returncode == 0: + if returncode == 0: if output: if output_type == 'json': try: @@ -1197,6 +1208,10 @@ class OpenShiftCLIConfig(object): return rval +# -*- -*- -*- End included fragment: lib/base.py -*- -*- -*- + +# -*- -*- -*- Begin included fragment: class/oc_label.py -*- -*- -*- + # pylint: disable=too-many-instance-attributes class OCLabel(OpenShiftCLI): ''' Class to wrap the oc command line tools ''' @@ -1486,6 +1501,10 @@ class OCLabel(OpenShiftCLI): 'results': 'Unknown state passed. %s' % state, 'state': "unknown"} +# -*- -*- -*- End included fragment: class/oc_label.py -*- -*- -*- + +# -*- -*- -*- Begin included fragment: ansible/oc_label.py -*- -*- -*- + def main(): ''' ansible oc module for labels ''' @@ -1496,7 +1515,7 @@ def main(): choices=['present', 'absent', 'list', 'add']), debug=dict(default=False, type='bool'), kind=dict(default=None, type='str', required=True, - choices=['node', 'pod']), + choices=['node', 'pod', 'namespace']), name=dict(default=None, type='str'), namespace=dict(default=None, type='str'), labels=dict(default=None, type='list'), @@ -1515,3 +1534,5 @@ def main(): if __name__ == '__main__': main() + +# -*- -*- -*- End included fragment: ansible/oc_label.py -*- -*- -*- diff --git a/roles/lib_openshift/src/ansible/oc_label.py b/roles/lib_openshift/src/ansible/oc_label.py index 53632678b..149b965d8 100644 --- a/roles/lib_openshift/src/ansible/oc_label.py +++ b/roles/lib_openshift/src/ansible/oc_label.py @@ -11,7 +11,7 @@ def main(): choices=['present', 'absent', 'list', 'add']), debug=dict(default=False, type='bool'), kind=dict(default=None, type='str', required=True, - choices=['node', 'pod']), + choices=['node', 'pod', 'namespace']), name=dict(default=None, type='str'), namespace=dict(default=None, type='str'), labels=dict(default=None, type='list'), diff --git a/roles/lib_openshift/src/doc/label b/roles/lib_openshift/src/doc/label index 8122bd4b0..1274d58c1 100644 --- a/roles/lib_openshift/src/doc/label +++ b/roles/lib_openshift/src/doc/label @@ -4,7 +4,7 @@ DOCUMENTATION = ''' --- module: oc_label -short_description: Create, modify, and idempotently manage openshift object labels. +short_description: Create, modify, and idempotently manage openshift labels. description: - Modify openshift labels programmatically. options: @@ -30,21 +30,22 @@ options: kind: description: - The kind of object that can be managed. - required: false + required: True default: None choices: - node - pod + - namespace aliases: [] labels: description: - - The labels to add to the resource. + - A list of labels to for the resource. required: false default: None aliases: [] selector: description: - - The selector to apply to the query + - The selector to apply to the resource query required: false default: None aliases: [] -- cgit v1.2.3 From 94fd71fa8fba70205519e18625975b83373c1535 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 31 Jan 2017 11:09:57 -0500 Subject: Adding unit test. Fixed redudant calls to get. --- roles/lib_openshift/library/oc_label.py | 60 ++++---- roles/lib_openshift/src/class/oc_label.py | 48 +++---- roles/lib_openshift/src/doc/label | 4 +- roles/lib_openshift/src/test/unit/oc_label.py | 188 ++++++++++++++++++++++++++ 4 files changed, 245 insertions(+), 55 deletions(-) create mode 100755 roles/lib_openshift/src/test/unit/oc_label.py (limited to 'roles/lib_openshift/src/doc/label') diff --git a/roles/lib_openshift/library/oc_label.py b/roles/lib_openshift/library/oc_label.py index a876346f4..8840bfefa 100644 --- a/roles/lib_openshift/library/oc_label.py +++ b/roles/lib_openshift/library/oc_label.py @@ -56,7 +56,6 @@ options: state: description: - State represents whether to create, modify, delete, or list - required: true default: present choices: ["present", "absent", "list", "add"] aliases: [] @@ -75,8 +74,7 @@ options: kind: description: - The kind of object that can be managed. - required: True - default: None + default: node choices: - node - pod @@ -888,11 +886,13 @@ class OpenShiftCLI(object): def _run(self, cmds, input_data): ''' Actually executes the command. This makes mocking easier. ''' + curr_env = os.environ.copy() + curr_env.update({'KUBECONFIG': self.kubeconfig}) proc = subprocess.Popen(cmds, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - env={'KUBECONFIG': self.kubeconfig}) + env=curr_env) stdout, stderr = proc.communicate(input_data) @@ -903,9 +903,9 @@ class OpenShiftCLI(object): '''Base command for oc ''' cmds = [] if oadm: - cmds = ['/usr/bin/oadm'] + cmds = ['oadm'] else: - cmds = ['/usr/bin/oc'] + cmds = ['oc'] if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -1212,6 +1212,7 @@ class OpenShiftCLIConfig(object): # -*- -*- -*- Begin included fragment: class/oc_label.py -*- -*- -*- + # pylint: disable=too-many-instance-attributes class OCLabel(OpenShiftCLI): ''' Class to wrap the oc command line tools ''' @@ -1233,12 +1234,22 @@ class OCLabel(OpenShiftCLI): self.kind = kind self.kubeconfig = kubeconfig self.labels = labels + self._curr_labels = None self.selector = selector - def get_current_labels(self): - ''' get the current labels on object ''' + @property + def current_labels(self): + '''property for the current labels''' + if self._curr_labels is None: + results = self.get() + self._curr_labels = results['labels'] - return self.get()['results']['labels'] + return self._curr_labels + + @current_labels.setter + def current_labels(self, data): + '''property setter for current labels''' + self._curr_labels = data def compare_labels(self, host_labels): ''' compare incoming labels against current labels''' @@ -1252,9 +1263,7 @@ class OCLabel(OpenShiftCLI): def all_user_labels_exist(self): ''' return whether all the labels already exist ''' - current_labels = self.get_current_labels() - - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: rbool = self.compare_labels(current_host_labels) if rbool == False: return False @@ -1262,9 +1271,8 @@ class OCLabel(OpenShiftCLI): def any_label_exists(self): ''' return whether any single label already exists ''' - current_labels = self.get_current_labels() - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: for label in self.labels: if label['key'] in current_host_labels: return True @@ -1283,8 +1291,7 @@ class OCLabel(OpenShiftCLI): ''' collect all the current label keys ''' current_label_keys = [] - current_labels = self.get_current_labels() - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: for key in current_host_labels.keys(): current_label_keys.append(key) @@ -1294,7 +1301,6 @@ class OCLabel(OpenShiftCLI): ''' return list of labels that are currently stored, but aren't in user-provided list ''' - current_labels = self.get_current_labels() extra_labels = [] user_label_keys = self.get_user_keys() current_label_keys = self.get_current_label_keys() @@ -1311,7 +1317,7 @@ class OCLabel(OpenShiftCLI): extra_labels = self.get_extra_current_labels() if len(extra_labels) > 0: - return True + return True else: return False @@ -1357,8 +1363,9 @@ class OCLabel(OpenShiftCLI): else: label_list.append({}) - result_dict['labels'] = label_list - result_dict['item_count'] = len(label_list) + self.current_labels = label_list + result_dict['labels'] = self.current_labels + result_dict['item_count'] = len(self.current_labels) result['results'] = result_dict return result @@ -1366,17 +1373,12 @@ class OCLabel(OpenShiftCLI): def cmd_template(self): ''' boilerplate oc command for modifying lables on this object ''' # let's build the cmd with what we have passed in - cmd = [] - if self.namespace: - cmd = cmd + ["-n", self.namespace] + cmd = ["label", self.kind] if self.selector: - cmd = cmd + ["--selector", self.selector] - - cmd = cmd + ["--config", self.kubeconfig, "label", self.kind] - - if self.name: - cmd = cmd + [self.name] + cmd.extend(["--selector", self.selector]) + elif self.name: + cmd.extend([self.name]) return cmd diff --git a/roles/lib_openshift/src/class/oc_label.py b/roles/lib_openshift/src/class/oc_label.py index 9c65b1760..4fc2ffc13 100644 --- a/roles/lib_openshift/src/class/oc_label.py +++ b/roles/lib_openshift/src/class/oc_label.py @@ -1,6 +1,7 @@ # pylint: skip-file # flake8: noqa + # pylint: disable=too-many-instance-attributes class OCLabel(OpenShiftCLI): ''' Class to wrap the oc command line tools ''' @@ -22,12 +23,22 @@ class OCLabel(OpenShiftCLI): self.kind = kind self.kubeconfig = kubeconfig self.labels = labels + self._curr_labels = None self.selector = selector - def get_current_labels(self): - ''' get the current labels on object ''' + @property + def current_labels(self): + '''property for the current labels''' + if self._curr_labels is None: + results = self.get() + self._curr_labels = results['labels'] + + return self._curr_labels - return self.get()['results']['labels'] + @current_labels.setter + def current_labels(self, data): + '''property setter for current labels''' + self._curr_labels = data def compare_labels(self, host_labels): ''' compare incoming labels against current labels''' @@ -41,9 +52,7 @@ class OCLabel(OpenShiftCLI): def all_user_labels_exist(self): ''' return whether all the labels already exist ''' - current_labels = self.get_current_labels() - - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: rbool = self.compare_labels(current_host_labels) if rbool == False: return False @@ -51,9 +60,8 @@ class OCLabel(OpenShiftCLI): def any_label_exists(self): ''' return whether any single label already exists ''' - current_labels = self.get_current_labels() - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: for label in self.labels: if label['key'] in current_host_labels: return True @@ -72,8 +80,7 @@ class OCLabel(OpenShiftCLI): ''' collect all the current label keys ''' current_label_keys = [] - current_labels = self.get_current_labels() - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: for key in current_host_labels.keys(): current_label_keys.append(key) @@ -83,7 +90,6 @@ class OCLabel(OpenShiftCLI): ''' return list of labels that are currently stored, but aren't in user-provided list ''' - current_labels = self.get_current_labels() extra_labels = [] user_label_keys = self.get_user_keys() current_label_keys = self.get_current_label_keys() @@ -100,7 +106,7 @@ class OCLabel(OpenShiftCLI): extra_labels = self.get_extra_current_labels() if len(extra_labels) > 0: - return True + return True else: return False @@ -146,8 +152,9 @@ class OCLabel(OpenShiftCLI): else: label_list.append({}) - result_dict['labels'] = label_list - result_dict['item_count'] = len(label_list) + self.current_labels = label_list + result_dict['labels'] = self.current_labels + result_dict['item_count'] = len(self.current_labels) result['results'] = result_dict return result @@ -155,17 +162,12 @@ class OCLabel(OpenShiftCLI): def cmd_template(self): ''' boilerplate oc command for modifying lables on this object ''' # let's build the cmd with what we have passed in - cmd = [] - if self.namespace: - cmd = cmd + ["-n", self.namespace] + cmd = ["label", self.kind] if self.selector: - cmd = cmd + ["--selector", self.selector] - - cmd = cmd + ["--config", self.kubeconfig, "label", self.kind] - - if self.name: - cmd = cmd + [self.name] + cmd.extend(["--selector", self.selector]) + elif self.name: + cmd.extend([self.name]) return cmd diff --git a/roles/lib_openshift/src/doc/label b/roles/lib_openshift/src/doc/label index 1274d58c1..e206d0d06 100644 --- a/roles/lib_openshift/src/doc/label +++ b/roles/lib_openshift/src/doc/label @@ -11,7 +11,6 @@ options: state: description: - State represents whether to create, modify, delete, or list - required: true default: present choices: ["present", "absent", "list", "add"] aliases: [] @@ -30,8 +29,7 @@ options: kind: description: - The kind of object that can be managed. - required: True - default: None + default: node choices: - node - pod diff --git a/roles/lib_openshift/src/test/unit/oc_label.py b/roles/lib_openshift/src/test/unit/oc_label.py new file mode 100755 index 000000000..875b97c42 --- /dev/null +++ b/roles/lib_openshift/src/test/unit/oc_label.py @@ -0,0 +1,188 @@ +#!/usr/bin/env python2 +''' + Unit tests for oc label +''' +# To run +# python -m unittest version +# +# . +# Ran 1 test in 0.597s +# +# OK + +import os +import sys +import unittest +import mock + +# Removing invalid variable names for tests so that I can +# keep them brief +# pylint: disable=invalid-name,no-name-in-module +# Disable import-error b/c our libraries aren't loaded in jenkins +# pylint: disable=import-error +# place class in our python path +module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501 +sys.path.insert(0, module_path) +from oc_label import OCLabel # noqa: E402 + + +class OCLabelTest(unittest.TestCase): + ''' + Test class for OCLabel + ''' + + def setUp(self): + ''' setup method will create a file and set to known configuration ''' + pass + + @mock.patch('oc_label.OCLabel._run') + def test_state_list(self, mock_cmd): + ''' Testing a label list ''' + params = {'name': 'default', + 'namespace': 'default', + 'labels': None, + 'state': 'list', + 'kind': 'namespace', + 'selector': None, + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False} + + ns = '''{ + "kind": "Namespace", + "apiVersion": "v1", + "metadata": { + "name": "default", + "selfLink": "/api/v1/namespaces/default", + "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22", + "resourceVersion": "403024", + "creationTimestamp": "2017-01-26T14:28:55Z", + "labels": { + "storage_pv_quota": "False" + }, + "annotations": { + "openshift.io/node-selector": "", + "openshift.io/sa.initialized-roles": "true", + "openshift.io/sa.scc.mcs": "s0:c1,c0", + "openshift.io/sa.scc.supplemental-groups": "1000000000/10000", + "openshift.io/sa.scc.uid-range": "1000000000/10000" + } + }, + "spec": { + "finalizers": [ + "kubernetes", + "openshift.io/origin" + ] + }, + "status": { + "phase": "Active" + } + }''' + + + mock_cmd.side_effect = [ + (0, ns, ''), + ] + + results = OCLabel.run_ansible(params, False) + + self.assertFalse(results['changed']) + self.assertTrue(results['results']['labels'] == [{'storage_pv_quota': 'False'}]) + + @mock.patch('oc_label.OCLabel._run') + def test_state_present(self, mock_cmd): + ''' Testing a label list ''' + params = {'name': 'default', + 'namespace': 'default', + 'labels': [ + {'key': 'awesomens', 'value': 'testinglabel'}, + {'key': 'storage_pv_quota', 'value': 'False'} + ], + 'state': 'present', + 'kind': 'namespace', + 'selector': None, + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False} + + ns = '''{ + "kind": "Namespace", + "apiVersion": "v1", + "metadata": { + "name": "default", + "selfLink": "/api/v1/namespaces/default", + "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22", + "resourceVersion": "403024", + "creationTimestamp": "2017-01-26T14:28:55Z", + "labels": { + "storage_pv_quota": "False" + }, + "annotations": { + "openshift.io/node-selector": "", + "openshift.io/sa.initialized-roles": "true", + "openshift.io/sa.scc.mcs": "s0:c1,c0", + "openshift.io/sa.scc.supplemental-groups": "1000000000/10000", + "openshift.io/sa.scc.uid-range": "1000000000/10000" + } + }, + "spec": { + "finalizers": [ + "kubernetes", + "openshift.io/origin" + ] + }, + "status": { + "phase": "Active" + } + }''' + + ns1 = '''{ + "kind": "Namespace", + "apiVersion": "v1", + "metadata": { + "name": "default", + "selfLink": "/api/v1/namespaces/default", + "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22", + "resourceVersion": "403024", + "creationTimestamp": "2017-01-26T14:28:55Z", + "labels": { + "storage_pv_quota": "False", + "awesomens": "testinglabel" + }, + "annotations": { + "openshift.io/node-selector": "", + "openshift.io/sa.initialized-roles": "true", + "openshift.io/sa.scc.mcs": "s0:c1,c0", + "openshift.io/sa.scc.supplemental-groups": "1000000000/10000", + "openshift.io/sa.scc.uid-range": "1000000000/10000" + } + }, + "spec": { + "finalizers": [ + "kubernetes", + "openshift.io/origin" + ] + }, + "status": { + "phase": "Active" + } + }''' + + + mock_cmd.side_effect = [ + (0, ns, ''), + (0, '', ''), + (0, ns1, ''), + ] + + results = OCLabel.run_ansible(params, False) + + self.assertTrue(results['changed']) + self.assertTrue(results['results']['results']['labels'][0] == \ + {'storage_pv_quota': 'False', 'awesomens': 'testinglabel'}) + + def tearDown(self): + '''TearDown method''' + pass + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3 From 4b054d7da5f404dfd8eb238b617e4dcf7dc93b17 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 2 Feb 2017 22:21:23 -0500 Subject: Doc enhancements. --- roles/lib_openshift/library/oc_label.py | 36 +++++++++++++++++++--- roles/lib_openshift/src/ansible/oc_label.py | 2 +- roles/lib_openshift/src/doc/label | 34 ++++++++++++++++++-- .../src/test/integration/filter_plugins/filters.py | 7 +++-- 4 files changed, 69 insertions(+), 10 deletions(-) (limited to 'roles/lib_openshift/src/doc/label') diff --git a/roles/lib_openshift/library/oc_label.py b/roles/lib_openshift/library/oc_label.py index 2096f0a1d..9481d2a47 100644 --- a/roles/lib_openshift/library/oc_label.py +++ b/roles/lib_openshift/library/oc_label.py @@ -55,7 +55,11 @@ description: options: state: description: - - State represents whether to create, modify, delete, or list + - State controls the action that will be taken with resource + - 'present' will create or update and object to the desired state + - 'absent' will ensure certain labels are removed + - 'list' will read the labels + - 'add' will insert labels to the already existing labels default: present choices: ["present", "absent", "list", "add"] aliases: [] @@ -82,7 +86,9 @@ options: aliases: [] labels: description: - - A list of labels to for the resource. + - A list of labels for the resource. + - Each list consists of a key and a value. + - eg, {'key': 'foo', 'value': 'bar'} required: false default: None aliases: [] @@ -98,7 +104,7 @@ extends_documentation_fragment: [] ''' EXAMPLES = ''' -- name: Add label to node +- name: Add a single label to a node's existing labels oc_label: name: ip-172-31-5-23.ec2.internal state: add @@ -106,6 +112,28 @@ EXAMPLES = ''' labels: - key: logging-infra-fluentd value: 'true' + +- name: remove a label from a node + oc_label: + name: ip-172-31-5-23.ec2.internal + state: absent + kind: node + labels: + - key: color + value: blue + +- name: Ensure node has these exact labels + oc_label: + name: ip-172-31-5-23.ec2.internal + state: present + kind: node + labels: + - key: color + value: green + - key: type + value: master + - key: environment + value: production ''' # -*- -*- -*- End included fragment: doc/label -*- -*- -*- @@ -1517,7 +1545,7 @@ def main(): state=dict(default='present', type='str', choices=['present', 'absent', 'list', 'add']), debug=dict(default=False, type='bool'), - kind=dict(default=None, type='str', required=True, + kind=dict(default='node', type='str', required=True, choices=['node', 'pod', 'namespace']), name=dict(default=None, type='str'), namespace=dict(default=None, type='str'), diff --git a/roles/lib_openshift/src/ansible/oc_label.py b/roles/lib_openshift/src/ansible/oc_label.py index 685712963..28f004621 100644 --- a/roles/lib_openshift/src/ansible/oc_label.py +++ b/roles/lib_openshift/src/ansible/oc_label.py @@ -10,7 +10,7 @@ def main(): state=dict(default='present', type='str', choices=['present', 'absent', 'list', 'add']), debug=dict(default=False, type='bool'), - kind=dict(default=None, type='str', required=True, + kind=dict(default='node', type='str', required=True, choices=['node', 'pod', 'namespace']), name=dict(default=None, type='str'), namespace=dict(default=None, type='str'), diff --git a/roles/lib_openshift/src/doc/label b/roles/lib_openshift/src/doc/label index e206d0d06..fb3ed2503 100644 --- a/roles/lib_openshift/src/doc/label +++ b/roles/lib_openshift/src/doc/label @@ -10,7 +10,11 @@ description: options: state: description: - - State represents whether to create, modify, delete, or list + - State controls the action that will be taken with resource + - 'present' will create or update and object to the desired state + - 'absent' will ensure certain labels are removed + - 'list' will read the labels + - 'add' will insert labels to the already existing labels default: present choices: ["present", "absent", "list", "add"] aliases: [] @@ -37,7 +41,9 @@ options: aliases: [] labels: description: - - A list of labels to for the resource. + - A list of labels for the resource. + - Each list consists of a key and a value. + - eg, {'key': 'foo', 'value': 'bar'} required: false default: None aliases: [] @@ -53,7 +59,7 @@ extends_documentation_fragment: [] ''' EXAMPLES = ''' -- name: Add label to node +- name: Add a single label to a node's existing labels oc_label: name: ip-172-31-5-23.ec2.internal state: add @@ -61,4 +67,26 @@ EXAMPLES = ''' labels: - key: logging-infra-fluentd value: 'true' + +- name: remove a label from a node + oc_label: + name: ip-172-31-5-23.ec2.internal + state: absent + kind: node + labels: + - key: color + value: blue + +- name: Ensure node has these exact labels + oc_label: + name: ip-172-31-5-23.ec2.internal + state: present + kind: node + labels: + - key: color + value: green + - key: type + value: master + - key: environment + value: production ''' diff --git a/roles/lib_openshift/src/test/integration/filter_plugins/filters.py b/roles/lib_openshift/src/test/integration/filter_plugins/filters.py index 71a29ab7d..6990a11a8 100644 --- a/roles/lib_openshift/src/test/integration/filter_plugins/filters.py +++ b/roles/lib_openshift/src/test/integration/filter_plugins/filters.py @@ -7,11 +7,14 @@ Custom filters for use in testing class FilterModule(object): - ''' Custom ansible filters ''' + ''' Custom filters for use in integration testing ''' @staticmethod def label_dict_to_key_value_list(label_dict): - ''' given a dict of labels/values, return list of key: value: pairs''' + ''' Given a dict of labels/values, return list of key: value: pairs + + These are only used in integration testing. + ''' label_list = [] for key in label_dict: -- cgit v1.2.3