From c791f9c6990ed8a968601fe750414df1affad1d8 Mon Sep 17 00:00:00 2001 From: Laurent Rochette Date: Tue, 6 Feb 2024 13:42:47 -0700 Subject: [PATCH] Change API call for app status (#673) * [1.4.2] change API call to improve performance and decrease lag * change some info message into debug * Change logging default level to error to match step * Update default version for image --- incubating/argo-cd-sync/CHANGELOG.md | 4 ++ incubating/argo-cd-sync/argocd_sync.py | 44 +++++++++---------- .../queries/get_app_status.graphql | 25 +++++------ .../queries/get_app_status.orig.graphql | 18 ++++++++ incubating/argo-cd-sync/step.yaml | 4 +- 5 files changed, 57 insertions(+), 38 deletions(-) create mode 100644 incubating/argo-cd-sync/queries/get_app_status.orig.graphql diff --git a/incubating/argo-cd-sync/CHANGELOG.md b/incubating/argo-cd-sync/CHANGELOG.md index 36c40d8d6..58f430cde 100644 --- a/incubating/argo-cd-sync/CHANGELOG.md +++ b/incubating/argo-cd-sync/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [1.4.2] - 2024-01-17 +### Changed +New graphql call to speed up query + ## [1.4.1] - 2023-10-31 ### Changed Add CA_BUNDLE option diff --git a/incubating/argo-cd-sync/argocd_sync.py b/incubating/argo-cd-sync/argocd_sync.py index 8ad448bf9..3deb5a3b9 100644 --- a/incubating/argo-cd-sync/argocd_sync.py +++ b/incubating/argo-cd-sync/argocd_sync.py @@ -22,7 +22,7 @@ CF_URL = os.getenv('CF_URL', 'https://g.codefresh.io') CF_API_KEY = os.getenv('CF_API_KEY') CF_STEP_NAME= os.getenv('CF_STEP_NAME', 'STEP_NAME') -LOG_LEVEL = os.getenv('LOG_LEVEL', "info") +LOG_LEVEL = os.getenv('LOG_LEVEL', "error") # Check the certificate or not accessing the API endpoint VERIFY = True if os.getenv('INSECURE', "False").lower() == "false" else False @@ -50,24 +50,24 @@ def main(): ingress_host = get_runtime_ingress_host() execute_argocd_sync(ingress_host) namespace=get_runtime_ns() - status = get_app_status(namespace) + status = get_app_status(ingress_host) if WAIT_HEALTHY: - status=waitHealthy (namespace) + status=waitHealthy (ingress_host) # if Wait failed, it's time for rollback if status != "HEALTHY" and ROLLBACK: logging.info("Application '%s' did not sync properly. Initiating rollback ", APPLICATION) revision = getRevision(namespace) - logging.info("latest healthy revision is %d", revision) + logging.info("Latest healthy revision is %d", revision) rollback(ingress_host, namespace, revision) logging.info("Waiting for rollback to happen") if WAIT_ROLLBACK: - status=waitHealthy (namespace) + status=waitHealthy (ingress_host) else: time.sleep(INTERVAL) - status=get_app_status(namespace) + status=get_app_status(ingress_host) else: export_variable('ROLLBACK_EXECUTED', "false") else: @@ -108,7 +108,7 @@ def getRevision(namespace): } } result = client.execute(query, variable_values=variables) - logging.info(result) + logging.debug("getRevision result: %s", result) loop=0 revision = -1 @@ -124,18 +124,18 @@ def getRevision(namespace): loop += 1 # we did not find a HEALTHY one in our page export_variable('ROLLBACK_EXECUTED', "false") - logging.error("Did not find a HEALTHY release among the lat %d", PAGE_SIZE) + logging.error("Did not find a HEALTHY release among the last %d", PAGE_SIZE) sys.exit(1) -def waitHealthy (namespace): - logging.debug ("Entering waitHealthy (ns: %s)", namespace) +def waitHealthy (ingress_host): + logging.debug ("Entering waitHealthy (ns: %s)", ingress_host) time.sleep(INTERVAL) - status = get_app_status(namespace) + status = get_app_status(ingress_host) logging.info("App status is %s", status) loop=0 while status != "HEALTHY" and loop < MAX_CHECKS: - status=get_app_status(namespace) + status=get_app_status(ingress_host) time.sleep(INTERVAL) logging.info("App status is %s after %d checks", status, loop) loop += 1 @@ -160,15 +160,15 @@ def rollback(ingress_host, namespace, revision): "dryRun": False, "prune": True } - logging.info("Rollback app: %s", variables) + logging.debug("Rollback variables: %s", variables) result = client.execute(query, variable_values=variables) - logging.info(result) + logging.debug("Rollback result: %s", result) export_variable('ROLLBACK_EXECUTED', "true") -def get_app_status(namespace): +def get_app_status(ingress_host): ## Get the health status of the app - gql_api_endpoint = CF_URL + '/2.0/api/graphql' + gql_api_endpoint = ingress_host + '/app-proxy/api/graphql' transport = RequestsHTTPTransport( url=gql_api_endpoint, headers={'authorization': CF_API_KEY}, @@ -178,13 +178,12 @@ def get_app_status(namespace): client = Client(transport=transport, fetch_schema_from_transport=False) query = get_query('get_app_status') ## gets gql query variables = { - "runtime": RUNTIME, - "name": APPLICATION, - "namespace": namespace + "name": APPLICATION } result = client.execute(query, variable_values=variables) - health = result['application']['healthStatus'] + logging.debug("App Status result: %s", result) + health = result['applicationProxyQuery']['status']['health']['status'] return health def get_query(query_name): @@ -245,9 +244,8 @@ def execute_argocd_sync(ingress_host): "prune": True } } - logging.info("Syncing app: %s", variables) result = client.execute(query, variable_values=variables) - logging.info(result) + logging.debug("Syncing App result: %s", result) def export_variable(var_name, var_value): @@ -260,7 +258,7 @@ def export_variable(var_name, var_value): with open('/meta/env_vars_to_export', 'a') as a_writer: a_writer.write(var_name + "=" + var_value+'\n') - logging.info("Exporting variable: %s=%s", var_name, var_value) + logging.debug("Exporting variable: %s=%s", var_name, var_value) ############################################################## diff --git a/incubating/argo-cd-sync/queries/get_app_status.graphql b/incubating/argo-cd-sync/queries/get_app_status.graphql index 796f42dd4..574b0d645 100644 --- a/incubating/argo-cd-sync/queries/get_app_status.graphql +++ b/incubating/argo-cd-sync/queries/get_app_status.graphql @@ -1,18 +1,17 @@ -query ApplicationsStatusesQuery( - $runtime: String! - $name: String! - $namespace: String -) { - application(runtime: $runtime, name: $name, namespace: $namespace) { +query appstatus ($name: String!) { + applicationProxyQuery( + name: $name + ){ metadata { - runtime name - namespace - cluster - __typename } - healthStatus - syncStatus - syncPolicy + status { + health { + status + } + sync { + status + } + } } } diff --git a/incubating/argo-cd-sync/queries/get_app_status.orig.graphql b/incubating/argo-cd-sync/queries/get_app_status.orig.graphql new file mode 100644 index 000000000..796f42dd4 --- /dev/null +++ b/incubating/argo-cd-sync/queries/get_app_status.orig.graphql @@ -0,0 +1,18 @@ +query ApplicationsStatusesQuery( + $runtime: String! + $name: String! + $namespace: String +) { + application(runtime: $runtime, name: $name, namespace: $namespace) { + metadata { + runtime + name + namespace + cluster + __typename + } + healthStatus + syncStatus + syncPolicy + } +} diff --git a/incubating/argo-cd-sync/step.yaml b/incubating/argo-cd-sync/step.yaml index 702452bf6..8874a8e20 100644 --- a/incubating/argo-cd-sync/step.yaml +++ b/incubating/argo-cd-sync/step.yaml @@ -1,7 +1,7 @@ kind: step-type metadata: name: argo-cd-sync - version: 1.4.1 + version: 1.4.2 isPublic: true description: Syncs Argo CD apps managed by our GitOps Runtimes sources: @@ -120,7 +120,7 @@ spec: }, "IMAGE_TAG": { "type": "string", - "default": "1.3.1", + "default": "1.4.2", "description": "OPTIONAL - To overwrite the tag to use" } }