Skip to content

Commit

Permalink
Prevent sync operation for app in auto-sync mode (#687)
Browse files Browse the repository at this point in the history
* v1.4.4: prevent sync operation for app in auto-sync mode

Signed-off-by: Laurent Rochette <[email protected]>

* Check for application existence before anything is done

Signed-off-by: Laurent Rochette <[email protected]>

---------

Signed-off-by: Laurent Rochette <[email protected]>
  • Loading branch information
lrochette authored Mar 15, 2024
1 parent 073a846 commit 2936640
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 13 deletions.
5 changes: 5 additions & 0 deletions incubating/argo-cd-sync/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.4.4] - 2024-03-07
### Fixed
Do not sync an application in auto-sync mode
Check for application existence before anything is done

## [1.4.3] - 2024-02-22
### Fixed
intercepting application not found for better error message
Expand Down
102 changes: 91 additions & 11 deletions incubating/argo-cd-sync/argocd_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import logging
import time
import sys
import json
import re

PAGE_SIZE = 10

Expand Down Expand Up @@ -54,7 +56,27 @@ def main():
export_variable(CF_OUTPUT_URL_VAR, link_to_app)

ingress_host = get_runtime_ingress_host()
execute_argocd_sync(ingress_host)

# Does the app exist
# if not let's wait it has been recorded
# but not too long in case of simple misspelling
is_app_real=application_exist(ingress_host)
count=1
while count <3 and is_app_real == False:
logging.debug("App does not exist yet %d", count)
time.sleep(INTERVAL)
count += 1
is_app_real=application_exist(ingress_host)

if application_exist(ingress_host) == False:
print(f"ERROR application {APPLICATION} does not seem to exist")
sys.exit(3)

if application_autosync(ingress_host) == False:
execute_argocd_sync(ingress_host)
else:
logging.info("Skipping synchronization as Application is in auto-sync mode")

namespace = get_runtime_ns()
health, sync = get_app_status(ingress_host)

Expand Down Expand Up @@ -253,7 +275,7 @@ def execute_argocd_sync(ingress_host):
retries=3,
)
client = Client(transport=transport, fetch_schema_from_transport=False)
query = get_query('argocd_sync') ## gets gql query
query = get_query('argocd_sync') ## gets gql query
variables = {
"applicationName": APPLICATION,
"options": {
Expand All @@ -273,15 +295,73 @@ def execute_argocd_sync(ingress_host):
print(f"ERROR: cannot sync Application {APPLICATION}")
logging.debug("Syncing App result: %s", err)
sys.exit(1)
# finally:
# print("finally block")
# logging.debug("Syncing App result: %s", result)
# if result.errors[0].message.contains("NOT_FOUND_ERROR"):
# printf("Application %s does not exit")
#
# else:
# # Application sync'ed properly
# logging.debug("Syncing App result: %s", result)

#
# Check for application existence
# if it does not exist, it will return 403 error
#
# Return True or False
#
def application_exist(ingress_host):
runtime_api_endpoint = ingress_host + '/app-proxy/api/graphql'
transport = RequestsHTTPTransport(
url=runtime_api_endpoint,
headers={'authorization': CF_API_KEY},
verify=VERIFY,
retries=3,
)
client = Client(transport=transport, fetch_schema_from_transport=False)
query = get_query('get_app_existence') ## gets gql query
variables = {
"applicationName": APPLICATION
}
try:
result = client.execute(query, variable_values=variables)
except TransportQueryError as err:
data = json.loads(re.sub('\'','\"', str(err)))
if (data["message"] == "Forbidden") and (data["extensions"] == 403):
return False
else:
print(f"ERROR: cannot test Application {APPLICATION}")
logging.error("Existence App result: %s", err)
sys.exit(1)
except Exception as err:
print(f"ERROR: cannot test Application {APPLICATION}")
logging.error("Existence App result: %s", err)
sys.exit(1)
return True

#
# Check if app is in auto-sync mode
#
# Return True or False
#
def application_autosync(ingress_host):
runtime_api_endpoint = ingress_host + '/app-proxy/api/graphql'
transport = RequestsHTTPTransport(
url=runtime_api_endpoint,
headers={'authorization': CF_API_KEY},
verify=VERIFY,
retries=3,
)
client = Client(transport=transport, fetch_schema_from_transport=False)
query = get_query('get_app_autosync') ## gets gql query
variables = {
"applicationName": APPLICATION
}
try:
result = client.execute(query, variable_values=variables)
except Exception as err:
print(f"ERROR: cannot get sync policy from Application {APPLICATION}")
logging.debug("Application Sync policy result: %s", err)
sys.exit(1)

logging.debug("App sync Policy: ", result['applicationProxyQuery']['spec']['syncPolicy']['automated'])
if result['applicationProxyQuery']['spec']['syncPolicy']['automated'] == None:
return False
else:
return True



def export_variable(var_name, var_value):
Expand Down
17 changes: 17 additions & 0 deletions incubating/argo-cd-sync/queries/get_app_autosync.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
query appsyncstatus ($applicationName: String!) {

applicationProxyQuery(
name: $applicationName
){
metadata {
name
}
spec {
syncPolicy {
automated {
prune
}
}
}
}
}
9 changes: 9 additions & 0 deletions incubating/argo-cd-sync/queries/get_app_existence.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query appexistence ($applicationName: String!) {
applicationProxyQuery(
name: $applicationName
){
metadata {
name
}
}
}
4 changes: 2 additions & 2 deletions incubating/argo-cd-sync/step.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
kind: step-type
metadata:
name: argo-cd-sync
version: 1.4.3
version: 1.4.4
isPublic: true
description: Syncs Argo CD apps managed by our GitOps Runtimes
sources:
Expand Down Expand Up @@ -120,7 +120,7 @@ spec:
},
"IMAGE_TAG": {
"type": "string",
"default": "1.4.3",
"default": "1.4.4",
"description": "OPTIONAL - To overwrite the tag to use"
}
}
Expand Down

0 comments on commit 2936640

Please sign in to comment.