Skip to content

Commit

Permalink
Follow FLYTE_PUSH_IMAGE_SPEC in default image builder (#2682)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas J. Fan <thomasjpfan@gmail.com>
  • Loading branch information
thomasjpfan authored Aug 13, 2024
1 parent bc2e000 commit 222ca40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions flytekit/image_spec/default_builder.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import json
import os
import re
import shutil
import subprocess
import sys
import tempfile
import warnings
from pathlib import Path
from string import Template
from subprocess import run
from typing import ClassVar

import click
Expand Down Expand Up @@ -251,7 +252,10 @@ class DefaultImageBuilder(ImageSpecBuilder):
}

def build_image(self, image_spec: ImageSpec) -> str:
return self._build_image(image_spec)
return self._build_image(
image_spec,
push=os.getenv("FLYTE_PUSH_IMAGE_SPEC", "True").lower() in ("true", "1"),
)

def _build_image(self, image_spec: ImageSpec, *, push: bool = True) -> str:
# For testing, set `push=False`` to just build the image locally and not push to
Expand Down Expand Up @@ -285,4 +289,4 @@ def _build_image(self, image_spec: ImageSpec, *, push: bool = True) -> str:

concat_command = " ".join(command)
click.secho(f"Run command: {concat_command} ", fg="blue")
subprocess.run(command, check=True)
run(command, check=True)
21 changes: 21 additions & 0 deletions tests/flytekit/unit/core/image_spec/test_default_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from unittest.mock import patch, Mock

import pytest

Expand Down Expand Up @@ -181,3 +182,23 @@ def test_build(tmp_path):
builder = DefaultImageBuilder()

builder.build_image(image_spec)


@pytest.mark.parametrize("push_image_spec", ["0", "1"])
def test_should_push_env(monkeypatch, push_image_spec):
image_spec = ImageSpec(name="my_flytekit", python_version="3.12", registry="localhost:30000")
monkeypatch.setenv("FLYTE_PUSH_IMAGE_SPEC", push_image_spec)

run_mock = Mock()
monkeypatch.setattr("flytekit.image_spec.default_builder.run", run_mock)

builder = DefaultImageBuilder()
builder.build_image(image_spec)

run_mock.assert_called_once()
call_args = run_mock.call_args.args

if push_image_spec == "0":
assert "--push" not in call_args[0]
else:
assert "--push" in call_args[0]

0 comments on commit 222ca40

Please sign in to comment.