Skip to content

Commit 8f01414

Browse files
committed
WIP: Starting to build puller/pusher instead of downloading
1 parent b886744 commit 8f01414

File tree

269 files changed

+42740
-36
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

269 files changed

+42740
-36
lines changed

container/pull.bzl

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
"container_pull rule"
15+
16+
load("@io_bazel_rules_go//go/private:common.bzl", "env_execute", "executable_extension")
17+
1518
_DOC = """A repository rule that pulls down a Docker base image in a manner suitable for use with the `base` attribute of `container_image`.
1619
1720
This is based on google/containerregistry using google/go-containerregistry.
@@ -81,30 +84,6 @@ _container_pull_attrs = {
8184
"platform_features": attr.string_list(
8285
doc = "Specifies platform features when pulling a multi-platform manifest list.",
8386
),
84-
"puller_darwin": attr.label(
85-
executable = True,
86-
default = Label("@go_puller_darwin//file:downloaded"),
87-
cfg = "host",
88-
doc = "Exposed to provide a way to test other pullers on macOS",
89-
),
90-
"puller_linux_amd64": attr.label(
91-
executable = True,
92-
default = Label("@go_puller_linux_amd64//file:downloaded"),
93-
cfg = "host",
94-
doc = "Exposed to provide a way to test other pullers on Linux",
95-
),
96-
"puller_linux_arm64": attr.label(
97-
executable = True,
98-
default = Label("@go_puller_linux_arm64//file:downloaded"),
99-
cfg = "host",
100-
doc = "Exposed to provide a way to test other pullers on Linux",
101-
),
102-
"puller_linux_s390x": attr.label(
103-
executable = True,
104-
default = Label("@go_puller_linux_s390x//file:downloaded"),
105-
cfg = "host",
106-
doc = "Exposed to provide a way to test other pullers on Linux",
107-
),
10887
"registry": attr.string(
10988
mandatory = True,
11089
doc = "The registry from which we are pulling.",
@@ -136,18 +115,9 @@ def _impl(repository_ctx):
136115

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

139-
puller = repository_ctx.attr.puller_linux_amd64
140-
if repository_ctx.os.name.lower().startswith("mac os"):
141-
puller = repository_ctx.attr.puller_darwin
142-
elif repository_ctx.os.name.lower().startswith("linux"):
143-
arch = repository_ctx.execute(["uname", "-m"]).stdout.strip()
144-
if arch == "arm64" or arch == "aarch64":
145-
puller = repository_ctx.attr.puller_linux_arm64
146-
elif arch == "s390x":
147-
puller = repository_ctx.attr.puller_linux_s390x
148-
118+
puller = str(repository_ctx.path(Label("@rules_docker_repository_tools//:bin/puller{}".format(executable_extension(repository_ctx)))))
149119
args = [
150-
repository_ctx.path(puller),
120+
puller,
151121
"-directory",
152122
repository_ctx.path("image"),
153123
"-os",
@@ -211,7 +181,25 @@ def _impl(repository_ctx):
211181
else:
212182
fail("'%s' is invalid value for PULLER_TIMEOUT. Must be an integer." % (timeout_in_secs))
213183

214-
result = repository_ctx.execute(args, **kwargs)
184+
env = {
185+
# TODO(gravypod): Fix this later
186+
# k: v
187+
# for k, v in repository_ctx.os.environ
188+
# if k.lower() in [
189+
# "home",
190+
#
191+
# # Used by the puller/pusher?
192+
# # TODO(gravypod): Validate that this is the case.
193+
# "ssh_auth_sock",
194+
# "ssl_cert_file",
195+
# "ssl_cert_dir",
196+
# "http_proxy",
197+
# "https_proxy",
198+
# "no_proxy",
199+
# ]
200+
}
201+
202+
result = env_execute(repository_ctx, args, environment = env, **kwargs)
215203
if result.return_code:
216204
fail("Pull command failed: %s (%s)" % (result.stderr, " ".join([str(a) for a in args])))
217205

go.sum

Lines changed: 519 additions & 0 deletions
Large diffs are not rendered by default.

internal/BUILD.bazel

Whitespace-only changes.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@io_bazel_rules_go//go/private:common.bzl", "env_execute", "executable_extension")
16+
load("@bazel_gazelle//internal:go_repository_cache.bzl", "read_cache_env")
17+
18+
_RULES_DOCKER_TOOLS_BUILD_FILE = """
19+
package(default_visibility = ["//visibility:public"])
20+
21+
filegroup(
22+
name = "puller",
23+
srcs = ["bin/puller{extension}"],
24+
)
25+
26+
filegroup(
27+
name = "pusher",
28+
srcs = ["bin/pusher{extension}"],
29+
)
30+
31+
exports_files(["ROOT"])
32+
"""
33+
34+
def _rules_docker_repository_tools_impl(ctx):
35+
# Create a link to the rules_docker repo. This will be our GOPATH.
36+
env = read_cache_env(ctx, str(ctx.path(ctx.attr.go_cache)))
37+
extension = executable_extension(ctx)
38+
go_tool = env["GOROOT"] + "/bin/go" + extension
39+
40+
ctx.symlink(
41+
ctx.path(Label("@io_bazel_rules_docker//:WORKSPACE")).dirname,
42+
"src/github.com/bazelbuild/rules_docker",
43+
)
44+
45+
env.update({
46+
"GOPATH": str(ctx.path(".")),
47+
# TODO(gravypod): make this more hermetic
48+
"GO111MODULE": "off",
49+
# workaround: avoid the Go SDK paths from leaking into the binary
50+
"GOROOT_FINAL": "GOROOT",
51+
# workaround: avoid cgo paths in /tmp leaking into binary
52+
"CGO_ENABLED": "0",
53+
})
54+
55+
if "PATH" in ctx.os.environ:
56+
# workaround: to find gcc for go link tool on Arm platform
57+
env["PATH"] = ctx.os.environ["PATH"]
58+
if "GOPROXY" in ctx.os.environ:
59+
env["GOPROXY"] = ctx.os.environ["GOPROXY"]
60+
61+
# Build the tools.
62+
args = [
63+
go_tool,
64+
"install",
65+
"-ldflags",
66+
"-w -s",
67+
"-gcflags",
68+
"all=-trimpath=" + env["GOPATH"],
69+
"-asmflags",
70+
"all=-trimpath=" + env["GOPATH"],
71+
"github.com/bazelbuild/rules_docker/container/go/cmd/puller",
72+
"github.com/bazelbuild/rules_docker/container/go/cmd/pusher",
73+
]
74+
result = env_execute(ctx, args, environment = env)
75+
if result.return_code:
76+
fail("failed to build tools: " + result.stderr)
77+
78+
# add a build file to export the tools
79+
ctx.file(
80+
"BUILD.bazel",
81+
_RULES_DOCKER_TOOLS_BUILD_FILE.format(extension = executable_extension(ctx)),
82+
False,
83+
)
84+
ctx.file(
85+
"ROOT",
86+
"",
87+
False,
88+
)
89+
90+
rules_docker_repository_tools = repository_rule(
91+
_rules_docker_repository_tools_impl,
92+
attrs = {
93+
"go_cache": attr.label(
94+
mandatory = True,
95+
allow_single_file = True,
96+
),
97+
},
98+
environ = [
99+
"GOCACHE",
100+
"GOPATH",
101+
"GO_REPOSITORY_USE_HOST_CACHE", # TODO(gravypod): Find out why this is here in rules_go
102+
],
103+
)
104+
"""go_repository_tools is a synthetic repository used by go_repository.
105+
106+
107+
go_repository depends on two Go binaries: fetch_repo and gazelle. We can't
108+
build these with Bazel inside a repository rule, and we don't want to manage
109+
prebuilt binaries, so we build them in here with go build, using whichever
110+
SDK rules_go is using.
111+
"""

repositories/go_repositories.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ repository.
2121

2222
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
2323
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
24+
load(
25+
"//internal:rules_docker_repository_tools.bzl",
26+
"rules_docker_repository_tools",
27+
)
2428

2529
def go_deps():
2630
"""Pull in external Go packages needed by Go binaries in this repo.
@@ -33,6 +37,11 @@ def go_deps():
3337
go_rules_dependencies()
3438
go_register_toolchains()
3539
gazelle_dependencies()
40+
41+
rules_docker_repository_tools(
42+
name = "rules_docker_repository_tools",
43+
go_cache = "@bazel_gazelle_go_repository_cache//:go.env",
44+
)
3645
excludes = native.existing_rules().keys()
3746
if "com_github_google_go_containerregistry" not in excludes:
3847
go_repository(

0 commit comments

Comments
 (0)