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

Merged
merged 6 commits into from
Oct 23, 2024
Merged
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
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

DEFAULT_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"] || DEFAULT_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

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
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