Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent sync operation for app in auto-sync mode #687

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to do another request, check autosync while checking existance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, I will refactor later as I need another call to check current sync status

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
Loading