Skip to content

Commit 029b9a1

Browse files
authored
Add tool target attribute to docker toolchain (#1946)
* Add tool target attribute to docker toolchain * Run buildifier and add docstrings * Add missing imports
1 parent f6ed806 commit 029b9a1

File tree

14 files changed

+99
-19
lines changed

14 files changed

+99
-19
lines changed

container/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ bzl_library(
135135
bzl_library(
136136
name = "layer_tools",
137137
srcs = ["layer_tools.bzl"],
138-
deps = ["//skylib:path"],
138+
deps = [
139+
"//skylib:docker",
140+
"//skylib:path",
141+
],
139142
)
140143

141144
bzl_library(

container/layer_tools.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ load(
2222
"//skylib:path.bzl",
2323
_get_runfile_path = "runfile",
2424
)
25+
load(
26+
"//skylib:docker.bzl",
27+
"docker_path",
28+
)
2529

2630
def _extract_layers(ctx, name, artifact):
2731
config_file = ctx.actions.declare_file(name + "." + artifact.basename + ".config")
@@ -277,7 +281,7 @@ def incremental_load(
277281
template = ctx.file.incremental_load_template,
278282
substitutions = {
279283
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
280-
"%{docker_tool_path}": toolchain_info.tool_path,
284+
"%{docker_tool_path}": docker_path(toolchain_info),
281285
"%{load_statements}": "\n".join(load_statements),
282286
"%{run_statements}": "\n".join(run_statements),
283287
"%{run}": str(run),

contrib/automatic_container_release/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports_files(["run_checker.sh.tpl"])
2323
bzl_library(
2424
name = "configs_test",
2525
srcs = ["configs_test.bzl"],
26+
deps = ["//skylib:docker"],
2627
)
2728

2829
bzl_library(

contrib/automatic_container_release/configs_test.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ load(
2020
"//skylib:path.bzl",
2121
"runfile",
2222
)
23+
load(
24+
"//skylib:docker.bzl",
25+
"docker_path",
26+
)
2327

2428
def _get_runfile_path(ctx, f):
2529
return "${RUNFILES}/%s" % runfile(ctx, f)
@@ -70,7 +74,7 @@ def _impl(ctx):
7074
substitutions = {
7175
"%{cmd_args}": " ".join(cmd_args),
7276
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
73-
"%{docker_path}": toolchain_info.tool_path,
77+
"%{docker_path}": docker_path(toolchain_info),
7478
"%{image_name}": ctx.attr._checker + ":" + ctx.attr.checker_tag,
7579
"%{spec_container_paths}": " ".join(spec_container_paths),
7680
"%{specs}": " ".join(specs),

docker/package_managers/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ bzl_library(
3434
deps = [
3535
"//container",
3636
"//docker/util",
37+
"//skylib:docker",
3738
],
3839
)

docker/package_managers/download_pkgs.bzl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ load(
1818
"//skylib:path.bzl",
1919
"runfile",
2020
)
21+
load(
22+
"//skylib:docker.bzl",
23+
"docker_path",
24+
)
2125

2226
def _generate_add_additional_repo_commands(ctx, additional_repos):
2327
return """printf "{repos}" >> /etc/apt/sources.list.d/{name}_repos.list""".format(
@@ -101,7 +105,7 @@ def _impl(ctx, image_tar = None, packages = None, additional_repos = None, outpu
101105
output = output_script,
102106
substitutions = {
103107
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
104-
"%{docker_tool_path}": toolchain_info.tool_path,
108+
"%{docker_tool_path}": docker_path(toolchain_info),
105109
"%{download_commands}": _generate_download_commands(ctx, packages, additional_repos),
106110
"%{image_id_extractor_path}": ctx.executable._extract_image_id.path,
107111
"%{image_tar}": image_tar.path,
@@ -127,7 +131,7 @@ def _impl(ctx, image_tar = None, packages = None, additional_repos = None, outpu
127131
output = output_executable,
128132
substitutions = {
129133
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
130-
"%{docker_tool_path}": toolchain_info.tool_path,
134+
"%{docker_tool_path}": docker_path(toolchain_info),
131135
"%{download_commands}": _generate_download_commands(ctx, packages, additional_repos),
132136
"%{image_id_extractor_path}": "${RUNFILES}/%s" % runfile(ctx, ctx.executable._extract_image_id),
133137
"%{image_tar}": image_tar.short_path,

docker/package_managers/install_pkgs.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ users will write something like:
3939
4040
"""
4141

42+
load(
43+
"//skylib:docker.bzl",
44+
"docker_path",
45+
)
46+
4247
def _generate_install_commands(tar, installation_cleanup_commands):
4348
return """
4449
tar -xvf {tar}
@@ -94,7 +99,7 @@ def _impl(ctx, image_tar = None, installables_tar = None, installation_cleanup_c
9499
output = image_util,
95100
substitutions = {
96101
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
97-
"%{docker_tool_path}": toolchain_info.tool_path,
102+
"%{docker_tool_path}": docker_path(toolchain_info),
98103
},
99104
is_executable = True,
100105
)
@@ -106,7 +111,7 @@ def _impl(ctx, image_tar = None, installables_tar = None, installation_cleanup_c
106111
substitutions = {
107112
"%{base_image_tar}": image_tar.path,
108113
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
109-
"%{docker_tool_path}": toolchain_info.tool_path,
114+
"%{docker_tool_path}": docker_path(toolchain_info),
110115
"%{image_id_extractor_path}": ctx.executable._extract_image_id.path,
111116
"%{installables_tar}": installables_tar_path,
112117
"%{installer_script}": install_script.path,

docker/util/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ exports_files([
4040
bzl_library(
4141
name = "util",
4242
srcs = ["run.bzl"],
43+
deps = ["//skylib:docker"],
4344
)
4445

4546
# Re-package config_stripper as a library to be used in unit tests.

docker/util/run.bzl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ load(
2929
"//skylib:zip.bzl",
3030
_zip_tools = "tools",
3131
)
32+
load(
33+
"//skylib:docker.bzl",
34+
"docker_path",
35+
)
3236

3337
def _extract_impl(
3438
ctx,
@@ -79,7 +83,7 @@ def _extract_impl(
7983
"%{commands}": _process_commands(commands),
8084
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
8185
"%{docker_run_flags}": " ".join(docker_run_flags),
82-
"%{docker_tool_path}": toolchain_info.tool_path,
86+
"%{docker_tool_path}": docker_path(toolchain_info),
8387
"%{extract_file}": extract_file,
8488
"%{image_id_extractor_path}": ctx.executable._extract_image_id.path,
8589
"%{image_tar}": image.path,
@@ -196,7 +200,7 @@ def _commit_impl(
196200
output = image_utils,
197201
substitutions = {
198202
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
199-
"%{docker_tool_path}": toolchain_info.tool_path,
203+
"%{docker_tool_path}": docker_path(toolchain_info),
200204
},
201205
is_executable = True,
202206
)
@@ -209,7 +213,7 @@ def _commit_impl(
209213
"%{commands}": _process_commands(commands),
210214
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
211215
"%{docker_run_flags}": " ".join(docker_run_flags),
212-
"%{docker_tool_path}": toolchain_info.tool_path,
216+
"%{docker_tool_path}": docker_path(toolchain_info),
213217
"%{image_id_extractor_path}": ctx.executable._extract_image_id.path,
214218
"%{image_tar}": image.path,
215219
"%{output_image}": "bazel/%s:%s" % (
@@ -344,7 +348,7 @@ def _commit_layer_impl(
344348
output = image_utils,
345349
substitutions = {
346350
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
347-
"%{docker_tool_path}": toolchain_info.tool_path,
351+
"%{docker_tool_path}": docker_path(toolchain_info),
348352
},
349353
is_executable = True,
350354
)
@@ -370,7 +374,7 @@ def _commit_layer_impl(
370374
"%{commands}": _process_commands(commands),
371375
"%{docker_flags}": " ".join(toolchain_info.docker_flags),
372376
"%{docker_run_flags}": " ".join(docker_run_flags),
373-
"%{docker_tool_path}": toolchain_info.tool_path,
377+
"%{docker_tool_path}": docker_path(toolchain_info),
374378
"%{env_file_path}": env_file.path,
375379
"%{image_id_extractor_path}": ctx.executable._extract_image_id.path,
376380
"%{image_last_layer_extractor_path}": ctx.executable._last_layer_extractor_tool.path,

skylib/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ package(default_visibility = ["//visibility:public"])
1818

1919
licenses(["notice"]) # Apache 2.0
2020

21+
bzl_library(
22+
name = "docker",
23+
srcs = ["docker.bzl"],
24+
)
25+
2126
bzl_library(
2227
name = "filetype",
2328
srcs = ["filetype.bzl"],

skylib/docker.bzl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2017 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Common functions for docker library"""
15+
16+
def docker_path(toolchain_info):
17+
"""Resolve the user-supplied docker path, if any.
18+
19+
Args:
20+
toolchain_info: The DockerToolchainInfo
21+
22+
Returns:
23+
Path to docker
24+
"""
25+
if toolchain_info.tool_target:
26+
return toolchain_info.tool_target.files_to_run.executable.path
27+
else:
28+
return toolchain_info.tool_path

toolchains/docker/BUILD.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ docker_toolchain(
2222
name = "toolchain",
2323
client_config = "%{DOCKER_CONFIG}",
2424
%{GZIP_ATTR}
25-
tool_path = "%{DOCKER_TOOL}",
25+
%{TOOL_ATTR}
2626
docker_flags = ["%{DOCKER_FLAGS}"],
2727
%{XZ_ATTR}
2828
)

toolchains/docker/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def _impl(ctx):
5858
# Get the DockerToolchainInfo provider
5959
toolchain_info = ctx.toolchains["@io_bazel_rules_docker//toolchains/docker:toolchain_type"].info
6060
# Path to the docker tool
61-
docker_path = toolchain_info.tool_path
61+
docker_path = docker_path(toolchain_info)
6262
...
6363
```
6464
See [toolchain.bzl](toolchain.bzl) for the definition of the DockerToolchainInfo provider

toolchains/docker/toolchain.bzl

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ DockerToolchainInfo = provider(
2828
"gzip_target": "Optional Bazel target for the gzip tool. " +
2929
"Should only be set if gzip_path is unset.",
3030
"tool_path": "Path to the docker executable",
31+
"tool_target": "Bazel target for the docker tool. " +
32+
"Should only be set if tool_path is unset.",
3133
"xz_path": "Optional path to the xz binary. This is used by " +
3234
"build_tar.py when the Python lzma module is unavailable. " +
3335
"If not set found via which.",
@@ -44,6 +46,7 @@ def _docker_toolchain_impl(ctx):
4446
gzip_path = ctx.attr.gzip_path,
4547
gzip_target = ctx.attr.gzip_target,
4648
tool_path = ctx.attr.tool_path,
49+
tool_target = ctx.attr.tool_target,
4750
xz_path = ctx.attr.xz_path,
4851
xz_target = ctx.attr.xz_target,
4952
),
@@ -80,6 +83,13 @@ docker_toolchain = rule(
8083
"tool_path": attr.string(
8184
doc = "Path to the docker binary.",
8285
),
86+
"tool_target": attr.label(
87+
allow_files = True,
88+
doc = "Bazel target for the docker tool. " +
89+
"Should only be set if tool_path is unset.",
90+
cfg = "host",
91+
executable = True,
92+
),
8393
"xz_path": attr.string(
8494
doc = "Optional path to the xz binary. This is used by " +
8595
"build_tar.py when the Python lzma module is unavailable.",
@@ -95,15 +105,20 @@ docker_toolchain = rule(
95105
)
96106

97107
def _toolchain_configure_impl(repository_ctx):
108+
if repository_ctx.attr.docker_target and repository_ctx.attr.docker_path:
109+
fail("Only one of docker_target or docker_path can be set.")
98110
if repository_ctx.attr.gzip_target and repository_ctx.attr.gzip_path:
99111
fail("Only one of gzip_target or gzip_path can be set.")
100112
if repository_ctx.attr.xz_target and repository_ctx.attr.xz_path:
101113
fail("Only one of xz_target or xz_path can be set.")
102-
tool_path = ""
103-
if repository_ctx.attr.docker_path:
104-
tool_path = repository_ctx.attr.docker_path
114+
115+
tool_attr = ""
116+
if repository_ctx.attr.docker_target:
117+
tool_attr = "tool_target = \"%s\"," % repository_ctx.attr.tool_target
118+
elif repository_ctx.attr.docker_path:
119+
tool_attr = "tool_path = \"%s\"," % repository_ctx.attr.docker_path
105120
elif repository_ctx.which("docker"):
106-
tool_path = repository_ctx.which("docker")
121+
tool_attr = "tool_path = \"%s\"," % repository_ctx.which("docker")
107122

108123
xz_attr = ""
109124
if repository_ctx.attr.xz_target:
@@ -130,7 +145,7 @@ def _toolchain_configure_impl(repository_ctx):
130145
{
131146
"%{DOCKER_CONFIG}": "%s" % client_config,
132147
"%{DOCKER_FLAGS}": "%s" % "\", \"".join(docker_flags),
133-
"%{DOCKER_TOOL}": "%s" % tool_path,
148+
"%{TOOL_ATTR}": "%s" % tool_attr,
134149
"%{GZIP_ATTR}": "%s" % gzip_attr,
135150
"%{XZ_ATTR}": "%s" % xz_attr,
136151
},
@@ -171,6 +186,11 @@ toolchain_configure = repository_rule(
171186
"be searched for in the path. If not available, running commands " +
172187
"that require docker (e.g., incremental load) will fail.",
173188
),
189+
"docker_target": attr.string(
190+
mandatory = False,
191+
doc = "The bazel target for the docker tool. " +
192+
"Can only be set if docker_path is not set.",
193+
),
174194
"gzip_path": attr.string(
175195
mandatory = False,
176196
doc = "The full path to the gzip binary. If not specified, a tool will " +

0 commit comments

Comments
 (0)