Combine multiarch and native/cache builders

Combine the two builders, as they are almost identical. The only
difference was whether the platforms were set.

The native cached builder wasn't using the context it created, so now
we do.

We'll set the driver to `docker-container` - it seems to be the default
but the Docker docs claim it is `docker`.
This commit is contained in:
Donal McBreen
2024-06-06 09:43:58 +01:00
committed by Donal McBreen
parent b7382ceeaf
commit 0ab838bc25
8 changed files with 76 additions and 82 deletions

View File

@@ -11,22 +11,17 @@ class Kamal::Commands::Builder < Kamal::Commands::Base
end
def target
if config.builder.multiarch?
if config.builder.remote?
if config.builder.local?
multiarch_remote
else
native_remote
end
else
multiarch
end
case
when !config.builder.multiarch? && !config.builder.cached?
native
when !config.builder.multiarch? && config.builder.cached?
local
when config.builder.local? && config.builder.remote?
multiarch_remote
when config.builder.remote?
native_remote
else
if config.builder.cached?
native_cached
else
native
end
local
end
end
@@ -34,16 +29,12 @@ class Kamal::Commands::Builder < Kamal::Commands::Base
@native ||= Kamal::Commands::Builder::Native.new(config)
end
def native_cached
@native ||= Kamal::Commands::Builder::Native::Cached.new(config)
end
def native_remote
@native ||= Kamal::Commands::Builder::Native::Remote.new(config)
end
def multiarch
@multiarch ||= Kamal::Commands::Builder::Multiarch.new(config)
def local
@local ||= Kamal::Commands::Builder::Local.new(config)
end
def multiarch_remote

View File

@@ -5,7 +5,10 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base
ENDPOINT_DOCKER_HOST_INSPECT = "'{{.Endpoints.docker.Host}}'"
delegate :argumentize, to: Kamal::Utils
delegate :args, :secrets, :dockerfile, :target, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, :ssh, to: :builder_config
delegate \
:args, :secrets, :dockerfile, :target, :local_arch, :local_host, :remote_arch, :remote_host,
:cache_from, :cache_to, :multiarch?, :ssh,
to: :builder_config
def clean
docker :image, :rm, "--force", config.absolute_image

View File

@@ -1,6 +1,6 @@
class Kamal::Commands::Builder::Multiarch < Kamal::Commands::Builder::Base
class Kamal::Commands::Builder::Local < Kamal::Commands::Builder::Base
def create
docker :buildx, :create, "--use", "--name", builder_name
docker :buildx, :create, "--name", builder_name, "--driver=docker-container"
end
def remove
@@ -16,7 +16,7 @@ class Kamal::Commands::Builder::Multiarch < Kamal::Commands::Builder::Base
def push
docker :buildx, :build,
"--push",
"--platform", platform_names,
*platform_options,
"--builder", builder_name,
*build_options,
build_context
@@ -28,14 +28,16 @@ class Kamal::Commands::Builder::Multiarch < Kamal::Commands::Builder::Base
private
def builder_name
"kamal-#{config.service}-multiarch"
"kamal-local"
end
def platform_names
if local_arch
"linux/#{local_arch}"
else
"linux/amd64,linux/arm64"
def platform_options
if multiarch?
if local_arch
[ "--platform", "linux/#{local_arch}" ]
else
[ "--platform", "linux/amd64,linux/arm64" ]
end
end
end
end

View File

@@ -1,4 +1,4 @@
class Kamal::Commands::Builder::Multiarch::Remote < Kamal::Commands::Builder::Multiarch
class Kamal::Commands::Builder::Multiarch::Remote < Kamal::Commands::Builder::Base
def create
combine \
create_contexts,
@@ -12,6 +12,21 @@ class Kamal::Commands::Builder::Multiarch::Remote < Kamal::Commands::Builder::Mu
super
end
def info
combine \
docker(:context, :ls),
docker(:buildx, :ls)
end
def push
docker :buildx, :build,
"--push",
*platform_options,
"--builder", builder_name,
*build_options,
build_context
end
def context_hosts
chain \
context_host(builder_name_with_arch(local_arch)),
@@ -24,7 +39,7 @@ class Kamal::Commands::Builder::Multiarch::Remote < Kamal::Commands::Builder::Mu
private
def builder_name
super + "-remote"
"kamal-#{config.service}-multiarch-remote"
end
def builder_name_with_arch(arch)
@@ -58,4 +73,12 @@ class Kamal::Commands::Builder::Multiarch::Remote < Kamal::Commands::Builder::Mu
def remove_context(arch)
docker :context, :rm, builder_name_with_arch(arch)
end
def platform_options
if local_arch
[ "--platform", "linux/#{local_arch}" ]
else
[ "--platform", "linux/amd64,linux/arm64" ]
end
end
end

View File

@@ -1,25 +0,0 @@
class Kamal::Commands::Builder::Native::Cached < Kamal::Commands::Builder::Native
def create
docker :buildx, :create, "--name", builder_name, "--use", "--driver=docker-container"
end
def remove
docker :buildx, :rm, builder_name
end
def push
docker :buildx, :build,
"--push",
*build_options,
build_context
end
def context_hosts
docker :buildx, :inspect, builder_name, "> /dev/null"
end
private
def builder_name
"kamal-#{config.service}-native-cached"
end
end