Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/docker/linux/alpine-3.20
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya authored Oct 9, 2024
2 parents a26d480 + 267592b commit 0733e2f
Show file tree
Hide file tree
Showing 21 changed files with 1,984 additions and 958 deletions.
21 changes: 11 additions & 10 deletions .github/workflows/build-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: go build

- name: go-test
run: go test -v ./...
run: go test -race -v ./...

test-windows:
runs-on: windows-2022
Expand Down Expand Up @@ -69,7 +69,7 @@ jobs:

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
uses: docker/metadata-action@70b2cdc6480c1a8b86edf1777157f8f437de2166
with:
images: ${{ env.IMAGE_REPOSITORY }}
tags: |
Expand All @@ -78,7 +78,7 @@ jobs:
suffix=-linux
- name: Login to Docker Hub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand All @@ -95,14 +95,15 @@ jobs:
run: docker buildx inspect

- name: Build and push image
uses: docker/build-push-action@v3
uses: docker/build-push-action@v6
with:
context: .
file: linux/Dockerfile
platforms: linux/amd64,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x,linux/386,linux/arm/v6
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,compression=uncompressed

build-image-windows:
strategy:
Expand All @@ -117,7 +118,7 @@ jobs:

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
uses: docker/metadata-action@70b2cdc6480c1a8b86edf1777157f8f437de2166
with:
images: ${{ env.IMAGE_REPOSITORY }}
tags: |
Expand All @@ -126,7 +127,7 @@ jobs:
suffix=-windows.${{ matrix.os-version }}
- name: Login to Docker Hub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand Down Expand Up @@ -155,14 +156,14 @@ jobs:

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
uses: docker/metadata-action@70b2cdc6480c1a8b86edf1777157f8f437de2166
with:
images: ${{ env.IMAGE_REPOSITORY }}
tags: |
type=sha,format=long
- name: Login to Docker Hub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand Down Expand Up @@ -219,14 +220,14 @@ jobs:

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
uses: docker/metadata-action@70b2cdc6480c1a8b86edf1777157f8f437de2166
with:
images: ${{ env.IMAGE_REPOSITORY }}
tags: |
type=sha,format=long
- name: Login to Docker Hub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- name: golangci-lint
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3
uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6.1.0
with:
version: v1.55.2
args: --timeout=3m
version: v1.60.3
13 changes: 7 additions & 6 deletions .github/workflows/publish-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4

- name: Login to Docker Hub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand All @@ -31,13 +31,14 @@ jobs:
run: docker buildx inspect

- name: Build and push image
uses: docker/build-push-action@v3
uses: docker/build-push-action@v6
with:
context: .
file: linux/Dockerfile
platforms: linux/amd64,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x,linux/386,linux/arm/v6
push: true
tags: ${{ env.IMAGE_REPOSITORY }}:${{ github.event.release.tag_name }}-linux
outputs: type=image,compression=uncompressed

release-windows:
strategy:
Expand All @@ -50,7 +51,7 @@ jobs:
- uses: actions/checkout@v4

- name: Login to Docker Hub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand All @@ -73,7 +74,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Login to Docker Hub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand Down Expand Up @@ -123,12 +124,12 @@ jobs:
check-release:
runs-on: ubuntu-latest
needs:
- publish-multi-arch-image
- release
steps:
- uses: actions/checkout@v4

- name: Login to Docker Hub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
if: github.repository == 'testcontainers/moby-ryuk'
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@fe52e97d262833ae07d05efaf1a239df3f1b5cd4 # v5.15.0
- uses: release-drafter/release-drafter@3f0f87098bd6b5c5b9a36d49c41d998ea58f9348 # v6.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@

vendor/
bin/

# Binary
moby-ryuk

# VS Code
.vscode
81 changes: 81 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
run:
timeout: 2m

linters-settings:
gosec:
excludes:
- G601 ## Implicit memory aliasing of items from a range statement - not possible in go 1.22.
cyclop:
max-complexity: 15
nestif:
min-complexity: 10
govet:
settings:
shadow:
strict: true
enable-all: true
nolintlint:
require-explanation: true
godot:
scope: all
nakedret:
max-func-lines: 0

linters:
enable-all: true
disable:
# Spammy / low value
- nonamedreturns
- varnamelen
- exhaustruct
- nlreturn
- wsl
- lll
- paralleltest
# Duplicate functionality.
- funlen
- gocognit
# Deprecated.
- execinquery
- gomnd
# Good but gets in the way too often.
- testpackage
# Unknown details about how Artemis works are flagged with TODO's.
- godox
# Seems to be broken.
- depguard
# Makes it messy for multiple optional tags.
- tagalign
# Not needed for go 1.22+.
- exportloopref
- errchkjson # Duplicate functionality for errcheck.

issues:
include:
- EXC0012
- EXC0014
exclude-rules:
# Exclude linters which aren't an issue in tests.
- path: _test\.go
linters:
- gochecknoglobals
- wrapcheck

# File mode permissions are fine for constants.
- text: "Magic number: 0o\\d+"
linters:
- mnd

# Field alignment in tests isn't a performance issue.
- text: fieldalignment
path: _test\.go

# Dynamic errors can provide useful context.
- text: "do not define dynamic errors, use wrapped static errors instead:"
linters:
- err113

# Interface casting is fine in mock.
- path: mock_test\.go
linters:
- forcetypeassert
89 changes: 63 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,80 @@
# Moby Ryuk

This project helps you to remove containers/networks/volumes/images by given filter after specified delay.
This project helps you to remove containers, networks, volumes and images by given filter after specified delay.

# Usage
## Building

1. Start it:
To build the binary only run:

$ RYUK_PORT=8080 ./bin/moby-ryuk
$ # You can also run it with Docker
$ docker run -v /var/run/docker.sock:/var/run/docker.sock -e RYUK_PORT=8080 -p 8080:8080 testcontainers/ryuk:0.6.0
```shell
go build
```

1. Connect via TCP:
To build the Linux docker container as the latest tag:

$ nc localhost 8080
```shell
docker build -f linux/Dockerfile -t testcontainers/ryuk:latest .
```

1. Send some filters:
## Usage

label=testing=true&health=unhealthy
ACK
label=something
ACK
To start it using the default settings:

1. Close the connection
```shell
docker run -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 testcontainers/ryuk:latest
```

1. Send more filters with "one-off" style:
If you want to test local changes with the default settings:

printf "label=something_else" | nc localhost 8080
```shell
go run .
```

1. See containers/networks/volumes deleted after 10s:
You can then simulate a connection from testcontainer container using:

2018/01/15 18:38:52 Timed out waiting for connection
2018/01/15 18:38:52 Deleting {"label":{"something":true}}
2018/01/15 18:38:52 Deleting {"label":{"something_else":true}}
2018/01/15 18:38:52 Deleting {"health":{"unhealthy":true},"label":{"testing=true":true}}
2018/01/15 18:38:52 Removed 1 container(s), 0 network(s), 0 volume(s), 0 image(s)
```shell
nc -N localhost 8080 << EOF
label=testing=true&label=testing.sessionid=mysession
label=something
EOF
```

You can send additional session information for monitoring using:

```shell
printf "label=something_else" | nc -N localhost 8080
```

In the ryuk window you'll see containers/networks/volumes deleted after 10s

```log
time=2024-09-30T19:42:30.000+01:00 level=INFO msg=starting connection_timeout=1m0s reconnection_timeout=10s request_timeout=10s shutdown_timeout=10m0s remove_retries=10 retry_offset=-1s port=8080 verbose=false
time=2024-09-30T19:42:30.001+01:00 level=INFO msg=listening address=[::]:8080
time=2024-09-30T19:42:30.001+01:00 level=INFO msg="client processing started"
time=2024-09-30T19:42:38.002+01:00 level=INFO msg="client connected" address=127.0.0.1:56432 clients=1
time=2024-09-30T19:42:38.002+01:00 level=INFO msg="adding filter" type=label values="[testing=true testing.sessionid=mysession]"
time=2024-09-30T19:42:38.002+01:00 level=INFO msg="adding filter" type=label values=[something]
time=2024-09-30T19:42:38.002+01:00 level=INFO msg="client disconnected" address=127.0.0.1:56432 clients=0
time=2024-09-30T19:42:42.047+01:00 level=INFO msg="adding filter" type=label values=[something_else]
time=2024-09-30T19:42:42.047+01:00 level=INFO msg="client connected" address=127.0.0.1:56434 clients=1
time=2024-09-30T19:42:42.047+01:00 level=INFO msg="client disconnected" address=127.0.0.1:56434 clients=0
time=2024-09-30T19:42:52.051+01:00 level=INFO msg="prune check" clients=0
time=2024-09-30T19:42:52.216+01:00 level=INFO msg="client processing stopped"
time=2024-09-30T19:42:52.216+01:00 level=INFO msg=removed containers=0 networks=0 volumes=0 images=0
time=2024-09-30T19:42:52.216+01:00 level=INFO msg=done
```

## Ryuk configuration

- `RYUK_CONNECTION_TIMEOUT` - Environment variable that defines the timeout for Ryuk to receive the first connection (default: 60s). Value layout is described in [time.ParseDuration](https://golang.org/pkg/time/#ParseDuration) documentation.
- `RYUK_PORT` - Environment variable that defines the port where Ryuk will be bound to (default: 8080).
- `RYUK_RECONNECTION_TIMEOUT` - Environment variable that defines the timeout for Ryuk to reconnect to Docker (default: 10s). Value layout is described in [time.ParseDuration](https://golang.org/pkg/time/#ParseDuration) documentation.
- `RYUK_VERBOSE` - Environment variable that defines if Ryuk should print debug logs (default: false).
The following environment variables can be configured to change the behaviour:

| Environment Variable | Default | Format | Description |
| --------------------------- | ------- | ------- | ------------ |
| `RYUK_CONNECTION_TIMEOUT` | `60s` | [Duration](https://golang.org/pkg/time/#ParseDuration) | The duration without receiving any connections which will trigger a shutdown |
| `RYUK_PORT` | `8080` | `uint16` | The port to listen on for connections |
| `RYUK_RECONNECTION_TIMEOUT` | `10s` | [Duration](https://golang.org/pkg/time/#ParseDuration) | The duration after the last connection closes which will trigger resource clean up and shutdown |
| `RYUK_REQUEST_TIMEOUT` | `10s` | [Duration](https://golang.org/pkg/time/#ParseDuration) | The timeout for any Docker requests |
| `RYUK_REMOVE_RETRIES` | `10` | `int` | The number of times to retry removing a resource |
| `RYUK_RETRY_OFFSET` | `-1s` | [Duration](https://golang.org/pkg/time/#ParseDuration) | The offset added to the start time of the prune pass that is used as the minimum resource creation time. Any resource created after this calculated time will trigger a retry to ensure in use resources are not removed |
| `RYUK_VERBOSE` | `false` | `bool` | Whether to enable verbose aka debug logging |
| `RYUK_SHUTDOWN_TIMEOUT` | `10m` | [Duration](https://golang.org/pkg/time/#ParseDuration) | The duration after shutdown has been requested when the remaining connections are ignored and prune checks start |
Loading

0 comments on commit 0733e2f

Please sign in to comment.