From 44b83151e34efb1e73558c1a2bbad09104db875a Mon Sep 17 00:00:00 2001 From: River He Date: Wed, 10 May 2023 03:02:20 +0000 Subject: [PATCH] Allow to inject environment variables to traefik --- lib/mrsk/commands/traefik.rb | 13 ++++++++++++- test/commands/traefik_test.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/mrsk/commands/traefik.rb b/lib/mrsk/commands/traefik.rb index da911cfb..eafe5088 100644 --- a/lib/mrsk/commands/traefik.rb +++ b/lib/mrsk/commands/traefik.rb @@ -1,5 +1,5 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base - delegate :argumentize, :optionize, to: Mrsk::Utils + delegate :argumentize, :argumentize_env_with_secrets, :optionize, to: Mrsk::Utils DEFAULT_IMAGE = "traefik:v2.9" CONTAINER_PORT = 80 @@ -10,6 +10,7 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base "--restart", "unless-stopped", "--publish", port, "--volume", "/var/run/docker.sock:/var/run/docker.sock", + *env_args, *config.logging_args, *label_args, *docker_options_args, @@ -61,6 +62,16 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base argumentize "--label", labels end + def env_args + env_config = config.traefik["env"] || {} + + if env_config.present? + argumentize_env_with_secrets(env_config) + else + [] + end + end + def labels config.traefik["labels"] || [] end diff --git a/test/commands/traefik_test.rb b/test/commands/traefik_test.rb index 8b807658..927f2c80 100644 --- a/test/commands/traefik_test.rb +++ b/test/commands/traefik_test.rb @@ -8,6 +8,12 @@ class CommandsTraefikTest < ActiveSupport::TestCase service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ], traefik: { "image" => @image, "args" => { "accesslog.format" => "json", "api.insecure" => true, "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } } } + + ENV["EXAMPLE_API_KEY"] = "456" + end + + teardown do + ENV.delete("EXAMPLE_API_KEY") end test "run" do @@ -65,6 +71,17 @@ class CommandsTraefikTest < ActiveSupport::TestCase new_command.run.join(" ") end + test "run with env configured" do + assert_equal \ + "docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock --log-opt max-size=\"10m\" #{@image} --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]["env"] = { "secret" => %w[EXAMPLE_API_KEY] } + assert_equal \ + "docker run --name traefik --detach --restart unless-stopped --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock -e EXAMPLE_API_KEY=\"456\" --log-opt max-size=\"10m\" #{@image} --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)