Skip to content

Commit

Permalink
Update podman_image to specify CPU arch when pulling image (#578)
Browse files Browse the repository at this point in the history
* Add test to sprcify CPU arch when pulling image

Signed-off-by: nishipy <goodisonev4@gmail.com>

* Update to specify CPU arch when pulling image

Signed-off-by: nishipy <goodisonev4@gmail.com>

* Add document for specifying arch

Signed-off-by: nishipy <goodisonev4@gmail.com>

* Fix for idempotency

Signed-off-by: nishipy <goodisonev4@gmail.com>

* Update plugins/modules/podman_image.py

Signed-off-by: nishipy <goodisonev4@gmail.com>

---------

Signed-off-by: nishipy <goodisonev4@gmail.com>
  • Loading branch information
nishipy authored Apr 15, 2023
1 parent e8c2703 commit ab64d5f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
25 changes: 22 additions & 3 deletions plugins/modules/podman_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
description:
- Build, pull, or push images using Podman.
options:
arch:
description:
- CPU architecutre for the container image
type: str
name:
description:
- Name of the image to pull, push, or delete. It may contain a tag using the format C(image:tag).
Expand Down Expand Up @@ -267,6 +271,11 @@
- name: nginx
tag: 3
dest: docker.io/acme
- name: Pull an image for a specific CPU architecture
containers.podman.podman_image:
name: nginx
arch: amd64
"""

RETURN = r"""
Expand Down Expand Up @@ -422,6 +431,7 @@ def __init__(self, module, results):
self.ca_cert_dir = self.module.params.get('ca_cert_dir')
self.build = self.module.params.get('build')
self.push_args = self.module.params.get('push_args')
self.arch = self.module.params.get('arch')

repo, repo_tag = parse_repository_tag(self.name)
if repo_tag:
Expand Down Expand Up @@ -526,9 +536,14 @@ def find_image(self, image_name=None):
rc, images, err = self._run(args, ignore_errors=True)
images = json.loads(images)
if len(images) > 0:
return images
else:
return None
inspect_json = self.inspect_image(image_name)
if self._is_target_arch(inspect_json, self.arch) or not self.arch:
return images

return None

def _is_target_arch(self, inspect_json=None, arch=None):
return arch and inspect_json[0]['Architecture'] == arch

def find_image_id(self, image_id=None):
if image_id is None:
Expand Down Expand Up @@ -560,6 +575,9 @@ def pull_image(self, image_name=None):

args = ['pull', image_name, '-q']

if self.arch:
args.extend(['--arch', self.arch])

if self.auth_file:
args.extend(['--authfile', self.auth_file])

Expand Down Expand Up @@ -762,6 +780,7 @@ def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(type='str', required=True),
arch=dict(type='str'),
tag=dict(type='str', default='latest'),
pull=dict(type='bool', default=True),
push=dict(type='bool', default=False),
Expand Down
42 changes: 41 additions & 1 deletion tests/integration/targets/podman_image/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
- name: Pull image with SHA256 tag
containers.podman.podman_image:
executable: "{{ test_executable | default('podman') }}"
name: "quay.io/coreos/etcd@{{ sha_image_info.images.0.Digest }}" #quay.io/coreos/coreos-installer:latest
name: "quay.io/coreos/etcd@{{ sha_image_info.images.0.Digest }}" #quay.io/coreos/coreos-installer:latest
state: present

- name: Create a build directory with a subdirectory
Expand Down Expand Up @@ -277,13 +277,53 @@
- "'Failed to find image bad_image' in bad_push.msg"
- "'image pull set to False' in bad_push.msg"

- name: Pull an image for a specific CPU architecture
containers.podman.podman_image:
executable: "{{ test_executable | default('podman') }}"
name: docker.io/library/ubuntu
arch: amd64
register: pull_arch1

- name: Pull the same image for the same CPU architecture
containers.podman.podman_image:
executable: "{{ test_executable | default('podman') }}"
name: docker.io/library/ubuntu
arch: amd64
register: pull_arch2

- name: Pull the same image but for another CPU architecture
containers.podman.podman_image:
executable: "{{ test_executable | default('podman') }}"
name: docker.io/library/ubuntu
arch: arm
register: pull_arch3

- name: Ensure the result of pulling image for another CPU architecture
assert:
that:
- "pull_arch2 is not changed"
- "pull_arch3 is changed"

- name: Get the image info
containers.podman.podman_image_info:
executable: "{{ test_executable | default('podman') }}"
name: docker.io/library/ubuntu
register: imageinfo_arch

- name: Ensure the CPU architecture of the image is as expected
assert:
that:
- item.Architecture == "arm"
loop: "{{ imageinfo_arch.images }}"

always:
- name: Cleanup images
containers.podman.podman_image:
executable: "{{ test_executable | default('podman') }}"
name: "{{ item }}"
state: absent
loop:
- docker.io/library/ubuntu
- quay.io/coreos/alpine-sh
- quay.io/coreos/etcd:v3.3.11
- localhost/testimage
Expand Down

0 comments on commit ab64d5f

Please sign in to comment.