Skip to content

Commit

Permalink
Convert dict.items() calls to use dicts.add. (bazelbuild#652)
Browse files Browse the repository at this point in the history
* Convert dict.items() calls to use dicts.add.

Also separate rules_docker's dependencies defs into a separate file:
repositories/repositories.bzl. This is needed because otherwise a cylic
dependency will occur because container_{pull, image, push, ...} now
depends on @bazel_skylib. But in order to load that dependency from
previously container/container.bzl, it will try to evaluate the load
statements of container_{pull, image, push, ...} which existed in the
same file before this change.
  • Loading branch information
xingao267 authored Jan 11, 2019
1 parent 8863658 commit d8e842c
Show file tree
Hide file tree
Showing 22 changed files with 334 additions and 253 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# Download the rules_docker repository at release v0.6.0
http_archive(
name = "io_bazel_rules_docker",
sha256 = "c0e9d27e6ca307e4ac0122d3dd1df001b9824373fb6fb8627cd2371068e51fef",
strip_prefix = "rules_docker-0.6.0",
urls = ["https://github.com/bazelbuild/rules_docker/archive/v0.6.0.tar.gz"],
# Replace with a real SHA256 checksum
sha256 = "{SHA256}"
strip_prefix = "rules_docker-{HEAD}",
urls = ["https://github.com/bazelbuild/rules_docker/archive/{HEAD}.tar.gz"],
)

# OPTIONAL: Call this to override the default docker toolchain configuration.
Expand All @@ -116,16 +117,19 @@ docker_toolchain_configure(
client_config="/path/to/docker/client/config",
)

# This is NOT needed when going through the language lang_image
# "repositories" function(s).
load(
"@io_bazel_rules_docker//container:container.bzl",
"container_pull",
"@io_bazel_rules_docker//repositories:repositories.bzl",
container_repositories = "repositories",
)

# This is NOT needed when going through the language lang_image
# "repositories" function(s).
container_repositories()

load(
"@io_bazel_rules_docker//container:container.bzl",
"container_pull",
)

container_pull(
name = "java_base",
registry = "gcr.io",
Expand Down
16 changes: 10 additions & 6 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
workspace(name = "io_bazel_rules_docker")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load(
"//toolchains/docker:toolchain.bzl",
docker_toolchain_configure = "toolchain_configure",
Expand All @@ -23,18 +24,21 @@ docker_toolchain_configure(
docker_path = "/usr/bin/docker",
)

# Consumers shouldn't need to do this themselves once WORKSPACE is
# instantiated recursively.
load(
"//container:container.bzl",
"container_load",
"container_pull",
"//repositories:repositories.bzl",
container_repositories = "repositories",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Consumers shouldn't need to do this themselves once WORKSPACE is
# instantiated recursively.
container_repositories()

load(
"//container:container.bzl",
"container_load",
"container_pull",
)

# These are for testing.
container_pull(
name = "distroless_base",
Expand Down
5 changes: 4 additions & 1 deletion cc/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ load(
"//lang:image.bzl",
"app_layer",
)
load(
"//repositories:repositories.bzl",
_repositories = "repositories",
)
load(
"//container:container.bzl",
"container_pull",
_repositories = "repositories",
)

# Load the resolved digests.
Expand Down
5 changes: 3 additions & 2 deletions container/bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""Rule for bundling Container images into a tarball."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(
"//skylib:label.bzl",
_string_to_label = "string_to_label",
Expand Down Expand Up @@ -87,7 +88,7 @@ def _container_bundle_impl(ctx):
)

container_bundle_ = rule(
attrs = dict({
attrs = dicts.add({
"images": attr.string_dict(),
# Implicit dependencies.
"image_targets": attr.label_list(allow_files = True),
Expand All @@ -96,7 +97,7 @@ container_bundle_ = rule(
default = False,
mandatory = False,
),
}.items() + _layer_tools.items()),
}, _layer_tools),
executable = True,
outputs = {
"out": "%{name}.tar",
Expand Down
199 changes: 0 additions & 199 deletions container/container.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ load(
"http_archive",
"http_file",
)
load(
"@io_bazel_rules_docker//toolchains/docker:toolchain.bzl",
_docker_toolchain_configure = "toolchain_configure",
)

# Explicitly re-export the functions
container_bundle = _container_bundle
Expand All @@ -40,203 +36,8 @@ container_layer = _container_layer
container_import = _container_import
container_pull = _container_pull
container_push = _container_push

container_load = _container_load

container = struct(
image = image,
)

# The release of the github.com/google/containerregistry to consume.
CONTAINERREGISTRY_RELEASE = "v0.0.34"

_local_tool_build_template = """
sh_binary(
name = "{name}",
srcs = ["bin/{name}"],
visibility = ["//visibility:public"],
)
"""

def _local_tool(repository_ctx):
rctx = repository_ctx
realpath = rctx.which(rctx.name)
rctx.symlink(realpath, "bin/%s" % rctx.name)
rctx.file(
"WORKSPACE",
'workspace(name = "{}")\n'.format(rctx.name),
)
rctx.file(
"BUILD",
_local_tool_build_template.format(name = rctx.name),
)

local_tool = repository_rule(
local = True,
implementation = _local_tool,
)

def repositories():
"""Download dependencies of container rules."""
excludes = native.existing_rules().keys()

if "puller" not in excludes:
http_file(
name = "puller",
executable = True,
sha256 = "2a3ccb6ef8f99ec0053b56380824a7c100ba00eb0e147d1bda748884113542f1",
urls = [("https://storage.googleapis.com/containerregistry-releases/" +
CONTAINERREGISTRY_RELEASE + "/puller.par")],
)

if "importer" not in excludes:
http_file(
name = "importer",
executable = True,
sha256 = "0eec1a4ffb26623dbb4075e5459fa0ede36548edf872d2691ebbcb3c4ccb8cf3",
urls = [("https://storage.googleapis.com/containerregistry-releases/" +
CONTAINERREGISTRY_RELEASE + "/importer.par")],
)

if "containerregistry" not in excludes:
http_archive(
name = "containerregistry",
sha256 = "8182728578f7d7178e7efcef8ce9074988a1a2667f20ecff5cf6234fba284dd3",
strip_prefix = "containerregistry-" + CONTAINERREGISTRY_RELEASE[1:],
urls = [("https://github.com/google/containerregistry/archive/" +
CONTAINERREGISTRY_RELEASE + ".tar.gz")],
)

# TODO(mattmoor): Remove all of this (copied from google/containerregistry)
# once transitive workspace instantiation lands.

if "httplib2" not in excludes:
# TODO(mattmoor): Is there a clean way to override?
http_archive(
name = "httplib2",
build_file_content = """
py_library(
name = "httplib2",
srcs = glob(["**/*.py"]),
data = ["cacerts.txt"],
visibility = ["//visibility:public"]
)""",
sha256 = "d9f568c183d1230f271e9c60bd99f3f2b67637c3478c9068fea29f7cca3d911f",
strip_prefix = "httplib2-0.11.3/python2/httplib2/",
type = "tar.gz",
urls = ["https://codeload.github.com/httplib2/httplib2/tar.gz/v0.11.3"],
)

# Used by oauth2client
if "six" not in excludes:
# TODO(mattmoor): Is there a clean way to override?
http_archive(
name = "six",
build_file_content = """
# Rename six.py to __init__.py
genrule(
name = "rename",
srcs = ["six.py"],
outs = ["__init__.py"],
cmd = "cat $< >$@",
)
py_library(
name = "six",
srcs = [":__init__.py"],
visibility = ["//visibility:public"],
)""",
sha256 = "e24052411fc4fbd1f672635537c3fc2330d9481b18c0317695b46259512c91d5",
strip_prefix = "six-1.9.0/",
type = "tar.gz",
urls = ["https://pypi.python.org/packages/source/s/six/six-1.9.0.tar.gz"],
)

# Used for authentication in containerregistry
if "oauth2client" not in excludes:
# TODO(mattmoor): Is there a clean way to override?
http_archive(
name = "oauth2client",
build_file_content = """
py_library(
name = "oauth2client",
srcs = glob(["**/*.py"]),
visibility = ["//visibility:public"],
deps = [
"@httplib2//:httplib2",
"@six//:six",
]
)""",
sha256 = "7230f52f7f1d4566a3f9c3aeb5ffe2ed80302843ce5605853bee1f08098ede46",
strip_prefix = "oauth2client-4.0.0/oauth2client/",
type = "tar.gz",
urls = ["https://codeload.github.com/google/oauth2client/tar.gz/v4.0.0"],
)

# Used for parallel execution in containerregistry
if "concurrent" not in excludes:
# TODO(mattmoor): Is there a clean way to override?
http_archive(
name = "concurrent",
build_file_content = """
py_library(
name = "concurrent",
srcs = glob(["**/*.py"]),
visibility = ["//visibility:public"]
)""",
sha256 = "a7086ddf3c36203da7816f7e903ce43d042831f41a9705bc6b4206c574fcb765",
strip_prefix = "pythonfutures-3.0.5/concurrent/",
type = "tar.gz",
urls = ["https://codeload.github.com/agronholm/pythonfutures/tar.gz/3.0.5"],
)

# For packaging python tools.
if "subpar" not in excludes:
http_archive(
name = "subpar",
sha256 = "cf3762b10426a1887d37f127b4c1390785ecb969254096eb714cc1db371f78d6",
strip_prefix = "subpar-a4f9b23bf01bcc7a52d458910af65a90ee991aff",
urls = ["https://github.com/google/subpar/archive/a4f9b23bf01bcc7a52d458910af65a90ee991aff.tar.gz"],
)

if "structure_test_linux" not in excludes:
http_file(
name = "structure_test_linux",
executable = True,
sha256 = "543577685b33f0483bd4df72534ac9f84c17c9315d8afdcc536cce3591bb8f7c",
urls = ["https://storage.googleapis.com/container-structure-test/v1.4.0/container-structure-test-linux-amd64"],
)

if "structure_test_darwin" not in excludes:
http_file(
name = "structure_test_darwin",
executable = True,
sha256 = "c1bc8664d411c6df23c002b41ab1b9a3d72ae930f194a997468bfae2f54ca751",
urls = ["https://storage.googleapis.com/container-structure-test/v1.4.0/container-structure-test-darwin-amd64"],
)

# For bzl_library.
if "bazel_skylib" not in excludes:
http_archive(
name = "bazel_skylib",
sha256 = "eb5c57e4c12e68c0c20bc774bfbc60a568e800d025557bc4ea022c6479acc867",
strip_prefix = "bazel-skylib-0.6.0",
urls = ["https://github.com/bazelbuild/bazel-skylib/archive/0.6.0.tar.gz"],
)

if "gzip" not in excludes:
local_tool(
name = "gzip",
)

native.register_toolchains(
# Register the default docker toolchain that expects the 'docker'
# executable to be in the PATH
"@io_bazel_rules_docker//toolchains/docker:default_linux_toolchain",
"@io_bazel_rules_docker//toolchains/docker:default_windows_toolchain",
"@io_bazel_rules_docker//toolchains/docker:default_osx_toolchain",
)

if "docker_config" not in excludes:
# Automatically configure the docker toolchain rule to use the default
# docker binary from the system path
_docker_toolchain_configure(name = "docker_config")
5 changes: 3 additions & 2 deletions container/flatten.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""A rule to flatten container images."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(
"//container:layer_tools.bzl",
_get_layers = "get_from_target",
Expand Down Expand Up @@ -60,7 +61,7 @@ def _impl(ctx):
return [FlattenInfo()]

container_flatten = rule(
attrs = dict({
attrs = dicts.add({
"image": attr.label(
allow_single_file = [".tar"],
mandatory = True,
Expand All @@ -71,7 +72,7 @@ container_flatten = rule(
executable = True,
allow_files = True,
),
}.items() + _layer_tools.items()),
}, _layer_tools),
outputs = {
"filesystem": "%{name}.tar",
"metadata": "%{name}.json",
Expand Down
5 changes: 3 additions & 2 deletions container/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ expectation in such cases is that users will write something like:
"""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(
"//skylib:filetype.bzl",
container_filetype = "container",
Expand Down Expand Up @@ -448,7 +449,7 @@ def _impl(
],
)

_attrs = dict(_layer.attrs.items() + {
_attrs = dicts.add(_layer.attrs, {
"base": attr.label(allow_files = container_filetype),
"legacy_repository_naming": attr.bool(default = False),
# TODO(mattmoor): Default this to False.
Expand Down Expand Up @@ -495,7 +496,7 @@ _attrs = dict(_layer.attrs.items() + {
cfg = "host",
executable = True,
),
}.items() + _hash_tools.items() + _layer_tools.items() + _zip_tools.items())
}, _hash_tools, _layer_tools, _zip_tools)

_outputs = dict(_layer.outputs)

Expand Down
Loading

0 comments on commit d8e842c

Please sign in to comment.