Skip to content

Commit 1fb1721

Browse files
committed
fix(role): drive namespace lookup via kubectl, not kubernetes.core.k8s_info
The k8s_info module requires the optional Python 'kubernetes' package on the controller, which is not installed in the project's CI environment (and is not a documented prerequisite for users either). The rest of this role already drives the cluster via `kubectl ... --output=json` for the same reason. Switch the cozy-system lookup to `kubectl get namespace ... \ --ignore-not-found --output=json` and parse the result with from_json. `--ignore-not-found` returns an empty stdout (rather than a non-zero exit) when the namespace is absent, so the same length-check gate as before keeps the foreign-owner and adopt tasks no-op'd on first install. The unit tests are unchanged because they check task names and when-clause structure, not the lookup module's internals. Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Aleksei Sviridkin <f@lex.la>
1 parent a5307cb commit 1fb1721

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

roles/cozystack/tasks/main.yml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,20 @@
8484
# does not exist or already carries the right metadata. Refuses to
8585
# proceed if the namespace is owned by a *different* helm release —
8686
# that case requires operator intervention, not silent re-adoption.
87+
# Use `kubectl get` rather than `kubernetes.core.k8s_info` so the
88+
# role works on controllers without the optional `kubernetes` Python
89+
# package — the rest of this file already drives the cluster via
90+
# kubectl for the same reason.
8791
- name: Look up cozy-system namespace
88-
kubernetes.core.k8s_info:
89-
kubeconfig: "{{ cozystack_kubeconfig }}"
90-
api_version: v1
91-
kind: Namespace
92-
name: cozy-system
92+
ansible.builtin.command:
93+
cmd: >-
94+
kubectl --kubeconfig {{ cozystack_kubeconfig }}
95+
get namespace cozy-system
96+
--ignore-not-found
97+
--output=json
9398
become: true
94-
register: _cozystack_ns_info
99+
changed_when: false
100+
register: _cozystack_ns_lookup
95101
when: not ansible_check_mode
96102

97103
- name: Refuse to overwrite cozy-system if owned by another helm release
@@ -106,7 +112,7 @@
106112
cozystack_release_name / cozystack_release_namespace with the
107113
existing owner.
108114
vars:
109-
_ns: "{{ (_cozystack_ns_info.resources | default([]) | first) | default({}) }}"
115+
_ns: "{{ (_cozystack_ns_lookup.stdout | default('') | length > 0) | ternary(_cozystack_ns_lookup.stdout | from_json, {}) }}"
110116
_labels: "{{ _ns.metadata.labels | default({}) }}"
111117
_annotations: "{{ _ns.metadata.annotations | default({}) }}"
112118
_release_name: "{{ _annotations.get('meta.helm.sh/release-name', '') }}"
@@ -116,7 +122,7 @@
116122
# prior install left annotations behind without the label.
117123
when:
118124
- not ansible_check_mode
119-
- _cozystack_ns_info.resources | default([]) | length > 0
125+
- _cozystack_ns_lookup.stdout | default('') | length > 0
120126
- >-
121127
(_labels.get('app.kubernetes.io/managed-by', '') == 'Helm'
122128
and (_release_name != cozystack_release_name
@@ -145,12 +151,12 @@
145151
register: _cozystack_ns_adopt
146152
changed_when: "'patched' in _cozystack_ns_adopt.stdout and '(no change)' not in _cozystack_ns_adopt.stdout"
147153
vars:
148-
_ns: "{{ (_cozystack_ns_info.resources | default([]) | first) | default({}) }}"
154+
_ns: "{{ (_cozystack_ns_lookup.stdout | default('') | length > 0) | ternary(_cozystack_ns_lookup.stdout | from_json, {}) }}"
149155
_labels: "{{ _ns.metadata.labels | default({}) }}"
150156
_annotations: "{{ _ns.metadata.annotations | default({}) }}"
151157
when:
152158
- not ansible_check_mode
153-
- _cozystack_ns_info.resources | default([]) | length > 0
159+
- _cozystack_ns_lookup.stdout | default('') | length > 0
154160
- _labels.get('app.kubernetes.io/managed-by', '') != 'Helm'
155161
or _annotations.get('meta.helm.sh/release-name', '') != cozystack_release_name
156162
or _annotations.get('meta.helm.sh/release-namespace', '') != cozystack_release_namespace

0 commit comments

Comments
 (0)