Skip to content

Commit

Permalink
Add more scripts to ease local debug, dev and testing (#97)
Browse files Browse the repository at this point in the history
* Add more scripts to ease local debug, dev and testing

* Restore scheduler

* Fix shell check

* Shell format

* Update readme

* PR remarks

* Fix debug config

Co-authored-by: Matteo Ruina <[email protected]>
Co-authored-by: Dimitar Hristov <[email protected]>
  • Loading branch information
3 people authored Jun 28, 2021
1 parent 73c9162 commit acc5800
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,4 @@ hack/.hack.*

# Ignore .env files
.env.local
__debug_bin
42 changes: 42 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"envFile": "${workspaceFolder}/.env.local",
"args": [],
"showLog": true
},
{
"name": "Integration tests - controller",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/controllers",
},
{
"name": "Unit tests - scheduler",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/internal/scheduler",
}
{
"name": "Unit tests - utils",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/internal/utils",
}
]
}
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ manager: generate fmt vet
run: generate fmt vet manifests
go run ./main.go --zap-devel=true

debug: fmt vet
./hack/kill-debug.sh
./hack/debug.sh

# Install CRDs into a cluster
install: manifests
kustomize build config/crd | kubectl apply -f -
Expand Down
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,44 @@ After running the script, you will have 3 kind clusters created locally:

If you want to create additional clusters, you can do so by running
```shell
bash hack/add-cluster <cluster-name> <recreate>
bash hack/add-cluster <cluster-name> <recreate> <labels>
```
This will spin up another kind cluster and register it against ArgoCD running in `kind-argocd-control-plane`

#### Deploying a test appset
#### Deploying local test resources

You can deploy a test appset to the default 3 clusters by running the following:
You can deploy a test appset and a progressive sync object to your kind environment via:

```shell
bash hack/deploy-test-appset.sh
bash hack/redeploy-dev-resources.sh
```
Feel free to extend the cluster generation section of the appset spec if you want to deploy it clusters that you have manually created.

### Debugging

```shell
make debug
```

Invoking the command above should spin up a Delve debugger server in headless mode. You can then use your IDE specific functionality or the delve client itself to attach to the remote process and debug it.

**NOTE**: On MacOSX, delve is currently unkillable in headless mode with `^C` or any other control signals that can be sent from the same terminal session. Instead, you'd need to run

``` shell
bash ./hack/kill-debug.sh
```

or

``` shell
make debug
```

from another terminal session to kill the debugger.

### Debugging tests

Delve can be used to debug tests as well. See `Test` launch configuration in `.vscode/launch.json`. Something similar should be achievable in your IDE of choice as well.
#### Update ArgoCD Application API package

Because of [https://github.com/argoproj/argo-cd/issues/4055](https://github.com/argoproj/argo-cd/issues/4055) we can't just run `go get github.com/argoproj/argo-cd`.
Expand Down
2 changes: 2 additions & 0 deletions hack/add-cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ source "$root"/dev-functions.sh

clustername=$1
recreate=$2
labels=$3

if [[ -z "$clustername" ]]; then
echo "Please provide a cluster name"
exit 1
fi

register_argocd_cluster "$clustername" "$recreate"
label_argocd_cluster "$clustername" "$labels"
10 changes: 10 additions & 0 deletions hack/debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

root=$(dirname "${BASH_SOURCE[0]}")

# shellcheck disable=SC1091
source .env.local

bash "$root/redeploy-dev-resources.sh"
dlv --listen=:2345 --headless=true --api-version=2 debug main.go -- --zap-devel=true
7 changes: 5 additions & 2 deletions hack/deploy-test-appset.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/bin/bash
set -ex
set -e

root=$(dirname "${BASH_SOURCE[0]}")

prevcontext=$(kubectl config current-context)
kubectl config use-context kind-argocd-control-plane

# TODO: Make this generate argo apps in all created clusters
kubectl delete --all applications -n argocd
kubectl delete applicationset -n argocd appset-goinfra || echo "Not found"
kubectl apply -f "$root"/dev/test-appset.yml

# TODO: Make this generate argo apps in all created clusters
kubectl create ns infrabin || echo "infrabin already exists"

kubectl config use-context kind-prc-cluster-1
Expand Down
15 changes: 15 additions & 0 deletions hack/deploy-test-progsync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e

root=$(dirname "${BASH_SOURCE[0]}")

prevcontext=$(kubectl config current-context)
kubectl config use-context kind-argocd-control-plane

# Remove finalizer to allow deletion to complete
kubectl patch progressivesync -n argocd goinfra -p '{"metadata":{"finalizers":null}}' --type=merge || echo "Not found. Continuing..."

kubectl delete progressivesync -n argocd goinfra || echo "Not found. Continuing..."
kubectl apply -f "$root"/dev/test-progsync.yml

kubectl config use-context "$prevcontext"
12 changes: 12 additions & 0 deletions hack/dev-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ function register_argocd_cluster() {
kubectl config use-context "$prevcontext"
}

function label_argocd_cluster() {

clustername=$1
labels=$2

prevcontext=$(kubectl config current-context)
kubectl config use-context kind-argocd-control-plane
clustersecret=$(kubectl get secrets -n argocd -l "argocd.argoproj.io/secret-type=cluster" -o json | jq -r ".items[] | select(.metadata.name | test(\"$clustername\")).metadata.name")
kubectl -n argocd label secret "$clustersecret" "$labels" --overwrite
kubectl config use-context "$prevcontext"
}

function local_argocd_login() {

prevcontext=$(kubectl config current-context)
Expand Down
32 changes: 32 additions & 0 deletions hack/dev/test-progsync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: argoproj.skyscanner.net/v1alpha1
kind: ProgressiveSync
metadata:
labels:
argocd.argoproj.io/instance: goinfra
name: goinfra
namespace: argocd
spec:
sourceRef:
apiGroup: argoproj.io/v1alpha1
kind: ApplicationSet
name: appset-goinfra
stages:
- maxParallel: 1
maxTargets: 1
name: one cell as canary in eu-west-1
targets:
clusters:
selector:
matchLabels:
region: eu-west-1
- maxParallel: 2
maxTargets: 100%
name: rollout to remaining cells in every other region
targets:
clusters:
selector:
matchExpressions:
- key: region
operator: NotIn
values:
- eu-west-1
6 changes: 6 additions & 0 deletions hack/kill-debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

# On mac delve refuses to die, so call this script to kill it and the actual go process it spawned
pgrep __debug_bin | xargs kill -SIGINT
pgrep dlv | xargs kill -SIGKILL
9 changes: 9 additions & 0 deletions hack/redeploy-dev-resources.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -e

root=$(dirname "${BASH_SOURCE[0]}")

make install

bash "$root/deploy-test-appset.sh"
bash "$root/deploy-test-progsync.sh"
5 changes: 5 additions & 0 deletions hack/setup-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ kubectl apply -f "$root"/dev/rbac.yml
# Register in-cluster in argo secrets
kubectl apply -f "$root"/dev/control-plane.yml

label_argocd_cluster "cluster-kubernetes.default" "region=eu-central-1"

# Create additional clusters to server as deployment targets
register_argocd_cluster "prc-cluster-1" true
register_argocd_cluster "prc-cluster-2" true

label_argocd_cluster "prc-cluster-1" "region=eu-west-1"
label_argocd_cluster "prc-cluster-2" "region=ap-northeast-1"

local_address=$(local_argocd_login)

# retry_argocd_exec can race with outputting to stdout with the rest of this script
Expand Down

0 comments on commit acc5800

Please sign in to comment.