From 1bd75933636dfdac16c1d9153f4596a57b350b22 Mon Sep 17 00:00:00 2001 From: Winnie Xu Date: Wed, 26 Jun 2019 14:27:24 -0400 Subject: [PATCH] Add new Starlark container load rule new_load.bzl (#929) * Add new Starlark container load rule * resolve buildifier error * add tests for loader * fix buildifier format error * revert version on line 377 of WORKSPACE * fix versioning conflict by changing mal-hashed value * update repositories bzl to include new loader binary release * update docs --- WORKSPACE | 10 ++++++ container/new_load.bzl | 63 +++++++++++++++++++++++++++++++++++ repositories/repositories.bzl | 8 +++++ tests/docker/BUILD | 24 +++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 container/new_load.bzl diff --git a/WORKSPACE b/WORKSPACE index 495858982..1300b3738 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -97,6 +97,16 @@ container_load( file = "//testdata:pause.tar", ) +load( + "//container:new_load.bzl", + "new_container_load", +) + +new_container_load( + name = "new_pause_tar", + file = "//testdata:pause.tar", +) + container_pull( name = "alpine_linux_amd64", registry = "index.docker.io", diff --git a/container/new_load.bzl b/container/new_load.bzl new file mode 100644 index 000000000..3f8951e64 --- /dev/null +++ b/container/new_load.bzl @@ -0,0 +1,63 @@ +# 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 loading an image from 'docker save' tarball or the current + container_pull tarball format into OCI intermediate layout. + +This extracts the tarball amd creates a filegroup of the untarred objects in OCI layout. +""" + +def _impl(repository_ctx): + """Core implementation of new_container_load.""" + + # Add an empty top-level BUILD file. + repository_ctx.file("BUILD", "") + + repository_ctx.file("image/BUILD", """ +package(default_visibility = ["//visibility:public"]) + +# TODO(xwinxu): this will be changed to new_container_import once that is implemented later +# similar to what we have in new_pull.bzl + +filegroup( + name = "image", + srcs = glob(["image/**"]), +) +exports_files(glob(["**"])) +""", executable = False) + + result = repository_ctx.execute([ + repository_ctx.path(repository_ctx.attr._loader), + "-directory", + repository_ctx.path("image"), + "-tarball", + repository_ctx.path(repository_ctx.attr.file), + ]) + + if result.return_code: + fail("Importing from tarball failed (status %s): %s" % (result.return_code, result.stderr)) + +new_container_load = repository_rule( + attrs = { + "file": attr.label( + allow_single_file = True, + mandatory = True, + ), + "_loader": attr.label( + executable = True, + default = Label("@loader//file:downloaded"), + cfg = "host", + ), + }, + implementation = _impl, +) diff --git a/repositories/repositories.bzl b/repositories/repositories.bzl index 4695f2d38..66364af46 100644 --- a/repositories/repositories.bzl +++ b/repositories/repositories.bzl @@ -84,6 +84,14 @@ def repositories(): CONTAINERREGISTRY_RELEASE + "/importer.par")], ) + if "loader" not in excludes: + http_file( + name = "loader", + executable = True, + sha256 = "30bbb44eae9651a55d07fb9d39f58936fe3d9817780e1887df07d7beb21ef5ad", + urls = [("https://storage.googleapis.com/rules_docker/a08df0ab2a345cd07359bb69672dcf21867e50e5/loader-linux-amd64")], + ) + if "containerregistry" not in excludes: http_archive( name = "containerregistry", diff --git a/tests/docker/BUILD b/tests/docker/BUILD index ea26a45fc..1c8dc5c42 100644 --- a/tests/docker/BUILD +++ b/tests/docker/BUILD @@ -430,6 +430,30 @@ file_test( file = "@new_distroless_base_both//image:oci-layout", ) +# To test the new container load binary +file_test( + name = "new_pause_tar_test_index_json", + content = """{ + "schemaVersion": 2, + "manifests": [ + { + "mediaType": "application/vnd.docker.distribution.manifest.v2+json", + "size": 586, + "digest": "sha256:22e0bb47f6dd1938d7c30febe82ac37e99907fd8c76421ab87829ad6a3636bf9" + } + ] +}""", + file = "@new_pause_tar//image:index.json", +) + +file_test( + name = "new_pause_tar_test_oci_layout", + content = """{ + "imageLayoutVersion": "1.0.0" +}""", + file = "@new_pause_tar//image:oci-layout", +) + create_banana_directory( name = "banana_directory", )