<#1929 Airflow Agent Refactor> Pull request opened...
# flyte-github
a
#1929 Airflow Agent Refactor Pull request opened by pingsutw TL;DR • Use imageSpec inside the airflow operator • Run dataflow operator in the container • Remove hacky code • Remove google dependencies Type ☐ Bug Fix ☑︎ Feature ☐ Plugin Are all requirements met? ☐ Code completed ☑︎ Smoke tested ☐ Unit tests added ☐ Code documentation added ☐ Any pending items have an associated Issue Complete description Here is an example
Copy code
import os

from airflow.providers.apache.beam.operators.beam import BeamRunPythonPipelineOperator, BeamRunJavaPipelineOperator
from airflow.providers.google.cloud.operators.dataflow import DataflowConfiguration
from click.testing import CliRunner

from flytekit import workflow, ImageSpec
from flytekit.clis.sdk_in_container import pyflyte

new_airflow_plugin = "git+https://github.com/flyteorg/flytekit.git@8b41e63fa50070bbdc0a3486763ceafec892f932#subdirectory=plugins/flytekit-airflow"
image_spec = ImageSpec(
    registry="pingsutw",
    base_image="pingsutw/flytekit:dataflow-v2",
    packages=["apache-airflow[google]", "apache-airflow-providers-apache-beam[google]", new_airflow_plugin],
    apt_packages=["git"],
    env={"AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT": "google-cloud-platform://"}
)


@workflow
def wf():
    BeamRunJavaPipelineOperator(
        task_id="start_java_pipeline_dataflow_runner",
        runner="DataflowRunner",
        job_class="org.apache.beam.examples.WordCount",
        pipeline_options={"output": "gs://opta-gcp-dogfood-gcp/spark",
                          "stagingLocation": "gs://opta-gcp-dogfood-gcp/staging",
                          "tempLocation": "gs://opta-gcp-dogfood-gcp/staging1",
                          "region": "us-east1"},
        jar="gs://opta-gcp-dogfood-gcp/spark/word-count-beam-bundled-0.1.jar",
        dataflow_config=DataflowConfiguration(job_name="test_dataflow",
                                              project_id="dogfood-gcp-dataplane",
                                              location="us-east1",
                                              wait_until_finished=False),
        container_image=image_spec
    )

    BeamRunPythonPipelineOperator(
        task_id="start_python_pipeline_dataflow_runner",
        runner="DataflowRunner",
        py_file="gs://opta-gcp-dogfood-gcp/spark/word_count.py",
        pipeline_options={"output": "gs://opta-gcp-dogfood-gcp/spark",
                          "input": "gs://opta-gcp-dogfood-gcp/spark/-00000-of-00001",
                          "stagingLocation": "gs://opta-gcp-dogfood-gcp/staging1",
                          "region": "us-east1",
                          "tempLocation": "gs://opta-gcp-dogfood-gcp/staging1"},
        py_interpreter="python3",
        dataflow_config=DataflowConfiguration(job_name="test_dataflow", project_id="dogfood-gcp-dataplane",
                                              location="us-east1",
                                              wait_until_finished=False),
        container_image=image_spec
    )


if __name__ == '__main__':
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "/Users/kevin/git/flytekit/somekey.json"
    os.environ['AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT'] = "google-cloud-platform://"
    os.environ['FLYTECTL_CONFIG'] = '/Users/kevin/.flyte/config-dogfood-gcp.yaml'
    runner = CliRunner()
    result = runner.invoke(pyflyte.main,
                           ["run", "--remote", "/Users/kevin/git/flytekit/flyte-example/airflow_example/dataflow_java.py",
                            "wf"])
    print(result.output)
Base image:
Copy code
ARG PYTHON_VERSION
FROM python:${PYTHON_VERSION}-slim-buster

MAINTAINER Flyte Team <users@flyte.org>
LABEL org.opencontainers.image.source=https://github.com/flyteorg/flytekit

WORKDIR /root
ENV PYTHONPATH /root

ARG VERSION
ARG DOCKER_IMAGE

RUN apt-get update && apt-get install build-essential -y

# Pod tasks should be exposed in the default image
RUN pip install -U flytekit==$VERSION \
	flytekitplugins-pod==$VERSION \
	flytekitplugins-deck-standard==$VERSION \
	scikit-learn

RUN apt-get update && \
    apt-get install -y curl gnupg && \
    echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
    curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg  add - && \
    apt-get update -y && \
    apt-get install google-cloud-sdk -y

RUN mkdir -p /usr/share/man/man1 /usr/share/man/man2 && \
    apt-get update &&\
    apt-get install -y --no-install-recommends openjdk-11-jre && \
    apt-get install ca-certificates-java -y && \
    apt-get clean && \
    update-ca-certificates -f;
ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64/


RUN useradd -u 1000 flytekit
RUN chown flytekit: /root
# Some packages will create config file under /home by default, so we need to make sure it's writable
RUN chown flytekit: /home
USER flytekit

ENV FLYTE_INTERNAL_IMAGE "$DOCKER_IMAGE"
image Tracking Issue flyteorg/flyte#3936 Follow-up issue NA flyteorg/flytekit Codecov: 82.19% of diff hit (target 85.81%) ✅ 29 other checks have passed 29/30 successful checks