refactor(longhorn): update disk tasks for namespace support and improve retry parameters

- Add longhorn_namespace to kubectl and k8s_json_patch operations
- Change patch format from plural 'patches' to singular 'patch'
- Adjust retry logic: reduce retries from 40 to 24, delay from 15 to 5
- Include KUBECONFIG and PATH environment variables for k8s operations
- Comment out python kubernetes library installation block with explanation
This commit is contained in:
a.kazantsev 2026-07-21 10:47:50 +03:00
parent 7327676b13
commit 5b728bf3e0

View file

@ -7,12 +7,12 @@
- name: Disks | Wait for worker nodes to appear in Longhorn
ansible.builtin.command:
argv: [kubectl, get, node.longhorn.io, "{{ item }}"]
argv: [kubectl, get, node.longhorn.io, "{{ item }}", -n, "{{ longhorn_namespace }}"]
loop: "{{ groups['workers'] }}"
register: _lh_node_wait
until: _lh_node_wait.rc == 0
retries: 40
delay: 15
retries: 24
delay: 5
changed_when: false
failed_when: _lh_node_wait.rc != 0 and _lh_node_wait.attempts | default(0) >= 24
environment:
@ -21,6 +21,18 @@
# ── Step 2: register dedicated disks via k8s_json_patch ──
# - name: Disks | Install python kubernetes library for kubernetes.core modules
# ansible.builtin.pip:
# name:
# - kubernetes
# # Rocky Linux's rpm-packaged python3-oauthlib (3.1.1) strips SIGNATURE_RSA
# # (crypto hardening), which breaks requests-oauthlib's import used by the
# # kubernetes library. A bare "oauthlib" pip requirement is satisfied by
# # that already-installed 3.1.1 and no-ops, so the version floor is
# # required to force pip to actually install a newer, unpatched build.
# - "oauthlib>=3.2.0"
# executable: pip3
- name: Disks | Build flat worker-disk list
ansible.builtin.set_fact:
_lh_worker_disks: "{{ _lh_worker_disks | default([]) + [hostvars[item].longhorn_disks | default([]) | map('combine', {'node': item}) | list] | flatten }}"
@ -30,8 +42,9 @@
kubernetes.core.k8s_json_patch:
api_version: longhorn.io/v1beta1
kind: Node
namespace: "{{ longhorn_namespace }}"
name: "{{ item.node }}"
patches:
patch:
- op: add
path: "/spec/disks/{{ item.mountpoint | basename | replace('/', '~1') }}"
value:
@ -44,6 +57,9 @@
label: "{{ item.node }}/{{ item.mountpoint | basename }}"
register: _disk_patch
changed_when: false
environment:
KUBECONFIG: "{{ longhorn_kubeconfig }}"
PATH: /usr/local/bin:/usr/bin:/bin
# ── Step 3: remove auto-created default disk at /var/lib/longhorn ─────────────
@ -51,6 +67,7 @@
kubernetes.core.k8s_info:
api_version: longhorn.io/v1beta1
kind: Node
namespace: "{{ longhorn_namespace }}"
name: "{{ item }}"
loop: "{{ groups['workers'] }}"
register: _lh_node_info
@ -63,8 +80,9 @@
kubernetes.core.k8s_json_patch:
api_version: longhorn.io/v1beta1
kind: Node
namespace: "{{ longhorn_namespace }}"
name: "{{ item.item }}"
patches:
patch:
- op: add
path: "/spec/disks/{{ item.resources[0].spec.disks | dict2items | selectattr('key', 'match', 'default-disk-.*') | first | default({key: ''}) | key | replace('/', '~1') }}"
value:
@ -77,13 +95,17 @@
- item.resources[0].spec.disks | default({}) | dict2items | selectattr('key', 'match', 'default-disk-.*') | selectattr('value.allowScheduling', 'equalto', true) | list | length > 0
register: _disable_default
changed_when: false
environment:
KUBECONFIG: "{{ longhorn_kubeconfig }}"
PATH: /usr/local/bin:/usr/bin:/bin
- name: Disks | Remove auto-created default disk
kubernetes.core.k8s_json_patch:
api_version: longhorn.io/v1beta1
kind: Node
namespace: "{{ longhorn_namespace }}"
name: "{{ item.item }}"
patches:
patch:
- op: remove
path: "/spec/disks/{{ item.resources[0].spec.disks | dict2items | selectattr('key', 'match', 'default-disk-.*') | map(attribute='key') | first | default('') | replace('/', '~1') }}"
loop: "{{ _lh_node_info.results }}"
@ -92,6 +114,9 @@
- item.resources[0].spec.disks | default({}) | dict2items | selectattr('key', 'match', 'default-disk-.*') | list | length > 0
register: _remove_default
changed_when: false
environment:
KUBECONFIG: "{{ longhorn_kubeconfig }}"
PATH: /usr/local/bin:/usr/bin:/bin
# ── Step 4: remove auto-created default disk from control_plane nodes ─────────
# Control plane nodes have no dedicated storage disks; Longhorn must not
@ -101,6 +126,7 @@
kubernetes.core.k8s_info:
api_version: longhorn.io/v1beta1
kind: Node
namespace: "{{ longhorn_namespace }}"
name: "{{ item }}"
loop: "{{ groups['control_plane'] }}"
register: _lh_cp_node_info
@ -114,8 +140,9 @@
kubernetes.core.k8s_json_patch:
api_version: longhorn.io/v1beta1
kind: Node
namespace: "{{ longhorn_namespace }}"
name: "{{ item.item }}"
patches:
patch:
- op: add
path: "/spec/disks/{{ item.resources[0].spec.disks | dict2items | selectattr('key', 'match', 'default-disk-.*') | first | default({key: ''}) | key | replace('/', '~1') }}"
value:
@ -128,13 +155,17 @@
- item.resources[0].spec.disks | default({}) | dict2items | selectattr('key', 'match', 'default-disk-.*') | selectattr('value.allowScheduling', 'equalto', true) | list | length > 0
register: _disable_cp_default
changed_when: false
environment:
KUBECONFIG: "{{ longhorn_kubeconfig }}"
PATH: /usr/local/bin:/usr/bin:/bin
- name: Disks | Remove auto-created default disk from control_plane nodes
kubernetes.core.k8s_json_patch:
api_version: longhorn.io/v1beta1
kind: Node
namespace: "{{ longhorn_namespace }}"
name: "{{ item.item }}"
patches:
patch:
- op: remove
path: "/spec/disks/{{ item.resources[0].spec.disks | dict2items | selectattr('key', 'match', 'default-disk-.*') | map(attribute='key') | first | default('') | replace('/', '~1') }}"
loop: "{{ _lh_cp_node_info.results }}"
@ -143,3 +174,6 @@
- item.resources[0].spec.disks | default({}) | dict2items | selectattr('key', 'match', 'default-disk-.*') | list | length > 0
register: _remove_cp_default
changed_when: false
environment:
KUBECONFIG: "{{ longhorn_kubeconfig }}"
PATH: /usr/local/bin:/usr/bin:/bin