diff --git a/e2e/custom_registry/.bazelrc b/e2e/custom_registry/.bazelrc new file mode 120000 index 00000000..c70ccda0 --- /dev/null +++ b/e2e/custom_registry/.bazelrc @@ -0,0 +1 @@ +../../.bazelrc \ No newline at end of file diff --git a/e2e/custom_registry/WORKSPACE b/e2e/custom_registry/WORKSPACE index 499ed3c7..fc120176 100644 --- a/e2e/custom_registry/WORKSPACE +++ b/e2e/custom_registry/WORKSPACE @@ -73,9 +73,9 @@ rules_pkg_dependencies() load("@contrib_rules_oci//oci:pull.bzl", "oci_pull") oci_pull( - name = "distroless_static", - digest = "sha256:c3c3d0230d487c0ad3a0d87ad03ee02ea2ff0b3dcce91ca06a1019e07de05f12", - image = "gcr.io/distroless/static", + name = "distroless_base", + digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86", + image = "gcr.io/distroless/base", platforms = [ "linux/amd64", "linux/arm64", diff --git a/e2e/custom_registry/app/BUILD.bazel b/e2e/custom_registry/app/BUILD.bazel new file mode 100644 index 00000000..17605bff --- /dev/null +++ b/e2e/custom_registry/app/BUILD.bazel @@ -0,0 +1,57 @@ +load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") +load("@contrib_rules_oci//oci:defs.bzl", "oci_image", "oci_tarball") +load("@rules_pkg//:pkg.bzl", "pkg_tar") + +go_library( + name = "app_lib", + srcs = ["main.go"], + importpath = "example.com/custom_registry/app", + visibility = ["//visibility:private"], + deps = ["@com_github_google_go_cmp//cmp"], +) + +go_binary( + name = "app", + embed = [":app_lib"], + visibility = ["//visibility:public"], +) + +pkg_tar( + name = "tar", + srcs = [":app"], +) + +platform_transition_filegroup( + name = "transition", + srcs = [":tar"], + target_platform = select({ + "@platforms//cpu:arm64": "@io_bazel_rules_go//go/toolchain:linux_arm64", + "@platforms//cpu:x86_64": "@io_bazel_rules_go//go/toolchain:linux_amd64", + }), +) + +oci_image( + name = "image", + architecture = select({ + "@platforms//cpu:arm64": "arm64", + "@platforms//cpu:x86_64": "amd64", + }), + base = "@distroless_base", + entrypoint = ["/app"], + os = "linux", + tars = [":transition"], +) + +# $ bazel build app:tarball +# $ docker load --input $(bazel cquery --output=files app:tarball) +# $ docker run --rm gcr.io/example:latest +# string( +# - "Hello World", +# + "Hello Go", +# ) +oci_tarball( + name = "tarball", + image = ":image", + repotags = ["gcr.io/example:latest"], +) diff --git a/e2e/custom_registry/app/main.go b/e2e/custom_registry/app/main.go new file mode 100644 index 00000000..acfddf76 --- /dev/null +++ b/e2e/custom_registry/app/main.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + "github.com/google/go-cmp/cmp" +) + +func main() { + fmt.Println(cmp.Diff("Hello World", "Hello Go")) +} diff --git a/e2e/custom_registry/image/BUILD.bazel b/e2e/custom_registry/image/BUILD.bazel deleted file mode 100644 index 65f95885..00000000 --- a/e2e/custom_registry/image/BUILD.bazel +++ /dev/null @@ -1,19 +0,0 @@ -load("@contrib_rules_oci//oci:defs.bzl", "oci_image") - -oci_image( - name = "image", - architecture = select({ - "@platforms//cpu:arm64": "arm64", - "@platforms//cpu:x86_64": "amd64", - }), - base = "@distroless_static", - cmd = [ - "--arg1", - "--arg2", - ], - entrypoint = ["/custom_bin"], - env = { - "ENV": "/test", - }, - os = "linux", -) diff --git a/e2e/custom_registry/registry/BUILD.bazel b/e2e/custom_registry/registry/BUILD.bazel index b2c28617..a49e351c 100644 --- a/e2e/custom_registry/registry/BUILD.bazel +++ b/e2e/custom_registry/registry/BUILD.bazel @@ -25,7 +25,7 @@ go_library( srcs = ["registry.go"], importpath = "example.com/custom_registry/registry", visibility = ["//visibility:private"], - deps = ["@com_github_google_go_containerregistry//pkg/registry:go_default_library"], + deps = ["@com_github_google_go_containerregistry//pkg/registry"], ) go_binary( diff --git a/e2e/custom_registry/registry/registry.go b/e2e/custom_registry/registry/registry.go index 013fab18..90118217 100644 --- a/e2e/custom_registry/registry/registry.go +++ b/e2e/custom_registry/registry/registry.go @@ -1,3 +1,4 @@ +// Workaround https://github.com/google/go-containerregistry/issues/1579 package main import ( diff --git a/examples/go/README.md b/examples/go/README.md new file mode 100644 index 00000000..90c551c3 --- /dev/null +++ b/examples/go/README.md @@ -0,0 +1,92 @@ +# Go example + +This is an example of how to migrate from +[`go_image`](https://github.com/bazelbuild/rules_docker#go_image) in rules_docker. + +## Base image + +First, you'll need a base image. + +It's wise to minimize changes by using the same one your current `go_image` uses. +The logic for choosing the default base in rules_docker is in +[https://github.com/bazelbuild/rules_docker/blob/fc729d85f284225cfc0b8c6d1d838f4b3e037749/go/image.bzl#L114](go_image.bzl) + +Or, you can just use bazel query to find out: + +``` +$ bazel query --output=build hello_world:hello_world_go_image +Starting local Bazel server and connecting to it... +# /home/alexeagle/Projects/hello_world/BUILD.bazel:22:9 +_app_layer( + name = "hello_world_go_image", + base = select({"@io_bazel_rules_docker//:debug": "@go_debug_image_base//image:image", "@io_bazel_rules_docker//:fastbuild": "@go_image_base//image:image", "@io_bazel_rules_docker//:optimized": "@go_image_base//image:image", "//conditions:default": "@go_image_base//image:image"}), +... +) +``` + +Since we don't use the "debug" config, this tells us that we use `@go_base_image`, we need one more lookup to see what that uses: + +``` +$ bazel query --output=build @go_image_base//image:image +# /shared/cache/bazel/user_base/bf7b6accf6f1187bd5511f3fbf7b21b9/external/go_image_base/image/BUILD:4:17 +container_import( + name = "image", + base_image_registry = "gcr.io", + base_image_repository = "distroless/base", +... +) +``` + +Now that we know it's `gcr.io/distroless/base` we can pull the same base image by adding to WORKSPACE: + +``` +load("@contrib_rules_oci//oci:pull.bzl", "oci_pull") + +oci_pull( + name = "distroless_base", + digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86", + image = "gcr.io/distroless/base", + platforms = ["linux/amd64","linux/arm64"], +) +``` + +See more details in the [oci_pull docs](/docs/pull.md) + +## The go_image + +rules_docker makes you repeat the attributes of `go_binary` into `go_layer`. +This is a "layering violation" (get it?). + +In rules_oci, you just start from a normal `go_binary` (typically by having Gazelle write it). +For this example let's say it's `go_binary(name = "app", ...)`. + +Next, put that file into a layer, which is just a `.tar` file: + +``` +load("@rules_pkg//:pkg.bzl", "pkg_tar") + +pkg_tar( + name = "tar", + srcs = [":app"], +) +``` + +Finally, add your layer to the base image: + +``` +load("@contrib_rules_oci//oci:defs.bzl", "oci_image") + +oci_image( + name = "image", + architecture = select({ + "@platforms//cpu:arm64": "arm64", + "@platforms//cpu:x86_64": "amd64", + }), + base = "@distroless_base", + tars = [":tar"], + entrypoint = ["/app"], + os = "linux", +) +``` + +See the complete example in /e2e/custom_registry.