From d0e2a1a494b7c772b97b37929d6fccacbd03c22d Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Thu, 4 Jul 2024 16:39:43 -0300 Subject: [PATCH] Support load balancing in caddy --- Caddyfile | 10 +++++---- Dockerfile | 1 - src/daemon/proxyconf.cr | 47 ++++++++++++++++++++++++----------------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/Caddyfile b/Caddyfile index eb450ae..2c16579 100644 --- a/Caddyfile +++ b/Caddyfile @@ -1,13 +1,15 @@ { - https_port 8888 - http_port 8887 + http_port 8888 + https_port 8887 + local_certs + admin off } -localhost:8888 { +http://localhost:8888 { handle_path /admin/terminal/* { reverse_proxy /* http://127.0.0.1:7681 } handle_path /admin/* { reverse_proxy /* http://127.0.0.1:3000 } -} \ No newline at end of file +} diff --git a/Dockerfile b/Dockerfile index 15575bc..6dc8b45 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,5 @@ COPY Caddyfile ./ COPY --from=build /home/app/bin/faaso-daemon /home/app/bin/faaso /usr/bin/ RUN mkdir /secrets -RUN echo "sarasa" > /secrets/sarlanga CMD ["/usr/bin/multirun", "-v", "faaso-daemon", "caddy run --config Caddyfile"] diff --git a/src/daemon/proxyconf.cr b/src/daemon/proxyconf.cr index 01e14c2..b2621aa 100644 --- a/src/daemon/proxyconf.cr +++ b/src/daemon/proxyconf.cr @@ -20,37 +20,46 @@ module Proxy def self.update_proxy_config docker_api = Docr::API.new(Docr::Client.new) containers = docker_api.containers.list(all: true) - - funkos = [] of String - containers.each { |container| - names = container.names.select &.starts_with? "/faaso-" - next if names.empty? - funkos << names[0][7..] - } - funkos.sort! - - config = %( + config = <<-CONFIG { - https_port 8888 - http_port 8887 + http_port 8888 + https_port 8887 local_certs + admin off } -localhost:8888 { +http://localhost:8888 { handle_path /admin/terminal/* { reverse_proxy /* http://127.0.0.1:7681 } handle_path /admin/* { reverse_proxy /* http://127.0.0.1:3000 } -) + funkos.map { |funko| %( - handle_path /faaso/#{funko.split("-")[0]}/* { - reverse_proxy /* http://#{funko}:3000 - } -) }.join("\n") + "}" - # FIXME that 👆🏼 is not a functional load balancing config if scale > 1 + + +CONFIG + + funkos = Funko::Funko.from_docker + funkos.each do |funko| + next if funko.name == "proxy" + containers = funko.containers + next if containers.empty? + funko_urls = containers.map { |container| + "http://#{container.names[0].lstrip("/")}:3000" + } + config += %( + handle_path /faaso/#{funko.name}/* { + reverse_proxy /* #{funko_urls.join(" ")} { + health_uri /ping + fail_duration 30s + } + } + ) + end + config += "\n}" if @@current_config != config + Log.info { "Updating proxy config" } File.open("Caddyfile", "w") do |file| file << config end