-
Notifications
You must be signed in to change notification settings - Fork 955
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1405 from Polber:jkinard/yaml-speed
PiperOrigin-RevId: 626020498
- Loading branch information
Showing
17 changed files
with
237 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
66 changes: 59 additions & 7 deletions
66
plugins/core-plugin/src/main/resources/Dockerfile-template-yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,68 @@ | ||
FROM ${baseContainerImage} | ||
#===================================================================# | ||
# Create build environment from base Python template launcher image # | ||
#===================================================================# | ||
FROM gcr.io/dataflow-templates-base/python311-template-launcher-base:latest as python-base | ||
|
||
# Copy template files to /template | ||
# Build args | ||
ARG WORKDIR=/template | ||
COPY main.py /template | ||
COPY requirements.txt /template | ||
COPY ${yamlTemplateName} /template/template.yaml | ||
ARG REQUIREMENTS_FILE=requirements.txt | ||
ARG BEAM_VERSION=${beamVersion} | ||
ARG BEAM_PACKAGE=apache-beam[dataframe,gcp,test,yaml]==$BEAM_VERSION | ||
ARG PY_VERSION=${pythonVersion} | ||
|
||
# Copy template files to /template | ||
RUN mkdir -p $WORKDIR | ||
COPY main.py requirements.txt* /template/ | ||
<#if copyOtherFiles??>COPY ${copyOtherFiles} /template/</#if> | ||
WORKDIR $WORKDIR | ||
|
||
ENV FLEX_TEMPLATE_PYTHON_REQUIREMENTS_FILE=requirements.txt | ||
# Create requirements.txt file if not provided | ||
RUN if ! [ -f requirements.txt ] ; then echo "$BEAM_PACKAGE" > requirements.txt ; fi | ||
|
||
# Install dependencies to launch the pipeline and download to reduce startup time | ||
RUN python -m venv /venv \ | ||
&& /venv/bin/pip install --no-cache-dir --upgrade pip setuptools \ | ||
&& /venv/bin/pip install --no-cache-dir -U -r $REQUIREMENTS_FILE \ | ||
&& /venv/bin/pip download --no-cache-dir --dest /tmp/dataflow-requirements-cache -r $REQUIREMENTS_FILE \ | ||
&& rm -rf /usr/local/lib/python$PY_VERSION/site-packages \ | ||
&& mv /venv/lib/python$PY_VERSION/site-packages /usr/local/lib/python$PY_VERSION/ | ||
|
||
|
||
#============================================================# | ||
# Create Distroless xlang image compatible with YamlTemplate # | ||
#============================================================# | ||
FROM ${baseJavaContainerImage} | ||
|
||
# Build args | ||
ARG CHIPSET_ARCH=x86_64-linux-gnu | ||
ARG PY_VERSION=${pythonVersion} | ||
|
||
# Set python environment variables | ||
ENV FLEX_TEMPLATE_PYTHON_PY_FILE=main.py | ||
ENV PIP_NO_DEPS=True | ||
|
||
# Copy template, python wheels and python launcher script from python-base | ||
COPY --from=python-base /template /template | ||
COPY --from=python-base /tmp/dataflow-requirements-cache /tmp/dataflow-requirements-cache | ||
COPY --from=python-base /opt/google/dataflow/python_template_launcher /opt/google/dataflow/python_template_launcher | ||
|
||
# Copy python and installed packages from python-base | ||
COPY --from=python-base /usr/local/bin/python$PY_VERSION /usr/local/bin/python | ||
COPY --from=python-base /usr/local/lib/python$PY_VERSION /usr/local/lib/python$PY_VERSION | ||
|
||
# Copy required shared libraries from python-base | ||
COPY --from=python-base /lib/$CHIPSET_ARCH/ld-*so* /lib64/ | ||
COPY --from=python-base /lib/$CHIPSET_ARCH/lib*so* /lib/$CHIPSET_ARCH/ | ||
COPY --from=python-base /usr/lib/$CHIPSET_ARCH/libffi* /usr/lib/$CHIPSET_ARCH/ | ||
COPY --from=python-base /usr/local/lib/libpython$PY_VERSION* /usr/local/lib/ | ||
|
||
# Copy minimal commands from python-base needed to execute template | ||
COPY --from=python-base /bin/dash /bin/sh | ||
COPY --from=python-base /usr/bin/which.debianutils /usr/bin/which | ||
|
||
# Copy licenses | ||
COPY --from=python-base /usr/licenses/ /usr/licenses/ | ||
|
||
WORKDIR /template | ||
|
||
ENTRYPOINT ["/opt/google/dataflow/python_template_launcher"] | ||
ENTRYPOINT ["/opt/google/dataflow/python_template_launcher"] |
79 changes: 79 additions & 0 deletions
79
...re-plugin/src/test/java/com/google/cloud/teleport/plugin/YamlDockerfileGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (C) 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.google.cloud.teleport.plugin; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.google.common.base.Charsets; | ||
import com.google.common.io.Files; | ||
import freemarker.template.TemplateException; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for class {@link PythonDockerfileGenerator}. */ | ||
@RunWith(JUnit4.class) | ||
public class YamlDockerfileGeneratorTest { | ||
private final File outputFolder = Files.createTempDir().getAbsoluteFile(); | ||
|
||
@Test | ||
public void testGenerateDockerfile() throws IOException, TemplateException { | ||
new File(outputFolder.getAbsolutePath() + "/word-count").mkdirs(); | ||
YamlDockerfileGenerator.generateDockerfile( | ||
"a java container image", | ||
"beam_version", | ||
"py_version", | ||
"word-count", | ||
List.of(), | ||
outputFolder); | ||
File outputFile = new File(outputFolder.getAbsolutePath() + "/word-count/Dockerfile"); | ||
|
||
assertTrue(outputFile.exists()); | ||
String fileContents = Files.toString(outputFile, Charsets.UTF_8); | ||
assertThat(fileContents).contains("FROM a java container image"); | ||
assertThat(fileContents).contains("=beam_version"); | ||
assertThat(fileContents).contains("=py_version"); | ||
assertThat(fileContents).contains("COPY main.py requirements.txt* /template/"); | ||
assertThat(fileContents) | ||
.doesNotContainMatch( | ||
"(?m)^(?!COPY main\\.py.*)(COPY(?!.*--from=).*/template.*$|COPY main\\.py.*)$"); | ||
} | ||
|
||
@Test | ||
public void testGenerateDockerfileWithOtherFiles() throws IOException, TemplateException { | ||
new File(outputFolder.getAbsolutePath() + "/word-count").mkdirs(); | ||
YamlDockerfileGenerator.generateDockerfile( | ||
"a java container image", | ||
"beam_version", | ||
"py_version", | ||
"word-count", | ||
List.of("other_file"), | ||
outputFolder); | ||
File outputFile = new File(outputFolder.getAbsolutePath() + "/word-count/Dockerfile"); | ||
|
||
assertTrue(outputFile.exists()); | ||
String fileContents = Files.toString(outputFile, Charsets.UTF_8); | ||
assertThat(fileContents).contains("FROM a java container image"); | ||
assertThat(fileContents).contains("=beam_version"); | ||
assertThat(fileContents).contains("=py_version"); | ||
assertThat(fileContents).contains("COPY main.py requirements.txt* /template/"); | ||
assertThat(fileContents).contains("COPY other_file /template/"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.