Merge pull request #883 from VazeeKevin/stdlib/argocd

stdlib: ArgoCD app support
This commit is contained in:
Sam Alba 2021-09-10 09:31:00 -07:00 committed by GitHub
commit 73fe95824c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 732 additions and 85 deletions

View File

@ -1,6 +1,7 @@
# Index
- [alpine](./alpine.md) - Base package for Alpine Linux
- [argocd](./argocd.md) - ArgoCD client operations
- [aws](./aws/README.md) - AWS base package
- [aws/cloudformation](./aws/cloudformation.md) - AWS CloudFormation
- [aws/ecr](./aws/ecr.md) - Amazon Elastic Container Registry (ECR)

137
docs/reference/argocd.md Normal file
View File

@ -0,0 +1,137 @@
---
sidebar_label: argocd
---
# alpha.dagger.io/argocd
ArgoCD client operations
```cue
import "alpha.dagger.io/argocd"
```
## argocd.#App
Create an ArgoCD application
### argocd.#App Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*config.server* | `string` |ArgoCD server |
|*config.project* | `*"default" \| string` |ArgoCD project |
|*config.token* | `dagger.#Secret` |ArgoCD authentication token |
|*name* | `string` |App name |
|*repo* | `string` |Repository url (git or helm) |
|*path* | `string` |Folder to deploy |
|*server* | `*"https://kubernetes.default.svc" \| string` |Destination server |
|*image.config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*image.config.server* | `string` |ArgoCD server |
|*image.config.project* | `*"default" \| string` |ArgoCD project |
|*image.config.token* | `dagger.#Secret` |ArgoCD authentication token |
|*namespace* | `*"default" \| string` |Destination namespace |
|*env.APP_NAME* | `string` |- |
|*env.APP_REPO* | `string` |- |
|*env.APP_PATH* | `string` |- |
|*env.APP_SERVER* | `*"https://kubernetes.default.svc" \| string` |- |
|*env.APP_NAMESPACE* | `*"default" \| string` |- |
### argocd.#App Outputs
_No output._
## argocd.#CLI
Re-usable CLI component
### argocd.#CLI Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*config.server* | `string` |ArgoCD server |
|*config.project* | `*"default" \| string` |ArgoCD project |
|*config.token* | `dagger.#Secret` |ArgoCD authentication token |
### argocd.#CLI Outputs
_No output._
## argocd.#Config
ArgoCD configuration
### argocd.#Config Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*server* | `string` |ArgoCD server |
|*project* | `*"default" \| string` |ArgoCD project |
|*token* | `dagger.#Secret` |ArgoCD authentication token |
### argocd.#Config Outputs
_No output._
## argocd.#Status
Get application's status
### argocd.#Status Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*config.server* | `string` |ArgoCD server |
|*config.project* | `*"default" \| string` |ArgoCD project |
|*config.token* | `dagger.#Secret` |ArgoCD authentication token |
|*name* | `string` |ArgoCD application |
### argocd.#Status Outputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*outputs.health* | `string` |Application health |
|*outputs.sync* | `string` |Application sync state |
|*outputs.namespace* | `string` |Namespace |
|*outputs.server* | `string` |Server |
|*outputs.urls* | `string` |Comma separated list of application URLs |
|*outputs.state* | `string` |Last operation state message |
## argocd.#Sync
Sync an application to its targer state
### argocd.#Sync Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*config.server* | `string` |ArgoCD server |
|*config.project* | `*"default" \| string` |ArgoCD project |
|*config.token* | `dagger.#Secret` |ArgoCD authentication token |
|*application* | `string` |ArgoCD application |
|*wait* | `*false \| bool` |Wait the application to sync correctly |
|*ctr.image.config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*ctr.image.config.server* | `string` |ArgoCD server |
|*ctr.image.config.project* | `*"default" \| string` |ArgoCD project |
|*ctr.image.config.token* | `dagger.#Secret` |ArgoCD authentication token |
|*ctr.env.APPLICATION* | `string` |- |
|*status.config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*status.config.server* | `string` |ArgoCD server |
|*status.config.project* | `*"default" \| string` |ArgoCD project |
|*status.config.token* | `dagger.#Secret` |ArgoCD authentication token |
|*status.name* | `string` |ArgoCD application |
### argocd.#Sync Outputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*status.outputs.health* | `string` |Application health |
|*status.outputs.sync* | `string` |Application sync state |
|*status.outputs.namespace* | `string` |Namespace |
|*status.outputs.server* | `string` |Server |
|*status.outputs.urls* | `string` |Comma separated list of application URLs |
|*status.outputs.state* | `string` |Last operation state message |

View File

@ -0,0 +1,45 @@
---
sidebar_label: argocd
---
# alpha.dagger.io/argocd
ArgoCD client operations
```cue
import "alpha.dagger.io/argocd"
```
## argocd.#CLI
Re-usable CLI component
### argocd.#CLI Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*config.server* | `string` |ArgoCD server |
|*config.project* | `*"default" \| string` |ArgoCD project |
|*config.token* | `dagger.#Secret` |ArgoCD authentication token |
### argocd.#CLI Outputs
_No output._
## argocd.#Config
ArgoCD configuration
### argocd.#Config Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*server* | `string` |ArgoCD server |
|*project* | `*"default" \| string` |ArgoCD project |
|*token* | `dagger.#Secret` |ArgoCD authentication token |
### argocd.#Config Outputs
_No output._

View File

@ -0,0 +1,72 @@
---
sidebar_label: app
---
# alpha.dagger.io/argocd/app
ArgoCD applications
```cue
import "alpha.dagger.io/argocd/app"
```
## app.#Application
Get an application
### app.#Application Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*config.server* | `string` |ArgoCD server |
|*config.project* | `*"default" \| string` |ArgoCD project |
|*config.token* | `dagger.#Secret` |ArgoCD authentication token |
|*name* | `string` |ArgoCD application |
### app.#Application Outputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*outputs.health* | `string` |Application health |
|*outputs.sync* | `string` |Application sync state |
|*outputs.namespace* | `string` |Namespace |
|*outputs.server* | `string` |Server |
|*outputs.urls* | `string` |Comma separated list of application URLs |
|*outputs.state* | `string` |Last operation state message |
## app.#Synchronization
Sync an application to its target state
### app.#Synchronization Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*config.server* | `string` |ArgoCD server |
|*config.project* | `*"default" \| string` |ArgoCD project |
|*config.token* | `dagger.#Secret` |ArgoCD authentication token |
|*application* | `string` |ArgoCD application |
### app.#Synchronization Outputs
_No output._
## app.#SynchronizedApplication
Wait for an application to reach a synced and healthy state
### app.#SynchronizedApplication Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*config.version* | `*"v2.0.5" \| string` |ArgoCD CLI binary version |
|*config.server* | `string` |ArgoCD server |
|*config.project* | `*"default" \| string` |ArgoCD project |
|*config.token* | `dagger.#Secret` |ArgoCD authentication token |
|*application* | `string` |ArgoCD application |
### app.#SynchronizedApplication Outputs
_No output._

View File

@ -0,0 +1,2 @@
# dagger state
state/**

View File

@ -0,0 +1,23 @@
plan:
package: ./argocd/tests/infra
name: argocd-infra
sops:
kms: []
gcp_kms: []
azure_kv: []
hc_vault: []
age:
- recipient: age1gxwmtwahzwdmrskhf90ppwlnze30lgpm056kuesrxzeuyclrwvpsupwtpk
enc: |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBmVmEzZ3RDdjlBRXBleURr
cmx6YW40ZHFLMHpqa2RYWUlIdTFIZlYyZ3pnCmVieTB2MXdid1l5ZitUMkprVGFz
RHl4elJzdWRTYS9MaXZkU29UMG1SSmcKLS0tIHFjNHhBVVVwcGwrVWZHOXoxdGps
RWlubERCTjFkSStyQXdpbSttbXB1RmcKQeRMIc8QGKI8HkoBXCQzR/gaBoiYTYxy
vLjf1kMlX4BvmwTpmDO9Wer8a/NXEwA3zpK/kHaoXGBZln8Lf4J87A==
-----END AGE ENCRYPTED FILE-----
lastmodified: "2021-09-09T10:52:18Z"
mac: ENC[AES256_GCM,data:B5CZFBYviKy/l8YYzg3luUdeFpRTGx5q+Q5b56SvIVvdp33TQwvU6bftiNOwUHjI3a3B2p70QNKxam+Yk7Pf9jC1nN6dR1jWh0RcicTjQOI6URN6NgS667/iCG7e94fKiVKlbgRq/P7eudnJrkhPsiqqHopmWg41g+izH+YOX2I=,iv:X6nxvaEdtnyUSEPODJLrPfuju906b9EYsye4MstBGRc=,tag:olbgC+EtQ8U8FJejQv5u/Q==,type:str]
pgp: []
encrypted_suffix: secret
version: 3.7.1

2
stdlib/.dagger/env/argocd/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# dagger state
state/**

26
stdlib/.dagger/env/argocd/values.yaml vendored Normal file
View File

@ -0,0 +1,26 @@
plan:
package: ./argocd/tests
name: argocd
inputs:
TestConfig.argocdConfig.server:
text: localhost:8080
sops:
kms: []
gcp_kms: []
azure_kv: []
hc_vault: []
age:
- recipient: age1gxwmtwahzwdmrskhf90ppwlnze30lgpm056kuesrxzeuyclrwvpsupwtpk
enc: |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBhZEhvaGFiVGVUcWZhcjVj
WmthWWRMMmFXSmRIbzNKUm9IMms0TUltcjBvClE4UHlwcHVGcjVJTHRhNUx1Lyt4
UHZlTlBRRnhtMmMzcmFPUGlBTXlVMncKLS0tIFpkSXU1SDUvZTR4MVgxNWxKV3V3
LzM4MENHSVZFNVJza3FhVGkyd0R4d3cKPI6V2o1BR8WVrsAdsuuKOW9Lw32c+KUh
WBHXbYucWjPeogNVIZK+/iJs4Pz92fQ1awdj0H47ro3d7I3QfZoeJQ==
-----END AGE ENCRYPTED FILE-----
lastmodified: "2021-09-10T09:08:35Z"
mac: ENC[AES256_GCM,data:RhRtuEL1xqfJ8pvofq91JMKElZY4Pqvb3SY3IhVevotGBAR6UvmKBA6Nfi3VsvbjfAlbA8/Ip+ifHNhuv6iS1kcj+u5Y7E+6esMdXoXlPmry1Jpt8fWu+zeO2t2XiVGsSEw8nLhPyZfMsp0KBoec1I43NUIKQXoPMbUFMMy1rLQ=,iv:Q817XyIlU/0BgUc6tNVZ8f9cz83I/3FbF8wvqaWNtso=,tag:/Ui9/AAT4RnnwfrOw83xew==,type:str]
pgp: []
encrypted_suffix: secret
version: 3.7.1

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

@ -0,0 +1,48 @@
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}
os.#Container & {
image: #CLI & {
"config": config
}
command: #"""
argocd app create "$APP_NAME" \
--repo "$APP_REPO" \
--path "$APP_PATH" \
--dest-server "$APP_SERVER" \
--dest-namespace "$APP_NAMESPACE"
"""#
always: true
env: {
APP_NAME: name
APP_REPO: repo
APP_PATH: path
APP_SERVER: server
APP_NAMESPACE: namespace
}
}
}

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

@ -0,0 +1,99 @@
// 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}
// Basic authentification to login
basicAuth: {
// Username
username: dagger.#Input & {string}
// Password
password: dagger.#Input & {dagger.#Secret}
} | *null
// ArgoCD authentication token
token: dagger.#Input & {*null | dagger.#Secret}
}
// 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
},
if config.basicAuth != null && config.token == null {
// Login to ArgoCD server
op.#Exec & {
args: ["sh", "-c", #"""
argocd login "$ARGO_SERVER" --username "$ARGO_USERNAME" --password $(cat /run/secrets/password) --insecure
"""#,
]
env: {
ARGO_SERVER: config.server
ARGO_USERNAME: config.basicAuth.username
}
mount: "/run/secrets/password": secret: config.basicAuth.password
}
},
if config.token != null && config.basicAuth == null {
// Write config file
op.#Exec & {
args: ["sh", "-c",
#"""
mkdir -p ~/.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
}
},
]
}

59
stdlib/argocd/status.cue Normal file
View File

@ -0,0 +1,59 @@
package argocd
import (
"alpha.dagger.io/dagger"
"alpha.dagger.io/dagger/op"
)
// Get application's status
#Status: {
// ArgoCD configuration
config: #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: #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
always: true
},
op.#Export & {
source: "/output.json"
format: "json"
},
]
}

41
stdlib/argocd/sync.cue Normal file
View File

@ -0,0 +1,41 @@
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
"""#
always: true
env: APPLICATION: application
if wait {
env: WAIT_FLAG: "wait"
}
}
status: #Status & {
config: ctr.image.config
name: application
}
}

View File

@ -0,0 +1,38 @@
package argocd
import (
"alpha.dagger.io/dagger"
"alpha.dagger.io/os"
)
TestConfig: argocdConfig: #Config & {
version: dagger.#Input & {*"v2.0.5" | string}
server: dagger.#Input & {*"dagger-example-argocd-server.tld" | string}
basicAuth: {
username: dagger.#Input & {*"admin" | string}
password: dagger.#Input & {dagger.#Secret}
}
}
TestClient: os.#Container & {
image: #CLI & {
config: TestConfig.argocdConfig
}
command: #"""
argocd account list | grep "$ARGOCD_USERNAME"
"""#
env: ARGOCD_USERNAME: TestConfig.argocdConfig.basicAuth.username
}
TestApp: #App & {
config: TestConfig.argocdConfig
name: "daggerci-test"
repo: "https://github.com/argoproj/argocd-example-apps.git"
path: "guestbook"
}
TestArgoCDStatus: #Sync & {
config: TestApp.config
application: TestApp.name
wait: true
}

View File

@ -0,0 +1,14 @@
package infra
import (
"alpha.dagger.io/dagger"
"alpha.dagger.io/kubernetes"
)
TestKubeconfig: dagger.#Input & {string}
TestArgoInfra: kubernetes.#Resources & {
kubeconfig: TestKubeconfig
namespace: "argocd"
url: "https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml"
}

View File

@ -74,13 +74,24 @@ skip_unless_local_localstack() {
#
# This is need to do kubernetes test in the CI.
skip_unless_local_kube() {
if [ -f ~/.kube/config ] && grep -q "user: kind-kind" ~/.kube/config &> /dev/null && grep -q "127.0.0.1" ~/.kube/config &> /dev/null; then
if [ -f ~/.kube/config ] && grep -q "127.0.0.1" ~/.kube/config &> /dev/null; then
echo "Kubernetes available"
else
skip "local kubernetes cluster not available"
fi
}
# Check if there is a local endpoint.
#
# This is necessary to do test with endpoint.
# Usage:
# skip_unless_endpoint_available "localhost:8080"
skip_unless_endpoint_available() {
local endpoint="$1"
curl "$endpoint"
}
# Cleanup local Localstack instances
setup_localstack() {
if [ "$(curl -s http://localhost:4566)" = '{"status": "running"}' ] && \

View File

@ -214,3 +214,32 @@ setup() {
skip "Azure CI infra not implemented yet - manually tested and working"
#dagger -e azure-storage up
}
@test "argocd" {
skip_unless_local_kube
# Deploy argoCD infra
dagger -e argocd-infra input text TestKubeconfig -f "$HOME"/.kube/config
dagger -e argocd-infra up
# Wait for infra to be ready
kubectl -n argocd wait --for=condition=available deployment -l "app.kubernetes.io/part-of=argocd" --timeout=45s
# Forward port
# We need to kill subprocess to avoid infinity loop
kubectl port-forward svc/argocd-server -n argocd 8080:443 >/dev/null 2>/dev/null &
sleep 3 || (pkill kubectl && exit 1)
# Run test
dagger -e argocd input secret TestConfig.argocdConfig.basicAuth.password "$(kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d)" || (pkill kubectl && exit 1)
dagger -e argocd up || (pkill kubectl && exit 1)
dagger -e argocd input unset TestConfig.argocdConfig.basicAuth.password || (pkill kubectl && exit 1)
# Kill Pid
pgrep kubectl && pkill kubectl
# Check output
run dagger -e argocd query TestArgoCDStatus.status.outputs.health -f json
assert_success
assert_output "\"Healthy\""
}