From b136cdcaaf24e664c75dd4fa429399e9d72ab5fa Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Thu, 9 Sep 2021 13:00:23 +0200 Subject: [PATCH] Split definition into files and add #App to create argocd project Signed-off-by: Tom Chauveau --- stdlib/argocd/app.cue | 47 +++++++++++++++ stdlib/argocd/{app/app.cue => status.cue} | 70 ++++------------------- stdlib/argocd/sync.cue | 35 ++++++++++++ 3 files changed, 94 insertions(+), 58 deletions(-) create mode 100644 stdlib/argocd/app.cue rename stdlib/argocd/{app/app.cue => status.cue} (52%) create mode 100644 stdlib/argocd/sync.cue diff --git a/stdlib/argocd/app.cue b/stdlib/argocd/app.cue new file mode 100644 index 00000000..42e2e09c --- /dev/null +++ b/stdlib/argocd/app.cue @@ -0,0 +1,47 @@ +package argocd + +import ( + "alpha.dagger.io/dagger" + "alpha.dagger.io/os" +) + +// Create an ArgoCD application +#App: { + // ArgoCD configuration + config: #Config + + // App name + name: dagger.#Input & {string} + + // Repository url (git or helm) + repo: dagger.#Input & {string} + + // Folder to deploy + path: dagger.#Input & {"." | string} + + // Destination server + server: dagger.#Input & {*"https://kubernetes.default.svc" | string} + + // Destination namespace + namespace: dagger.#Input & {*"default" | string} + + _ctr: os.Container & { + from: #CLI & { + "config": config + } + command: #""" + argocd app create "$APP_NAME" \ + --repo "$APP_REPO" \ + --path "$APP_PATH" \ + --dest-server "$APP_SERVER" \ + --dest-namespace "$APP_NAMESPACE" + """# + env: { + APP_NAME: name + APP_REPO: repo + APP_PATH: path + APP_SERVER: server + APP_NAMESPACE: namespace + } + } +} diff --git a/stdlib/argocd/app/app.cue b/stdlib/argocd/status.cue similarity index 52% rename from stdlib/argocd/app/app.cue rename to stdlib/argocd/status.cue index 2e39ad64..9900b6d7 100644 --- a/stdlib/argocd/app/app.cue +++ b/stdlib/argocd/status.cue @@ -1,15 +1,14 @@ -// ArgoCD applications -package app +package argocd import ( - "alpha.dagger.io/argocd" "alpha.dagger.io/dagger" "alpha.dagger.io/dagger/op" ) -// Get an application -#Application: { - config: argocd.#Config +// Get application's status +#Status: { + // ArgoCD configuration + config: #Config // ArgoCD application name: dagger.#Input & {string} @@ -37,7 +36,7 @@ import ( outputs: #up: [ op.#Load & { - from: argocd.#CLI & { + from: #CLI & { "config": config } }, @@ -45,10 +44,15 @@ import ( op.#Exec & { args: ["sh", "-c", #""" + ls ~/.argocd + cat ~/.argocd/config argocd app get "$APPLICATION" --output json | jq '{health:.status.health.status,sync:.status.sync.status,namespace:.spec.destination.namespace,server:.spec.destination.server,urls:.status.summary.externalURLs|join(","),state:.status.operationState.message}' > /output.json """#, ] - env: APPLICATION: name + env: { + APPLICATION: name + ARGOCD_OPTS: "--port-forward-namespace argocd" + } }, op.#Export & { @@ -57,53 +61,3 @@ import ( }, ] } - -// Sync an application to its target state -#Synchronization: { - config: argocd.#Config - - // ArgoCD application - application: dagger.#Input & {string} - - #up: [ - op.#Load & { - from: argocd.#CLI & { - "config": config - } - }, - - op.#Exec & { - args: [ - "sh", "-c", #""" - argocd app sync "$APPLICATION" - """#, - ] - env: APPLICATION: application - }, - ] -} - -// Wait for an application to reach a synced and healthy state -#SynchronizedApplication: { - config: argocd.#Config - - // ArgoCD application - application: dagger.#Input & {string} - - #up: [ - op.#Load & { - from: argocd.#CLI & { - "config": config - } - }, - - op.#Exec & { - args: [ - "sh", "-c", #""" - argocd app wait "$APPLICATION" - """#, - ] - env: APPLICATION: application - }, - ] -} diff --git a/stdlib/argocd/sync.cue b/stdlib/argocd/sync.cue new file mode 100644 index 00000000..d6d3d2df --- /dev/null +++ b/stdlib/argocd/sync.cue @@ -0,0 +1,35 @@ +package argocd + +import ( + "alpha.dagger.io/dagger" + "alpha.dagger.io/os" +) + +// Sync an application to its targer state +#Sync: { + // ArgoCD configuration + config: #Config + + // ArgoCD application + application: dagger.#Input & {string} + + // Wait the application to sync correctly + wait: dagger.#Input & {*false | bool} + + _ctr: os.#Container & { + image: #CLI & { + "config": config + } + command: #""" + argocd app sync "$APPLICATION" + + if [ -n "$WAIT_FLAG" ]; then + argocd app wait "$APPLICATION" + fi + """# + env: APPLICATION: application + if wait { + env: WAIT_FLAG: "wait" + } + } +}