Skip to content

Commit

Permalink
Only download kubeadm images where needed (kubernetes-sigs#10899)
Browse files Browse the repository at this point in the history
* Refactor of kubeadm images listing

Instead of setting multiples facts, we directly create the dict we need from
kubeadm output.

* Remove useless 'default' filters in roles/download

* Only download kubeadm images where needed
  • Loading branch information
VannTen authored Feb 8, 2024
1 parent ad565ad commit 4b0a134
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
1 change: 1 addition & 0 deletions galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tags:
repository: https://github.com/kubernetes-sigs/kubespray
dependencies:
ansible.utils: '>=2.5.0'
community.general: '>=3.0.0'
build_ignore:
- .github
- '*.tar.gz'
Expand Down
42 changes: 39 additions & 3 deletions roles/download/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
- name: Download | Prepare working directories and variables
import_tasks: prep_download.yml
when:
- not skip_downloads | default(false)
- not skip_downloads
tags:
- download
- upload

- name: Download | Get kubeadm binary and list of required images
include_tasks: prep_kubeadm_images.yml
when:
- not skip_downloads | default(false)
- not skip_downloads
- inventory_hostname in groups['kube_control_plane']
tags:
- download
Expand All @@ -22,8 +22,44 @@
vars:
download: "{{ download_defaults | combine(item.value) }}"
include_file: "download_{% if download.container %}container{% else %}file{% endif %}.yml"
kubeadm_images: "{{ skip_kubeadm_images | ternary({}, _kubeadm_images) }}"
# The trick (converting list of tuples to list of dicts) below come from
# https://docs.ansible.com/ansible/latest/collections/community/general/dict_filter.html#examples
_kubeadm_images: "{{ dict(names | map('regex_replace', '^(.*)', 'kubeadm_\\1') |
zip( repos | zip(_tags, _groups) |
map('zip', keys) | map('map', 'reverse') | map('community.general.dict') |
map('combine', defaults))) |
dict2items | rejectattr('key', 'in', excluded) | items2dict }}"
keys:
- repo
- tag
- groups
images: "{{ kubeadm_images_raw.stdout_lines | map('split', ':') }}"
_tags: "{{ images | map(attribute=1) }}"
repos: "{{ images | map(attribute=0) }}"
names: "{{ repos | map('split', '/') | map(attribute=-1) }}"
_groups: "{{ names | map('extract', images_groups) }}"
defaults:
enabled: true
container: true
excluded:
- kubeadm_coredns
- kubeadm_pause
images_groups:
coredns: []
pause: []
kube-proxy:
- k8s_cluster
etcd:
- etcd
kube-scheduler:
- kube_control_plane
kube-controller-manager:
- kube_control_plane
kube-apiserver:
- kube_control_plane
when:
- not skip_downloads | default(false)
- not skip_downloads
- download.enabled
- item.value.enabled
- (not (item.value.container | default(false))) or (item.value.container and download_container)
Expand Down
33 changes: 3 additions & 30 deletions roles/download/tasks/prep_kubeadm_images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
dest: "{{ kube_config_dir }}/kubeadm-images.yaml"
mode: 0644
when:
- not skip_kubeadm_images | default(false)
- not skip_kubeadm_images

- name: Prep_kubeadm_images | Copy kubeadm binary from download dir to system path
copy:
Expand All @@ -36,36 +36,9 @@
state: file

- name: Prep_kubeadm_images | Generate list of required images
shell: "set -o pipefail && {{ bin_dir }}/kubeadm config images list --config={{ kube_config_dir }}/kubeadm-images.yaml | grep -Ev 'coredns|pause'"
args:
executable: /bin/bash
command: "{{ bin_dir }}/kubeadm config images list --config={{ kube_config_dir }}/kubeadm-images.yaml"
register: kubeadm_images_raw
run_once: true
changed_when: false
when:
- not skip_kubeadm_images | default(false)

- name: Prep_kubeadm_images | Parse list of images
vars:
kubeadm_images_list: "{{ kubeadm_images_raw.stdout_lines }}"
set_fact:
kubeadm_image:
key: "kubeadm_{{ (item | regex_replace('^(?:.*\\/)*', '')).split(':')[0] }}"
value:
enabled: true
container: true
repo: "{{ item | regex_replace('^(.*):.*$', '\\1') }}"
tag: "{{ item | regex_replace('^.*:(.*)$', '\\1') }}"
groups: k8s_cluster
loop: "{{ kubeadm_images_list | flatten(levels=1) }}"
register: kubeadm_images_cooked
run_once: true
when:
- not skip_kubeadm_images | default(false)

- name: Prep_kubeadm_images | Convert list of images to dict for later use
set_fact:
kubeadm_images: "{{ kubeadm_images_cooked.results | map(attribute='ansible_facts.kubeadm_image') | list | items2dict }}"
run_once: true
when:
- not skip_kubeadm_images | default(false)
- not skip_kubeadm_images

0 comments on commit 4b0a134

Please sign in to comment.