Skip to content

Commit

Permalink
feat: set ryuk port using the environment variable (#64)
Browse files Browse the repository at this point in the history
* feat: set Ryuk port from the environment

* chore: deprecate the usage of the port flag

If the env var is present, it will take precedence over the flag

* feat: support configuring reconnection timeout from the environment

* docs: document the new ways to configure Ryuk

* docs: update usage

* fix: use Docker Hub
  • Loading branch information
mdelapenya authored Mar 10, 2023
1 parent 078eae9 commit 5ab583f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ This project helps you to remove containers/networks/volumes/images by given fil

1. Start it:

$ ./bin/moby-ryuk -p 8080
$ RYUK_PORT=8080 ./bin/moby-ryuk
$ # You can also run it with Docker
$ docker run -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 quay.io/testcontainers/ryuk
$ docker run -v /var/run/docker.sock:/var/run/docker.sock -e RYUK_PORT=8080 -p 8080:8080 docker.io/testcontainers/ryuk

1. Connect via TCP:

Expand Down Expand Up @@ -38,3 +38,5 @@ This project helps you to remove containers/networks/volumes/images by given fil
## 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.
28 changes: 25 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
Expand All @@ -22,8 +23,10 @@ import (
)

const (
connectionTimeoutEnv string = "RYUK_CONNECTION_TIMEOUT"
ryukLabel string = "org.testcontainers.ryuk"
connectionTimeoutEnv string = "RYUK_CONNECTION_TIMEOUT"
portEnv string = "RYUK_PORT"
reconnectionTimeoutEnv string = "RYUK_RECONNECTION_TIMEOUT"
ryukLabel string = "org.testcontainers.ryuk"
)

var (
Expand All @@ -43,14 +46,15 @@ type config struct {
// while parsing RYUK_CONNECTION_TIMEOUT the error is returned.
func newConfig(args []string) (*config, error) {
cfg := config{
Port: 8080,
ConnectionTimeout: 60 * time.Second,
ReconnectionTimeout: 10 * time.Second,
}

fs := flag.NewFlagSet("ryuk", flag.ExitOnError)
fs.SetOutput(os.Stdout)

fs.IntVar(&cfg.Port, "p", 8080, "Port to bind at")
fs.IntVar(&cfg.Port, "p", 8080, "Deprecated: please use the "+portEnv+" environment variable to set the port to bind at")

err := fs.Parse(args)
if err != nil {
Expand All @@ -66,6 +70,24 @@ func newConfig(args []string) (*config, error) {
cfg.ConnectionTimeout = parsedTimeout
}

if port, ok := os.LookupEnv(portEnv); ok {
parsedPort, err := strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("failed to parse \"%s\": %s", portEnv, err)
}

cfg.Port = parsedPort
}

if timeout, ok := os.LookupEnv(reconnectionTimeoutEnv); ok {
parsedTimeout, err := time.ParseDuration(timeout)
if err != nil {
return nil, fmt.Errorf("failed to parse \"%s\": %s", reconnectionTimeoutEnv, err)
}

cfg.ReconnectionTimeout = parsedTimeout
}

return &cfg, nil
}

Expand Down
46 changes: 43 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,25 +313,65 @@ func TestPrune(t *testing.T) {
}

func Test_newConfig(t *testing.T) {
t.Run("should return an error when failing to parse the environment variable", func(t *testing.T) {
t.Run("should return an error when failing to parse RYUK_CONNECTION_TIMEOUT environment variable", func(t *testing.T) {
t.Setenv(connectionTimeoutEnv, "bad_value")

config, err := newConfig([]string{})
require.NotNil(t, err)
require.Nil(t, config)
})

t.Run("should set connectionTimeout with the environment variable", func(t *testing.T) {
t.Run("should set connectionTimeout with RYUK_CONNECTION_TIMEOUT environment variable", func(t *testing.T) {
t.Setenv(connectionTimeoutEnv, "10s")

config, err := newConfig([]string{})
require.Nil(t, err)
assert.Equal(t, 10*time.Second, config.ConnectionTimeout)
})

t.Run("should set port", func(t *testing.T) {
t.Run("should return an error when failing to parse RYUK_PORT environment variable", func(t *testing.T) {
t.Setenv(portEnv, "bad_value")

config, err := newConfig([]string{})
require.NotNil(t, err)
require.Nil(t, config)
})

t.Run("should set connectionTimeout with RYUK_PORT environment variable", func(t *testing.T) {
t.Setenv(portEnv, "8081")

config, err := newConfig([]string{})
require.Nil(t, err)
assert.Equal(t, 8081, config.Port)
})

t.Run("should return an error when failing to parse RYUK_RECONNECTION_TIMEOUT environment variable", func(t *testing.T) {
t.Setenv(reconnectionTimeoutEnv, "bad_value")

config, err := newConfig([]string{})
require.NotNil(t, err)
require.Nil(t, config)
})

t.Run("should set connectionTimeout with RYUK_RECONNECTION_TIMEOUT environment variable", func(t *testing.T) {
t.Setenv(reconnectionTimeoutEnv, "100s")

config, err := newConfig([]string{})
require.Nil(t, err)
assert.Equal(t, 100*time.Second, config.ReconnectionTimeout)
})

t.Run("should set port with port flag", func(t *testing.T) {
config, err := newConfig([]string{"-p", "3000"})
require.Nil(t, err)
assert.Equal(t, 3000, config.Port)
})

t.Run("should set port from env with port flag and RYUK_PORT environment variable", func(t *testing.T) {
t.Setenv(portEnv, "8081")

config, err := newConfig([]string{"-p", "3000"})
require.Nil(t, err)
assert.Equal(t, 8081, config.Port)
})
}

0 comments on commit 5ab583f

Please sign in to comment.