From ab64d5f953e8dc8b2ca4cb97bc1228d6ff58b80d Mon Sep 17 00:00:00 2001 From: nishipy <41185206+nishipy@users.noreply.github.com> Date: Sat, 15 Apr 2023 19:49:16 +0900 Subject: [PATCH] Update podman_image to specify CPU arch when pulling image (#578) * Add test to sprcify CPU arch when pulling image Signed-off-by: nishipy * Update to specify CPU arch when pulling image Signed-off-by: nishipy * Add document for specifying arch Signed-off-by: nishipy * Fix for idempotency Signed-off-by: nishipy * Update plugins/modules/podman_image.py Signed-off-by: nishipy --------- Signed-off-by: nishipy --- plugins/modules/podman_image.py | 25 +++++++++-- .../targets/podman_image/tasks/main.yml | 42 ++++++++++++++++++- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/plugins/modules/podman_image.py b/plugins/modules/podman_image.py index b7a9f10..206bf2d 100644 --- a/plugins/modules/podman_image.py +++ b/plugins/modules/podman_image.py @@ -15,6 +15,10 @@ DOCUMENTATION = r''' 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). @@ -267,6 +271,11 @@ EXAMPLES = r""" - 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""" @@ -422,6 +431,7 @@ class PodmanImageManager(object): 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: @@ -526,9 +536,14 @@ class PodmanImageManager(object): 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: @@ -560,6 +575,9 @@ class PodmanImageManager(object): args = ['pull', image_name, '-q'] + if self.arch: + args.extend(['--arch', self.arch]) + if self.auth_file: args.extend(['--authfile', self.auth_file]) @@ -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), diff --git a/tests/integration/targets/podman_image/tasks/main.yml b/tests/integration/targets/podman_image/tasks/main.yml index 7a72bfb..645d0a0 100644 --- a/tests/integration/targets/podman_image/tasks/main.yml +++ b/tests/integration/targets/podman_image/tasks/main.yml @@ -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 @@ -277,6 +277,45 @@ - "'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: @@ -284,6 +323,7 @@ name: "{{ item }}" state: absent loop: + - docker.io/library/ubuntu - quay.io/coreos/alpine-sh - quay.io/coreos/etcd:v3.3.11 - localhost/testimage