stdlib: argocd app support

Signed-off-by: Kevin Poirot <kevin@vazee.fr>
This commit is contained in:
Kevin Poirot
2021-08-13 16:43:06 +02:00
committed by Sam Alba
parent f29de14a2f
commit f8f39ca75b
9 changed files with 367 additions and 0 deletions

109
stdlib/argocd/app/app.cue Normal file
View File

@@ -0,0 +1,109 @@
// ArgoCD applications
package app
import (
"alpha.dagger.io/argocd"
"alpha.dagger.io/dagger"
"alpha.dagger.io/dagger/op"
)
// Get an application
#Application: {
config: argocd.#Config
// ArgoCD application
name: dagger.#Input & {string}
// ArgoCD CLI output
outputs: {
// Application health
health: dagger.#Output & {string}
// Application sync state
sync: dagger.#Output & {string}
// Namespace
namespace: dagger.#Output & {string}
// Server
server: dagger.#Output & {string}
// Comma separated list of application URLs
urls: dagger.#Output & {string}
// Last operation state message
state: dagger.#Output & {string}
}
outputs: #up: [
op.#Load & {
from: argocd.#CLI & {
"config": config
}
},
op.#Exec & {
args: ["sh", "-c",
#"""
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
},
op.#Export & {
source: "/output.json"
format: "json"
},
]
}
// 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
},
]
}

72
stdlib/argocd/argocd.cue Normal file
View File

@@ -0,0 +1,72 @@
// ArgoCD client operations
package argocd
import (
"alpha.dagger.io/alpine"
"alpha.dagger.io/dagger"
"alpha.dagger.io/dagger/op"
)
// ArgoCD configuration
#Config: {
// ArgoCD CLI binary version
version: *"v2.0.5" | dagger.#Input & {string}
// ArgoCD server
server: dagger.#Input & {string}
// ArgoCD project
project: *"default" | dagger.#Input & {string}
// ArgoCD authentication token
token: dagger.#Secret & dagger.#Input
}
// Re-usable CLI component
#CLI: {
config: #Config
#up: [
op.#Load & {
from: alpine.#Image & {
package: bash: "=~5.1"
package: jq: "=~1.6"
package: curl: true
}
},
// Install the ArgoCD CLI
op.#Exec & {
args: ["sh", "-c",
#"""
curl -sSL -o /usr/local/bin/argocd https://github.com/argoproj/argo-cd/releases/download/$VERSION/argocd-linux-amd64 &&
chmod +x /usr/local/bin/argocd
"""#,
]
env: VERSION: config.version
},
// Write config file
op.#Exec & {
args: ["sh", "-c",
#"""
mkdir ~/.argocd && cat > ~/.argocd/config << EOF
contexts:
- name: "$SERVER"
server: "$SERVER"
user: "$SERVER"
current-context: "$SERVER"
servers:
- grpc-web-root-path: ""
server: "$SERVER"
users:
- auth-token: $(cat /run/secrets/token)
name: "$SERVER"
EOF
"""#,
]
mount: "/run/secrets/token": secret: config.token
env: SERVER: config.server
},
]
}

View File

@@ -0,0 +1,33 @@
package app
import (
"alpha.dagger.io/argocd"
"alpha.dagger.io/dagger"
"alpha.dagger.io/dagger/op"
)
TestConfig: argocdConfig: argocd.#Config & {
version: "v2.0.5"
server: "dagger-example-argocd-server.tld"
token: dagger.#Secret & dagger.#Input
}
TestArgocd: #up: [
// Initialize ArgoCD CLI binary
op.#Load & {
from: argocd.#CLI & {
config: TestConfig.argocdConfig
}
},
// Check the binary and its version
op.#Exec & {
args: [
"sh", "-c",
#"""
argocd version --output json | jq -e 'all(.client.Version; startswith("$VERSION"))'
"""#,
]
env: VERSION: TestConfig.argocdConfig.version
},
]