Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to override network #1020

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/kamal/commands/accessory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Kamal::Commands::Accessory < Kamal::Commands::Base
attr_reader :accessory_config
delegate :service_name, :image, :hosts, :port, :files, :directories, :cmd,
:publish_args, :env_args, :volume_args, :label_args, :option_args,
:network_args, :publish_args, :env_args, :volume_args, :label_args, :option_args,
:secrets_io, :secrets_path, :env_directory,
to: :accessory_config

Expand All @@ -15,7 +15,7 @@ def run
"--name", service_name,
"--detach",
"--restart", "unless-stopped",
"--network", "kamal",
*network_args,
*config.logging_args,
*publish_args,
*env_args,
Expand Down Expand Up @@ -64,7 +64,7 @@ def execute_in_new_container(*command, interactive: false)
docker :run,
("-it" if interactive),
"--rm",
"--network", "kamal",
*network_args,
*env_args,
*volume_args,
image,
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/commands/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def run(hostname: nil)
"--detach",
"--restart unless-stopped",
"--name", container_name,
"--network", "kamal",
*config.network_args,
*([ "--hostname", hostname ] if hostname),
"-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"",
"-e", "KAMAL_VERSION=\"#{config.version}\"",
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/commands/app/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def execute_in_new_container(*command, interactive: false, env:)
docker :run,
("-it" if interactive),
"--rm",
"--network", "kamal",
*config.network_args,
*role&.env_args(host),
*argumentize("--env", env),
*config.volume_args,
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/commands/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Kamal::Commands::Proxy < Kamal::Commands::Base
def run
docker :run,
"--name", container_name,
"--network", "kamal",
*config.network_args,
"--detach",
"--restart", "unless-stopped",
"--volume", "kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy",
Expand Down
9 changes: 9 additions & 0 deletions lib/kamal/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Kamal::Configuration
PROXY_MINIMUM_VERSION = "v0.7.0"
PROXY_HTTP_PORT = 80
PROXY_HTTPS_PORT = 443
NETWORK = "kamal"

class << self
def create_from(config_file:, destination: nil, version: nil)
Expand Down Expand Up @@ -190,6 +191,10 @@ def logging_args
logging.args
end

def network_args
argumentize "--network", network
end


def readiness_delay
raw_config.readiness_delay || 7
Expand Down Expand Up @@ -290,6 +295,10 @@ def to_h
end

private
def network
raw_config["network"] || NETWORK
end

# Will raise ArgumentError if any required config keys are missing
def ensure_destination_if_required
if require_destination? && destination.nil?
Expand Down
10 changes: 10 additions & 0 deletions lib/kamal/configuration/accessory.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Kamal::Configuration::Accessory
include Kamal::Configuration::Validation

NETWORK = "kamal"

delegate :argumentize, :optionize, to: Kamal::Utils

attr_reader :name, :accessory_config, :env
Expand Down Expand Up @@ -38,6 +40,10 @@ def port
end
end

def network_args
argumentize "--network", network
end

def publish_args
argumentize "--publish", port if port
end
Expand Down Expand Up @@ -173,4 +179,8 @@ def hosts_from_roles
accessory_config["roles"].flat_map { |role| config.role(role).hosts }
end
end

def network
accessory_config["network"] || NETWORK
end
end
8 changes: 8 additions & 0 deletions lib/kamal/configuration/docs/accessory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@ accessories:
# They are not created or copied before mounting:
volumes:
- /path/to/mysql-logs:/var/log/mysql

# Network
#
# The network the accessory will be attached to.
#
# Defaults to kamal:
network: custom

7 changes: 7 additions & 0 deletions lib/kamal/configuration/docs/configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,10 @@ logging:
# Alias configuration, see kamal docs alias:
aliases:
...

# Network
#
# The network the application will be attached to.
#
# Defaults to kamal:
network: custom
7 changes: 7 additions & 0 deletions lib/kamal/configuration/docs/proxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,10 @@ proxy:
# By default, kamal-proxy will not forward the headers if the `ssl` option is set to `true`, and
# will forward them if it is set to `false`.
forward_headers: true

# Network
#
# The network the proxy container will be attached to.
#
# Defaults to kamal:
network: custom
9 changes: 9 additions & 0 deletions lib/kamal/configuration/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Kamal::Configuration::Proxy

DEFAULT_LOG_REQUEST_HEADERS = [ "Cache-Control", "Last-Modified", "User-Agent" ]
CONTAINER_NAME = "kamal-proxy"
NETWORK = "kamal"

delegate :argumentize, :optionize, to: Kamal::Utils

Expand Down Expand Up @@ -51,6 +52,10 @@ def deploy_command_args(target:)
optionize ({ target: "#{target}:#{app_port}" }).merge(deploy_options)
end

def network_args
argumentize "--network", network
end

def merge(other)
self.class.new config: config, proxy_config: proxy_config.deep_merge(other.proxy_config)
end
Expand All @@ -59,4 +64,8 @@ def merge(other)
def seconds_duration(value)
value ? "#{value}s" : nil
end

def network
proxy_config["network"] || NETWORK
end
end
8 changes: 8 additions & 0 deletions test/commands/accessory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ class CommandsAccessoryTest < ActiveSupport::TestCase
new_command(:busybox).run.join(" ")
end

test "run in custom network" do
@config[:accessories]["mysql"]["network"] = "custom"

assert_equal \
"docker run --name app-mysql --detach --restart unless-stopped --network custom --log-opt max-size=\"10m\" --publish 3306:3306 --env MYSQL_ROOT_HOST=\"%\" --env-file .kamal/apps/app/env/accessories/mysql.env --label service=\"app-mysql\" private.registry/mysql:8.0",
new_command(:mysql).run.join(" ")
end

test "start" do
assert_equal \
"docker container start app-mysql",
Expand Down
8 changes: 8 additions & 0 deletions test/commands/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ class CommandsAppTest < ActiveSupport::TestCase
new_command.run.join(" ")
end

test "run in custom network" do
@config[:network] = "custom"

assert_equal \
"docker run --detach --restart unless-stopped --name app-web-999 --network custom -e KAMAL_CONTAINER_NAME=\"app-web-999\" -e KAMAL_VERSION=\"999\" --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" --label service=\"app\" --label role=\"web\" --label destination dhh/app:999",
new_command.run.join(" ")
end

test "start" do
assert_equal \
"docker start app-web-999",
Expand Down
8 changes: 8 additions & 0 deletions test/commands/proxy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class CommandsProxyTest < ActiveSupport::TestCase
new_command.run.join(" ")
end

test "run in custom network" do
@config[:network] = "custom"

assert_equal \
"docker run --name kamal-proxy --network custom --detach --restart unless-stopped --volume kamal-proxy-config:/home/kamal-proxy/.config/kamal-proxy $(cat .kamal/proxy/options || echo \"--publish 80:80 --publish 443:443\") basecamp/kamal-proxy:#{Kamal::Configuration::PROXY_MINIMUM_VERSION}",
new_command.run.join(" ")
end

test "proxy start" do
assert_equal \
"docker container start kamal-proxy",
Expand Down
9 changes: 9 additions & 0 deletions test/configuration/accessory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,13 @@ class ConfigurationAccessoryTest < ActiveSupport::TestCase
test "options" do
assert_equal [ "--cpus", "\"4\"", "--memory", "\"2GB\"" ], @config.accessory(:redis).option_args
end

test "network_args default" do
assert_equal [ "--network", "kamal" ], @config.accessory(:mysql).network_args
end

test "network_args with configured options" do
@deploy[:accessories]["mysql"]["network"] = "database"
assert_equal [ "--network", "database" ], @config.accessory(:mysql).network_args
end
end
9 changes: 9 additions & 0 deletions test/configuration/proxy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ class ConfigurationProxyTest < ActiveSupport::TestCase
assert_not config.proxy.deploy_options.has_key?(:tls)
end

test "network_args defaults" do
assert_equal [ "--network", "kamal" ], config.proxy.network_args
end

test "network_args with configured options" do
@deploy[:proxy] = { "network" => "example" }
assert_equal [ "--network", "example" ], config.proxy.network_args
end

private
def config
Kamal::Configuration.new(@deploy)
Expand Down
9 changes: 9 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ class ConfigurationTest < ActiveSupport::TestCase
assert_equal [ "--log-driver", "\"local\"", "--log-opt", "max-size=\"100m\"", "--log-opt", "max-file=\"5\"" ], config.logging_args
end

test "network_args default" do
assert_equal [ "--network", "kamal" ], @config.network_args
end

test "network_args with configured options" do
config = Kamal::Configuration.new(@deploy.tap { |c| c.merge!(network: "custom") })
assert_equal [ "--network", "custom" ], config.network_args
end

test "erb evaluation of yml config" do
config = Kamal::Configuration.create_from config_file: Pathname.new(File.expand_path("fixtures/deploy.erb.yml", __dir__))
assert_equal "my-user", config.registry.username
Expand Down