forked from CentaurusInfra/arktos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.bzl
132 lines (127 loc) · 5.29 KB
/
container.bzl
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
# Copyright 2019 The Kubernetes Authors.
#
# 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.
load("@io_bazel_rules_docker//container:container.bzl", "container_bundle", "container_image")
load("@io_bazel_rules_docker//contrib:push-all.bzl", "docker_push")
load("//build:platforms.bzl", "go_platform_constraint")
# multi_arch_container produces a private internal container_image, multiple
# arch-specific tagged container_bundles (named NAME-ARCH), an alias
# from NAME to the appropriately NAME-ARCH container_bundle target, and a
# genrule for NAME.tar copying the appropriate NAME-ARCH container bundle
# tarball output for the currently-configured architecture.
# Additionally, if docker_push_tags is provided, uses multi_arch_container_push
# to create container_bundles named push-NAME-ARCH with the provided push tags,
# along with a push-NAME docker_push target.
# Args:
# name: name used for the alias; the internal container_image and
# container_bundles are based on this name
# architectures: list of architectures (in GOARCH naming parlance) to
# configure
# base: base image to use for the containers. The format string {ARCH} will
# be replaced with the configured GOARCH.
# docker_tags: list of docker tags to apply to the image. The format string
# {ARCH} will be replaced with the configured GOARCH; any stamping variables
# should be escaped, e.g. {{STABLE_MY_VAR}}.
# docker_push_tags: list of docker tags to apply to the image for pushing.
# The format string {ARCH} will be replaced with the configured GOARCH;
# any stamping variables should be escaped, e.g. {{STABLE_MY_VAR}}.
# tags: will be applied to all targets
# visiblity: will be applied only to the container_bundles; the internal
# container_image is private
# All other args will be applied to the internal container_image.
def multi_arch_container(
name,
architectures,
base,
docker_tags,
docker_push_tags = None,
tags = None,
visibility = None,
**kwargs):
container_image(
name = "%s-internal" % name,
base = select({
go_platform_constraint(os = "linux", arch = arch): base.format(ARCH = arch)
for arch in architectures
}),
tags = tags,
visibility = ["//visibility:private"],
**kwargs
)
for arch in architectures:
container_bundle(
name = "%s-%s" % (name, arch),
images = {
docker_tag.format(ARCH = arch): ":%s-internal" % name
for docker_tag in docker_tags
},
tags = tags,
visibility = visibility,
)
native.alias(
name = name,
actual = select({
go_platform_constraint(os = "linux", arch = arch): "%s-%s" % (name, arch)
for arch in architectures
}),
)
native.genrule(
name = "gen_%s.tar" % name,
outs = ["%s.tar" % name],
srcs = select({
go_platform_constraint(os = "linux", arch = arch): ["%s-%s.tar" % (name, arch)]
for arch in architectures
}),
cmd = "cp $< $@",
output_to_bindir = True,
)
if docker_push_tags:
multi_arch_container_push(
name = name,
architectures = architectures,
docker_tags_images = {docker_push_tag: ":%s-internal" % name for docker_push_tag in docker_push_tags},
tags = tags,
)
# multi_arch_container_push creates container_bundles named push-NAME-ARCH for
# the provided architectures, populating them with the images directory.
# It additionally creates a push-NAME docker_push rule which can be run to
# push the images to a Docker repository.
# Args:
# name: name used for targets created by this macro; the internal
# container_bundles are based on this name
# architectures: list of architectures (in GOARCH naming parlance) to
# configure
# docker_tags_images: dictionary mapping docker tag to the corresponding
# container_image target. The format string {ARCH} will be replaced
# in tags with the configured GOARCH; any stamping variables should be
# escaped, e.g. {{STABLE_MY_VAR}}.
# tags: applied to container_bundle targets
def multi_arch_container_push(
name,
architectures,
docker_tags_images,
tags = None):
for arch in architectures:
container_bundle(
name = "push-%s-%s" % (name, arch),
images = {tag.format(ARCH = arch): image for tag, image in docker_tags_images.items()},
tags = tags,
visibility = ["//visibility:private"],
)
docker_push(
name = "push-%s" % name,
bundle = select({
go_platform_constraint(os = "linux", arch = arch): "push-%s-%s" % (name, arch)
for arch in architectures
}),
)