From 5a2126235a159112509f85beeae42bee41fc3735 Mon Sep 17 00:00:00 2001 From: "Philip K. Warren" Date: Tue, 18 Jun 2024 09:14:50 -0500 Subject: [PATCH] Reduce image layers in protoc v25.3 plugins (#1287) --- .../protocolbuffers/cpp/v25.3/.dockerignore | 1 + plugins/protocolbuffers/cpp/v25.3/Dockerfile | 13 ++++++++---- .../cpp/v25.3/disable_cpp_editions.patch | 20 +++++++++++++++++++ .../protocolbuffers/csharp/v25.3/Dockerfile | 9 ++++++--- plugins/protocolbuffers/java/v25.3/Dockerfile | 9 ++++++--- .../protocolbuffers/kotlin/v25.3/Dockerfile | 9 ++++++--- .../protocolbuffers/objc/v25.3/.dockerignore | 1 + plugins/protocolbuffers/objc/v25.3/Dockerfile | 13 ++++++++---- .../objc/v25.3/disable_objc_editions.patch | 16 +++++++++++++++ plugins/protocolbuffers/php/v25.3/Dockerfile | 9 ++++++--- plugins/protocolbuffers/pyi/v25.3/Dockerfile | 7 +++++-- .../protocolbuffers/pyi/v25.3/buf.plugin.yaml | 2 +- .../protocolbuffers/python/v25.3/Dockerfile | 9 ++++++--- plugins/protocolbuffers/ruby/v25.3/Dockerfile | 9 ++++++--- 14 files changed, 98 insertions(+), 29 deletions(-) create mode 100644 plugins/protocolbuffers/cpp/v25.3/disable_cpp_editions.patch create mode 100644 plugins/protocolbuffers/objc/v25.3/disable_objc_editions.patch diff --git a/plugins/protocolbuffers/cpp/v25.3/.dockerignore b/plugins/protocolbuffers/cpp/v25.3/.dockerignore index c80455688..ab1677e27 100644 --- a/plugins/protocolbuffers/cpp/v25.3/.dockerignore +++ b/plugins/protocolbuffers/cpp/v25.3/.dockerignore @@ -1,4 +1,5 @@ * !BUILD !cpp.cc +!disable_cpp_editions.patch !Dockerfile diff --git a/plugins/protocolbuffers/cpp/v25.3/Dockerfile b/plugins/protocolbuffers/cpp/v25.3/Dockerfile index cc38d31df..5cbb19ef7 100644 --- a/plugins/protocolbuffers/cpp/v25.3/Dockerfile +++ b/plugins/protocolbuffers/cpp/v25.3/Dockerfile @@ -1,4 +1,4 @@ -# syntax=docker/dockerfile:1.5 +# syntax=docker/dockerfile:1.7 FROM debian:bookworm-20240211 AS build ARG TARGETARCH @@ -16,9 +16,14 @@ RUN curl -fsSL -o protoc.tar.gz https://github.com/protocolbuffers/protobuf/rele && rm protoc.tar.gz RUN bazel build '//:protoc_lib' COPY --link BUILD cpp.cc plugins/ -RUN bazel build '//plugins:protoc-gen-cpp.stripped' +COPY --link disable_cpp_editions.patch . +RUN git apply < disable_cpp_editions.patch \ + && bazel build '//plugins:protoc-gen-cpp.stripped' -FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608 -COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-cpp . +FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base + +FROM scratch +COPY --link --from=base / / +COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-cpp . USER nobody ENTRYPOINT ["/protoc-gen-cpp"] diff --git a/plugins/protocolbuffers/cpp/v25.3/disable_cpp_editions.patch b/plugins/protocolbuffers/cpp/v25.3/disable_cpp_editions.patch new file mode 100644 index 000000000..c4a57ab64 --- /dev/null +++ b/plugins/protocolbuffers/cpp/v25.3/disable_cpp_editions.patch @@ -0,0 +1,20 @@ +diff --git a/src/google/protobuf/compiler/cpp/generator.h b/src/google/protobuf/compiler/cpp/generator.h +index 64f334d5b..fd6e08e84 100644 +--- a/src/google/protobuf/compiler/cpp/generator.h ++++ b/src/google/protobuf/compiler/cpp/generator.h +@@ -70,14 +70,7 @@ class PROTOC_EXPORT CppGenerator : public CodeGenerator { + std::string* error) const override; + + uint64_t GetSupportedFeatures() const override { +- return FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS; +- } +- +- Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; } +- Edition GetMaximumEdition() const override { return Edition::EDITION_2023; } +- +- std::vector GetFeatureExtensions() const override { +- return {GetExtensionReflection(pb::cpp)}; ++ return FEATURE_PROTO3_OPTIONAL; + } + + private: diff --git a/plugins/protocolbuffers/csharp/v25.3/Dockerfile b/plugins/protocolbuffers/csharp/v25.3/Dockerfile index e6d842016..404f83346 100644 --- a/plugins/protocolbuffers/csharp/v25.3/Dockerfile +++ b/plugins/protocolbuffers/csharp/v25.3/Dockerfile @@ -1,4 +1,4 @@ -# syntax=docker/dockerfile:1.5 +# syntax=docker/dockerfile:1.7 FROM debian:bookworm-20240211 AS build ARG TARGETARCH @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib' COPY --link BUILD csharp.cc plugins/ RUN bazel build '//plugins:protoc-gen-csharp.stripped' -FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608 -COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-csharp . +FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base + +FROM scratch +COPY --link --from=base / / +COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-csharp . USER nobody ENTRYPOINT ["/protoc-gen-csharp"] diff --git a/plugins/protocolbuffers/java/v25.3/Dockerfile b/plugins/protocolbuffers/java/v25.3/Dockerfile index 2b32fd4d2..0346960e3 100644 --- a/plugins/protocolbuffers/java/v25.3/Dockerfile +++ b/plugins/protocolbuffers/java/v25.3/Dockerfile @@ -1,4 +1,4 @@ -# syntax=docker/dockerfile:1.5 +# syntax=docker/dockerfile:1.7 FROM debian:bookworm-20240211 AS build ARG TARGETARCH @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib' COPY --link BUILD java.cc plugins/ RUN bazel build '//plugins:protoc-gen-java.stripped' -FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608 -COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-java . +FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base + +FROM scratch +COPY --link --from=base / / +COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-java . USER nobody ENTRYPOINT ["/protoc-gen-java"] diff --git a/plugins/protocolbuffers/kotlin/v25.3/Dockerfile b/plugins/protocolbuffers/kotlin/v25.3/Dockerfile index 679d6ad36..ffa175a5b 100644 --- a/plugins/protocolbuffers/kotlin/v25.3/Dockerfile +++ b/plugins/protocolbuffers/kotlin/v25.3/Dockerfile @@ -1,4 +1,4 @@ -# syntax=docker/dockerfile:1.5 +# syntax=docker/dockerfile:1.7 FROM debian:bookworm-20240211 AS build ARG TARGETARCH @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib' COPY --link BUILD kotlin.cc plugins/ RUN bazel build '//plugins:protoc-gen-kotlin.stripped' -FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608 -COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-kotlin . +FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base + +FROM scratch +COPY --link --from=base / / +COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-kotlin . USER nobody ENTRYPOINT ["/protoc-gen-kotlin"] diff --git a/plugins/protocolbuffers/objc/v25.3/.dockerignore b/plugins/protocolbuffers/objc/v25.3/.dockerignore index 28438c51d..d395eb454 100644 --- a/plugins/protocolbuffers/objc/v25.3/.dockerignore +++ b/plugins/protocolbuffers/objc/v25.3/.dockerignore @@ -1,4 +1,5 @@ * !BUILD +!disable_objc_editions.patch !Dockerfile !objectivec.cc diff --git a/plugins/protocolbuffers/objc/v25.3/Dockerfile b/plugins/protocolbuffers/objc/v25.3/Dockerfile index 5161d63ca..473324466 100644 --- a/plugins/protocolbuffers/objc/v25.3/Dockerfile +++ b/plugins/protocolbuffers/objc/v25.3/Dockerfile @@ -1,4 +1,4 @@ -# syntax=docker/dockerfile:1.5 +# syntax=docker/dockerfile:1.7 FROM debian:bookworm-20240211 AS build ARG TARGETARCH @@ -16,9 +16,14 @@ RUN curl -fsSL -o protoc.tar.gz https://github.com/protocolbuffers/protobuf/rele && rm protoc.tar.gz RUN bazel build '//:protoc_lib' COPY --link BUILD objectivec.cc plugins/ -RUN bazel build '//plugins:protoc-gen-objectivec.stripped' +COPY --link BUILD disable_objc_editions.patch . +RUN git apply < disable_objc_editions.patch \ + && bazel build '//plugins:protoc-gen-objectivec.stripped' -FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608 -COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-objectivec . +FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base + +FROM scratch +COPY --link --from=base / / +COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-objectivec . USER nobody ENTRYPOINT ["/protoc-gen-objectivec"] diff --git a/plugins/protocolbuffers/objc/v25.3/disable_objc_editions.patch b/plugins/protocolbuffers/objc/v25.3/disable_objc_editions.patch new file mode 100644 index 000000000..ff540208e --- /dev/null +++ b/plugins/protocolbuffers/objc/v25.3/disable_objc_editions.patch @@ -0,0 +1,16 @@ +diff --git a/src/google/protobuf/compiler/objectivec/generator.h b/src/google/protobuf/compiler/objectivec/generator.h +index be5a6a448..4da2dba0d 100644 +--- a/src/google/protobuf/compiler/objectivec/generator.h ++++ b/src/google/protobuf/compiler/objectivec/generator.h +@@ -47,10 +47,8 @@ class PROTOC_EXPORT ObjectiveCGenerator : public CodeGenerator { + std::string* error) const override; + + uint64_t GetSupportedFeatures() const override { +- return (FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS); ++ return FEATURE_PROTO3_OPTIONAL; + } +- Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; } +- Edition GetMaximumEdition() const override { return Edition::EDITION_2023; } + }; + + } // namespace objectivec diff --git a/plugins/protocolbuffers/php/v25.3/Dockerfile b/plugins/protocolbuffers/php/v25.3/Dockerfile index 636260286..bdc2fba6f 100644 --- a/plugins/protocolbuffers/php/v25.3/Dockerfile +++ b/plugins/protocolbuffers/php/v25.3/Dockerfile @@ -1,4 +1,4 @@ -# syntax=docker/dockerfile:1.5 +# syntax=docker/dockerfile:1.7 FROM debian:bookworm-20240211 AS build ARG TARGETARCH @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib' COPY --link BUILD php.cc plugins/ RUN bazel build '//plugins:protoc-gen-php.stripped' -FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608 -COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-php . +FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base + +FROM scratch +COPY --link --from=base / / +COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-php . USER nobody ENTRYPOINT ["/protoc-gen-php"] diff --git a/plugins/protocolbuffers/pyi/v25.3/Dockerfile b/plugins/protocolbuffers/pyi/v25.3/Dockerfile index d72f7f85b..f00baf58e 100644 --- a/plugins/protocolbuffers/pyi/v25.3/Dockerfile +++ b/plugins/protocolbuffers/pyi/v25.3/Dockerfile @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib' COPY BUILD pyi.cc plugins/ RUN bazel build '//plugins:protoc-gen-pyi.stripped' -FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608 -COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-pyi . +FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base + +FROM scratch +COPY --link --from=base / / +COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-pyi . USER nobody ENTRYPOINT ["/protoc-gen-pyi"] diff --git a/plugins/protocolbuffers/pyi/v25.3/buf.plugin.yaml b/plugins/protocolbuffers/pyi/v25.3/buf.plugin.yaml index ccfb47839..c92e405c5 100644 --- a/plugins/protocolbuffers/pyi/v25.3/buf.plugin.yaml +++ b/plugins/protocolbuffers/pyi/v25.3/buf.plugin.yaml @@ -14,5 +14,5 @@ registry: requires_python: ">=3.8" deps: # https://pypi.org/project/protobuf/ - - "protobuf" + - "protobuf~=4.25" - "types-protobuf" diff --git a/plugins/protocolbuffers/python/v25.3/Dockerfile b/plugins/protocolbuffers/python/v25.3/Dockerfile index 42d13d574..6de3887af 100644 --- a/plugins/protocolbuffers/python/v25.3/Dockerfile +++ b/plugins/protocolbuffers/python/v25.3/Dockerfile @@ -1,4 +1,4 @@ -# syntax=docker/dockerfile:1.5 +# syntax=docker/dockerfile:1.7 FROM debian:bookworm-20240211 AS build ARG TARGETARCH @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib' COPY --link BUILD python.cc plugins/ RUN bazel build '//plugins:protoc-gen-python.stripped' -FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608 -COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-python . +FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base + +FROM scratch +COPY --link --from=base / / +COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-python . USER nobody ENTRYPOINT ["/protoc-gen-python"] diff --git a/plugins/protocolbuffers/ruby/v25.3/Dockerfile b/plugins/protocolbuffers/ruby/v25.3/Dockerfile index a436d7693..2ec9324b9 100644 --- a/plugins/protocolbuffers/ruby/v25.3/Dockerfile +++ b/plugins/protocolbuffers/ruby/v25.3/Dockerfile @@ -1,4 +1,4 @@ -# syntax=docker/dockerfile:1.5 +# syntax=docker/dockerfile:1.7 FROM debian:bookworm-20240211 AS build ARG TARGETARCH @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib' COPY --link BUILD ruby.cc plugins/ RUN bazel build '//plugins:protoc-gen-ruby.stripped' -FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608 -COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-ruby . +FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base + +FROM scratch +COPY --link --from=base / / +COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-ruby . USER nobody ENTRYPOINT ["/protoc-gen-ruby"]