Skip to content

Commit

Permalink
Add new Starlark container load rule new_load.bzl (#929)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xwinxu authored and k8s-ci-robot committed Jun 26, 2019
1 parent ac23af6 commit 1bd7593
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
10 changes: 10 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
63 changes: 63 additions & 0 deletions container/new_load.bzl
Original file line number Diff line number Diff line change
@@ -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,
)
8 changes: 8 additions & 0 deletions repositories/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions tests/docker/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down

0 comments on commit 1bd7593

Please sign in to comment.