diff --git a/README.md b/README.md index 4119a77d..379dcbee 100644 --- a/README.md +++ b/README.md @@ -426,6 +426,46 @@ traefik: host_port: 8080 ``` +### Configure docker options for traefik + +We allow users to pass additional docker options to the trafik container like + +```yaml +traefik: + options: + publish: + - 8080:8080 + volumes: + - /tmp/example.json:/tmp/example.json + memory: 512m +``` + +This will start the traefik container with a command like: `docker run ... --volume /tmp/example.json:/tmp/example.json --publish 8080:8080 ` + + +### Configure alternate entrypoints for traefik + +You can configure multiple entrypoints for traefik like so: + +```yaml +service: myservice + +labels: + traefik.tcp.routers.other.rule: 'HostSNI(`*`)' + traefik.tcp.routers.other.entrypoints: otherentrypoint + traefik.tcp.services.other.loadbalancer.server.port: 9000 + traefik.http.routers.myservice.entrypoints: web + traefik.http.services.myservice.loadbalancer.server.port: 8080 + +traefik: + options: + publish: + - 9000:9000 + args: + entrypoints.web.address: ':80' + entrypoints.otherentrypoint.address: ':9000' +``` + ### Configuring build args for new images Build arguments that aren't secret can also be configured: diff --git a/lib/mrsk/commands/traefik.rb b/lib/mrsk/commands/traefik.rb index b9ff19c1..ee86543f 100644 --- a/lib/mrsk/commands/traefik.rb +++ b/lib/mrsk/commands/traefik.rb @@ -10,6 +10,7 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base "--log-opt", "max-size=#{MAX_LOG_SIZE}", "--publish", port, "--volume", "/var/run/docker.sock:/var/run/docker.sock", + *docker_options_args, "traefik", "--providers.docker", "--log.level=DEBUG", @@ -54,6 +55,10 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base end private + def docker_options_args + optionize(config.traefik["options"] || {}) + end + def cmd_option_args if args = config.traefik["args"] optionize args, with: "=" diff --git a/lib/mrsk/utils.rb b/lib/mrsk/utils.rb index 1e763250..b7515c5b 100644 --- a/lib/mrsk/utils.rb +++ b/lib/mrsk/utils.rb @@ -26,14 +26,19 @@ module Mrsk::Utils # Returns a list of shell-dashed option arguments. If the value is true, it's treated like a value-less option. def optionize(args, with: nil) options = if with - args.collect { |(key, value)| value == true ? "--#{key}" : "--#{key}#{with}#{escape_shell_value(value)}" } + flatten_args(args).collect { |(key, value)| value == true ? "--#{key}" : "--#{key}#{with}#{escape_shell_value(value)}" } else - args.collect { |(key, value)| [ "--#{key}", value == true ? nil : escape_shell_value(value) ] } + flatten_args(args).collect { |(key, value)| [ "--#{key}", value == true ? nil : escape_shell_value(value) ] } end options.flatten.compact end + # Flattens a one-to-many structure into an array of two-element arrays each containing a key-value pair + def flatten_args(args) + args.flat_map { |key, value| value.try(:map) { |entry| [key, entry] } || [ [ key, value ] ] } + end + # Copied from SSHKit::Backend::Abstract#redact to be available inside Commands classes def redact(arg) # Used in execute_command to hide redact() args a user passes in arg.to_s.extend(SSHKit::Redaction) # to_s due to our inability to extend Integer, etc diff --git a/test/commands/traefik_test.rb b/test/commands/traefik_test.rb index 5ca51e13..f2b67f91 100644 --- a/test/commands/traefik_test.rb +++ b/test/commands/traefik_test.rb @@ -19,6 +19,39 @@ class CommandsTraefikTest < ActiveSupport::TestCase new_command.run.join(" ") end + test "run with ports configured" do + assert_equal \ + "docker run --name traefik --detach --restart unless-stopped --log-opt max-size=10m --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock traefik --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"", + new_command.run.join(" ") + + @config[:traefik]["options"] = {"publish" => %w[9000:9000 9001:9001]} + assert_equal \ + "docker run --name traefik --detach --restart unless-stopped --log-opt max-size=10m --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --publish \"9000:9000\" --publish \"9001:9001\" traefik --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"", + new_command.run.join(" ") + end + + test "run with volumes configured" do + assert_equal \ + "docker run --name traefik --detach --restart unless-stopped --log-opt max-size=10m --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock traefik --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"", + new_command.run.join(" ") + + @config[:traefik]["options"] = {"volume" => %w[./letsencrypt/acme.json:/letsencrypt/acme.json] } + assert_equal \ + "docker run --name traefik --detach --restart unless-stopped --log-opt max-size=10m --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --volume \"./letsencrypt/acme.json:/letsencrypt/acme.json\" traefik --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"", + new_command.run.join(" ") + end + + test "run with several options configured" do + assert_equal \ + "docker run --name traefik --detach --restart unless-stopped --log-opt max-size=10m --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock traefik --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"", + new_command.run.join(" ") + + @config[:traefik]["options"] = {"volume" => %w[./letsencrypt/acme.json:/letsencrypt/acme.json], "publish" => %w[8080:8080], "memory" => "512m"} + assert_equal \ + "docker run --name traefik --detach --restart unless-stopped --log-opt max-size=10m --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --volume \"./letsencrypt/acme.json:/letsencrypt/acme.json\" --publish \"8080:8080\" --memory \"512m\" traefik --providers.docker --log.level=DEBUG --accesslog.format=\"json\" --api.insecure --metrics.prometheus.buckets=\"0.1,0.3,1.2,5.0\"", + new_command.run.join(" ") + end + test "run without configuration" do @config.delete(:traefik)