Skip to content

Commit 09c1c9e

Browse files
authored
Enable Java SDK Distroless container image variants. (apache#33173)
* Enable Java SDK Distroless container image variant * Add LANG environment and /usr/lib/locale * Use examples tests instead
1 parent d342dd3 commit 09c1c9e

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

Diff for: .github/trigger_files/beam_PostCommit_Java.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
{}
1+
{
2+
"comment": "Modify this file in a trivial way to cause this test suite to run",
3+
"modification": 1
4+
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"comment": "Modify this file in a trivial way to cause this test suite to run",
3-
"modification": 1
3+
"modification": 2
44
}

Diff for: runners/google-cloud-dataflow-java/build.gradle

+63
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,69 @@ def createRunnerV2ValidatesRunnerTest = { Map args ->
273273
}
274274
}
275275

276+
tasks.register('examplesJavaRunnerV2IntegrationTestDistroless', Test.class) {
277+
group = "verification"
278+
dependsOn 'buildAndPushDistrolessContainerImage'
279+
def javaVer = project.findProperty('testJavaVersion')
280+
def repository = "us.gcr.io/apache-beam-testing/${System.getenv('USER')}"
281+
def tag = project.findProperty('dockerTag')
282+
def imageURL = "${repository}/beam_${javaVer}_sdk_distroless:${tag}"
283+
def pipelineOptions = [
284+
"--runner=TestDataflowRunner",
285+
"--project=${gcpProject}",
286+
"--region=${gcpRegion}",
287+
"--tempRoot=${dataflowValidatesTempRoot}",
288+
"--sdkContainerImage=${imageURL}",
289+
"--experiments=use_unified_worker,use_runner_v2",
290+
"--firestoreDb=${firestoreDb}",
291+
]
292+
systemProperty "beamTestPipelineOptions", JsonOutput.toJson(pipelineOptions)
293+
294+
include '**/*IT.class'
295+
296+
maxParallelForks 4
297+
classpath = configurations.examplesJavaIntegrationTest
298+
testClassesDirs = files(project(":examples:java").sourceSets.test.output.classesDirs)
299+
useJUnit { }
300+
}
301+
302+
tasks.register('buildAndPushDistrolessContainerImage', Task.class) {
303+
// Only Java 17 and 21 are supported.
304+
// See https://github.com/GoogleContainerTools/distroless/tree/main/java#image-contents.
305+
def allowed = ["java17", "java21"]
306+
doLast {
307+
def javaVer = project.findProperty('testJavaVersion')
308+
if (!allowed.contains(javaVer)) {
309+
throw new GradleException("testJavaVersion must be one of ${allowed}, got: ${javaVer}")
310+
}
311+
if (!project.hasProperty('dockerTag')) {
312+
throw new GradleException("dockerTag is missing but required")
313+
}
314+
def repository = "us.gcr.io/apache-beam-testing/${System.getenv('USER')}"
315+
def tag = project.findProperty('dockerTag')
316+
def imageURL = "${repository}/beam_${javaVer}_sdk_distroless:${tag}"
317+
exec {
318+
executable 'docker'
319+
workingDir rootDir
320+
args = [
321+
'buildx',
322+
'build',
323+
'-t',
324+
imageURL,
325+
'-f',
326+
'sdks/java/container/Dockerfile-distroless',
327+
"--build-arg=BEAM_BASE=gcr.io/apache-beam-testing/beam-sdk/beam_${javaVer}_sdk",
328+
"--build-arg=DISTROLESS_BASE=gcr.io/distroless/${javaVer}-debian12",
329+
'.'
330+
]
331+
}
332+
exec {
333+
executable 'docker'
334+
args = ['push', imageURL]
335+
}
336+
}
337+
}
338+
276339
// Push docker images to a container registry for use within tests.
277340
// NB: Tasks which consume docker images from the registry should depend on this
278341
// task directly ('dependsOn buildAndPushDockerJavaContainer'). This ensures the correct

Diff for: sdks/java/container/Dockerfile-distroless

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
###############################################################################
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
###############################################################################
18+
19+
# ARG BEAM_BASE is the Beam SDK container image built using sdks/python/container/Dockerfile.
20+
ARG BEAM_BASE
21+
22+
# ARG DISTROLESS_BASE is the distroless container image URL. For available distroless Java images,
23+
# see https://github.com/GoogleContainerTools/distroless/tree/main?tab=readme-ov-file#what-images-are-available.
24+
# Only Java versions 17 and 21 are supported.
25+
ARG DISTROLESS_BASE
26+
FROM ${BEAM_BASE} AS base
27+
ARG TARGETARCH
28+
ENV LANG C.UTF-8
29+
30+
LABEL Author="Apache Beam <[email protected]>"
31+
32+
RUN if [ -z "${TARGETARCH}" ]; then echo "fatal: TARGETARCH not set; run as docker buildx build or use --build-arg=TARGETARCH=amd64|arm64" >&2; exit 1; fi
33+
34+
FROM ${DISTROLESS_BASE}:latest-${TARGETARCH} AS distroless
35+
36+
COPY --from=base /opt /opt
37+
38+
# Along with the LANG environment variable above, prevents internally discovered failing bugs related to Dataflow Flex
39+
# template character encodings.
40+
COPY --from=base /usr/lib/locale /usr/lib/locale
41+
42+
ENTRYPOINT ["/opt/apache/beam/boot"]

0 commit comments

Comments
 (0)