Skip to content

Commit ce2a7bb

Browse files
authored
feat(cli): add support for building images for java connectors (#522)
1 parent 522c8b8 commit ce2a7bb

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

airbyte_cdk/utils/docker.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from airbyte_cdk.models.connector_metadata import ConnectorLanguage, MetadataFile
1717
from airbyte_cdk.utils.docker_image_templates import (
1818
DOCKERIGNORE_TEMPLATE,
19+
JAVA_CONNECTOR_DOCKERFILE_TEMPLATE,
1920
MANIFEST_ONLY_DOCKERFILE_TEMPLATE,
2021
PYTHON_CONNECTOR_DOCKERFILE_TEMPLATE,
2122
)
@@ -197,6 +198,22 @@ def build_connector_image(
197198

198199
base_tag = f"{metadata.data.dockerRepository}:{tag}"
199200
arch_images: list[str] = []
201+
202+
if metadata.data.language == ConnectorLanguage.JAVA:
203+
# This assumes that the repo root ('airbyte') is three levels above the
204+
# connector directory (airbyte/airbyte-integrations/connectors/source-foo).
205+
repo_root = connector_directory.parent.parent.parent
206+
# For Java connectors, we need to build the connector tar file first.
207+
subprocess.run(
208+
[
209+
"./gradlew",
210+
f":airbyte-integrations:connectors:{connector_name}:distTar",
211+
],
212+
cwd=repo_root,
213+
text=True,
214+
check=True,
215+
)
216+
200217
for arch in [ArchEnum.AMD64, ArchEnum.ARM64]:
201218
docker_tag = f"{base_tag}-{arch.value}"
202219
docker_tag_parts = docker_tag.split("/")
@@ -248,10 +265,7 @@ def get_dockerfile_template(
248265
return MANIFEST_ONLY_DOCKERFILE_TEMPLATE
249266

250267
if metadata.data.language == ConnectorLanguage.JAVA:
251-
raise ValueError(
252-
f"Java and Kotlin connectors are not yet supported. "
253-
"Please use airbyte-ci or gradle to build your image."
254-
)
268+
return JAVA_CONNECTOR_DOCKERFILE_TEMPLATE
255269

256270
raise ValueError(
257271
f"Unsupported connector language: {metadata.data.language}. "
@@ -322,10 +336,20 @@ def verify_connector_image(
322336
)
323337
# check that the output is valid JSON
324338
if result.stdout:
325-
try:
326-
json.loads(result.stdout)
327-
except json.JSONDecodeError:
328-
logger.error("Invalid JSON output from spec command.")
339+
found_spec_output = False
340+
for line in result.stdout.split("\n"):
341+
if line.strip():
342+
try:
343+
# Check if the line is a valid JSON object
344+
msg = json.loads(line)
345+
if isinstance(msg, dict) and "type" in msg and msg["type"] == "SPEC":
346+
found_spec_output = True
347+
348+
except json.JSONDecodeError as e:
349+
logger.warning(f"Invalid JSON output from spec command: {e}: {line}")
350+
351+
if not found_spec_output:
352+
logger.error("No valid JSON output found for spec command.")
329353
return False
330354
else:
331355
logger.error("No output from spec command.")

airbyte_cdk/utils/docker_image_templates.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
DOCKERIGNORE_TEMPLATE: str = "\n".join(
1515
[
1616
"# This file is auto-generated. Do not edit.",
17-
# "*,"
17+
"*," # Ignore everything not explicitly allowed below
1818
"build/",
19+
"!build/distributions/*.tar",
1920
".venv/",
2021
"secrets/",
2122
"!setup.py",
@@ -36,7 +37,7 @@
3637
# PYTHON CONNECTOR IMAGE ##
3738
###########################
3839

39-
PYTHON_CONNECTOR_DOCKERFILE_TEMPLATE = """
40+
PYTHON_CONNECTOR_DOCKERFILE_TEMPLATE = r"""
4041
# syntax=docker/dockerfile:1
4142
# check=skip=all
4243
ARG BASE_IMAGE
@@ -99,3 +100,37 @@
99100
ENV AIRBYTE_ENTRYPOINT="python ./main.py"
100101
ENTRYPOINT ["python", "./main.py"]
101102
"""
103+
104+
#########################
105+
# JAVA CONNECTOR IMAGE ##
106+
#########################
107+
108+
JAVA_CONNECTOR_DOCKERFILE_TEMPLATE = """
109+
# Java connector Dockerfile for Airbyte
110+
111+
# Build arguments
112+
ARG BASE_IMAGE
113+
114+
# Base image - using Amazon Corretto (Amazon's distribution of OpenJDK)
115+
FROM ${BASE_IMAGE}
116+
ARG CONNECTOR_KEBAB_NAME
117+
118+
# Set permissions for downloaded files
119+
RUN chmod +x /airbyte/base.sh /airbyte/javabase.sh && \
120+
chown airbyte:airbyte /airbyte/base.sh /airbyte/javabase.sh /airbyte/dd-java-agent.jar
121+
122+
ENV AIRBYTE_ENTRYPOINT="/airbyte/base.sh"
123+
ENV APPLICATION="${CONNECTOR_KEBAB_NAME}"
124+
125+
# Add the connector TAR file and extract it
126+
COPY ./build/distributions/${CONNECTOR_KEBAB_NAME}.tar /tmp/${CONNECTOR_KEBAB_NAME}.tar
127+
RUN tar xf /tmp/${CONNECTOR_KEBAB_NAME}.tar --strip-components=1 -C /airbyte && \
128+
rm -rf /tmp/${CONNECTOR_KEBAB_NAME}.tar && \
129+
chown -R airbyte:airbyte /airbyte
130+
131+
# Set the non-root user
132+
USER airbyte
133+
134+
# Set entrypoint
135+
ENTRYPOINT ["/airbyte/base.sh"]
136+
"""

0 commit comments

Comments
 (0)