Skip to content

Commit

Permalink
feat: demonstrate fine grained layering
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Aug 3, 2023
1 parent ebcad93 commit f7edfad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
6 changes: 5 additions & 1 deletion oci_python_image/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

oci.pull(
name = "distroless_python",
digest = "sha256:b48e216f7c4adcf24fecd7016f3b8ead76866a19571819f67f47c1ccaf899717",
image = "python",
digest = "sha256:9a1b705aecedc76e8bf87dfca091d7093df3f2dd4765af6c250134ce4298a584",
platforms = [
"linux/amd64",
"linux/arm64/v8"
]
)

use_repo(oci, "distroless_python")
4 changes: 2 additions & 2 deletions oci_python_image/hello_world/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ platform(
)

platform_transition_filegroup(
name = "transitioned_image",
name = "platform_image",
srcs = [":image"],
target_platform = select({
"@platforms//cpu:arm64": ":aarch64_linux",
Expand All @@ -66,7 +66,7 @@ platform_transition_filegroup(
# $ docker run --rm gcr.io/oci_python_hello_world:latest
oci_tarball(
name = "tarball",
image = ":image",
image = ":platform_image",
repo_tags = ["gcr.io/oci_python_hello_world:latest"],
)

Expand Down
15 changes: 15 additions & 0 deletions oci_python_image/py_image_layer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def py_image_layer(name, binary, root = None, **kwargs):

runfiles(
name = "%s/app/runfiles" % name,
include = "**/*",
exclude = "**/*python*-linux*/**",
**runfiles_kwargs
)

runfiles(
name = "%s/python/runfiles" % name,
include = "**/*python*-linux*/**",
**runfiles_kwargs
)

Expand All @@ -51,11 +59,18 @@ def py_image_layer(name, binary, root = None, **kwargs):
srcs = ["%s/app/runfiles" % name],
**pkg_tar_kwargs
)

pkg_tar(
name = "%s/python" % name,
srcs = ["%s/python/runfiles" % name],
**pkg_tar_kwargs
)

native.filegroup(
name = name,
srcs = [
"%s/app" % name,
"%s/python" % name,
],
**common_kwargs
)
17 changes: 9 additions & 8 deletions oci_python_image/workaround_rules_pkg_153/runfiles.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ See https://github.com/bazelbuild/rules_pkg/issues/153

load("@rules_pkg//:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo", "PackageSymlinkInfo")
load("@aspect_bazel_lib//lib:paths.bzl", "to_manifest_path")
load("@aspect_bazel_lib//lib:glob_match.bzl", "glob_match")

def _runfile_path(ctx, file, runfiles_dir):
return "/".join([runfiles_dir, to_manifest_path(ctx, file)])

def _should_include(destination, include, exclude):
included = include in destination or include == ""
excluded = exclude in destination and exclude != ""
return included and not excluded
def glob(path, include, exclude = ""):
if exclude and glob_match(exclude, path):
return False
return glob_match(include, path)

def _runfiles_impl(ctx):
default = ctx.attr.binary[DefaultInfo]
Expand All @@ -23,7 +24,7 @@ def _runfiles_impl(ctx):

file_map = {}

if _should_include(executable_path, ctx.attr.include, ctx.attr.exclude):
if glob(executable_path, ctx.attr.include, ctx.attr.exclude):
file_map[executable_path] = executable

manifest = default.files_to_run.runfiles_manifest
Expand All @@ -33,7 +34,7 @@ def _runfiles_impl(ctx):

for file in files.to_list():
destination = _runfile_path(ctx, file, runfiles_dir)
if _should_include(destination, ctx.attr.include, ctx.attr.exclude):
if glob(destination, ctx.attr.include, ctx.attr.exclude):
file_map[destination] = file

# executable should not go into runfiles directory so we add it to files here.
Expand All @@ -45,7 +46,7 @@ def _runfiles_impl(ctx):
# root_symlinks and symlinks and why they have to be handled differently.
for symlink in default.data_runfiles.symlinks.to_list():
destination = "/".join([runfiles_dir, ctx.workspace_name, symlink.path])
if not _should_include(destination, ctx.attr.include, ctx.attr.exclude):
if not glob(destination, ctx.attr.include, ctx.attr.exclude):
continue
if hasattr(file_map, destination):
file_map.pop(destination)
Expand All @@ -58,7 +59,7 @@ def _runfiles_impl(ctx):

for symlink in default.data_runfiles.root_symlinks.to_list():
destination = "/".join([runfiles_dir, symlink.path])
if not _should_include(destination, ctx.attr.include, ctx.attr.exclude):
if not glob(destination, ctx.attr.include, ctx.attr.exclude):
continue
if hasattr(file_map, destination):
file_map.pop(destination)
Expand Down

0 comments on commit f7edfad

Please sign in to comment.