Skip to content

Commit

Permalink
Add debugging commands into taskfiles, update makefile, dockerfile, g…
Browse files Browse the repository at this point in the history
…oreleaser
  • Loading branch information
Tit Petric authored and titpetric committed May 23, 2024
1 parent e1ea94a commit 149349f
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 12 deletions.
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM debian:bookworm AS assets
# If you need to tweak the environment for testing, you can override the
# `GO_VERSION` and `PYTHON_VERSION` as docker build arguments.

ARG GO_VERSION=1.21.4
ARG GO_VERSION=1.22.3
ARG PYTHON_VERSION=3.11.6

WORKDIR /assets
Expand All @@ -16,7 +16,7 @@ RUN apt update && apt install wget -y && \

FROM debian:bookworm

ARG GO_VERSION=1.21.4
ARG GO_VERSION=1.22.3
ARG PYTHON_VERSION=3.11.6

COPY --from=assets /assets/ /tmp/
Expand All @@ -31,7 +31,7 @@ RUN tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz && \

# Build essentials

RUN apt update && apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl wget libbz2-dev -y
RUN apt update && apt install git build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl wget libbz2-dev -y

# Install $PYTHON_VERSION

Expand Down Expand Up @@ -62,6 +62,8 @@ ADD go.mod go.sum /opt/tyk-gateway/
RUN go mod download
ADD . /opt/tyk-gateway

RUN git config --global --add safe.directory /opt/tyk-gateway

RUN make build

COPY tyk.conf.example tyk.conf
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GOINSTALL=$(GOCMD) install
BINARY_NAME=tyk
BINARY_LINUX=tyk
BUILD_PLATFORM=linux/amd64
TAGS=coprocess grpc goplugin
TAGS=goplugin
CONF=tyk.conf

TEST_REGEX=.
Expand Down
32 changes: 32 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,35 @@ tasks:
yq -i ".dockers[0].extra_files |= [\"$replacement\"]" ci/goreleaser/goreleaser-el7.yml
yq -i ".dockers[4].extra_files |= [\"$replacement\"]" ci/goreleaser/goreleaser.yml
- echo goreleaser release --clean --snapshot -f ci/goreleaser/goreleaser.yml

test:build:
desc: "Build with go 1.22"
vars:
buildDir: /src
cmds:
- |
docker run --rm \
-v $PWD:{{.buildDir}} \
-v /var/run/docker.sock:/var/run/docker.sock \
-w {{.buildDir}} \
-v /cache:/cache \
-e GOMODCACHE=/cache/mod \
-e GOCACHE=/cache/go-build \
tykio/golang-cross:1.22-bookworm \
bash -c "git config --global --add safe.directory {{.buildDir}}; goreleaser --clean -f ci/goreleaser/goreleaser.yml --snapshot --skip=sign"
test:build-binary:
desc: "Build with go 1.22"
vars:
buildDir: /src
cmds:
- |
docker run --rm \
-v $PWD:{{.buildDir}} \
-v /var/run/docker.sock:/var/run/docker.sock \
-w {{.buildDir}} \
-v /cache:/cache \
-e GOMODCACHE=/cache/mod \
-e GOCACHE=/cache/go-build \
tykio/golang-cross:1.22-bookworm \
bash -c "git config --global --add safe.directory {{.buildDir}}; goreleaser build --single-target --debug --clean -f ci/goreleaser/goreleaser.yml --snapshot"
4 changes: 4 additions & 0 deletions ci/goreleaser/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
builds:
- id: std-linux
flags:
- -race
- -trimpath
- -tags=goplugin
- -buildvcs=false
- -gcflags
- -l -N
ldflags:
- -X github.com/TykTechnologies/tyk/internal/build.Version={{.Version}}
- -X github.com/TykTechnologies/tyk/internal/build.Commit={{.FullCommit}}
Expand Down
80 changes: 77 additions & 3 deletions ci/tests/tracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ This docker-compose file defines a multi-service application including the follo
- `redis`: This service runs another Redis server (version 4.0, based on Alpine Linux). The Redis server is configured to use an append-only file for data persistence.
- `httpbin`: This service runs an HTTP Request & Response Service, which is a simple HTTP server for testing and debugging.



- `otel-collector`: This service runs an instance of the OpenTelemetry Collector (version 0.80.0). It is configured using the file /otel-local-config.yml, which is mounted from the local file ./tracetest/collector.config.yml.

- `tracetest`: This service runs an application from the Docker image kubeshop/tracetest:v0.11.16. It depends on the otel-collector and postgres services. It uses a provisioning file located at `/configs/tracetest/tracetest-provision.yml` and a configuration file located at `/configs/tracetest/tracetest.yml`.

It needs the following services to work:

- `postgres`: This service runs a PostgreSQL database (version 14). It uses environment variables to set the PostgreSQL user and password. The health of the PostgreSQL service is checked by running the pg_isready command, which checks the connection to the database server.
Expand All @@ -23,4 +22,79 @@ It needs the following services to work:


The `tests` folder contains all the `*.yml` declarations of tests that are going to be executed. Please take into consideration:
- the target URL for gateway APIs is `http://httpbin:80/` for HTTP APIs

- the target URL for gateway APIs is `http://httpbin:80/` for HTTP APIs

# Running the tests

The following should be used for general testing:

- Build `internal/tyk-gateway` in the project root by running `make docker`
- Run `task` in this folder to run tests against the internal image
- You are able to override the docker image by setting `GATEWAY_IMAGE=<your image> task`.

# Debugging with delve

To install and use the delve debugger, run `task install:delve` with the
following parameters that configure the docker images:

- `task install:delve input=<source-image> output=<destination-image>`

If the `output` parameter is omitted, `internal/tyk-gateway` image will
be created. The command installs delve locally and then copies it into
the final image.

```
# task install:delve input=tykio/tyk-gateway:v5.3.1
task: [install:delve] go install github.com/go-delve/delve/cmd/dlv@latest
task: [install:delve] docker create --name tc tykio/tyk-gateway:v5.3.1
aa57b10afaa6f649c54958fb711830dffca0581100c030636b204a5b932eba66
task: [install:delve] docker cp $GOBIN/dlv tc:/usr/bin/dlv
Successfully copied 19.6MB to tc:/usr/bin/dlv
task: [install:delve] docker cp tc:/opt/tyk-gateway/tyk ./tyk-debug
Successfully copied 98.2MB to /root/tyk/tyk/ci/tests/tracing/tyk-debug
task: [install:delve] docker commit tc internal/tyk-gateway
sha256:8a6be7a091dd3dab140a8514e223bc231abb4f28ab7f99e839235632305f14f1
task: [install:delve] docker rm -f tc
tc
```

The task will copy out the gateway binary from the input image into `./tyk-debug`.
The binary can be used for further inspection, e.g. `go version -m tyk-debug`.

As shown, it will create `internal/tyk-gateway` image which is the default
used for the tests. If you want to create a different image, you may
pass the `output` parameter.

To use the produced image:

- `task debug=true` will run the default image with delve, use `GATEWAY_IMAGE` if you need to adjust it,
- in another terminal run `dlv connect localhost:2345` and hit `c` to continue.

Passing `debug=true` uses the `docker-compose.debug.yml` taskfile which invokes gateway over delve.

```diff
--- docker-compose.yml 2024-05-23 12:43:32.497847236 +0200
+++ docker-compose.debug.yml 2024-05-23 12:43:24.025868887 +0200
@@ -93,12 +93,20 @@
platform: linux/amd64
ports:
- 9000:8080
+ - 2345:2345
env_file:
- ./configs/tyk.env
volumes:
- ./apps:/opt/tyk-gateway/apps
depends_on:
- redis
+ entrypoint: dlv
+ command:
+ - exec
+ - /opt/tyk-gateway/tyk
+ - --listen=:2345
+ - --api-version=2
+ - --headless

tyk-checker:
platform: linux/amd64
```
27 changes: 22 additions & 5 deletions ci/tests/tracing/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ version: "3"
includes:
tracetest: ./tracetest/Taskfile.yml

vars:
debug: "false"
output: internal/tyk-gateway

tasks:
default:
desc: "setup, execute and shutdown e2e opentelemetry tests"
env:
COMPOSE_FILE: '{{if eq .debug "true"}}docker-compose.debug.yml{{else}}docker-compose.yml{{end}}'
cmds:
- defer:
task: teardown
- task: setup
- task: info
- task: test
- defer: task teardown
- task setup
- task info
- task test

info:
desc: "Print pre-flight info"
Expand Down Expand Up @@ -53,3 +58,15 @@ tasks:
desc: "teardown e2e opentelemetry tests enviroment"
cmds:
- docker compose down --remove-orphans

install:delve:
desc: "produce debug image with delve, extract binary"
requires:
vars: [ input ]
cmds:
- go install github.com/go-delve/delve/cmd/dlv@latest
- defer: docker rm -f tc
- docker create --name tc {{.input}}
- docker cp $GOBIN/dlv tc:/usr/bin/dlv
- docker cp tc:/opt/tyk-gateway/tyk ./tyk-debug
- docker commit tc {{.output}}
137 changes: 137 additions & 0 deletions ci/tests/tracing/docker-compose.debug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# yamllint disable rule:line-length
---
services:
cache:
image: redis:6
platform: linux/amd64
healthcheck:
test:
- CMD
- redis-cli
- ping
timeout: 3s
interval: 1s
retries: 60
restart: unless-stopped

otel-collector:
image: otel/opentelemetry-collector-contrib:0.100.0
platform: linux/amd64
command:
- --config
- /otel-local-config.yml
environment:
TRACETEST_ENDPOINT: tracetest:4317
ports:
- 4317:4317
- 4318:4318
volumes:
- ./configs/otelcollector/collector.config.yml:/otel-local-config.yml

postgres:
image: postgres:14
platform: linux/amd64
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
healthcheck:
test:
- CMD-SHELL
- pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"
timeout: 5s
interval: 1s
retries: 60

queue:
image: rabbitmq:3.8-management
platform: linux/amd64
healthcheck:
test:
- CMD-SHELL
- rabbitmq-diagnostics -q check_running
timeout: 5s
interval: 1s
retries: 60
restart: unless-stopped

tracetest:
image: kubeshop/tracetest:v1.0.0
platform: linux/amd64
command:
- --provisioning-file
- /app/provision.yml
depends_on:
tyk-checker:
condition: service_healthy
otel-collector:
condition: service_started
postgres:
condition: service_healthy
environment:
TRACETEST_DEV: true
healthcheck:
test:
- CMD
- wget
- --spider
- localhost:11633
timeout: 3s
interval: 1s
retries: 60
ports:
- 11633:11633
volumes:
- type: bind
source: ./configs/tracetest/tracetest.yml
target: /app/tracetest.yml
- type: bind
source: ./configs/tracetest/tracetest-provision.yml
target: /app/provision.yml

tyk:
image: ${GATEWAY_IMAGE:-internal/tyk-gateway}
platform: linux/amd64
ports:
- 9000:8080
- 2345:2345
env_file:
- ./configs/tyk.env
volumes:
- ./apps:/opt/tyk-gateway/apps
depends_on:
- redis
entrypoint: dlv
command:
- exec
- /opt/tyk-gateway/tyk
- --listen=:2345
- --api-version=2
- --headless

tyk-checker:
platform: linux/amd64
image: badouralix/curl-jq
command: tail -f /dev/null
depends_on:
- tyk
healthcheck:
test: curl -s --fail http://tyk:8080/hello | jq --exit-status -n 'inputs | if has("status") then .status=="pass" else false end' > /dev/null || exit 1
interval: 5s
retries: 10
start_period: 4s
timeout: 10s

redis:
platform: linux/amd64
image: redis:6
command: redis-server --appendonly yes

httpbin:
platform: linux/amd64
image: kennethreitz/httpbin:latest

grpcapi:
platform: linux/amd64
image: romk/grpc-helloworld-reflection
ports:
- 50001:50051

0 comments on commit 149349f

Please sign in to comment.