Skip to content

Commit

Permalink
docker_image: allow to specify build platform (#54)
Browse files Browse the repository at this point in the history
* Allow to specify build platform.

* Add basic tests.
  • Loading branch information
felixfontein authored Dec 25, 2020
1 parent 5fa53e2 commit d40c240
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/54-docker_image-build-platform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "docker_image - support ``platform`` when building images (https://github.com/ansible-collections/community.docker/issues/22, https://github.com/ansible-collections/community.docker/pull/54)."
13 changes: 13 additions & 0 deletions plugins/modules/docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
- When building an image specifies an intermediate build stage by
name as a final stage for the resulting image.
type: str
platform:
description:
- Platform in the format C(os[/arch[/variant]]).
type: str
version_added: 1.1.0
archive_path:
description:
- Use with state C(present) to archive an image to a .tar file.
Expand Down Expand Up @@ -365,6 +370,7 @@ def __init__(self, client, results):
self.http_timeout = build.get('http_timeout')
self.push = parameters.get('push')
self.buildargs = build.get('args')
self.build_platform = build.get('platform')
self.use_config_proxy = build.get('use_config_proxy')

# If name contains a tag, it takes precedence over tag parameter.
Expand Down Expand Up @@ -634,6 +640,8 @@ def build_image(self):
params['buildargs'] = {}
if self.target:
params['target'] = self.target
if self.build_platform is not None:
params['platform'] = self.build_platform

build_output = []
for line in self.client.build(**params):
Expand Down Expand Up @@ -702,6 +710,7 @@ def main():
use_config_proxy=dict(type='bool'),
target=dict(type='str'),
etc_hosts=dict(type='dict'),
platform=dict(type='str'),
)),
archive_path=dict(type='path'),
force_source=dict(type='bool', default=False),
Expand Down Expand Up @@ -736,12 +745,16 @@ def detect_use_config_proxy(client):
def detect_etc_hosts(client):
return client.module.params['build'] and bool(client.module.params['build'].get('etc_hosts'))

def detect_platform(client):
return client.module.params['build'] and client.module.params['build'].get('platform') is not None

option_minimal_versions = dict()
option_minimal_versions["build.cache_from"] = dict(docker_py_version='2.1.0', docker_api_version='1.25', detect_usage=detect_build_cache_from)
option_minimal_versions["build.network"] = dict(docker_py_version='2.4.0', docker_api_version='1.25', detect_usage=detect_build_network)
option_minimal_versions["build.target"] = dict(docker_py_version='2.4.0', detect_usage=detect_build_target)
option_minimal_versions["build.use_config_proxy"] = dict(docker_py_version='3.7.0', detect_usage=detect_use_config_proxy)
option_minimal_versions["build.etc_hosts"] = dict(docker_py_version='2.6.0', docker_api_version='1.27', detect_usage=detect_etc_hosts)
option_minimal_versions["build.platform"] = dict(docker_py_version='3.0.0', docker_api_version='1.32', detect_usage=detect_platform)

client = AnsibleDockerClient(
argument_spec=argument_spec,
Expand Down
62 changes: 57 additions & 5 deletions tests/integration/targets/docker_image/tasks/tests/options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
pull: no
source: build
register: buildargs_1
ignore_errors: yes

- name: buildargs (idempotency)
docker_image:
Expand All @@ -43,6 +44,7 @@
pull: no
source: build
register: buildargs_2
ignore_errors: yes

- name: cleanup
docker_image:
Expand All @@ -53,7 +55,7 @@
- assert:
that:
- buildargs_1 is changed
- buildargs_2 is not changed
- buildargs_2 is not failed and buildargs_2 is not changed
when: docker_py_version is version('1.6.0', '>=')

- assert:
Expand All @@ -63,7 +65,7 @@
when: docker_py_version is version('1.6.0', '<')

####################################################################
## container_limits ################################################
## build.container_limits ##########################################
####################################################################

- name: container_limits (Failed due to min memory limit)
Expand Down Expand Up @@ -108,7 +110,7 @@
- "container_limits_1 is changed or container_limits_2 is changed and not (container_limits_1 is changed and container_limits_2 is changed)"

####################################################################
## dockerfile ######################################################
## build.dockerfile ################################################
####################################################################

- name: dockerfile
Expand All @@ -133,6 +135,56 @@
- "('FROM ' ~ docker_test_image_alpine) in dockerfile_1.stdout"
- dockerfile_1['image']['Config']['WorkingDir'] == '/newdata'

####################################################################
## build.platform ##################################################
####################################################################

- name: cleanup
docker_image:
name: "{{ iname }}"
state: absent
force_absent: yes

- name: build.platform
docker_image:
name: "{{ iname }}"
build:
path: "{{ output_dir }}/files"
platform: linux
pull: no
source: build
register: platform_1
ignore_errors: yes

- name: build.platform (idempotency)
docker_image:
name: "{{ iname }}"
build:
path: "{{ output_dir }}/files"
platform: linux
pull: no
source: build
register: platform_2
ignore_errors: yes

- name: cleanup
docker_image:
name: "{{ iname }}"
state: absent
force_absent: yes

- assert:
that:
- platform_1 is changed
- platform_2 is not failed and platform_2 is not changed
when: docker_py_version is version('3.0.0', '>=')

- assert:
that:
- platform_1 is failed
- platform_2 is failed
when: docker_py_version is version('3.0.0', '<')

####################################################################
## force ###########################################################
####################################################################
Expand Down Expand Up @@ -216,7 +268,7 @@
- archive_image['image']['Id'] == load_image['image']['Id']

####################################################################
## path ############################################################
## build.path ######################################################
####################################################################

- name: Build image
Expand Down Expand Up @@ -249,7 +301,7 @@
- path_2 is not changed

####################################################################
## target ##########################################################
## build.target ####################################################
####################################################################

- name: Build multi-stage image
Expand Down

0 comments on commit d40c240

Please sign in to comment.