-
Notifications
You must be signed in to change notification settings - Fork 694
Expand file tree
/
Copy pathbundle.bzl
More file actions
170 lines (151 loc) · 5.88 KB
/
bundle.bzl
File metadata and controls
170 lines (151 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Rule for bundling Container images into a tarball."""
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@io_bazel_rules_docker//container:providers.bzl", "BundleInfo", "PushInfo", "STAMP_ATTR", "StampSettingInfo")
load(
"//container:layer_tools.bzl",
_assemble_image = "assemble",
_get_layers = "get_from_target",
_incr_load = "incremental_load",
_layer_tools = "tools",
)
load(
"//skylib:label.bzl",
_string_to_label = "string_to_label",
)
_DOC = """A rule that aliases and saves N images into a single `docker save` tarball.
This can be consumed in 2 different ways:
- The output tarball could be used for `docker load` to load all images to docker daemon.
- The emitted BundleInfo provider could be consumed by contrib/push-all.bzl rules to
create an executable target which tag and push multiple images to a container registry.
"""
def _container_bundle_impl(ctx):
"""Implementation for the container_bundle rule."""
# Compute the set of layers from the image_targets.
image_target_dict = _string_to_label(
ctx.attr.image_targets,
ctx.attr.image_target_strings,
)
images = {}
runfiles = []
stamp = ctx.attr.stamp[StampSettingInfo].value
for unresolved_tag in ctx.attr.images:
# Allow users to put make variables into the tag name.
tag = ctx.expand_make_variables("images", unresolved_tag, {})
target = ctx.attr.images[unresolved_tag]
layer = _get_layers(ctx, ctx.label.name, image_target_dict[target])
images[tag] = layer
runfiles.append(layer.get("config"))
runfiles.append(layer.get("config_digest"))
runfiles += layer.get("unzipped_layer", [])
runfiles += layer.get("diff_id", [])
if layer.get("legacy"):
runfiles.append(layer.get("legacy"))
for push_target in ctx.attr.push_targets:
push_info = push_target[PushInfo]
tag = push_info.registry + "/" + push_info.repository + ":" + push_info.tag
layer = _get_layers(ctx, ctx.label.name, push_info.image)
images[tag] = layer
runfiles.append(layer.get("config"))
runfiles.append(layer.get("config_digest"))
runfiles += layer.get("unzipped_layer", [])
runfiles += layer.get("diff_id", [])
if layer.get("legacy"):
runfiles.append(layer.get("legacy"))
if push_info.tag_file:
runfiles.append(push_info.tag_file)
_incr_load(
ctx,
images,
ctx.outputs.executable,
stamp = stamp,
)
_assemble_image(
ctx,
images,
ctx.outputs.tar_output,
ctx.attr.experimental_tarball_format,
stamp = stamp,
)
stamp_files = [ctx.info_file, ctx.version_file] if stamp else []
return [
BundleInfo(
container_images = images,
stamp = stamp,
),
DefaultInfo(
executable = ctx.outputs.executable,
files = depset(),
runfiles = ctx.runfiles(files = (stamp_files + runfiles)),
),
OutputGroupInfo(
tar = depset([ctx.outputs.tar_output]),
),
]
container_bundle_ = rule(
doc = _DOC,
attrs = dicts.add({
"image_target_strings": attr.string_list(),
# Implicit dependencies.
"image_targets": attr.label_list(allow_files = True),
"images": attr.string_dict(),
"push_targets": attr.label_list(allow_files = True, providers = [PushInfo]),
"stamp": STAMP_ATTR,
"tar_output": attr.output(),
"experimental_tarball_format": attr.string(
values = [
"legacy",
"compressed",
],
default = "legacy",
doc = ("The tarball format to use when producing an image .tar file. " +
"Defaults to \"legacy\", which contains uncompressed layers. " +
"If set to \"compressed\", the resulting tarball will contain " +
"compressed layers, but is only loadable by newer versions of " +
"docker. This is an experimental attribute, which is subject " +
"to change or removal: do not depend on its exact behavior."),
),
}, _layer_tools),
executable = True,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
implementation = _container_bundle_impl,
)
# Produces a new container image tarball compatible with 'docker load', which
# contains the N listed 'images', each aliased with their key.
#
# Example:
# container_bundle(
# name = "foo",
# images = {
# "ubuntu:latest": ":blah",
# "foo.io/bar:canary": "//baz:asdf",
# }
# )
def container_bundle(**kwargs):
"""Package several container images into a single tarball.
Args:
**kwargs: See above.
"""
for reserved in ["image_targets", "image_target_strings"]:
if reserved in kwargs:
fail("reserved for internal use by container_bundle macro", attr = reserved)
if "images" in kwargs:
values = {value: None for value in kwargs["images"].values()}.keys()
kwargs["image_targets"] = values
kwargs["image_target_strings"] = values
name = kwargs["name"]
tar_output = kwargs.pop("tar_output", name + ".tar")
kwargs["tar_output"] = tar_output
container_bundle_(**kwargs)