-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from robusta-dev/argo-action
argo app sync
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from pydantic import SecretStr | ||
|
||
from robusta.api import * | ||
|
||
|
||
class ArgoBaseParams(BaseModel): | ||
argo_url: str | ||
argo_token: SecretStr | ||
argo_verify_server_cert: bool = True | ||
|
||
|
||
class ArgoAppParams(ArgoBaseParams): | ||
argo_app_name: str | ||
rate_limit_seconds: int = 1800 | ||
|
||
|
||
@action | ||
def argo_app_sync(event: ExecutionBaseEvent, params: ArgoAppParams): | ||
|
||
if not RateLimiter.mark_and_test( | ||
"argo_app_sync", params.argo_url + params.argo_app_name, params.rate_limit_seconds | ||
): | ||
return | ||
|
||
argo_client = ArgoCDClient(params.argo_url, params.argo_token.get_secret_value(), params.argo_verify_server_cert) | ||
success = argo_client.sync_application(params.argo_app_name) | ||
|
||
finding = Finding( | ||
title="Argo CD application sync", | ||
aggregation_key="argo_app_sync", | ||
finding_type=FindingType.REPORT, | ||
failure=False | ||
) | ||
finding.add_enrichment([ | ||
MarkdownBlock(f"Argo CD app sync for application: *{params.argo_app_name}* ended with *{'success' if success else 'failure'}*") | ||
]) | ||
event.add_finding(finding) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import logging | ||
|
||
import requests | ||
|
||
|
||
class ArgoCDClient: | ||
SYNC_APP_PATTERN = "{0}/api/v1/applications/{1}/sync" | ||
|
||
def __init__(self, url: str, token: str, verify_cert: bool = True): | ||
self.url = url | ||
self.token = token | ||
self.verify_cert = verify_cert | ||
self.headers = { | ||
"Authorization": f"Bearer {self.token}", | ||
} | ||
|
||
def sync_application(self, application_name: str) -> bool: | ||
sync_url = self.SYNC_APP_PATTERN.format(self.url, application_name) | ||
|
||
argo_response = requests.post(sync_url, headers=self.headers, verify=self.verify_cert) | ||
if argo_response.status_code != 200: | ||
logging.error(f"Failed to sync application {application_name} on argo {self.url} {argo_response.reason}") | ||
return False | ||
|
||
return True |