Skip to content

Commit

Permalink
WIP: Starting to build puller/pusher instead of downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
gravypod committed Jul 23, 2021
1 parent b886744 commit 8f01414
Show file tree
Hide file tree
Showing 269 changed files with 42,740 additions and 36 deletions.
60 changes: 24 additions & 36 deletions container/pull.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"container_pull rule"

load("@io_bazel_rules_go//go/private:common.bzl", "env_execute", "executable_extension")

_DOC = """A repository rule that pulls down a Docker base image in a manner suitable for use with the `base` attribute of `container_image`.
This is based on google/containerregistry using google/go-containerregistry.
Expand Down Expand Up @@ -81,30 +84,6 @@ _container_pull_attrs = {
"platform_features": attr.string_list(
doc = "Specifies platform features when pulling a multi-platform manifest list.",
),
"puller_darwin": attr.label(
executable = True,
default = Label("@go_puller_darwin//file:downloaded"),
cfg = "host",
doc = "Exposed to provide a way to test other pullers on macOS",
),
"puller_linux_amd64": attr.label(
executable = True,
default = Label("@go_puller_linux_amd64//file:downloaded"),
cfg = "host",
doc = "Exposed to provide a way to test other pullers on Linux",
),
"puller_linux_arm64": attr.label(
executable = True,
default = Label("@go_puller_linux_arm64//file:downloaded"),
cfg = "host",
doc = "Exposed to provide a way to test other pullers on Linux",
),
"puller_linux_s390x": attr.label(
executable = True,
default = Label("@go_puller_linux_s390x//file:downloaded"),
cfg = "host",
doc = "Exposed to provide a way to test other pullers on Linux",
),
"registry": attr.string(
mandatory = True,
doc = "The registry from which we are pulling.",
Expand Down Expand Up @@ -136,18 +115,9 @@ def _impl(repository_ctx):

import_rule_tags = "[\"{}\"]".format("\", \"".join(repository_ctx.attr.import_tags))

puller = repository_ctx.attr.puller_linux_amd64
if repository_ctx.os.name.lower().startswith("mac os"):
puller = repository_ctx.attr.puller_darwin
elif repository_ctx.os.name.lower().startswith("linux"):
arch = repository_ctx.execute(["uname", "-m"]).stdout.strip()
if arch == "arm64" or arch == "aarch64":
puller = repository_ctx.attr.puller_linux_arm64
elif arch == "s390x":
puller = repository_ctx.attr.puller_linux_s390x

puller = str(repository_ctx.path(Label("@rules_docker_repository_tools//:bin/puller{}".format(executable_extension(repository_ctx)))))
args = [
repository_ctx.path(puller),
puller,
"-directory",
repository_ctx.path("image"),
"-os",
Expand Down Expand Up @@ -211,7 +181,25 @@ def _impl(repository_ctx):
else:
fail("'%s' is invalid value for PULLER_TIMEOUT. Must be an integer." % (timeout_in_secs))

result = repository_ctx.execute(args, **kwargs)
env = {
# TODO(gravypod): Fix this later
# k: v
# for k, v in repository_ctx.os.environ
# if k.lower() in [
# "home",
#
# # Used by the puller/pusher?
# # TODO(gravypod): Validate that this is the case.
# "ssh_auth_sock",
# "ssl_cert_file",
# "ssl_cert_dir",
# "http_proxy",
# "https_proxy",
# "no_proxy",
# ]
}

result = env_execute(repository_ctx, args, environment = env, **kwargs)
if result.return_code:
fail("Pull command failed: %s (%s)" % (result.stderr, " ".join([str(a) for a in args])))

Expand Down
519 changes: 519 additions & 0 deletions go.sum

Large diffs are not rendered by default.

Empty file added internal/BUILD.bazel
Empty file.
111 changes: 111 additions & 0 deletions internal/rules_docker_repository_tools.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Copyright 2019 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.

load("@io_bazel_rules_go//go/private:common.bzl", "env_execute", "executable_extension")
load("@bazel_gazelle//internal:go_repository_cache.bzl", "read_cache_env")

_RULES_DOCKER_TOOLS_BUILD_FILE = """
package(default_visibility = ["//visibility:public"])
filegroup(
name = "puller",
srcs = ["bin/puller{extension}"],
)
filegroup(
name = "pusher",
srcs = ["bin/pusher{extension}"],
)
exports_files(["ROOT"])
"""

def _rules_docker_repository_tools_impl(ctx):
# Create a link to the rules_docker repo. This will be our GOPATH.
env = read_cache_env(ctx, str(ctx.path(ctx.attr.go_cache)))
extension = executable_extension(ctx)
go_tool = env["GOROOT"] + "/bin/go" + extension

ctx.symlink(
ctx.path(Label("@io_bazel_rules_docker//:WORKSPACE")).dirname,
"src/github.com/bazelbuild/rules_docker",
)

env.update({
"GOPATH": str(ctx.path(".")),
# TODO(gravypod): make this more hermetic
"GO111MODULE": "off",
# workaround: avoid the Go SDK paths from leaking into the binary
"GOROOT_FINAL": "GOROOT",
# workaround: avoid cgo paths in /tmp leaking into binary
"CGO_ENABLED": "0",
})

if "PATH" in ctx.os.environ:
# workaround: to find gcc for go link tool on Arm platform
env["PATH"] = ctx.os.environ["PATH"]
if "GOPROXY" in ctx.os.environ:
env["GOPROXY"] = ctx.os.environ["GOPROXY"]

# Build the tools.
args = [
go_tool,
"install",
"-ldflags",
"-w -s",
"-gcflags",
"all=-trimpath=" + env["GOPATH"],
"-asmflags",
"all=-trimpath=" + env["GOPATH"],
"github.com/bazelbuild/rules_docker/container/go/cmd/puller",
"github.com/bazelbuild/rules_docker/container/go/cmd/pusher",
]
result = env_execute(ctx, args, environment = env)
if result.return_code:
fail("failed to build tools: " + result.stderr)

# add a build file to export the tools
ctx.file(
"BUILD.bazel",
_RULES_DOCKER_TOOLS_BUILD_FILE.format(extension = executable_extension(ctx)),
False,
)
ctx.file(
"ROOT",
"",
False,
)

rules_docker_repository_tools = repository_rule(
_rules_docker_repository_tools_impl,
attrs = {
"go_cache": attr.label(
mandatory = True,
allow_single_file = True,
),
},
environ = [
"GOCACHE",
"GOPATH",
"GO_REPOSITORY_USE_HOST_CACHE", # TODO(gravypod): Find out why this is here in rules_go
],
)
"""go_repository_tools is a synthetic repository used by go_repository.
go_repository depends on two Go binaries: fetch_repo and gazelle. We can't
build these with Bazel inside a repository rule, and we don't want to manage
prebuilt binaries, so we build them in here with go build, using whichever
SDK rules_go is using.
"""
9 changes: 9 additions & 0 deletions repositories/go_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ repository.

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load(
"//internal:rules_docker_repository_tools.bzl",
"rules_docker_repository_tools",
)

def go_deps():
"""Pull in external Go packages needed by Go binaries in this repo.
Expand All @@ -33,6 +37,11 @@ def go_deps():
go_rules_dependencies()
go_register_toolchains()
gazelle_dependencies()

rules_docker_repository_tools(
name = "rules_docker_repository_tools",
go_cache = "@bazel_gazelle_go_repository_cache//:go.env",
)
excludes = native.existing_rules().keys()
if "com_github_google_go_containerregistry" not in excludes:
go_repository(
Expand Down
Loading

0 comments on commit 8f01414

Please sign in to comment.