Skip to content

Commit

Permalink
Allow to inject environment variables to traefik
Browse files Browse the repository at this point in the history
  • Loading branch information
Novtopro committed May 10, 2023
1 parent 0defcbb commit 44b8315
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/mrsk/commands/traefik.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +10,7 @@ def run
"--restart", "unless-stopped",
"--publish", port,
"--volume", "/var/run/docker.sock:/var/run/docker.sock",
*env_args,
*config.logging_args,
*label_args,
*docker_options_args,
Expand Down Expand Up @@ -61,6 +62,16 @@ def label_args
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
Expand Down
17 changes: 17 additions & 0 deletions test/commands/traefik_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 44b8315

Please sign in to comment.