Skip to content

Commit 8495f7c

Browse files
committed
bazel: add docker_push rules
1 parent 5b7602a commit 8495f7c

6 files changed

Lines changed: 83 additions & 11 deletions

File tree

build/BUILD

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package(default_visibility = ["//visibility:public"])
22

33
load("@io_k8s_repo_infra//defs:build.bzl", "release_filegroup")
44
load(":code_generation_test.bzl", "code_generation_test_suite")
5-
load(":container.bzl", "multi_arch_container")
5+
load(":container.bzl", "multi_arch_container", "multi_arch_container_push")
66
load(":platforms.bzl", "SERVER_PLATFORMS", "for_platforms")
77

88
code_generation_test_suite(
@@ -69,7 +69,12 @@ DOCKERIZED_BINARIES = {
6969
for_server = ["//build/debs:%s-{ARCH}.deb" % binary],
7070
only_os = "linux",
7171
)),
72-
docker_tags = ["k8s.gcr.io/%s:{{STABLE_DOCKER_TAG}}" % binary],
72+
# Since the multi_arch_container macro replaces the {ARCH} format string,
73+
# we need to escape the stamping vars.
74+
# Also see comment above about why the push tags use ARCH while the
75+
# non-push tags do not.
76+
docker_push_tags = ["{{STABLE_DOCKER_PUSH_REGISTRY}}/%s-{ARCH}:{{STABLE_DOCKER_TAG}}" % binary],
77+
docker_tags = ["{{STABLE_DOCKER_REGISTRY}}/%s:{{STABLE_DOCKER_TAG}}" % binary],
7378
stamp = True,
7479
symlinks = {
7580
# Some cluster startup scripts expect to find the binaries in /usr/local/bin,
@@ -80,6 +85,17 @@ DOCKERIZED_BINARIES = {
8085
visibility = ["//visibility:private"],
8186
) for binary, meta in DOCKERIZED_BINARIES.items()]
8287

88+
# Also roll up all images into a single bundle to push with one target.
89+
multi_arch_container_push(
90+
name = "server-images",
91+
architectures = SERVER_PLATFORMS["linux"],
92+
docker_tags_images = {
93+
"{{STABLE_DOCKER_PUSH_REGISTRY}}/%s-{ARCH}:{{STABLE_DOCKER_TAG}}" % binary: "%s-internal" % binary
94+
for binary in DOCKERIZED_BINARIES.keys()
95+
},
96+
tags = ["manual"],
97+
)
98+
8399
[genrule(
84100
name = binary + "_docker_tag",
85101
srcs = [meta["target"]],

build/container.bzl

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
# limitations under the License.
1414

1515
load("@io_bazel_rules_docker//container:container.bzl", "container_bundle", "container_image")
16+
load("@io_bazel_rules_docker//contrib:push-all.bzl", "docker_push")
1617
load("//build:platforms.bzl", "go_platform_constraint")
1718

1819
# multi_arch_container produces a private internal container_image, multiple
1920
# arch-specific tagged container_bundles (named NAME-ARCH) and aliases
2021
# from NAME and NAME.tar to the appropriately NAME-ARCH container_bundle target
2122
# for the currently-configured architecture.
23+
# Additionally, if docker_push_tags is provided, uses multi_arch_container_push
24+
# to create container_bundles named push-NAME-ARCH with the provided push tags,
25+
# along with a push-NAME docker_push target.
2226
# Args:
2327
# name: name used for the alias; the internal container_image and
2428
# container_bundles are based on this name
@@ -29,7 +33,10 @@ load("//build:platforms.bzl", "go_platform_constraint")
2933
# docker_tags: list of docker tags to apply to the image. The format string
3034
# {ARCH} will be replaced with the configured GOARCH; any stamping variables
3135
# should be escaped, e.g. {{STABLE_MY_VAR}}.
32-
# tags: will be applied to all rules
36+
# docker_push_tags: list of docker tags to apply to the image for pushing.
37+
# The format string {ARCH} will be replaced with the configured GOARCH;
38+
# any stamping variables should be escaped, e.g. {{STABLE_MY_VAR}}.
39+
# tags: will be applied to all targets
3340
# visiblity: will be applied only to the container_bundles; the internal
3441
# container_image is private
3542
# All other args will be applied to the internal container_image.
@@ -38,6 +45,7 @@ def multi_arch_container(
3845
architectures,
3946
base,
4047
docker_tags,
48+
docker_push_tags = None,
4149
tags = None,
4250
visibility = None,
4351
**kwargs):
@@ -70,3 +78,45 @@ def multi_arch_container(
7078
for arch in architectures
7179
}),
7280
)
81+
82+
if docker_push_tags:
83+
multi_arch_container_push(
84+
name = name,
85+
architectures = architectures,
86+
docker_tags_images = {docker_push_tag: ":%s-internal" % name for docker_push_tag in docker_push_tags},
87+
tags = tags,
88+
)
89+
90+
# multi_arch_container_push creates container_bundles named push-NAME-ARCH for
91+
# the provided architectures, populating them with the images directory.
92+
# It additionally creates a push-NAME docker_push rule which can be run to
93+
# push the images to a Docker repository.
94+
# Args:
95+
# name: name used for targets created by this macro; the internal
96+
# container_bundles are based on this name
97+
# architectures: list of architectures (in GOARCH naming parlance) to
98+
# configure
99+
# docker_tags_images: dictionary mapping docker tag to the corresponding
100+
# container_image target. The format string {ARCH} will be replaced
101+
# in tags with the configured GOARCH; any stamping variables should be
102+
# escaped, e.g. {{STABLE_MY_VAR}}.
103+
# tags: applied to container_bundle targets
104+
def multi_arch_container_push(
105+
name,
106+
architectures,
107+
docker_tags_images,
108+
tags = None):
109+
for arch in architectures:
110+
container_bundle(
111+
name = "push-%s-%s" % (name, arch),
112+
images = {tag.format(ARCH = arch): image for tag, image in docker_tags_images.items()},
113+
tags = tags,
114+
visibility = ["//visibility:private"],
115+
)
116+
docker_push(
117+
name = "push-%s" % name,
118+
bundle = select({
119+
go_platform_constraint(os = "linux", arch = arch): "push-%s-%s" % (name, arch)
120+
for arch in architectures
121+
}),
122+
)

build/root/BUILD.root

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ filegroup(
3232
visibility = ["//visibility:private"],
3333
)
3434

35-
# TODO: also add container_push rules,
36-
# and don't forget about the conformance and hyperkube images
35+
# TODO: collect all relevant docker_push targets into one target that can be run:
36+
# //build:push-server-images
37+
# //cluster/images/conformance:push-conformance
38+
# //cluster/images/hyperkube:push-hyperkube
3739
gcs_upload(
3840
name = "push-build",
3941
data = [

cluster/images/conformance/BUILD

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ multi_arch_container(
2828
"-c",
2929
"/run_e2e.sh",
3030
],
31-
# {ARCH} is replaced by the macro, but STABLE_DOCKER_TAG is replaced by the
32-
# build stamping, so we need to escape it
33-
docker_tags = ["k8s.gcr.io/conformance-{ARCH}:{{STABLE_DOCKER_TAG}}"],
31+
# {ARCH} is replaced by the macro, but STABLE_ vars are replaced by the
32+
# build stamping, so we need to escape them
33+
docker_push_tags = ["{{STABLE_DOCKER_PUSH_REGISTRY}}/conformance-{ARCH}:{{STABLE_DOCKER_TAG}}"],
34+
docker_tags = ["{{STABLE_DOCKER_REGISTRY}}/conformance-{ARCH}:{{STABLE_DOCKER_TAG}}"],
3435
env = {
3536
"E2E_FOCUS": "\[Conformance\]",
3637
"E2E_SKIP": "",

cluster/images/hyperkube/BUILD

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ multi_arch_container(
55
name = "hyperkube",
66
architectures = SERVER_PLATFORMS["linux"],
77
base = "@debian-hyperkube-base-{ARCH}//image",
8-
# {ARCH} is replaced by the macro, but STABLE_DOCKER_TAG is replaced by the
9-
# build stamping, so we need to escape it
10-
docker_tags = ["k8s.gcr.io/hyperkube-{ARCH}:{{STABLE_DOCKER_TAG}}"],
8+
# {ARCH} is replaced by the macro, but STABLE_ vars are replaced by the
9+
# build stamping, so we need to escape them
10+
docker_push_tags = ["{{STABLE_DOCKER_PUSH_REGISTRY}}/hyperkube-{ARCH}:{{STABLE_DOCKER_TAG}}"],
11+
docker_tags = ["{{STABLE_DOCKER_REGISTRY}}/hyperkube-{ARCH}:{{STABLE_DOCKER_TAG}}"],
1112
files = [
1213
"//cmd/hyperkube",
1314
],

hack/print-workspace-status.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ STABLE_BUILD_SCM_REVISION ${KUBE_GIT_VERSION-}
3838
STABLE_BUILD_MAJOR_VERSION ${KUBE_GIT_MAJOR-}
3939
STABLE_BUILD_MINOR_VERSION ${KUBE_GIT_MINOR-}
4040
STABLE_DOCKER_TAG ${KUBE_GIT_VERSION/+/_}
41+
STABLE_DOCKER_REGISTRY ${KUBE_DOCKER_REGISTRY:-k8s.gcr.io}
42+
STABLE_DOCKER_PUSH_REGISTRY ${KUBE_DOCKER_PUSH_REGISTRY:-${KUBE_DOCKER_REGISTRY:-staging-k8s.gcr.io}}
4143
gitCommit ${KUBE_GIT_COMMIT-}
4244
gitTreeState ${KUBE_GIT_TREE_STATE-}
4345
gitVersion ${KUBE_GIT_VERSION-}

0 commit comments

Comments
 (0)