Skip to content

Commit 2936640

Browse files
authored
Prevent sync operation for app in auto-sync mode (#687)
* v1.4.4: prevent sync operation for app in auto-sync mode Signed-off-by: Laurent Rochette <laurent.rochette@codefresh.io> * Check for application existence before anything is done Signed-off-by: Laurent Rochette <laurent.rochette@codefresh.io> --------- Signed-off-by: Laurent Rochette <laurent.rochette@codefresh.io>
1 parent 073a846 commit 2936640

File tree

5 files changed

+124
-13
lines changed

5 files changed

+124
-13
lines changed

incubating/argo-cd-sync/CHANGELOG.md

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

3+
## [1.4.4] - 2024-03-07
4+
### Fixed
5+
Do not sync an application in auto-sync mode
6+
Check for application existence before anything is done
7+
38
## [1.4.3] - 2024-02-22
49
### Fixed
510
intercepting application not found for better error message

incubating/argo-cd-sync/argocd_sync.py

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import logging
66
import time
77
import sys
8+
import json
9+
import re
810

911
PAGE_SIZE = 10
1012

@@ -54,7 +56,27 @@ def main():
5456
export_variable(CF_OUTPUT_URL_VAR, link_to_app)
5557

5658
ingress_host = get_runtime_ingress_host()
57-
execute_argocd_sync(ingress_host)
59+
60+
# Does the app exist
61+
# if not let's wait it has been recorded
62+
# but not too long in case of simple misspelling
63+
is_app_real=application_exist(ingress_host)
64+
count=1
65+
while count <3 and is_app_real == False:
66+
logging.debug("App does not exist yet %d", count)
67+
time.sleep(INTERVAL)
68+
count += 1
69+
is_app_real=application_exist(ingress_host)
70+
71+
if application_exist(ingress_host) == False:
72+
print(f"ERROR application {APPLICATION} does not seem to exist")
73+
sys.exit(3)
74+
75+
if application_autosync(ingress_host) == False:
76+
execute_argocd_sync(ingress_host)
77+
else:
78+
logging.info("Skipping synchronization as Application is in auto-sync mode")
79+
5880
namespace = get_runtime_ns()
5981
health, sync = get_app_status(ingress_host)
6082

@@ -253,7 +275,7 @@ def execute_argocd_sync(ingress_host):
253275
retries=3,
254276
)
255277
client = Client(transport=transport, fetch_schema_from_transport=False)
256-
query = get_query('argocd_sync') ## gets gql query
278+
query = get_query('argocd_sync') ## gets gql query
257279
variables = {
258280
"applicationName": APPLICATION,
259281
"options": {
@@ -273,15 +295,73 @@ def execute_argocd_sync(ingress_host):
273295
print(f"ERROR: cannot sync Application {APPLICATION}")
274296
logging.debug("Syncing App result: %s", err)
275297
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)
298+
299+
#
300+
# Check for application existence
301+
# if it does not exist, it will return 403 error
302+
#
303+
# Return True or False
304+
#
305+
def application_exist(ingress_host):
306+
runtime_api_endpoint = ingress_host + '/app-proxy/api/graphql'
307+
transport = RequestsHTTPTransport(
308+
url=runtime_api_endpoint,
309+
headers={'authorization': CF_API_KEY},
310+
verify=VERIFY,
311+
retries=3,
312+
)
313+
client = Client(transport=transport, fetch_schema_from_transport=False)
314+
query = get_query('get_app_existence') ## gets gql query
315+
variables = {
316+
"applicationName": APPLICATION
317+
}
318+
try:
319+
result = client.execute(query, variable_values=variables)
320+
except TransportQueryError as err:
321+
data = json.loads(re.sub('\'','\"', str(err)))
322+
if (data["message"] == "Forbidden") and (data["extensions"] == 403):
323+
return False
324+
else:
325+
print(f"ERROR: cannot test Application {APPLICATION}")
326+
logging.error("Existence App result: %s", err)
327+
sys.exit(1)
328+
except Exception as err:
329+
print(f"ERROR: cannot test Application {APPLICATION}")
330+
logging.error("Existence App result: %s", err)
331+
sys.exit(1)
332+
return True
333+
334+
#
335+
# Check if app is in auto-sync mode
336+
#
337+
# Return True or False
338+
#
339+
def application_autosync(ingress_host):
340+
runtime_api_endpoint = ingress_host + '/app-proxy/api/graphql'
341+
transport = RequestsHTTPTransport(
342+
url=runtime_api_endpoint,
343+
headers={'authorization': CF_API_KEY},
344+
verify=VERIFY,
345+
retries=3,
346+
)
347+
client = Client(transport=transport, fetch_schema_from_transport=False)
348+
query = get_query('get_app_autosync') ## gets gql query
349+
variables = {
350+
"applicationName": APPLICATION
351+
}
352+
try:
353+
result = client.execute(query, variable_values=variables)
354+
except Exception as err:
355+
print(f"ERROR: cannot get sync policy from Application {APPLICATION}")
356+
logging.debug("Application Sync policy result: %s", err)
357+
sys.exit(1)
358+
359+
logging.debug("App sync Policy: ", result['applicationProxyQuery']['spec']['syncPolicy']['automated'])
360+
if result['applicationProxyQuery']['spec']['syncPolicy']['automated'] == None:
361+
return False
362+
else:
363+
return True
364+
285365

286366

287367
def export_variable(var_name, var_value):
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
query appsyncstatus ($applicationName: String!) {
2+
3+
applicationProxyQuery(
4+
name: $applicationName
5+
){
6+
metadata {
7+
name
8+
}
9+
spec {
10+
syncPolicy {
11+
automated {
12+
prune
13+
}
14+
}
15+
}
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
query appexistence ($applicationName: String!) {
2+
applicationProxyQuery(
3+
name: $applicationName
4+
){
5+
metadata {
6+
name
7+
}
8+
}
9+
}

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.3
4+
version: 1.4.4
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.3",
123+
"default": "1.4.4",
124124
"description": "OPTIONAL - To overwrite the tag to use"
125125
}
126126
}

0 commit comments

Comments
 (0)