-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathimage.bzl
More file actions
286 lines (258 loc) · 10.4 KB
/
Copy pathimage.bzl
File metadata and controls
286 lines (258 loc) · 10.4 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
""" image """
load("@com_github_datadog_rules_oci//oci:providers.bzl", "OCIDescriptor", "OCILayout")
# buildifier: disable=function-docstring
def get_descriptor_file(ctx, desc):
if hasattr(desc, "descriptor_file"):
return desc.descriptor_file
out = ctx.actions.declare_file(desc.digest)
ctx.actions.write(
output = out,
content = json.encode({
"mediaType": desc.media_type,
"size": desc.size,
"digest": desc.digest,
"urls": desc.urls,
"annotations": desc.annotations,
}),
)
return out
def _oci_image_layer_impl(ctx):
toolchain = ctx.toolchains["@com_github_datadog_rules_oci//oci:toolchain"]
descriptor_file = ctx.actions.declare_file("{}.descriptor.json".format(ctx.label.name))
ctx.actions.run(
executable = toolchain.sdk.ocitool,
arguments = [
"create-layer",
"--out={}".format(ctx.outputs.layer.path),
"--outd={}".format(descriptor_file.path),
"--dir={}".format(ctx.attr.directory),
"--bazel-label={}".format(ctx.label),
] +
["--file={}".format(f.path) for f in ctx.files.files] +
["--symlink={}={}".format(k, v) for k, v in ctx.attr.symlinks.items()] +
["--file-map={}={}".format(k.files.to_list()[0].path, v) for k, v in ctx.attr.file_map.items()],
inputs = ctx.files.files + ctx.files.file_map,
outputs = [
descriptor_file,
ctx.outputs.layer,
],
)
return [
OCIDescriptor(
descriptor_file = descriptor_file,
file = ctx.outputs.layer,
),
]
oci_image_layer = rule(
implementation = _oci_image_layer_impl,
doc = "Create a tarball and an OCI descriptor for it",
attrs = {
"files": attr.label_list(
doc = "List of files to include under `directory`",
allow_files = True,
),
"directory": attr.string(
doc = "Directory in the tarball to place the `files`",
),
"symlinks": attr.string_dict(
doc = "Dictionary of symlink -> target entries to place in the tarball",
),
"file_map": attr.label_keyed_string_dict(
doc = "Dictionary of file -> file location in tarball",
allow_files = True,
),
},
toolchains = ["@com_github_datadog_rules_oci//oci:toolchain"],
outputs = {
"layer": "%{name}-layer.tar.gz",
},
)
def _oci_image_index_impl(ctx):
toolchain = ctx.toolchains["@com_github_datadog_rules_oci//oci:toolchain"]
layout_files = depset(None, transitive = [m[OCILayout].files for m in ctx.attr.manifests])
index_desc_file = ctx.actions.declare_file("{}.index.descriptor.json".format(ctx.label.name))
index_file = ctx.actions.declare_file("{}.index.json".format(ctx.label.name))
layout_file = ctx.actions.declare_file("{}.index.layout.json".format(ctx.label.name))
desc_files = []
for manifest in ctx.attr.manifests:
desc_files.append(get_descriptor_file(ctx, manifest[OCIDescriptor]))
outputs = [
index_file,
index_desc_file,
layout_file,
]
ctx.actions.run(
executable = toolchain.sdk.ocitool,
arguments = ["--layout={}".format(m[OCILayout].blob_index.path) for m in ctx.attr.manifests] +
[
"create-index",
"--out-index={}".format(index_file.path),
"--out-layout={}".format(layout_file.path),
"--outd={}".format(index_desc_file.path),
] +
["--desc={}".format(d.path) for d in desc_files] +
["--annotations={}={}".format(k, v) for k, v in ctx.attr.annotations.items()],
inputs = desc_files + layout_files.to_list(),
outputs = outputs,
)
return [
OCIDescriptor(
descriptor_file = index_desc_file,
),
OCILayout(
blob_index = layout_file,
files = depset(direct = [index_file, layout_file], transitive = [layout_files]),
),
DefaultInfo(
files = depset(outputs),
),
]
oci_image_index = rule(
implementation = _oci_image_index_impl,
doc = """
""",
attrs = {
"manifests": attr.label_list(
doc = """
""",
),
"annotations": attr.string_dict(
doc = """
""",
),
},
toolchains = ["@com_github_datadog_rules_oci//oci:toolchain"],
)
def _oci_image_impl(ctx):
toolchain = ctx.toolchains["@com_github_datadog_rules_oci//oci:toolchain"]
base_desc = get_descriptor_file(ctx, ctx.attr.base[OCIDescriptor])
base_layout = ctx.attr.base[OCILayout]
manifest_desc_file = ctx.actions.declare_file("{}.manifest.descriptor.json".format(ctx.label.name))
manifest_file = ctx.actions.declare_file("{}.manifest.json".format(ctx.label.name))
config_file = ctx.actions.declare_file("{}.config.json".format(ctx.label.name))
layout_file = ctx.actions.declare_file("{}.layout.json".format(ctx.label.name))
entrypoint_config_file = ctx.actions.declare_file("{}.entrypoint.config.json".format(ctx.label.name))
entrypoint_config = struct(
entrypoint = ctx.attr.entrypoint,
)
ctx.actions.write(
output = entrypoint_config_file,
content = json.encode(entrypoint_config),
)
annotations = ctx.attr.annotations
# Backwards compatibility: code that doesn't use the labels attr will expect annotations to be
# used as labels
labels = ctx.attr.labels or ctx.attr.annotations
layer_descriptor_files = [get_descriptor_file(ctx, f[OCIDescriptor]) for f in ctx.attr.layers]
layer_and_descriptor_paths = zip(
[f.path for f in ctx.files.layers],
[f.path for f in layer_descriptor_files],
)
ctx.actions.run(
executable = toolchain.sdk.ocitool,
arguments = [
"--layout={}".format(base_layout.blob_index.path),
"append-layers",
"--bazel-version-file={}".format(ctx.version_file.path),
"--base={}".format(base_desc.path),
"--os={}".format(ctx.attr.os),
"--arch={}".format(ctx.attr.arch),
"--out-manifest={}".format(manifest_file.path),
"--out-config={}".format(config_file.path),
"--out-layout={}".format(layout_file.path),
"--outd={}".format(manifest_desc_file.path),
"--entrypoint={}".format(entrypoint_config_file.path),
] +
[
"--layer={}={}".format(layer, descriptor)
for layer, descriptor in layer_and_descriptor_paths
] +
["--annotations={}={}".format(k, v) for k, v in annotations.items()] +
["--labels={}={}".format(k, v) for k, v in labels.items()] +
["--env={}".format(env) for env in ctx.attr.env],
inputs = [
ctx.version_file,
base_desc,
base_layout.blob_index,
entrypoint_config_file,
] + ctx.files.layers +
layer_descriptor_files +
base_layout.files.to_list(),
outputs = [
manifest_file,
config_file,
layout_file,
manifest_desc_file,
],
)
return [
OCIDescriptor(
descriptor_file = manifest_desc_file,
),
OCILayout(
blob_index = layout_file,
files = depset(
ctx.files.layers + [manifest_file, config_file, layout_file],
transitive = [base_layout.files],
),
),
DefaultInfo(
files = depset([
entrypoint_config_file,
manifest_file,
config_file,
layout_file,
manifest_desc_file,
]),
),
]
oci_image = rule(
implementation = _oci_image_impl,
doc = """Creates a new image manifest and config by appending the `layers` to an existing image
manifest and config defined by `base`. If `base` is an image index, then `os` and `arch` will
be used to extract the image manifest.""",
attrs = {
"base": attr.label(
doc = """A base image, as defined by oci_pull or oci_image""",
mandatory = True,
providers = [
OCIDescriptor,
OCILayout,
],
),
"entrypoint": attr.string_list(
doc = """A list of entrypoints for the image; these will be inserted into the generated
OCI image config""",
),
"os": attr.string(
doc = "Used to extract a manifest from base if base is an index",
),
"arch": attr.string(
doc = "Used to extract a manifest from base if base is an index",
),
"env": attr.string_list(
doc = """Entries are in the format of `VARNAME=VARVALUE`. These values act as defaults and
are merged with any specified when creating a container.""",
),
"layers": attr.label_list(
doc = "A list of layers defined by oci_image_layer",
providers = [
OCIDescriptor,
],
),
"annotations": attr.string_dict(
doc = """[OCI Annotations](https://github.com/opencontainers/image-spec/blob/main/annotations.md)
to add to the manifest.""",
),
"labels": attr.string_dict(
doc = """labels that will be applied to the image configuration, as defined in
[the OCI config](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
These behave the same way as
[docker LABEL](https://docs.docker.com/engine/reference/builder/#label);
in particular, labels from the base image are inherited. An empty value for a label
will cause that label to be deleted. For backwards compatibility, if this is not set,
then the value of annotations will be used instead.""",
),
},
toolchains = ["@com_github_datadog_rules_oci//oci:toolchain"],
)