From 652be27fc58deee5a8d4a9c0b5c94e34c30ec321 Mon Sep 17 00:00:00 2001 From: Michael Haigh Date: Fri, 24 May 2024 15:37:18 -0400 Subject: [PATCH] adding local file, and label support to "--v3 manage cluster" Signed-off-by: Michael Haigh --- astraSDK/k8s.py | 3 +++ tkSrc/create.py | 8 ++++---- tkSrc/helpers.py | 16 +++++++++------- tkSrc/manage.py | 5 ++++- tkSrc/parser.py | 36 +++++++++++++++++++++++++++--------- 5 files changed, 47 insertions(+), 21 deletions(-) diff --git a/astraSDK/k8s.py b/astraSDK/k8s.py index cf7e86c..6969e18 100644 --- a/astraSDK/k8s.py +++ b/astraSDK/k8s.py @@ -1016,6 +1016,7 @@ def main( apiToken, regCred, registry=None, + label=None, # should be of 'acs.example.com/policy=allowed' format name="astra-connector", namespace="astra-connector", ): @@ -1039,6 +1040,8 @@ def main( }, }, } + if label: + body["spec"]["labels"] = {label.split("=")[0]: label.split("=")[1]} return createResource( quiet=self.quiet, dry_run=self.dry_run, diff --git a/tkSrc/create.py b/tkSrc/create.py index 64ca516..c149f0a 100644 --- a/tkSrc/create.py +++ b/tkSrc/create.py @@ -143,8 +143,8 @@ def createV3ConnectorOperator(v3, dry_run, skip_tls_verify, verbose, operator_ve context, config_file = tuple(v3.split("@")) helpers.run( f"kubectl --insecure-skip-tls-verify={skip_tls_verify} --context={context} " - f"-v={6 if verbose else 0} apply --dry_run={dry_run if dry_run else 'none'} -f " - f"{helpers.getOperatorURL(operator_version)}", + f"-v={6 if verbose else 0} apply --dry_run={dry_run if dry_run else 'none'} " + f"-f {operator_version}", env={"KUBECONFIG": os.path.expanduser(config_file)} if config_file != "None" else None, ) @@ -314,7 +314,7 @@ def createV3Hook( name=helpers.isRFC1123(name), action=operation.split("-")[1], appName=app, - arguments=helpers.prependDump(helpers.createHookList(hookArguments), prepend=4), + arguments=helpers.prependDump(helpers.createNargsList(hookArguments), prepend=4), hookSource=encodedStr, matchingCriteria=helpers.prependDump( helpers.createCriteriaList( @@ -579,7 +579,7 @@ def main(args, ard, config=None): args.script, args.operation.split("-")[0], args.operation.split("-")[1], - helpers.createHookList(args.hookArguments), + helpers.createNargsList(args.hookArguments), matchingCriteria=helpers.createCriteriaList( args.containerImage, args.namespace, diff --git a/tkSrc/helpers.py b/tkSrc/helpers.py index d2eb3aa..ef45651 100644 --- a/tkSrc/helpers.py +++ b/tkSrc/helpers.py @@ -42,20 +42,22 @@ def createHelmStr(flagName, values): return returnStr -def createHookList(hookArguments): - """Create a list of strings to be used for --hookArguments, as nargs="*" can provide - a variety of different types of lists of lists depending on how the user uses it. - User Input argParse Value createHookList Return - ---------- -------------- --------------------- +def createNargsList(arguments): + """Create a list of strings to be used for --hookArguments and --labels, as nargs="*" can + provide a variety of different types of lists of lists depending on how the user uses it. + User Input argParse Value createNargsList Return + ---------- -------------- ---------------------- -a arg1 [['arg1']] ['arg1'] -a arg1 arg2 [['arg1', 'arg2']] ['arg1', 'arg2'] -a arg1 -a arg2 [['arg1'], ['arg2']] ['arg1', 'arg2'] -a "arg1 s_arg" arg2 [['arg1 s_arg', 'arg2']] ['arg1 s_arg', 'arg2'] -a "arg1 s_arg" arg2 -a arg3 [['arg1 s_arg', 'arg2'], ['arg3']] ['arg1 s_arg', 'arg2', 'arg3'] """ + if arguments is None: + return None returnList = [] - if hookArguments: - for arg in hookArguments: + if arguments: + for arg in arguments: if isinstance(arg, list): for a in arg: returnList.append(a) diff --git a/tkSrc/manage.py b/tkSrc/manage.py index 671f0a2..066ba2f 100644 --- a/tkSrc/manage.py +++ b/tkSrc/manage.py @@ -154,6 +154,7 @@ def manageV3Cluster( cloudID, headless, ard, + label=None, config=None, ): helpers.isRFC1123(clusterName) @@ -216,6 +217,7 @@ def manageV3Cluster( apiToken["metadata"]["name"], regCred, registry=registry, + label=label, ) if not connector: raise SystemExit("astraSDK.k8s.createAstraConnector() failed") @@ -438,12 +440,13 @@ def main(args, ard, config=None): args.quiet, args.verbose, args.clusterName, - args.operator_version, + args.filename if args.filename else helpers.getOperatorURL(args.operator_version), args.regCred, args.registry, args.cloudID, args.headless, ard, + label=args.label, config=config, ) else: diff --git a/tkSrc/parser.py b/tkSrc/parser.py index 5ef08b3..4b0384b 100644 --- a/tkSrc/parser.py +++ b/tkSrc/parser.py @@ -1183,7 +1183,7 @@ def create_asup_args(self): "--dataWindowEnd", default=None, help=f"specify an ISO-8601 timestamp, like: {now.isoformat(timespec='seconds')}" - " (defaults to current time of request)" + " (defaults to current time of request)", ) quickTime = self.subparserCreateAsup.add_argument_group( "Quick Time Frame", @@ -1842,14 +1842,6 @@ def manage_cluster_args(self): required=(False if len(self.acl.clouds) == 1 else True), help="The cloudID to add the cluster to (only required if # of clouds > 1)", ) - self.subparserManageCluster.add_argument( - "-v", - "--operator-version", - required=False, - default="24.02.0-202403151353", - help="Optionally specify the astra-connector-operator version " - "(default: %(default)s)", - ) self.subparserManageCluster.add_argument( "--regCred", choices=(None if self.plaidMode else self.acl.credentials), @@ -1863,9 +1855,35 @@ def manage_cluster_args(self): help="optionally specify the FQDN of the ACP image source registry " "(defaults to cr.)", ) + self.subparserManageCluster.add_argument( + "-l", + "--label", + default=None, + help="optionally specify a label to be added to the AstraConnector CR, for " + "example: 'acs.example.com/policy=allowed'", + ) self.subparserManageCluster.add_argument( "--headless", action="store_true", default=False, help=argparse.SUPPRESS ) + versionGroup = self.subparserManageCluster.add_argument_group( + "operatorGroup", "Astra Connector Operator Version to install" + ) + verMEGroup = versionGroup.add_mutually_exclusive_group() + verMEGroup.add_argument( + "-v", + "--operator-version", + required=False, + default="24.02.0-202403151353", + help="Optionally specify the astra-connector-operator version to install from " + "GitHub (default: %(default)s)", + ) + verMEGroup.add_argument( + "-f", + "--filename", + required=False, + default=None, + help="Optionally specify the local astra-connector-operator file to install", + ) else: self.subparserManageCluster.add_argument( "cluster",