From 18b32023c5eef98ab738c2828ce3d8d64f55af60 Mon Sep 17 00:00:00 2001 From: Arun Kumar Mohan Date: Thu, 30 Jul 2020 10:59:37 +0530 Subject: [PATCH 1/2] ceph: allowing ceph filesystem argument to be optional If we are unable to get or infer a default value for ceph filesystem script won't be throwing an error. Signed-off-by: Arun Kumar Mohan --- .../ceph/create-external-cluster-resources.py | 78 ++++++++++++------- 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/cluster/examples/kubernetes/ceph/create-external-cluster-resources.py b/cluster/examples/kubernetes/ceph/create-external-cluster-resources.py index 0494843ff..4c0833a53 100644 --- a/cluster/examples/kubernetes/ceph/create-external-cluster-resources.py +++ b/cluster/examples/kubernetes/ceph/create-external-cluster-resources.py @@ -251,21 +251,16 @@ class RadosJSON: def get_cephfs_data_pool_details(self): cmd_json = {"prefix": "fs ls", "format": "json"} ret_val, json_out, err_msg = self._common_cmd_json_gen(cmd_json) - # if there is an unsuccessful attempt, - if ret_val != 0 or len(json_out) == 0: - raise ExecutionFailureException( - "'fs ls' command failed.\nError: {}".format( - err_msg if ret_val != 0 else "No filesystem detected.")) - # if there are multiple filesystems present, and - # no specific filesystem name and data-pool names are provided, - # then raise an exception - if len(json_out) > 1 and not self._arg_parser.cephfs_filesystem_name: - # if the arguments are not provided, generate an error message - raise ExecutionFailureException( - "More than ONE filesystems detected.\n" + - "{}\n\n".format(json_out) + - "Please manually provide the details for " + - "'--cephfs-filesystem-name'") + # if there is an unsuccessful attempt, report an error + if ret_val != 0: + # if fs and data_pool arguments are not set, silently return + if self._arg_parser.cephfs_filesystem_name == "" and self._arg_parser.cephfs_data_pool_name == "": + return + # if user has provided any of the + # '--cephfs-filesystem-name' or '--cephfs-data-pool-name' arguments, + # raise an exception as we are unable to verify the args + raise ExecutionFailureException("'fs ls' ceph call failed with error: {}".format(err_msg)) + matching_json_out = {} # if '--cephfs-filesystem-name' argument is provided, # check whether the provided filesystem-name exists or not @@ -281,29 +276,60 @@ class RadosJSON: self._arg_parser.cephfs_filesystem_name, [str(x['name']) for x in json_out])) matching_json_out = matching_json_out_list[0] + # if cephfs filesystem name is not provided, + # try to get a default fs name by doing the following else: - self._arg_parser.cephfs_filesystem_name = str(json_out[0]['name']) - matching_json_out = json_out[0] + # a. check if there is only one filesystem is present + if len(json_out) == 1: + matching_json_out = json_out[0] + # b. or else, check if data_pool name is provided + elif self._arg_parser.cephfs_data_pool_name: + # and if present, check whether there exists a fs which has the data_pool + for eachJ in json_out: + if self._arg_parser.cephfs_data_pool_name in eachJ['data_pools']: + matching_json_out = eachJ + break + # if there is no matching fs exists, that means provided data_pool name is invalid + if not matching_json_out: + raise ExecutionFailureException( + "Provided data_pool name, {}, does not exists".format( + self._arg_parser.cephfs_data_pool_name)) + # c. if nothing is set and couldn't find a default, + else: + # just return silently + return + + if matching_json_out: + self._arg_parser.cephfs_filesystem_name = str(matching_json_out['name']) + if type(matching_json_out['data_pools']) == list: - if len(matching_json_out['data_pools']) == 0: - raise ExecutionFailureException("No 'data_pools' found.") # if the user has already provided data-pool-name, # through --cephfs-data-pool-name if self._arg_parser.cephfs_data_pool_name: # if the provided name is not matching with the one in the list if self._arg_parser.cephfs_data_pool_name not in matching_json_out['data_pools']: raise ExecutionFailureException( - ("Provided 'data-pool-name': '{}', " + - "doesn't match from the data-pools' list: {}").format( - self._arg_parser.cephfs_data_pool_name, - [str(x) for x in matching_json_out['data_pools']])) + "{}: '{}', {}: {}".format( + "Provided data-pool-name", + self._arg_parser.cephfs_data_pool_name, + "doesn't match from the data-pools' list", + [str(x) for x in matching_json_out['data_pools']])) + # if data_pool name is not provided, + # then try to find a default data pool name else: + # if no data_pools exist, silently return + if len(matching_json_out['data_pools']) == 0: + return self._arg_parser.cephfs_data_pool_name = str( matching_json_out['data_pools'][0]) + # if there are more than one 'data_pools' exist, + # then warn the user that we are using the selected name if len(matching_json_out['data_pools']) > 1: - print("WARNING: Multiple data pools detected.\n" + - "{}\n".format([str(x) for x in matching_json_out['data_pools']]) + - "Using the data-pool: {}\n".format(self._arg_parser.cephfs_data_pool_name)) + print("{}: {}\n{}: '{}'\n".format( + "WARNING: Multiple data pools detected", + [str(x) for x in matching_json_out['data_pools']], + "Using the data-pool", + self._arg_parser.cephfs_data_pool_name)) def create_cephCSIKeyring_RBDNode(self): cmd_json = {"prefix": "auth get-or-create", From cba605a9300e680b4cc745ac758b68d9d9ba78ba Mon Sep 17 00:00:00 2001 From: Arun Kumar Mohan Date: Thu, 30 Jul 2020 20:09:50 +0530 Subject: [PATCH 2/2] ceph: provide storageclass-es if there are valid parameters 'ceph-rgw' storageclass will be provided only if 'rgw_endpoint' exists 'cephfs' storageclass will be provided only if ceph-fs and/or data-pool parameters are valid. Signed-off-by: Arun Kumar Mohan --- .../ceph/create-external-cluster-resources.py | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/cluster/examples/kubernetes/ceph/create-external-cluster-resources.py b/cluster/examples/kubernetes/ceph/create-external-cluster-resources.py index 4c0833a53..dfc1ebfff 100644 --- a/cluster/examples/kubernetes/ceph/create-external-cluster-resources.py +++ b/cluster/examples/kubernetes/ceph/create-external-cluster-resources.py @@ -481,21 +481,6 @@ class RadosJSON: "pool": self.out_map['RBD_POOL_NAME'] } }, - { - "name": "cephfs", - "kind": "StorageClass", - "data": { - "fsName": self.out_map['CEPHFS_FS_NAME'], - "pool": self.out_map['CEPHFS_POOL_NAME'] - } - }, - { - "name": "ceph-rgw", - "kind": "StorageClass", - "data": { - "endpoint": self.out_map['RGW_ENDPOINT'] - } - }, { "name": "monitoring-endpoint", "kind": "CephCluster", @@ -505,6 +490,27 @@ class RadosJSON: } } ] + # if 'CEPHFS_FS_NAME' exists, then only add 'cephfs' StorageClass + if self.out_map['CEPHFS_FS_NAME']: + json_out.append( + { + "name": "cephfs", + "kind": "StorageClass", + "data": { + "fsName": self.out_map['CEPHFS_FS_NAME'], + "pool": self.out_map['CEPHFS_POOL_NAME'] + } + }) + # if 'RGW_ENDPOINT' exists, then only add 'ceph-rgw' StorageClass + if self.out_map['RGW_ENDPOINT']: + json_out.append( + { + "name": "ceph-rgw", + "kind": "StorageClass", + "data": { + "endpoint": self.out_map['RGW_ENDPOINT'] + } + }) return json.dumps(json_out)+LINESEP def main(self):