-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: fix
make clean
and refactor (#82)
* ci: fix `make clean` and refactor The integration cleanup script (`.ci/clean_kong.sh`) had a couple warts: * the `--filter Name=<name>` arg performs substring matching, so if you have an unrelated container named `kong-foo` running, it would enter the wrong branch and throw an error * manually tracking docker resources for cleanup gets pretty tedious I updated it to filter resources based on a known docker label instead. This involved touching a few different files to add docker labels and such, so I wound up DRY-ing up some of the CI setup scripts along the way. * Apply suggestions from code review Put some bash vars in brackets Co-authored-by: Patryk Małek <[email protected]> --------- Co-authored-by: Patryk Małek <[email protected]>
- Loading branch information
Showing
4 changed files
with
147 additions
and
147 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,29 +1,23 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
set -euo pipefail | ||
|
||
NETWORK_NAME=deck-test | ||
source ./.ci/lib.sh | ||
|
||
PG_CONTAINER_NAME=pg | ||
GATEWAY_CONTAINER_NAME=kong | ||
cleanup() { | ||
local -r resource=${1?resource type required} | ||
shift | ||
|
||
if [[ $(docker ps -a --filter Name=${GATEWAY_CONTAINER_NAME}) != "" ]]; then | ||
echo "remove container ${GATEWAY_CONTAINER_NAME}" | ||
docker rm -f ${GATEWAY_CONTAINER_NAME} | ||
else | ||
echo "container ${GATEWAY_CONTAINER_NAME} not found, skip removing" | ||
fi | ||
docker "$resource" ls "$@" \ | ||
--filter "label=$DOCKER_LABEL" \ | ||
--quiet \ | ||
| while read -r id; do | ||
docker "$resource" rm \ | ||
--force \ | ||
"$id" | ||
done | ||
} | ||
|
||
if [[ $(docker ps -a --filter Name=${PG_CONTAINER_NAME}) != "" ]]; then | ||
echo "remove container ${PG_CONTAINER_NAME}" | ||
docker rm -f ${PG_CONTAINER_NAME} | ||
else | ||
echo "container ${PG_CONTAINER_NAME} not found, skip removing" | ||
fi | ||
|
||
if [[ $(docker network ls --filter Name=${NETWORK_NAME}) != "" ]]; then | ||
echo "remove docker network ${NETWORK_NAME}" | ||
docker network rm ${NETWORK_NAME} | ||
else | ||
echo "docker network ${NETWORK_NAME} does not exist, skip removing" | ||
fi | ||
cleanup container --all | ||
cleanup volume | ||
cleanup network |
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,82 @@ | ||
set -euo pipefail | ||
|
||
readonly NETWORK_NAME=go-db-reconciler-test | ||
readonly DOCKER_LABEL=com.konghq.test.go-database-reconciler=1 | ||
readonly KONG_PG_HOST=kong-postgres | ||
readonly KONG_PG_USER=kong | ||
readonly KONG_PG_DATABASE=kong | ||
readonly KONG_PG_PASSWORD=kong | ||
readonly KONG_PG_PORT=5432 | ||
|
||
readonly DOCKER_ARGS=( | ||
--label "$DOCKER_LABEL" | ||
--network "$NETWORK_NAME" | ||
-e "KONG_DATABASE=postgres" | ||
-e "KONG_PG_HOST=$KONG_PG_HOST" | ||
-e "KONG_PG_PORT=$KONG_PG_PORT" | ||
-e "KONG_PG_USER=$KONG_PG_USER" | ||
-e "KONG_PG_DATABASE=$KONG_PG_DATABASE" | ||
-e "KONG_PG_PASSWORD=$KONG_PG_PASSWORD" | ||
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" | ||
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" | ||
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" | ||
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" | ||
-e "KONG_LOG_LEVEL=${KONG_LOG_LEVEL:-notice}" | ||
) | ||
|
||
waitContainer() { | ||
local -r container=$1 | ||
shift | ||
|
||
for _ in {1..100}; do | ||
echo "waiting for $container" | ||
if docker exec \ | ||
--user root \ | ||
"$container" \ | ||
"$@" | ||
then | ||
return | ||
fi | ||
sleep 0.2 | ||
done | ||
|
||
echo "FATAL: failed waiting for $container" | ||
exit 1 | ||
} | ||
|
||
initNetwork() { | ||
docker network create \ | ||
--label "$DOCKER_LABEL" \ | ||
"$NETWORK_NAME" | ||
} | ||
|
||
initDb() { | ||
docker run \ | ||
--rm \ | ||
--detach \ | ||
--name "$KONG_PG_HOST" \ | ||
--label "$DOCKER_LABEL" \ | ||
--network $NETWORK_NAME \ | ||
-p "${KONG_PG_PORT}:${KONG_PG_PORT}" \ | ||
-e "POSTGRES_USER=$KONG_PG_USER" \ | ||
-e "POSTGRES_DB=$KONG_PG_DATABASE" \ | ||
-e "POSTGRES_PASSWORD=$KONG_PG_PASSWORD" \ | ||
postgres:9.6 | ||
|
||
waitContainer "$KONG_PG_HOST" pg_isready | ||
} | ||
|
||
initMigrations() { | ||
local -r image=$1 | ||
shift | ||
|
||
docker run \ | ||
--rm \ | ||
"${DOCKER_ARGS[@]}" \ | ||
"$@" \ | ||
"$image" \ | ||
kong migrations bootstrap \ | ||
--yes \ | ||
--force \ | ||
--db-timeout 30 | ||
} |
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 |
---|---|---|
@@ -1,65 +1,27 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
set -euo pipefail | ||
|
||
KONG_IMAGE=${KONG_IMAGE} | ||
NETWORK_NAME=deck-test | ||
source ./.ci/lib.sh | ||
|
||
PG_CONTAINER_NAME=pg | ||
DATABASE_USER=kong | ||
DATABASE_NAME=kong | ||
KONG_DB_PASSWORD=kong | ||
KONG_PG_HOST=pg | ||
KONG_IMAGE=${KONG_IMAGE?KONG_IMAGE is required to be set} | ||
|
||
GATEWAY_CONTAINER_NAME=kong | ||
|
||
waitContainer() { | ||
for try in {1..100}; do | ||
echo "waiting for $1" | ||
docker exec --user root $2 $3 && break; | ||
sleep 0.2 | ||
done | ||
} | ||
|
||
# create docker network | ||
docker network create $NETWORK_NAME | ||
|
||
# Start a PostgreSQL container | ||
docker run --rm -d --name $PG_CONTAINER_NAME \ | ||
--network=$NETWORK_NAME \ | ||
-p 5432:5432 \ | ||
-e "POSTGRES_USER=$DATABASE_USER" \ | ||
-e "POSTGRES_DB=$DATABASE_NAME" \ | ||
-e "POSTGRES_PASSWORD=$KONG_DB_PASSWORD" \ | ||
postgres:9.6 | ||
initNetwork | ||
initDb | ||
initMigrations "$KONG_IMAGE" | ||
|
||
waitContainer "PostGres" $PG_CONTAINER_NAME pg_isready | ||
|
||
# Prepare the Kong database | ||
docker run --rm --network=$NETWORK_NAME \ | ||
-e "KONG_DATABASE=postgres" \ | ||
-e "KONG_PG_HOST=$KONG_PG_HOST" \ | ||
-e "KONG_PG_PASSWORD=$KONG_DB_PASSWORD" \ | ||
-e "KONG_PASSWORD=$KONG_DB_PASSWORD" \ | ||
$KONG_IMAGE kong migrations bootstrap | ||
GATEWAY_CONTAINER_NAME=kong | ||
|
||
# Start Kong Gateway | ||
docker run -d --name $GATEWAY_CONTAINER_NAME \ | ||
--network=$NETWORK_NAME \ | ||
-e "KONG_DATABASE=postgres" \ | ||
-e "KONG_PG_HOST=$KONG_PG_HOST" \ | ||
-e "KONG_PG_USER=$DATABASE_USER" \ | ||
-e "KONG_PG_PASSWORD=$KONG_DB_PASSWORD" \ | ||
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ | ||
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \ | ||
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \ | ||
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \ | ||
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \ | ||
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \ | ||
-p 8000:8000 \ | ||
-p 8443:8443 \ | ||
-p 127.0.0.1:8001:8001 \ | ||
-p 127.0.0.1:8444:8444 \ | ||
$KONG_IMAGE | ||
|
||
waitContainer "Kong" $GATEWAY_CONTAINER_NAME "kong health" | ||
docker run \ | ||
--detach \ | ||
--name $GATEWAY_CONTAINER_NAME \ | ||
"${DOCKER_ARGS[@]}" \ | ||
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \ | ||
-p 8000:8000 \ | ||
-p 8443:8443 \ | ||
-p 127.0.0.1:8001:8001 \ | ||
-p 127.0.0.1:8444:8444 \ | ||
"$KONG_IMAGE" | ||
|
||
waitContainer "$GATEWAY_CONTAINER_NAME" kong health |
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