Skip to content

Commit 0e787b6

Browse files
authored
URL to app and better error catch (#686)
* [FT-185] 1.4.3 Move creation of link to app earlier in process * Exit with error when app is in OUT_OF_SYNC state * Fix variable name and misspelling * change status into health * Fix syntax errors * V1.4.3: intercepting application not found for better error message --------- Signed-off-by: Laurent Rochette <laurent.rochette@codefresh.io>
1 parent b0ce513 commit 0e787b6

File tree

4 files changed

+81
-36
lines changed

4 files changed

+81
-36
lines changed

incubating/argo-cd-sync/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
env_vars_to_export
2+
vars.sh

incubating/argo-cd-sync/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## [1.4.3] - 2024-02-22
4+
### Fixed
5+
intercepting application not found for better error message
6+
7+
### Changed
8+
Move the creation of the link to the application earlier
9+
Exit with error when app is in OUT_OF_SYNC state
10+
311
## [1.4.2] - 2024-01-17
412
### Changed
513
New graphql call to speed up query

incubating/argo-cd-sync/argocd_sync.py

Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from gql import Client, gql
22
from gql.transport.requests import RequestsHTTPTransport
3+
from gql.transport.exceptions import TransportQueryError
34
import os
45
import logging
56
import time
@@ -12,17 +13,17 @@
1213

1314
# Wait and Rollback options
1415
WAIT_HEALTHY = True if os.getenv('WAIT_HEALTHY', "false").lower() == "true" else False
15-
INTERVAL = int(os.getenv('INTERVAL'))
16-
MAX_CHECKS = int(os.getenv('MAX_CHECKS'))
16+
INTERVAL = int(os.getenv('INTERVAL'))
17+
MAX_CHECKS = int(os.getenv('MAX_CHECKS'))
1718

1819
WAIT_ROLLBACK = True if os.getenv('WAIT_ROLLBACK', "false").lower() == "true" else False
19-
ROLLBACK = True if os.getenv('ROLLBACK', "false").lower() == "true" else False
20+
ROLLBACK = True if os.getenv('ROLLBACK', "false").lower() == "true" else False
2021
if WAIT_ROLLBACK: ROLLBACK = True
2122

22-
CF_URL = os.getenv('CF_URL', 'https://g.codefresh.io')
23-
CF_API_KEY = os.getenv('CF_API_KEY')
24-
CF_STEP_NAME= os.getenv('CF_STEP_NAME', 'STEP_NAME')
25-
LOG_LEVEL = os.getenv('LOG_LEVEL', "error")
23+
CF_URL = os.getenv('CF_URL', 'https://g.codefresh.io')
24+
CF_API_KEY = os.getenv('CF_API_KEY')
25+
CF_STEP_NAME = os.getenv('CF_STEP_NAME', 'STEP_NAME')
26+
LOG_LEVEL = os.getenv('LOG_LEVEL', "error")
2627

2728
# Check the certificate or not accessing the API endpoint
2829
VERIFY = True if os.getenv('INSECURE', "False").lower() == "false" else False
@@ -47,41 +48,51 @@ def main():
4748
logging.debug("VERIFY: %s", VERIFY)
4849
logging.debug("BUNDLE: %s", CA_BUNDLE)
4950

51+
## Generating link to the Apps Dashboard
52+
CF_OUTPUT_URL_VAR = CF_STEP_NAME + '_CF_OUTPUT_URL'
53+
link_to_app = get_link_to_apps_dashboard()
54+
export_variable(CF_OUTPUT_URL_VAR, link_to_app)
55+
5056
ingress_host = get_runtime_ingress_host()
5157
execute_argocd_sync(ingress_host)
52-
namespace=get_runtime_ns()
53-
status = get_app_status(ingress_host)
58+
namespace = get_runtime_ns()
59+
health, sync = get_app_status(ingress_host)
5460

5561
if WAIT_HEALTHY:
56-
status=waitHealthy (ingress_host)
62+
health, sync = waitHealthy (ingress_host)
5763

5864
# if Wait failed, it's time for rollback
59-
if status != "HEALTHY" and ROLLBACK:
65+
# Failed: Not healthy or out of sync
66+
if ((health != "HEALTHY") or (sync == 'OUT_OF_SYNC')) and ROLLBACK:
6067
logging.info("Application '%s' did not sync properly. Initiating rollback ", APPLICATION)
6168
revision = getRevision(namespace)
6269
logging.info("Latest healthy revision is %d", revision)
6370

6471
rollback(ingress_host, namespace, revision)
65-
logging.info("Waiting for rollback to happen")
72+
6673
if WAIT_ROLLBACK:
67-
status=waitHealthy (ingress_host)
74+
logging.info("Waiting for rollback to happen")
75+
health, sync = waitHealthy (ingress_host)
6876
else:
6977
time.sleep(INTERVAL)
70-
status=get_app_status(ingress_host)
78+
health, sync = get_app_status(ingress_host)
7179
else:
7280
export_variable('ROLLBACK_EXECUTED', "false")
81+
82+
#
83+
# We care about those only if we want a HEALTH app
84+
#
85+
if health != "HEALTHY":
86+
logging.error("Health Status is not HEALTHY. Exiting with error.")
87+
sys.exit(1)
88+
if sync == 'OUT_OF_SYNC':
89+
logging.error("Sync Status is OUT OF SYNC. Exiting with error.")
90+
sys.exit(1)
7391
else:
7492
export_variable('ROLLBACK_EXECUTED', "false")
7593

76-
export_variable('HEALTH_STATUS', status)
94+
export_variable('HEALTH_STATUS', health)
7795

78-
## Generating link to the Apps Dashboard
79-
CF_OUTPUT_URL_VAR = CF_STEP_NAME + '_CF_OUTPUT_URL'
80-
link_to_app = get_link_to_apps_dashboard()
81-
export_variable(CF_OUTPUT_URL_VAR, link_to_app)
82-
if status != "HEALTHY":
83-
logging.debug("Status is not HEALTHY. Exiting with error.")
84-
sys.exit(1)
8596

8697
#######################################################################
8798

@@ -128,19 +139,20 @@ def getRevision(namespace):
128139
sys.exit(1)
129140

130141
def waitHealthy (ingress_host):
131-
logging.debug ("Entering waitHealthy (ns: %s)", ingress_host)
142+
logging.debug ("Entering waitHealthy (host: %s)", ingress_host)
132143

133144
time.sleep(INTERVAL)
134-
status = get_app_status(ingress_host)
135-
logging.info("App status is %s", status)
145+
health, sync = get_app_status(ingress_host)
146+
logging.info("App health: %s and sync: %s", health, sync)
136147
loop=0
137-
while status != "HEALTHY" and loop < MAX_CHECKS:
138-
status=get_app_status(ingress_host)
148+
while ((health != "HEALTHY") or (sync == 'OUT_OF_SYNC')) and loop < MAX_CHECKS:
149+
logging.info("App health: %s and sync: %s after %d checks", health, sync, loop)
139150
time.sleep(INTERVAL)
140-
logging.info("App status is %s after %d checks", status, loop)
151+
health, sync=get_app_status(ingress_host)
141152
loop += 1
142-
logging.debug ("Returning waitHealthy with '%s'", status)
143-
return status
153+
154+
logging.debug ("Returning waitHealthy with health: '%s' and sync: '%s'", health, sync)
155+
return health, sync
144156

145157
def rollback(ingress_host, namespace, revision):
146158
logging.debug ("Entering rollback(%s, %s, %s)", ingress_host, namespace, revision)
@@ -167,7 +179,10 @@ def rollback(ingress_host, namespace, revision):
167179

168180

169181
def get_app_status(ingress_host):
170-
## Get the health status of the app
182+
## Get the health and sync status of the app
183+
# Health: HEALTHY, PROGRESSING
184+
# Sync: OUT_OF_SYNC, SYNCED
185+
171186
gql_api_endpoint = ingress_host + '/app-proxy/api/graphql'
172187
transport = RequestsHTTPTransport(
173188
url=gql_api_endpoint,
@@ -184,7 +199,8 @@ def get_app_status(ingress_host):
184199

185200
logging.debug("App Status result: %s", result)
186201
health = result['applicationProxyQuery']['status']['health']['status']
187-
return health
202+
sync = result['applicationProxyQuery']['status']['sync']['status']
203+
return health, sync
188204

189205
def get_query(query_name):
190206
## To do: get query content from a variable, failback to a file
@@ -244,8 +260,28 @@ def execute_argocd_sync(ingress_host):
244260
"prune": True
245261
}
246262
}
247-
result = client.execute(query, variable_values=variables)
248-
logging.debug("Syncing App result: %s", result)
263+
try:
264+
result = client.execute(query, variable_values=variables)
265+
except TransportQueryError as err:
266+
if "NOT_FOUND_ERROR" in str(err):
267+
print(f"ERROR: Application {APPLICATION} does not exist")
268+
else:
269+
print(f"ERROR: cannot sync Application {APPLICATION}")
270+
logging.debug("Syncing App result: %s", err)
271+
sys.exit(2)
272+
except Exception as err:
273+
print(f"ERROR: cannot sync Application {APPLICATION}")
274+
logging.debug("Syncing App result: %s", err)
275+
sys.exit(1)
276+
# finally:
277+
# print("finally block")
278+
# logging.debug("Syncing App result: %s", result)
279+
# if result.errors[0].message.contains("NOT_FOUND_ERROR"):
280+
# printf("Application %s does not exit")
281+
#
282+
# else:
283+
# # Application sync'ed properly
284+
# logging.debug("Syncing App result: %s", result)
249285

250286

251287
def export_variable(var_name, var_value):

incubating/argo-cd-sync/step.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
kind: step-type
22
metadata:
33
name: argo-cd-sync
4-
version: 1.4.2
4+
version: 1.4.3
55
isPublic: true
66
description: Syncs Argo CD apps managed by our GitOps Runtimes
77
sources:
@@ -120,7 +120,7 @@ spec:
120120
},
121121
"IMAGE_TAG": {
122122
"type": "string",
123-
"default": "1.4.2",
123+
"default": "1.4.3",
124124
"description": "OPTIONAL - To overwrite the tag to use"
125125
}
126126
}

0 commit comments

Comments
 (0)