From d7c2c2a606aae0edbff29f61ebda88b5e58c06ed Mon Sep 17 00:00:00 2001 From: Jeff Grafton Date: Wed, 6 Mar 2019 11:40:33 -0800 Subject: [PATCH] bazel: refactor multi-arch container builds into starlark --- build/container.bzl | 72 ++++++++++++++++++++++++++++++++ cluster/images/conformance/BUILD | 35 ++++++---------- cluster/images/hyperkube/BUILD | 32 +++++--------- 3 files changed, 94 insertions(+), 45 deletions(-) create mode 100644 build/container.bzl diff --git a/build/container.bzl b/build/container.bzl new file mode 100644 index 0000000000..a11e14ffb1 --- /dev/null +++ b/build/container.bzl @@ -0,0 +1,72 @@ +# 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("//build:platforms.bzl", "go_platform_constraint") + +# multi_arch_container produces a private internal container_image, multiple +# arch-specific tagged container_bundles (named NAME-ARCH) and aliases +# from NAME and NAME.tar to the appropriately NAME-ARCH container_bundle target +# for the currently-configured architecture. +# 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}}. +# tags: will be applied to all rules +# 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, + 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, + ) + for suffix in ["", ".tar"]: + native.alias( + name = "%s%s" % (name, suffix), + actual = select({ + go_platform_constraint(os = "linux", arch = arch): "%s-%s%s" % (name, arch, suffix) + for arch in architectures + }), + ) diff --git a/cluster/images/conformance/BUILD b/cluster/images/conformance/BUILD index 00e8e33435..3f4fa46342 100644 --- a/cluster/images/conformance/BUILD +++ b/cluster/images/conformance/BUILD @@ -1,5 +1,6 @@ -load("@io_bazel_rules_docker//container:container.bzl", "container_bundle", "container_image", "container_layer") -load("//build:platforms.bzl", "SERVER_PLATFORMS", "for_platforms") +load("@io_bazel_rules_docker//container:container.bzl", "container_layer") +load("//build:platforms.bzl", "SERVER_PLATFORMS") +load("//build:container.bzl", "multi_arch_container") container_layer( name = "cluster-srcs", @@ -18,17 +19,18 @@ container_layer( ], ) -container_image( - name = "conformance-internal", - base = select(for_platforms( - for_server = "@debian-hyperkube-base-{ARCH}//image", - only_os = "linux", - )), +multi_arch_container( + name = "conformance", + architectures = SERVER_PLATFORMS["linux"], + base = "@debian-hyperkube-base-{ARCH}//image", cmd = [ "/bin/bash", "-c", "/run_e2e.sh", ], + # {ARCH} is replaced by the macro, but STABLE_DOCKER_TAG is replaced by the + # build stamping, so we need to escape it + docker_tags = ["k8s.gcr.io/conformance-{ARCH}:{{STABLE_DOCKER_TAG}}"], env = { "E2E_FOCUS": "\[Conformance\]", "E2E_SKIP": "", @@ -45,23 +47,10 @@ container_image( ":bins", ], stamp = True, - workdir = "/usr/local/bin", -) - -[container_bundle( - name = "conformance-%s" % arch, - images = {"k8s.gcr.io/conformance-%s:{STABLE_DOCKER_TAG}" % arch: "conformance-internal"}, tags = ["manual"], visibility = ["//visibility:public"], -) for arch in SERVER_PLATFORMS["linux"]] - -[alias( - name = "conformance%s" % suffix, - actual = select(for_platforms(for_server = "conformance-{ARCH}%s" % suffix)), -) for suffix in [ - "", - ".tar", -]] + workdir = "/usr/local/bin", +) filegroup( name = "package-srcs", diff --git a/cluster/images/hyperkube/BUILD b/cluster/images/hyperkube/BUILD index 201f5abd35..f0e376edb0 100644 --- a/cluster/images/hyperkube/BUILD +++ b/cluster/images/hyperkube/BUILD @@ -1,32 +1,20 @@ -load("@io_bazel_rules_docker//container:container.bzl", "container_bundle", "container_image") -load("//build:platforms.bzl", "SERVER_PLATFORMS", "for_platforms") +load("//build:container.bzl", "multi_arch_container") +load("//build:platforms.bzl", "SERVER_PLATFORMS") -container_image( - name = "hyperkube-internal", - base = select(for_platforms( - for_server = "@debian-hyperkube-base-{ARCH}//image", - only_os = "linux", - )), +multi_arch_container( + name = "hyperkube", + architectures = SERVER_PLATFORMS["linux"], + base = "@debian-hyperkube-base-{ARCH}//image", + # {ARCH} is replaced by the macro, but STABLE_DOCKER_TAG is replaced by the + # build stamping, so we need to escape it + docker_tags = ["k8s.gcr.io/hyperkube-{ARCH}:{{STABLE_DOCKER_TAG}}"], files = [ "//cmd/hyperkube", ], stamp = True, -) - -[container_bundle( - name = "hyperkube-%s" % arch, - images = {"k8s.gcr.io/hyperkube-%s:{STABLE_DOCKER_TAG}" % arch: "hyperkube-internal"}, tags = ["manual"], visibility = ["//visibility:public"], -) for arch in SERVER_PLATFORMS["linux"]] - -[alias( - name = "hyperkube%s" % suffix, - actual = select(for_platforms(for_server = "hyperkube-{ARCH}%s" % suffix)), -) for suffix in [ - "", - ".tar", -]] +) filegroup( name = "package-srcs",