From bbcc90e4d191af86adf8b833c779e270a5162ccc Mon Sep 17 00:00:00 2001 From: Krzysztof Adamski Date: Tue, 5 Sep 2023 10:53:32 +0200 Subject: [PATCH 1/2] Configurable Healthcheck Expose Port --- lib/kamal/commands/healthcheck.rb | 9 ++++++--- lib/kamal/configuration.rb | 2 +- test/commands/healthcheck_test.rb | 3 ++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/kamal/commands/healthcheck.rb b/lib/kamal/commands/healthcheck.rb index d3c949233..4327087dd 100644 --- a/lib/kamal/commands/healthcheck.rb +++ b/lib/kamal/commands/healthcheck.rb @@ -1,5 +1,4 @@ class Kamal::Commands::Healthcheck < Kamal::Commands::Base - EXPOSED_PORT = 3999 def run web = config.role(:web) @@ -7,7 +6,7 @@ def run docker :run, "--detach", "--name", container_name_with_version, - "--publish", "#{EXPOSED_PORT}:#{config.healthcheck["port"]}", + "--publish", "#{exposed_port}:#{config.healthcheck["port"]}", "--label", "service=#{container_name}", "-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"", *web.env_args, @@ -52,6 +51,10 @@ def container_id end def health_url - "http://localhost:#{EXPOSED_PORT}#{config.healthcheck["path"]}" + "http://localhost:#{exposed_port}#{config.healthcheck["path"]}" + end + + def exposed_port + config.healthcheck["exposed_port"] end end diff --git a/lib/kamal/configuration.rb b/lib/kamal/configuration.rb index d6e8d7578..28da91efa 100644 --- a/lib/kamal/configuration.rb +++ b/lib/kamal/configuration.rb @@ -145,7 +145,7 @@ def sshkit def healthcheck - { "path" => "/up", "port" => 3000, "max_attempts" => 7 }.merge(raw_config.healthcheck || {}) + { "path" => "/up", "port" => 3000, "max_attempts" => 7, "exposed_port" => 3999 }.merge(raw_config.healthcheck || {}) end def readiness_delay diff --git a/test/commands/healthcheck_test.rb b/test/commands/healthcheck_test.rb index 4fad7830a..9d47c3136 100644 --- a/test/commands/healthcheck_test.rb +++ b/test/commands/healthcheck_test.rb @@ -40,8 +40,9 @@ class CommandsHealthcheckTest < ActiveSupport::TestCase test "run with custom options" do @config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere" } } } + @config[:healthcheck] = { "exposed_port" => 4999 } assert_equal \ - "docker run --detach --name healthcheck-app-123 --publish 3999:3000 --label service=healthcheck-app -e KAMAL_CONTAINER_NAME=\"healthcheck-app\" --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" --mount \"somewhere\" dhh/app:123", + "docker run --detach --name healthcheck-app-123 --publish 4999:3000 --label service=healthcheck-app -e KAMAL_CONTAINER_NAME=\"healthcheck-app\" --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" --mount \"somewhere\" dhh/app:123", new_command.run.join(" ") end From c2b2f7ea332361c1e8a531123814b49d77a12aad Mon Sep 17 00:00:00 2001 From: Krzysztof Adamski Date: Wed, 6 Sep 2023 10:16:59 +0200 Subject: [PATCH 2/2] Fixing Tests --- test/configuration_test.rb | 2 +- test/integration/main_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 425407e57..89aeaf844 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -263,7 +263,7 @@ class ConfigurationTest < ActiveSupport::TestCase :volume_args=>["--volume", "/local/path:/container/path"], :builder=>{}, :logging=>["--log-opt", "max-size=\"10m\""], - :healthcheck=>{ "path"=>"/up", "port"=>3000, "max_attempts" => 7 }} + :healthcheck=>{ "path"=>"/up", "port"=>3000, "max_attempts" => 7, "exposed_port" => 3999 }} assert_equal expected_config, @config.to_h end diff --git a/test/integration/main_test.rb b/test/integration/main_test.rb index 59d73e8f6..3a6d55875 100644 --- a/test/integration/main_test.rb +++ b/test/integration/main_test.rb @@ -54,6 +54,6 @@ class MainTest < IntegrationTest assert_equal({ user: "root", auth_methods: [ "publickey" ], keepalive: true, keepalive_interval: 30, log_level: :fatal }, config[:ssh_options]) assert_equal({ "multiarch" => false, "args" => { "COMMIT_SHA" => version } }, config[:builder]) assert_equal [ "--log-opt", "max-size=\"10m\"" ], config[:logging] - assert_equal({ "path" => "/up", "port" => 3000, "max_attempts" => 7, "cmd" => "wget -qO- http://localhost > /dev/null" }, config[:healthcheck]) + assert_equal({ "path" => "/up", "port" => 3000, "max_attempts" => 7, "exposed_port" => 3999, "cmd" => "wget -qO- http://localhost > /dev/null" }, config[:healthcheck]) end end