Skip to content

Commit

Permalink
Add support for Containerfile in addition to Dockerfile
Browse files Browse the repository at this point in the history
Also fix autocomplete
Adjust unit tests

Signed-off-by: Ygal Blum <[email protected]>
  • Loading branch information
ygalblum authored and eranco74 committed May 11, 2023
1 parent 46c1562 commit 26e65ac
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
5 changes: 4 additions & 1 deletion skipper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def build(ctx, images_to_build, container_context, cache):
utils.logger.info('Building image: %s', image)

if not os.path.exists(dockerfile):
utils.logger.warning('Dockerfile %s does not exist! Skipping...', dockerfile)
utils.logger.warning('File %s does not exist! Skipping...', dockerfile)
continue

fqdn_image = image + ':' + tag
Expand Down Expand Up @@ -415,6 +415,9 @@ def runner_run(command):
utils.logger.info("No build container tag was provided")

docker_file = utils.image_to_dockerfile(image)
if docker_file is None:
sys.exit(f'Could not find any dockerfile for {image}')

utils.logger.info("Building image using docker file: %s", docker_file)
if container_context is not None:
build_context = container_context
Expand Down
12 changes: 6 additions & 6 deletions skipper/data/skipper-complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ __contains_word () {


_get_images_from_dockerfiles() {
local dockerfiles=$( ls Dockerfile.* )
for dockerfile in $dockerfiles; do
echo ${dockerfile##*.};
local dockerfiles=$( find * -type f -regex "\(Docker\|Container\)file\..*" )
for dockerfile in $dockerfiles; do
echo ${dockerfile##*.};
done
}

Expand Down Expand Up @@ -156,7 +156,7 @@ _skipper_completion() {
if [[ $cur == -* ]]; then
COMPREPLY=( $(compgen -W "${OPTS[RUN]}" -- $cur) )
else
if [[ $prev == -f ]]; then
if [[ $prev == -f ]]; then
COMPREPLY=( $(compgen -f -X '!*[mM]akefile' -- $cur) )
else
makefile=$(_get_makefile ${COMP_WORDS[*]})
Expand All @@ -172,12 +172,12 @@ _skipper_completion() {
command sed -nf <(_make_target_extract_script $mode "$cur") ) )
fi
fi

else
if [[ $cur == -* ]]; then
COMPREPLY=( $(compgen -W "${OPTS[GLOBAL]}" -- $cur) )
else
COMPREPLY=( $(compgen -W "$COMMANDS" -- $cur) )
COMPREPLY=( $(compgen -W "$COMMANDS" -- $cur) )
fi
fi

Expand Down
8 changes: 6 additions & 2 deletions skipper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,15 @@ def generate_fqdn_image(registry, namespace, image, tag='latest'):


def image_to_dockerfile(image):
return 'Dockerfile.' + image
prefixes = ['Docker', 'Container']
docker_files = [file for file in [prefix + "file." + image for prefix in prefixes] if os.path.exists(file)]
if len(docker_files) > 1:
logger.warning('Found more than one dockerfile for %s. Using %s', image, docker_files[0])
return docker_files[0] if len(docker_files) else None


def dockerfile_to_image(dockerfile):
return dockerfile.replace('Dockerfile.', '')
return dockerfile.split('.', 1)[-1]


def is_tool(name):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,7 @@ def test_run_non_interactive(self, skipper_runner_run_mock):
env_file=())

@mock.patch('subprocess.check_output', mock.MagicMock(autospec=True, return_value=''))
@mock.patch('skipper.utils.image_to_dockerfile', mock.MagicMock(autospec=True, side_effect=lambda x: 'Dockerfile.'+x))
@mock.patch('skipper.runner.run', autospec=True, return_value=0)
def test_run_without_build_container_tag(self, skipper_runner_run_mock):
global_params = self.global_params[:-2]
Expand All @@ -1414,6 +1415,7 @@ def test_run_without_build_container_tag(self, skipper_runner_run_mock):
skipper_runner_run_mock.assert_has_calls(expected_commands)

@mock.patch('subprocess.check_output', mock.MagicMock(autospec=True, return_value=''))
@mock.patch('skipper.utils.image_to_dockerfile', mock.MagicMock(autospec=True, side_effect=lambda x: 'Dockerfile.'+x))
@mock.patch('skipper.runner.run', autospec=True, return_value=0)
def test_run_without_build_container_tag_cached(self, skipper_runner_run_mock):
global_params = self.global_params[:-2]
Expand Down Expand Up @@ -1796,6 +1798,7 @@ def test_make_with_defaults_from_config_file(self, skipper_runner_run_mock):
env_file=())

@mock.patch('subprocess.check_output', mock.MagicMock(autospec=True, return_value=''))
@mock.patch('skipper.utils.image_to_dockerfile', mock.MagicMock(autospec=True, side_effect=lambda x: 'Dockerfile.'+x))
@mock.patch('skipper.runner.run', autospec=True, return_value=0)
def test_make_without_build_container_tag(self, skipper_runner_run_mock):
global_params = self.global_params[:-2]
Expand Down

0 comments on commit 26e65ac

Please sign in to comment.