diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e191b813..740c9e13 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -115,14 +115,20 @@ jobs: type=raw,value=${{ needs.prepare.outputs.zk_version }}-apache-${{ needs.prepare.outputs.tag }} type=raw,value=${{ needs.prepare.outputs.zk_version }}-apache labels: | - org.opencontainers.image.description=Apache Zookeeper ${{ needs.prepare.outputs.zk_version }} with OpenJDK 25 - # Rebuilt on every release, so it also picks up the latest JDK 25 patch - # release and Debian security updates. + org.opencontainers.image.description=Apache Zookeeper ${{ needs.prepare.outputs.zk_version }} + # Rebuilt on every release with Debian security updates and the selected JDK. - name: Build and push zookeeper Apache image id: build-zk-apache uses: docker/build-push-action@v7 with: context: docker/zookeeper-image + # Enable these together when selecting an alternate JDK release: + # secrets: | + # JAVA_DOWNLOAD_APIKEY=${{ secrets.JAVA_DOWNLOAD_APIKEY }} + # Add these overrides under build-args: + # java_version=25 + # java_download_base_url=${{ secrets.JAVA_DOWNLOAD_BASE_URL }} + # java_download_version=25.0.1.1 platforms: linux/amd64,linux/arm64 build-args: | ZK_VERSION=${{ needs.prepare.outputs.zk_version }} @@ -151,7 +157,7 @@ jobs: tags: | type=raw,value=${{ needs.prepare.outputs.zk_version }}-${{ needs.prepare.outputs.tag }} labels: | - org.opencontainers.image.description=Apache Zookeeper image build with java-25 + org.opencontainers.image.description=Apache Zookeeper image - name: Build and push zookeeper image id: build-zk uses: docker/build-push-action@v7 diff --git a/.github/workflows/zookeeper-images.yaml b/.github/workflows/zookeeper-images.yaml index df3b8e1e..07b19e7c 100644 --- a/.github/workflows/zookeeper-images.yaml +++ b/.github/workflows/zookeeper-images.yaml @@ -87,7 +87,7 @@ jobs: run: | docker run --rm --entrypoint bash -e ZK_VERSION zookeeper:ci -ec ' java -version - java -XshowSettings:properties -version 2>&1 | grep -Eq "^[[:space:]]*java.specification.version = 25$" + java -XshowSettings:properties -version 2>&1 | grep -Eq "^[[:space:]]*java.specification.version = ${JAVA_VERSION}$" zkServer.sh version 2>/dev/null | tee /dev/stderr | grep -q "${ZK_VERSION}" test -s /opt/libs/zu.jar set +e diff --git a/docker/Dockerfile b/docker/Dockerfile index 4f9f1c09..4bdd8a04 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1 # check=skip=InvalidDefaultArgInFrom # # Copyright (c) 2018 Dell Inc., or its subsidiaries. All Rights Reserved. @@ -9,12 +10,14 @@ # http://www.apache.org/licenses/LICENSE-2.0 # -# Base Apache ZooKeeper + OpenJDK 25 image built from docker/zookeeper-image. +# Base Apache ZooKeeper + OpenJDK image built from docker/zookeeper-image. # It is not pinned here: the build-zookeeper release job builds that image # first and passes its digest in, so both always come from the same commit # and shared zookeeperVersion. For local builds use `make build-zk-image`. ARG BASE_IMAGE +FROM ${BASE_IMAGE} AS runtime + # zu.jar is architecture-independent bytecode, so build it once on the native # build platform instead of re-running Gradle under emulation for every target. FROM --platform=$BUILDPLATFORM gradle:jdk25 AS zu-builder @@ -22,9 +25,12 @@ FROM --platform=$BUILDPLATFORM gradle:jdk25 AS zu-builder RUN mkdir /zu COPY zu /zu WORKDIR /zu -RUN ./gradlew shadowJar --no-daemon +RUN --mount=type=bind,from=runtime,source=/usr/local,target=/runtime-java \ + java_version="$(sed -n 's/^JAVA_VERSION="\([0-9][0-9]*\).*/\1/p' /runtime-java/openjdk-*/release)" && \ + test -n "$java_version" && \ + ./gradlew -PjavaVersion="$java_version" shadowJar --no-daemon -FROM ${BASE_IMAGE} +FROM runtime COPY bin /usr/local/bin RUN chmod +x /usr/local/bin/* COPY --from=zu-builder /zu/build/libs/zu.jar /opt/libs/ @@ -34,7 +40,7 @@ RUN apt-get -q update && \ # OCI standard labels LABEL org.opencontainers.image.title="Apache Zookeeper" -LABEL org.opencontainers.image.description="Apache Zookeeper image build with java-25" +LABEL org.opencontainers.image.description="Apache Zookeeper image build with java-${JAVA_VERSION}" LABEL org.opencontainers.image.source="https://github.com/adobe/zookeeper-operator" LABEL org.opencontainers.image.url="https://github.com/adobe/zookeeper-operator" LABEL org.opencontainers.image.documentation="https://github.com/adobe/zookeeper-operator" diff --git a/docker/zookeeper-image/Dockerfile b/docker/zookeeper-image/Dockerfile index eddefd9c..0465ae4c 100644 --- a/docker/zookeeper-image/Dockerfile +++ b/docker/zookeeper-image/Dockerfile @@ -1,5 +1,11 @@ +# syntax=docker/dockerfile:1 + # backported from https://github.com/docker-library/openjdk/blob/4c2b1a57869e24c5fcfbea6a17e558c710d4f6bd/21/jdk/slim-bullseye/Dockerfile -FROM debian:bookworm-slim +FROM debian:trixie-slim + +ARG java_version=25 +ARG java_download_version=${java_version} +ARG java_download_base_url=https://download.oracle.com/java RUN set -eux; \ apt-get update; \ @@ -8,24 +14,30 @@ RUN set -eux; \ ; \ rm -rf /var/lib/apt/lists/* -ENV JAVA_HOME=/usr/local/openjdk-25 +ENV JAVA_HOME=/usr/local/openjdk-${java_version} ENV PATH=$JAVA_HOME/bin:$PATH # Default to UTF-8 file.encoding ENV LANG=C.UTF-8 -ENV JAVA_VERSION=25 +ENV JAVA_VERSION=${java_version} -RUN set -eux; \ +RUN --mount=type=secret,id=JAVA_DOWNLOAD_APIKEY,env=JAVA_DOWNLOAD_APIKEY \ + set -eux; \ + \ + case "$java_download_base_url" in \ + https://download.oracle.com/*) java_download_base_url="${java_download_base_url}/${java_version}/latest" ;; \ + *) java_download_base_url="${java_download_base_url}/${java_version}" ;; \ + esac; \ \ arch="$(dpkg --print-architecture)"; \ case "$arch" in \ 'amd64') \ - downloadUrl='https://download.oracle.com/java/25/latest/jdk-25_linux-x64_bin.tar.gz'; \ - downloadSha256='https://download.oracle.com/java/25/latest/jdk-25_linux-x64_bin.tar.gz.sha256'; \ + downloadUrl="${java_download_base_url}/jdk-${java_download_version}_linux-x64_bin.tar.gz"; \ + downloadSha256="${downloadUrl}.sha256"; \ ;; \ 'arm64') \ - downloadUrl='https://download.oracle.com/java/25/latest/jdk-25_linux-aarch64_bin.tar.gz'; \ - downloadSha256='https://download.oracle.com/java/25/latest/jdk-25_linux-aarch64_bin.tar.gz.sha256'; \ + downloadUrl="${java_download_base_url}/jdk-${java_download_version}_linux-aarch64_bin.tar.gz"; \ + downloadSha256="${downloadUrl}.sha256"; \ ;; \ *) echo >&2 "error: unsupported architecture: '$arch'"; exit 1 ;; \ esac; \ @@ -33,12 +45,16 @@ RUN set -eux; \ savedAptMark="$(apt-mark showmanual)"; \ apt-get update; \ apt-get install -y --no-install-recommends \ - wget \ + curl \ ; \ rm -rf /var/lib/apt/lists/*; \ \ - wget --progress=dot:giga -O openjdk.tgz "$downloadUrl"; \ - SHA256=$(wget -qO- $downloadSha256) ; \ + set --; \ + if [ -n "${JAVA_DOWNLOAD_APIKEY:+set}" ]; then \ + set -- --variable '%JAVA_DOWNLOAD_APIKEY' --expand-header 'Authorization: Bearer {{JAVA_DOWNLOAD_APIKEY}}'; \ + fi; \ + curl --fail --location "$@" --output openjdk.tgz "$downloadUrl"; \ + SHA256="$(curl --fail --silent --show-error --location "$@" "$downloadSha256")"; \ echo "$SHA256 *openjdk.tgz" | sha256sum --strict --check -; \ \ mkdir -p "$JAVA_HOME"; \ @@ -162,7 +178,7 @@ ENV PATH=$PATH:/$DISTRO_NAME/bin \ # OCI standard labels LABEL org.opencontainers.image.title="Apache Zookeeper" -LABEL org.opencontainers.image.description="Apache Zookeeper ${ZK_VERSION} with OpenJDK 25" +LABEL org.opencontainers.image.description="Apache Zookeeper ${ZK_VERSION} with OpenJDK ${java_version}" LABEL org.opencontainers.image.source="https://github.com/adobe/zookeeper-operator" LABEL org.opencontainers.image.url="https://github.com/adobe/zookeeper-operator" LABEL org.opencontainers.image.documentation="https://github.com/adobe/zookeeper-operator" diff --git a/docker/zookeeper-image/README.md b/docker/zookeeper-image/README.md new file mode 100644 index 00000000..0e440757 --- /dev/null +++ b/docker/zookeeper-image/README.md @@ -0,0 +1,69 @@ +# Apache ZooKeeper base image + +Build from the repository root: + +```bash +docker buildx build --load \ + --build-arg ZK_VERSION="$(./scripts/zookeeper-version.sh)" \ + -t zookeeper-apache:local docker/zookeeper-image +``` + +The image uses Debian Trixie and defaults to Java 25. Oracle's public JDK +download URL is the default, without authentication. + +## Alternate JDK downloads + +Override `java_download_base_url` and `java_download_version` to use another +source. The Dockerfile appends `java_version` (default: `25`) and `latest` +for Oracle, or only `java_version` for alternate sources. Do not include +the Java-major directory in the base URL. Alternate sources must +provide Oracle-style archive filenames and matching `.sha256` files for +each architecture (`x64` and `aarch64`). + +For example, add these options to the build command: + +```bash +--build-arg java_download_base_url=https://jdk.example.com/java \ +--build-arg java_version=21 \ +--build-arg java_download_version=21.0.13.1 +``` + +This requests `/java/21/jdk-21.0.13.1_linux-_bin.tar.gz` +and its `.sha256` file. Both downloads must succeed and the checksum must match. + +For authenticated downloads, pass an optional API key as a BuildKit secret: + +```bash +--secret id=JAVA_DOWNLOAD_APIKEY,env=JAVA_DOWNLOAD_APIKEY +``` + +The key is sent as an `Authorization: Bearer` header, is not stored in the +image, and is not forwarded to a different host on redirects. + +Release builds also default to Oracle. To use an alternate source, add the +commented overrides in `.github/workflows/release.yml` to the Apache ZooKeeper +build step's `build-args`, configure the GitHub Actions secret +`JAVA_DOWNLOAD_BASE_URL`, and select a matching `java_download_version`. +For authenticated downloads, also enable the commented `secrets` input and +configure `JAVA_DOWNLOAD_APIKEY`. PR image builds continue to use Oracle +without secrets. + +## Full ZooKeeper image + +After building the base image above, build the operator-flavoured image: + +```bash +docker buildx build --load \ + --build-arg BASE_IMAGE=zookeeper-apache:local \ + -t zookeeper:local docker +``` + +The full image reads the base image's JDK release metadata and uses its +Java major version for both Java and Kotlin compilation targets in `zu`. +Set `java_version` only when building the base image; no second version +argument is needed for the full image. + +The native Gradle builder uses JDK 25 independently of the selected runtime. +Direct Gradle builds, including `./gradlew build --no-daemon`, read the +default from the same base Dockerfile. An explicit `-PjavaVersion=` +overrides that target when needed. diff --git a/docker/zu/build.gradle.kts b/docker/zu/build.gradle.kts index 9b2896b0..497fdafd 100644 --- a/docker/zu/build.gradle.kts +++ b/docker/zu/build.gradle.kts @@ -11,9 +11,17 @@ repositories { mavenCentral() } +val javaVersion = JavaVersion.toVersion( + providers.gradleProperty("javaVersion").orElse(providers.provider { + file("../zookeeper-image/Dockerfile").readLines() + .single { it.startsWith("ARG java_version=") } + .substringAfter("=") + }).get() +) + java { - sourceCompatibility = JavaVersion.VERSION_25 - targetCompatibility = JavaVersion.VERSION_25 + sourceCompatibility = javaVersion + targetCompatibility = javaVersion } val zookeeperVersion = "3.9.7" @@ -36,6 +44,6 @@ tasks.withType() { tasks.withType { compilerOptions { - jvmTarget.set(JvmTarget.JVM_25) + jvmTarget.set(JvmTarget.fromTarget(javaVersion.majorVersion)) } } diff --git a/scripts/test_zookeeper_version.py b/scripts/test_zookeeper_version.py index fce0e6d4..dc483ae7 100644 --- a/scripts/test_zookeeper_version.py +++ b/scripts/test_zookeeper_version.py @@ -1,3 +1,4 @@ +import hashlib import json import os from pathlib import Path @@ -10,6 +11,99 @@ ROOT = Path(__file__).resolve().parents[1] +class JavaDownloadTest(unittest.TestCase): + def run_download(self, arch="amd64", base="https://download.oracle.com/java", + major="25", version="25", api_key=None, corrupt=False): + dockerfile = (ROOT / "docker/zookeeper-image/Dockerfile").read_text() + download = dockerfile.split("ENV JAVA_VERSION=", 1)[1].split( + "RUN ", 1 + )[1].split('mkdir -p "$JAVA_HOME"', 1)[0] + if download.startswith("--mount="): + download = download.split("\\\n", 1)[1] + download = download.replace("\\\n", "").rstrip() + "\n" + variables = {key: value for key, value in os.environ.items() + if key != "JAVA_DOWNLOAD_APIKEY"} + variables.update( + ARCH=arch, java_version=major, java_download_version=version, + java_download_base_url=base, + DOWNLOAD_SHA=hashlib.sha256( + b"wrong" if corrupt else b"artifact" + ).hexdigest(), + ) + if api_key is not None: + variables["JAVA_DOWNLOAD_APIKEY"] = api_key + with tempfile.TemporaryDirectory() as directory: + result = subprocess.run( + ["bash", "-ec", + 'dpkg() { printf "%s\\n" "$ARCH"; }\n' + 'apt-mark() { :; }\n' + 'apt-get() { :; }\n' + 'rm() { :; }\n' + 'wget() { return 99; }\n' + 'curl() {\n' + ' python3 -c \'import json, sys; ' + 'open("downloads.jsonl", "a").write(' + 'json.dumps(sys.argv[1:]) + "\\n")\' "$@"\n' + ' for url; do :; done\n' + ' case "$url" in\n' + ' *.sha256) printf "%s\\n" "$DOWNLOAD_SHA" ;;\n' + ' *) printf artifact > openjdk.tgz ;;\n' + ' esac\n' + '}\n' + download], + cwd=directory, env=variables, text=True, capture_output=True, + ) + log = Path(directory) / "downloads.jsonl" + calls = [json.loads(line) for line in log.read_text().splitlines() + ] if log.exists() else [] + return result, calls + + def test_oracle_defaults_preserve_both_architecture_urls(self): + for arch, name in (("amd64", "x64"), ("arm64", "aarch64")): + with self.subTest(arch=arch): + result, calls = self.run_download(arch=arch) + self.assertEqual(result.returncode, 0, result.stderr) + url = ("https://download.oracle.com/java/25/latest/" + f"jdk-25_linux-{name}_bin.tar.gz") + self.assertEqual([call[-1] for call in calls], + [url, url + ".sha256"]) + self.assertNotIn("--expand-header", calls[0]) + + def test_alternate_source_uses_patch_version_and_optional_authentication(self): + for arch, name in (("amd64", "x64"), ("arm64", "aarch64")): + for key in (None, "synthetic-download-key"): + with self.subTest(arch=arch, authenticated=key is not None): + result, calls = self.run_download( + arch=arch, base="https://jdk.example.test/java", + major="21", version="21.0.13.1", api_key=key, + ) + self.assertEqual(result.returncode, 0, result.stderr) + url = ("https://jdk.example.test/java/21/" + f"jdk-21.0.13.1_linux-{name}_bin.tar.gz") + self.assertEqual([call[-1] for call in calls], + [url, url + ".sha256"]) + for call in calls: + self.assertIn("--fail", call) + self.assertIn("--location", call) + self.assertEqual("--expand-header" in call, key is not None) + if key is not None: + self.assertIn("%JAVA_DOWNLOAD_APIKEY", call) + self.assertIn("Authorization: Bearer {{JAVA_DOWNLOAD_APIKEY}}", + call) + self.assertNotIn(key, " ".join(call)) + self.assertNotIn(key, result.stderr) + + def test_checksum_mismatch_stops_the_build(self): + result, _ = self.run_download(corrupt=True) + self.assertNotEqual(result.returncode, 0) + self.assertIn("FAILED", result.stdout) + + def test_unsupported_architecture_stops_before_downloading(self): + result, calls = self.run_download(arch="s390x") + self.assertNotEqual(result.returncode, 0) + self.assertIn("unsupported architecture", result.stderr) + self.assertEqual(calls, []) + + class ZookeeperDownloadTest(unittest.TestCase): def run_download(self, available_url, empty=False): dockerfile = (ROOT / "docker/zookeeper-image/Dockerfile").read_text() diff --git a/scripts/test_zu_runtime.py b/scripts/test_zu_runtime.py index 30b0f5df..3c481c6c 100644 --- a/scripts/test_zu_runtime.py +++ b/scripts/test_zu_runtime.py @@ -2,12 +2,52 @@ import os from pathlib import Path import subprocess +import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] +class ZuJavaVersionTest(unittest.TestCase): + def run_build(self, release): + dockerfile = (ROOT / "docker/Dockerfile").read_text() + build = dockerfile.split("WORKDIR /zu\n", 1)[1].split( + "\nFROM ", 1 + )[0].strip().removeprefix("RUN ") + if build.startswith("--mount="): + build = build.split("\\\n", 1)[1] + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + jdk = root / "runtime-java/openjdk-selected" + jdk.mkdir(parents=True) + (jdk / "release").write_text(release) + gradle = root / "gradlew" + gradle.write_text('#!/bin/sh\nprintf "%s\\n" "$@"\n') + gradle.chmod(0o700) + return subprocess.run( + ["sh", "-ec", build.replace("\\\n", "").replace( + "/runtime-java", str(root / "runtime-java") + )], + cwd=root, text=True, capture_output=True, + ) + + def test_compilation_target_follows_base_image_jdk(self): + for release, major in (('JAVA_VERSION="21.0.13.1"\n', "21"), + ('JAVA_VERSION="25.0.5.1"\n', "25")): + with self.subTest(major=major): + result = self.run_build(release) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout.splitlines(), [ + f"-PjavaVersion={major}", "shadowJar", "--no-daemon", + ]) + + def test_missing_java_version_stops_before_gradle(self): + result = self.run_build('IMPLEMENTOR="Oracle Corporation"\n') + self.assertNotEqual(result.returncode, 0) + self.assertEqual(result.stdout, "") + + class ZuRuntimeTest(unittest.TestCase): def run_zu(self, env=None, java_body=None): variables = {