From 780c5ee7c61cd59a87d440dcc4a07121520b775e Mon Sep 17 00:00:00 2001 From: ericzzzzzzz <102683393+ericzzzzzzz@users.noreply.github.com> Date: Tue, 12 Dec 2023 08:10:31 -0500 Subject: [PATCH 1/4] chore: change bazel example --- examples/bazel/BUILD | 12 ++--- examples/bazel/README.md | 20 -------- examples/bazel/WORKSPACE | 53 +++++++-------------- examples/bazel/deploy.yaml | 25 ++++++++++ examples/bazel/k8s/k8s-pod.yaml | 8 ---- examples/bazel/main.go | 13 ----- examples/bazel/skaffold.yaml | 17 +++++-- integration/examples/bazel/BUILD | 12 ++--- integration/examples/bazel/README.md | 20 -------- integration/examples/bazel/WORKSPACE | 53 +++++++-------------- integration/examples/bazel/deploy.yaml | 25 ++++++++++ integration/examples/bazel/k8s/k8s-pod.yaml | 8 ---- integration/examples/bazel/main.go | 13 ----- integration/examples/bazel/skaffold.yaml | 15 ++++-- 14 files changed, 121 insertions(+), 173 deletions(-) delete mode 100644 examples/bazel/README.md create mode 100644 examples/bazel/deploy.yaml delete mode 100644 examples/bazel/k8s/k8s-pod.yaml delete mode 100644 examples/bazel/main.go delete mode 100644 integration/examples/bazel/README.md create mode 100644 integration/examples/bazel/deploy.yaml delete mode 100644 integration/examples/bazel/k8s/k8s-pod.yaml delete mode 100644 integration/examples/bazel/main.go diff --git a/examples/bazel/BUILD b/examples/bazel/BUILD index ba00afe13d3..821f8c599d9 100644 --- a/examples/bazel/BUILD +++ b/examples/bazel/BUILD @@ -1,9 +1,7 @@ -load("@io_bazel_rules_docker//go:image.bzl", "go_image") +load("@rules_oci//oci:defs.bzl", "oci_tarball") -go_image( - name = "skaffold_example", - srcs = ["main.go"], - goos = "linux", - goarch = "amd64", - static = "on", +oci_tarball( + name = "hello.tar", + image = "@hello//:hello", + repo_tags = ["hello:latest"], ) diff --git a/examples/bazel/README.md b/examples/bazel/README.md deleted file mode 100644 index 9a3592c8163..00000000000 --- a/examples/bazel/README.md +++ /dev/null @@ -1,20 +0,0 @@ -### Example: bazel - -[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/GoogleContainerTools/skaffold&cloudshell_open_in_editor=README.md&cloudshell_workspace=examples/bazel) - -Bazel is one of the supported builders in Skaffold. - -The way you configure it in `skaffold.yaml` is the following build stanza: - -```yaml -build: - artifacts: - - image: skaffold-example - context: . - bazel: - target: //:skaffold_example.tar -``` - -1. make sure the `context` contains the bazel files (`WORKSPACE`, `BUILD`) -2. add `bazel` section to each artifact -3. specify `target` - our builder will use this to load to the image to the Docker daemon diff --git a/examples/bazel/WORKSPACE b/examples/bazel/WORKSPACE index 3b5a76e1117..07e22a13b7b 100644 --- a/examples/bazel/WORKSPACE +++ b/examples/bazel/WORKSPACE @@ -1,47 +1,30 @@ -workspace(name = "skaffold") - load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( - name = "io_bazel_rules_docker", - sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf", - urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"], + name = "rules_oci", + sha256 = "db57efd706f01eb3ce771468366baa1614b5b25f4cce99757e2b8d942155b8ec", + strip_prefix = "rules_oci-1.0.0", + url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.0.0/rules_oci-v1.0.0.tar.gz", ) -http_archive( - name = "io_bazel_rules_go", - sha256 = "a8d6b1b354d371a646d2f7927319974e0f9e52f73a2452d2b3877118169eb6bb", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.3/rules_go-v0.23.3.tar.gz", - "https://github.com/bazelbuild/rules_go/releases/download/v0.23.3/rules_go-v0.23.3.tar.gz", - ], -) +load("@rules_oci//oci:dependencies.bzl", "rules_oci_dependencies") -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") +rules_oci_dependencies() -go_rules_dependencies() - -go_register_toolchains( - go_version = "1.14.4", -) +load("@rules_oci//oci:repositories.bzl", "LATEST_CRANE_VERSION", "LATEST_ZOT_VERSION", "oci_register_toolchains") -load( - "@io_bazel_rules_docker//repositories:repositories.bzl", - container_repositories = "repositories", +oci_register_toolchains( + name = "oci", + crane_version = LATEST_CRANE_VERSION, + # Uncommenting the zot toolchain will cause it to be used instead of crane for some tasks. + # Note that it does not support docker-format images. + # zot_version = LATEST_ZOT_VERSION, ) -container_repositories() +load("@rules_oci//oci:pull.bzl", "oci_pull") -load( - "@io_bazel_rules_docker//repositories:deps.bzl", - container_deps = "deps", +oci_pull( + name = "hello", + digest = "sha256:845f77fab71033404f4cfceaa1ddb27b70c3551ceb22a5e7f4498cdda6c9daea", + image = "us-docker.pkg.dev/google-samples/containers/gke/hello-app", ) - -container_deps() - -load( - "@io_bazel_rules_docker//go:image.bzl", - _go_image_repos = "repositories", -) - -_go_image_repos() diff --git a/examples/bazel/deploy.yaml b/examples/bazel/deploy.yaml new file mode 100644 index 00000000000..528f39217a9 --- /dev/null +++ b/examples/bazel/deploy.yaml @@ -0,0 +1,25 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: helloweb + labels: + app: hello +spec: + selector: + matchLabels: + app: hello + tier: web + template: + metadata: + labels: + app: hello + tier: web + spec: + containers: + - name: hello-app + image: hello-image + ports: + - containerPort: 8080 + resources: + requests: + cpu: 200m \ No newline at end of file diff --git a/examples/bazel/k8s/k8s-pod.yaml b/examples/bazel/k8s/k8s-pod.yaml deleted file mode 100644 index 84a5318a0e1..00000000000 --- a/examples/bazel/k8s/k8s-pod.yaml +++ /dev/null @@ -1,8 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - name: bazel -spec: - containers: - - name: bazel - image: skaffold-bazel diff --git a/examples/bazel/main.go b/examples/bazel/main.go deleted file mode 100644 index 8f98567919a..00000000000 --- a/examples/bazel/main.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import ( - "fmt" - "time" -) - -func main() { - for { - fmt.Println("Hello bazel!!!!") - time.Sleep(time.Second * 1) - } -} diff --git a/examples/bazel/skaffold.yaml b/examples/bazel/skaffold.yaml index 5fba81c18fc..a0c56cc8afb 100644 --- a/examples/bazel/skaffold.yaml +++ b/examples/bazel/skaffold.yaml @@ -1,7 +1,16 @@ -apiVersion: skaffold/v4beta8 +apiVersion: skaffold/v4beta9 kind: Config +metadata: + name: hello build: + tagPolicy: + sha256: {} artifacts: - - image: skaffold-bazel - bazel: - target: //:skaffold_example.tar + - image: hello-image + bazel: + target: //:hello.tar +deploy: + kubectl: {} +manifests: + rawYaml: + - "deploy.yaml" diff --git a/integration/examples/bazel/BUILD b/integration/examples/bazel/BUILD index ba00afe13d3..821f8c599d9 100644 --- a/integration/examples/bazel/BUILD +++ b/integration/examples/bazel/BUILD @@ -1,9 +1,7 @@ -load("@io_bazel_rules_docker//go:image.bzl", "go_image") +load("@rules_oci//oci:defs.bzl", "oci_tarball") -go_image( - name = "skaffold_example", - srcs = ["main.go"], - goos = "linux", - goarch = "amd64", - static = "on", +oci_tarball( + name = "hello.tar", + image = "@hello//:hello", + repo_tags = ["hello:latest"], ) diff --git a/integration/examples/bazel/README.md b/integration/examples/bazel/README.md deleted file mode 100644 index 9a3592c8163..00000000000 --- a/integration/examples/bazel/README.md +++ /dev/null @@ -1,20 +0,0 @@ -### Example: bazel - -[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/GoogleContainerTools/skaffold&cloudshell_open_in_editor=README.md&cloudshell_workspace=examples/bazel) - -Bazel is one of the supported builders in Skaffold. - -The way you configure it in `skaffold.yaml` is the following build stanza: - -```yaml -build: - artifacts: - - image: skaffold-example - context: . - bazel: - target: //:skaffold_example.tar -``` - -1. make sure the `context` contains the bazel files (`WORKSPACE`, `BUILD`) -2. add `bazel` section to each artifact -3. specify `target` - our builder will use this to load to the image to the Docker daemon diff --git a/integration/examples/bazel/WORKSPACE b/integration/examples/bazel/WORKSPACE index 3b5a76e1117..07e22a13b7b 100644 --- a/integration/examples/bazel/WORKSPACE +++ b/integration/examples/bazel/WORKSPACE @@ -1,47 +1,30 @@ -workspace(name = "skaffold") - load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( - name = "io_bazel_rules_docker", - sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf", - urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"], + name = "rules_oci", + sha256 = "db57efd706f01eb3ce771468366baa1614b5b25f4cce99757e2b8d942155b8ec", + strip_prefix = "rules_oci-1.0.0", + url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.0.0/rules_oci-v1.0.0.tar.gz", ) -http_archive( - name = "io_bazel_rules_go", - sha256 = "a8d6b1b354d371a646d2f7927319974e0f9e52f73a2452d2b3877118169eb6bb", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.3/rules_go-v0.23.3.tar.gz", - "https://github.com/bazelbuild/rules_go/releases/download/v0.23.3/rules_go-v0.23.3.tar.gz", - ], -) +load("@rules_oci//oci:dependencies.bzl", "rules_oci_dependencies") -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") +rules_oci_dependencies() -go_rules_dependencies() - -go_register_toolchains( - go_version = "1.14.4", -) +load("@rules_oci//oci:repositories.bzl", "LATEST_CRANE_VERSION", "LATEST_ZOT_VERSION", "oci_register_toolchains") -load( - "@io_bazel_rules_docker//repositories:repositories.bzl", - container_repositories = "repositories", +oci_register_toolchains( + name = "oci", + crane_version = LATEST_CRANE_VERSION, + # Uncommenting the zot toolchain will cause it to be used instead of crane for some tasks. + # Note that it does not support docker-format images. + # zot_version = LATEST_ZOT_VERSION, ) -container_repositories() +load("@rules_oci//oci:pull.bzl", "oci_pull") -load( - "@io_bazel_rules_docker//repositories:deps.bzl", - container_deps = "deps", +oci_pull( + name = "hello", + digest = "sha256:845f77fab71033404f4cfceaa1ddb27b70c3551ceb22a5e7f4498cdda6c9daea", + image = "us-docker.pkg.dev/google-samples/containers/gke/hello-app", ) - -container_deps() - -load( - "@io_bazel_rules_docker//go:image.bzl", - _go_image_repos = "repositories", -) - -_go_image_repos() diff --git a/integration/examples/bazel/deploy.yaml b/integration/examples/bazel/deploy.yaml new file mode 100644 index 00000000000..528f39217a9 --- /dev/null +++ b/integration/examples/bazel/deploy.yaml @@ -0,0 +1,25 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: helloweb + labels: + app: hello +spec: + selector: + matchLabels: + app: hello + tier: web + template: + metadata: + labels: + app: hello + tier: web + spec: + containers: + - name: hello-app + image: hello-image + ports: + - containerPort: 8080 + resources: + requests: + cpu: 200m \ No newline at end of file diff --git a/integration/examples/bazel/k8s/k8s-pod.yaml b/integration/examples/bazel/k8s/k8s-pod.yaml deleted file mode 100644 index 84a5318a0e1..00000000000 --- a/integration/examples/bazel/k8s/k8s-pod.yaml +++ /dev/null @@ -1,8 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - name: bazel -spec: - containers: - - name: bazel - image: skaffold-bazel diff --git a/integration/examples/bazel/main.go b/integration/examples/bazel/main.go deleted file mode 100644 index 8f98567919a..00000000000 --- a/integration/examples/bazel/main.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import ( - "fmt" - "time" -) - -func main() { - for { - fmt.Println("Hello bazel!!!!") - time.Sleep(time.Second * 1) - } -} diff --git a/integration/examples/bazel/skaffold.yaml b/integration/examples/bazel/skaffold.yaml index 727bca1428d..a0c56cc8afb 100644 --- a/integration/examples/bazel/skaffold.yaml +++ b/integration/examples/bazel/skaffold.yaml @@ -1,7 +1,16 @@ apiVersion: skaffold/v4beta9 kind: Config +metadata: + name: hello build: + tagPolicy: + sha256: {} artifacts: - - image: skaffold-bazel - bazel: - target: //:skaffold_example.tar + - image: hello-image + bazel: + target: //:hello.tar +deploy: + kubectl: {} +manifests: + rawYaml: + - "deploy.yaml" From f23419bd6ec12a08a4251a07cc3a75b115381f88 Mon Sep 17 00:00:00 2001 From: ericzzzzzzz <102683393+ericzzzzzzz@users.noreply.github.com> Date: Tue, 12 Dec 2023 11:40:06 -0500 Subject: [PATCH 2/4] chore: update bazel example --- examples/bazel/.bazelrc | 1 + examples/bazel/.bazelversion | 1 + examples/bazel/.gitignore | 2 + examples/bazel/BUILD | 7 --- examples/bazel/BUILD.bazel | 53 +++++++++++++++++++ examples/bazel/MODULE.bazel | 28 ++++++++++ examples/bazel/README.md | 20 +++++++ examples/bazel/WORKSPACE | 30 ----------- examples/bazel/WORKSPACE.bazel | 1 + examples/bazel/deploy.yaml | 29 +++------- examples/bazel/main.go | 14 +++++ examples/bazel/skaffold.yaml | 4 +- integration/examples/bazel/.bazelrc | 1 + integration/examples/bazel/.bazelversion | 1 + integration/examples/bazel/.gitignore | 2 + integration/examples/bazel/BUILD | 7 --- integration/examples/bazel/BUILD.bazel | 53 +++++++++++++++++++ integration/examples/bazel/MODULE.bazel | 28 ++++++++++ integration/examples/bazel/README.md | 20 +++++++ integration/examples/bazel/WORKSPACE | 30 ----------- integration/examples/bazel/WORKSPACE.bazel | 1 + integration/examples/bazel/deploy.yaml | 29 +++------- integration/examples/bazel/main.go | 14 +++++ integration/examples/bazel/skaffold.yaml | 4 +- .../bazel-rules-oci/bazel-bazel-rules-oci | 1 + .../testdata/bazel-rules-oci/bazel-bin | 1 + .../testdata/bazel-rules-oci/bazel-out | 1 + .../testdata/bazel-rules-oci/bazel-testlogs | 1 + 28 files changed, 260 insertions(+), 124 deletions(-) create mode 100644 examples/bazel/.bazelrc create mode 100644 examples/bazel/.bazelversion create mode 100644 examples/bazel/.gitignore delete mode 100644 examples/bazel/BUILD create mode 100644 examples/bazel/BUILD.bazel create mode 100644 examples/bazel/MODULE.bazel create mode 100644 examples/bazel/README.md delete mode 100644 examples/bazel/WORKSPACE create mode 100644 examples/bazel/WORKSPACE.bazel create mode 100644 examples/bazel/main.go create mode 100644 integration/examples/bazel/.bazelrc create mode 100644 integration/examples/bazel/.bazelversion create mode 100644 integration/examples/bazel/.gitignore delete mode 100644 integration/examples/bazel/BUILD create mode 100644 integration/examples/bazel/BUILD.bazel create mode 100644 integration/examples/bazel/MODULE.bazel create mode 100644 integration/examples/bazel/README.md delete mode 100644 integration/examples/bazel/WORKSPACE create mode 100644 integration/examples/bazel/WORKSPACE.bazel create mode 100644 integration/examples/bazel/main.go create mode 120000 integration/testdata/bazel-rules-oci/bazel-bazel-rules-oci create mode 120000 integration/testdata/bazel-rules-oci/bazel-bin create mode 120000 integration/testdata/bazel-rules-oci/bazel-out create mode 120000 integration/testdata/bazel-rules-oci/bazel-testlogs diff --git a/examples/bazel/.bazelrc b/examples/bazel/.bazelrc new file mode 100644 index 00000000000..3ce91d27219 --- /dev/null +++ b/examples/bazel/.bazelrc @@ -0,0 +1 @@ +common --enable_bzlmod diff --git a/examples/bazel/.bazelversion b/examples/bazel/.bazelversion new file mode 100644 index 00000000000..dfda3e0b4f0 --- /dev/null +++ b/examples/bazel/.bazelversion @@ -0,0 +1 @@ +6.1.0 diff --git a/examples/bazel/.gitignore b/examples/bazel/.gitignore new file mode 100644 index 00000000000..0684e4bfe51 --- /dev/null +++ b/examples/bazel/.gitignore @@ -0,0 +1,2 @@ +node_modules +bazel-* diff --git a/examples/bazel/BUILD b/examples/bazel/BUILD deleted file mode 100644 index 821f8c599d9..00000000000 --- a/examples/bazel/BUILD +++ /dev/null @@ -1,7 +0,0 @@ -load("@rules_oci//oci:defs.bzl", "oci_tarball") - -oci_tarball( - name = "hello.tar", - image = "@hello//:hello", - repo_tags = ["hello:latest"], -) diff --git a/examples/bazel/BUILD.bazel b/examples/bazel/BUILD.bazel new file mode 100644 index 00000000000..4149f2e2c2d --- /dev/null +++ b/examples/bazel/BUILD.bazel @@ -0,0 +1,53 @@ +load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") +load("@rules_go//go:def.bzl", "go_binary", "go_library") +load("@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"], +) + +go_binary( + name = "app", + embed = [":app_lib"], + visibility = ["//visibility:public"], +) + +# Put app go_binary into a tar layer. +pkg_tar( + name = "app_layer", + srcs = [":app"], + # If the binary depends on RUNFILES, uncomment the attribute below. + # include_runfiles = True +) + +oci_image( + name = "image", + base = "@distroless_base", + entrypoint = ["/app"], + tars = [":app_layer"], +) + +# This is the target that should be released to the target platform +platform_transition_filegroup( + name = "transitioned_image", + srcs = [":image"], + target_platform = select({ + "@platforms//cpu:arm64": "@rules_go//go/toolchain:linux_arm64", + "@platforms//cpu:x86_64": "@rules_go//go/toolchain:linux_amd64", + }), +) + +# $ bazel build :tarball +# $ docker load --input $(bazel cquery --output=files :tarball) +# $ docker run --rm gcr.io/example:latest + +oci_tarball( + name = "skaffold-example.tar", + # Use the image built for the exec platform rather than the target platform + image = ":image", + repo_tags = ["gcr.io/example:latest"], +) diff --git a/examples/bazel/MODULE.bazel b/examples/bazel/MODULE.bazel new file mode 100644 index 00000000000..135d0b812ff --- /dev/null +++ b/examples/bazel/MODULE.bazel @@ -0,0 +1,28 @@ +bazel_dep(name = "aspect_bazel_lib", version = "1.31.1") +bazel_dep(name = "gazelle", version = "0.31.0") +bazel_dep(name = "platforms", version = "0.0.5") +bazel_dep(name = "rules_oci", version = "1.2.0") +bazel_dep(name = "rules_pkg", version = "0.8.1") +bazel_dep(name = "rules_go", version = "0.39.1") + +go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") + +go_deps.module( + path = "github.com/google/go-cmp", + sum = "h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=", + version = "v0.5.9", +) + +oci = use_extension("@rules_oci//oci:extensions.bzl", "oci") + +oci.pull( + name = "distroless_base", + digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86", + image = "gcr.io/distroless/base", + platforms = [ + "linux/amd64", + "linux/arm64", + ], +) + +use_repo(oci, "distroless_base") diff --git a/examples/bazel/README.md b/examples/bazel/README.md new file mode 100644 index 00000000000..224230d966e --- /dev/null +++ b/examples/bazel/README.md @@ -0,0 +1,20 @@ +### Example: bazel + +[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/GoogleContainerTools/skaffold&cloudshell_open_in_editor=README.md&cloudshell_workspace=examples/bazel) + +Bazel is one of the supported builders in Skaffold. + +The way you configure it in `skaffold.yaml` is the following build stanza: + +```yaml +build: + artifacts: + - image: skaffold-bazel + context: . + bazel: + target: //:skaffold_example.tar +``` + +1. make sure the `context` contains the bazel files (`WORKSPACE`, `BUILD`) +2. add `bazel` section to each artifact +3. specify `target` - our builder will use this to load to the image to the Docker daemon \ No newline at end of file diff --git a/examples/bazel/WORKSPACE b/examples/bazel/WORKSPACE deleted file mode 100644 index 07e22a13b7b..00000000000 --- a/examples/bazel/WORKSPACE +++ /dev/null @@ -1,30 +0,0 @@ -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "rules_oci", - sha256 = "db57efd706f01eb3ce771468366baa1614b5b25f4cce99757e2b8d942155b8ec", - strip_prefix = "rules_oci-1.0.0", - url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.0.0/rules_oci-v1.0.0.tar.gz", -) - -load("@rules_oci//oci:dependencies.bzl", "rules_oci_dependencies") - -rules_oci_dependencies() - -load("@rules_oci//oci:repositories.bzl", "LATEST_CRANE_VERSION", "LATEST_ZOT_VERSION", "oci_register_toolchains") - -oci_register_toolchains( - name = "oci", - crane_version = LATEST_CRANE_VERSION, - # Uncommenting the zot toolchain will cause it to be used instead of crane for some tasks. - # Note that it does not support docker-format images. - # zot_version = LATEST_ZOT_VERSION, -) - -load("@rules_oci//oci:pull.bzl", "oci_pull") - -oci_pull( - name = "hello", - digest = "sha256:845f77fab71033404f4cfceaa1ddb27b70c3551ceb22a5e7f4498cdda6c9daea", - image = "us-docker.pkg.dev/google-samples/containers/gke/hello-app", -) diff --git a/examples/bazel/WORKSPACE.bazel b/examples/bazel/WORKSPACE.bazel new file mode 100644 index 00000000000..64d7fc061a1 --- /dev/null +++ b/examples/bazel/WORKSPACE.bazel @@ -0,0 +1 @@ +# Marker file: this is the root of a Bazel workspace diff --git a/examples/bazel/deploy.yaml b/examples/bazel/deploy.yaml index 528f39217a9..aa19e600205 100644 --- a/examples/bazel/deploy.yaml +++ b/examples/bazel/deploy.yaml @@ -1,25 +1,8 @@ -apiVersion: apps/v1 -kind: Deployment +apiVersion: v1 +kind: Pod metadata: - name: helloweb - labels: - app: hello + name: bazel spec: - selector: - matchLabels: - app: hello - tier: web - template: - metadata: - labels: - app: hello - tier: web - spec: - containers: - - name: hello-app - image: hello-image - ports: - - containerPort: 8080 - resources: - requests: - cpu: 200m \ No newline at end of file + containers: + - name: bazel + image: skaffold-bazel \ No newline at end of file diff --git a/examples/bazel/main.go b/examples/bazel/main.go new file mode 100644 index 00000000000..593721cfe2e --- /dev/null +++ b/examples/bazel/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + for { + fmt.Println("Hello world!") + + time.Sleep(time.Second * 1) + } +} diff --git a/examples/bazel/skaffold.yaml b/examples/bazel/skaffold.yaml index a0c56cc8afb..90a0e26b993 100644 --- a/examples/bazel/skaffold.yaml +++ b/examples/bazel/skaffold.yaml @@ -6,9 +6,9 @@ build: tagPolicy: sha256: {} artifacts: - - image: hello-image + - image: skaffold-bazel bazel: - target: //:hello.tar + target: //:skaffold-example.tar deploy: kubectl: {} manifests: diff --git a/integration/examples/bazel/.bazelrc b/integration/examples/bazel/.bazelrc new file mode 100644 index 00000000000..3ce91d27219 --- /dev/null +++ b/integration/examples/bazel/.bazelrc @@ -0,0 +1 @@ +common --enable_bzlmod diff --git a/integration/examples/bazel/.bazelversion b/integration/examples/bazel/.bazelversion new file mode 100644 index 00000000000..dfda3e0b4f0 --- /dev/null +++ b/integration/examples/bazel/.bazelversion @@ -0,0 +1 @@ +6.1.0 diff --git a/integration/examples/bazel/.gitignore b/integration/examples/bazel/.gitignore new file mode 100644 index 00000000000..0684e4bfe51 --- /dev/null +++ b/integration/examples/bazel/.gitignore @@ -0,0 +1,2 @@ +node_modules +bazel-* diff --git a/integration/examples/bazel/BUILD b/integration/examples/bazel/BUILD deleted file mode 100644 index 821f8c599d9..00000000000 --- a/integration/examples/bazel/BUILD +++ /dev/null @@ -1,7 +0,0 @@ -load("@rules_oci//oci:defs.bzl", "oci_tarball") - -oci_tarball( - name = "hello.tar", - image = "@hello//:hello", - repo_tags = ["hello:latest"], -) diff --git a/integration/examples/bazel/BUILD.bazel b/integration/examples/bazel/BUILD.bazel new file mode 100644 index 00000000000..4149f2e2c2d --- /dev/null +++ b/integration/examples/bazel/BUILD.bazel @@ -0,0 +1,53 @@ +load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") +load("@rules_go//go:def.bzl", "go_binary", "go_library") +load("@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"], +) + +go_binary( + name = "app", + embed = [":app_lib"], + visibility = ["//visibility:public"], +) + +# Put app go_binary into a tar layer. +pkg_tar( + name = "app_layer", + srcs = [":app"], + # If the binary depends on RUNFILES, uncomment the attribute below. + # include_runfiles = True +) + +oci_image( + name = "image", + base = "@distroless_base", + entrypoint = ["/app"], + tars = [":app_layer"], +) + +# This is the target that should be released to the target platform +platform_transition_filegroup( + name = "transitioned_image", + srcs = [":image"], + target_platform = select({ + "@platforms//cpu:arm64": "@rules_go//go/toolchain:linux_arm64", + "@platforms//cpu:x86_64": "@rules_go//go/toolchain:linux_amd64", + }), +) + +# $ bazel build :tarball +# $ docker load --input $(bazel cquery --output=files :tarball) +# $ docker run --rm gcr.io/example:latest + +oci_tarball( + name = "skaffold-example.tar", + # Use the image built for the exec platform rather than the target platform + image = ":image", + repo_tags = ["gcr.io/example:latest"], +) diff --git a/integration/examples/bazel/MODULE.bazel b/integration/examples/bazel/MODULE.bazel new file mode 100644 index 00000000000..135d0b812ff --- /dev/null +++ b/integration/examples/bazel/MODULE.bazel @@ -0,0 +1,28 @@ +bazel_dep(name = "aspect_bazel_lib", version = "1.31.1") +bazel_dep(name = "gazelle", version = "0.31.0") +bazel_dep(name = "platforms", version = "0.0.5") +bazel_dep(name = "rules_oci", version = "1.2.0") +bazel_dep(name = "rules_pkg", version = "0.8.1") +bazel_dep(name = "rules_go", version = "0.39.1") + +go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") + +go_deps.module( + path = "github.com/google/go-cmp", + sum = "h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=", + version = "v0.5.9", +) + +oci = use_extension("@rules_oci//oci:extensions.bzl", "oci") + +oci.pull( + name = "distroless_base", + digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86", + image = "gcr.io/distroless/base", + platforms = [ + "linux/amd64", + "linux/arm64", + ], +) + +use_repo(oci, "distroless_base") diff --git a/integration/examples/bazel/README.md b/integration/examples/bazel/README.md new file mode 100644 index 00000000000..224230d966e --- /dev/null +++ b/integration/examples/bazel/README.md @@ -0,0 +1,20 @@ +### Example: bazel + +[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/GoogleContainerTools/skaffold&cloudshell_open_in_editor=README.md&cloudshell_workspace=examples/bazel) + +Bazel is one of the supported builders in Skaffold. + +The way you configure it in `skaffold.yaml` is the following build stanza: + +```yaml +build: + artifacts: + - image: skaffold-bazel + context: . + bazel: + target: //:skaffold_example.tar +``` + +1. make sure the `context` contains the bazel files (`WORKSPACE`, `BUILD`) +2. add `bazel` section to each artifact +3. specify `target` - our builder will use this to load to the image to the Docker daemon \ No newline at end of file diff --git a/integration/examples/bazel/WORKSPACE b/integration/examples/bazel/WORKSPACE deleted file mode 100644 index 07e22a13b7b..00000000000 --- a/integration/examples/bazel/WORKSPACE +++ /dev/null @@ -1,30 +0,0 @@ -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "rules_oci", - sha256 = "db57efd706f01eb3ce771468366baa1614b5b25f4cce99757e2b8d942155b8ec", - strip_prefix = "rules_oci-1.0.0", - url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.0.0/rules_oci-v1.0.0.tar.gz", -) - -load("@rules_oci//oci:dependencies.bzl", "rules_oci_dependencies") - -rules_oci_dependencies() - -load("@rules_oci//oci:repositories.bzl", "LATEST_CRANE_VERSION", "LATEST_ZOT_VERSION", "oci_register_toolchains") - -oci_register_toolchains( - name = "oci", - crane_version = LATEST_CRANE_VERSION, - # Uncommenting the zot toolchain will cause it to be used instead of crane for some tasks. - # Note that it does not support docker-format images. - # zot_version = LATEST_ZOT_VERSION, -) - -load("@rules_oci//oci:pull.bzl", "oci_pull") - -oci_pull( - name = "hello", - digest = "sha256:845f77fab71033404f4cfceaa1ddb27b70c3551ceb22a5e7f4498cdda6c9daea", - image = "us-docker.pkg.dev/google-samples/containers/gke/hello-app", -) diff --git a/integration/examples/bazel/WORKSPACE.bazel b/integration/examples/bazel/WORKSPACE.bazel new file mode 100644 index 00000000000..64d7fc061a1 --- /dev/null +++ b/integration/examples/bazel/WORKSPACE.bazel @@ -0,0 +1 @@ +# Marker file: this is the root of a Bazel workspace diff --git a/integration/examples/bazel/deploy.yaml b/integration/examples/bazel/deploy.yaml index 528f39217a9..aa19e600205 100644 --- a/integration/examples/bazel/deploy.yaml +++ b/integration/examples/bazel/deploy.yaml @@ -1,25 +1,8 @@ -apiVersion: apps/v1 -kind: Deployment +apiVersion: v1 +kind: Pod metadata: - name: helloweb - labels: - app: hello + name: bazel spec: - selector: - matchLabels: - app: hello - tier: web - template: - metadata: - labels: - app: hello - tier: web - spec: - containers: - - name: hello-app - image: hello-image - ports: - - containerPort: 8080 - resources: - requests: - cpu: 200m \ No newline at end of file + containers: + - name: bazel + image: skaffold-bazel \ No newline at end of file diff --git a/integration/examples/bazel/main.go b/integration/examples/bazel/main.go new file mode 100644 index 00000000000..593721cfe2e --- /dev/null +++ b/integration/examples/bazel/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + for { + fmt.Println("Hello world!") + + time.Sleep(time.Second * 1) + } +} diff --git a/integration/examples/bazel/skaffold.yaml b/integration/examples/bazel/skaffold.yaml index a0c56cc8afb..90a0e26b993 100644 --- a/integration/examples/bazel/skaffold.yaml +++ b/integration/examples/bazel/skaffold.yaml @@ -6,9 +6,9 @@ build: tagPolicy: sha256: {} artifacts: - - image: hello-image + - image: skaffold-bazel bazel: - target: //:hello.tar + target: //:skaffold-example.tar deploy: kubectl: {} manifests: diff --git a/integration/testdata/bazel-rules-oci/bazel-bazel-rules-oci b/integration/testdata/bazel-rules-oci/bazel-bazel-rules-oci new file mode 120000 index 00000000000..1e633c9ddc7 --- /dev/null +++ b/integration/testdata/bazel-rules-oci/bazel-bazel-rules-oci @@ -0,0 +1 @@ +/private/var/tmp/_bazel_ericwork/92e3dfdc5a41714cb41907e47b02381a/execroot/__main__ \ No newline at end of file diff --git a/integration/testdata/bazel-rules-oci/bazel-bin b/integration/testdata/bazel-rules-oci/bazel-bin new file mode 120000 index 00000000000..b20c79eb163 --- /dev/null +++ b/integration/testdata/bazel-rules-oci/bazel-bin @@ -0,0 +1 @@ +/private/var/tmp/_bazel_ericwork/92e3dfdc5a41714cb41907e47b02381a/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin \ No newline at end of file diff --git a/integration/testdata/bazel-rules-oci/bazel-out b/integration/testdata/bazel-rules-oci/bazel-out new file mode 120000 index 00000000000..ff0fb2ab482 --- /dev/null +++ b/integration/testdata/bazel-rules-oci/bazel-out @@ -0,0 +1 @@ +/private/var/tmp/_bazel_ericwork/92e3dfdc5a41714cb41907e47b02381a/execroot/__main__/bazel-out \ No newline at end of file diff --git a/integration/testdata/bazel-rules-oci/bazel-testlogs b/integration/testdata/bazel-rules-oci/bazel-testlogs new file mode 120000 index 00000000000..5f75cc6006b --- /dev/null +++ b/integration/testdata/bazel-rules-oci/bazel-testlogs @@ -0,0 +1 @@ +/private/var/tmp/_bazel_ericwork/92e3dfdc5a41714cb41907e47b02381a/execroot/__main__/bazel-out/darwin_arm64-fastbuild/testlogs \ No newline at end of file From e28625e64df0f539a3ba0d26dd90274511a718a0 Mon Sep 17 00:00:00 2001 From: ericzzzzzzz <102683393+ericzzzzzzz@users.noreply.github.com> Date: Tue, 12 Dec 2023 11:41:39 -0500 Subject: [PATCH 3/4] chore: remove redundant dirs --- integration/testdata/bazel-rules-oci/bazel-bazel-rules-oci | 1 - integration/testdata/bazel-rules-oci/bazel-bin | 1 - integration/testdata/bazel-rules-oci/bazel-out | 1 - integration/testdata/bazel-rules-oci/bazel-testlogs | 1 - 4 files changed, 4 deletions(-) delete mode 120000 integration/testdata/bazel-rules-oci/bazel-bazel-rules-oci delete mode 120000 integration/testdata/bazel-rules-oci/bazel-bin delete mode 120000 integration/testdata/bazel-rules-oci/bazel-out delete mode 120000 integration/testdata/bazel-rules-oci/bazel-testlogs diff --git a/integration/testdata/bazel-rules-oci/bazel-bazel-rules-oci b/integration/testdata/bazel-rules-oci/bazel-bazel-rules-oci deleted file mode 120000 index 1e633c9ddc7..00000000000 --- a/integration/testdata/bazel-rules-oci/bazel-bazel-rules-oci +++ /dev/null @@ -1 +0,0 @@ -/private/var/tmp/_bazel_ericwork/92e3dfdc5a41714cb41907e47b02381a/execroot/__main__ \ No newline at end of file diff --git a/integration/testdata/bazel-rules-oci/bazel-bin b/integration/testdata/bazel-rules-oci/bazel-bin deleted file mode 120000 index b20c79eb163..00000000000 --- a/integration/testdata/bazel-rules-oci/bazel-bin +++ /dev/null @@ -1 +0,0 @@ -/private/var/tmp/_bazel_ericwork/92e3dfdc5a41714cb41907e47b02381a/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin \ No newline at end of file diff --git a/integration/testdata/bazel-rules-oci/bazel-out b/integration/testdata/bazel-rules-oci/bazel-out deleted file mode 120000 index ff0fb2ab482..00000000000 --- a/integration/testdata/bazel-rules-oci/bazel-out +++ /dev/null @@ -1 +0,0 @@ -/private/var/tmp/_bazel_ericwork/92e3dfdc5a41714cb41907e47b02381a/execroot/__main__/bazel-out \ No newline at end of file diff --git a/integration/testdata/bazel-rules-oci/bazel-testlogs b/integration/testdata/bazel-rules-oci/bazel-testlogs deleted file mode 120000 index 5f75cc6006b..00000000000 --- a/integration/testdata/bazel-rules-oci/bazel-testlogs +++ /dev/null @@ -1 +0,0 @@ -/private/var/tmp/_bazel_ericwork/92e3dfdc5a41714cb41907e47b02381a/execroot/__main__/bazel-out/darwin_arm64-fastbuild/testlogs \ No newline at end of file From e2ac47f51ce1b329781df8284d1e78e88f50b740 Mon Sep 17 00:00:00 2001 From: ericzzzzzzz <102683393+ericzzzzzzz@users.noreply.github.com> Date: Tue, 12 Dec 2023 11:51:55 -0500 Subject: [PATCH 4/4] chore: change example project schema version --- examples/bazel/skaffold.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/bazel/skaffold.yaml b/examples/bazel/skaffold.yaml index 90a0e26b993..a6e5c9d090f 100644 --- a/examples/bazel/skaffold.yaml +++ b/examples/bazel/skaffold.yaml @@ -1,4 +1,4 @@ -apiVersion: skaffold/v4beta9 +apiVersion: skaffold/v4beta8 kind: Config metadata: name: hello