Skip to content

Commit 5fdbfd7

Browse files
vakwetuclaude
authored andcommitted
[multiple] Refactor inline Python/shell patterns to cleaner alternatives
Replace python3 -c JSON parsing in wait_for_cluster.yml with jq expressions. Move the inline python3 heredoc for OSDPD renaming in execute_step.yml to a standalone script (roles/kustomize_deploy/files/uniquify_osdpd.py) invoked via ansible.builtin.script. Replace the shell+openssl+python fingerprint loop in update-central-ca-bundle.yaml with a kubernetes.core.k8s_info until task that checks for the leaf cert PEM as a substring of the combined bundle using Jinja2. Signed-off-by: Ade Lee <alee@redhat.com> Co-Authored-By: Claude <noreply@anthropic.com> Made-with: Cursor
1 parent fc0aa2c commit 5fdbfd7

4 files changed

Lines changed: 65 additions & 78 deletions

File tree

hooks/playbooks/skmo/update-central-ca-bundle.yaml

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -121,42 +121,19 @@
121121
# every cert in combined-ca-bundle, retrying until it appears.
122122
# -------------------------------------------------------------------------
123123
- name: Wait for leaf region CA to appear in combined-ca-bundle
124-
ansible.builtin.shell: |
125-
set -euo pipefail
126-
TMPDIR=$(mktemp -d)
127-
trap "rm -rf $TMPDIR" EXIT
128-
129-
echo "{{ _leaf_certs.results[0].resources[0].data['tls.crt'] }}" | \
130-
base64 -d > "$TMPDIR/leaf-ca.crt"
131-
FINGERPRINT=$(openssl x509 -noout -fingerprint -in "$TMPDIR/leaf-ca.crt" \
132-
| cut -d= -f2)
133-
134-
oc get secret combined-ca-bundle \
135-
-n {{ central_namespace }} \
136-
-o jsonpath='{.data.tls-ca-bundle\.pem}' \
137-
| base64 -d > "$TMPDIR/bundle.pem"
138-
139-
python3 - "$FINGERPRINT" "$TMPDIR/bundle.pem" <<'PYEOF'
140-
import sys, subprocess, re
141-
target, bundle_file = sys.argv[1], sys.argv[2]
142-
bundle = open(bundle_file).read()
143-
certs = re.findall(
144-
r'-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----',
145-
bundle, re.DOTALL
124+
kubernetes.core.k8s_info:
125+
api_version: v1
126+
kind: Secret
127+
namespace: "{{ central_namespace }}"
128+
name: combined-ca-bundle
129+
register: _combined_bundle
130+
until: >-
131+
(_combined_bundle.resources | length > 0) and
132+
(
133+
_leaf_certs.results[0].resources[0].data['tls.crt'] | b64decode
134+
in
135+
(_combined_bundle.resources | first).data['tls-ca-bundle.pem'] | b64decode
146136
)
147-
for cert in certs:
148-
r = subprocess.run(
149-
['openssl', 'x509', '-noout', '-fingerprint'],
150-
input=cert.encode(), capture_output=True
151-
)
152-
if target in r.stdout.decode():
153-
sys.exit(0)
154-
sys.exit(1)
155-
PYEOF
156-
args:
157-
executable: /bin/bash
158-
register: _ca_reconciled
159-
until: _ca_reconciled.rc == 0
160137
retries: 30
161138
delay: 10
162139
changed_when: false
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
"""Append a run suffix to OpenStackDataPlaneDeployment resource names.
3+
4+
Usage: uniquify_osdpd.py <manifest_path> <suffix>
5+
6+
Reads the multi-document YAML file at <manifest_path>, appends <suffix>
7+
to the metadata.name of every OpenStackDataPlaneDeployment resource that
8+
does not already end with that suffix, and writes the result back in place.
9+
Prints a "Renamed: <old> -> <new>" line for each renamed resource so that
10+
the calling Ansible task can use changed_when on stdout.
11+
"""
12+
import sys
13+
import yaml
14+
15+
path, suffix = sys.argv[1], sys.argv[2]
16+
17+
with open(path) as f:
18+
docs = [d for d in yaml.safe_load_all(f) if d is not None]
19+
20+
for doc in docs:
21+
if doc.get("kind") == "OpenStackDataPlaneDeployment":
22+
name = doc["metadata"]["name"]
23+
if not name.endswith("-" + suffix):
24+
doc["metadata"]["name"] = name + "-" + suffix
25+
print("Renamed: {} -> {}".format(name, doc["metadata"]["name"]))
26+
27+
with open(path, "w") as f:
28+
yaml.dump_all(docs, f, default_flow_style=False)

roles/kustomize_deploy/tasks/execute_step.yml

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -254,26 +254,14 @@
254254

255255
- name: "Uniquify OpenStackDataPlaneDeployment names in {{ stage.path }}"
256256
when: _cifmw_kustomize_deploy_run_suffix | default('') | string | length > 0
257-
changed_when: "'Renamed:' in _rename_osdpd.stdout"
257+
ansible.builtin.script:
258+
executable: python3
259+
cmd: >-
260+
{{ role_path }}/files/uniquify_osdpd.py
261+
{{ _output | quote }}
262+
{{ _cifmw_kustomize_deploy_run_suffix | string | quote }}
258263
register: _rename_osdpd
259-
ansible.builtin.shell:
260-
executable: /bin/bash
261-
cmd: |
262-
python3 - << 'PYEOF'
263-
import yaml, sys
264-
path = "{{ _output }}"
265-
suffix = "{{ _cifmw_kustomize_deploy_run_suffix }}"
266-
with open(path) as f:
267-
docs = [d for d in yaml.safe_load_all(f) if d is not None]
268-
for doc in docs:
269-
if doc.get('kind') == 'OpenStackDataPlaneDeployment':
270-
name = doc['metadata']['name']
271-
if not name.endswith('-' + suffix):
272-
doc['metadata']['name'] = name + '-' + suffix
273-
print('Renamed: ' + name + ' -> ' + doc['metadata']['name'])
274-
with open(path, 'w') as f:
275-
yaml.dump_all(docs, f, default_flow_style=False)
276-
PYEOF
264+
changed_when: "'Renamed:' in _rename_osdpd.stdout"
277265

278266
- name: "Store kustomized content in artifacts for {{ stage.path }}"
279267
ansible.builtin.copy:

roles/openshift_adm/tasks/wait_for_cluster.yml

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,15 @@
7070
set -eo pipefail
7171
MCP_JSON=$(oc get mcp -o json)
7272
73-
UPDATING=$(echo "$MCP_JSON" | \
74-
python3 -c "
75-
import json, sys
76-
data = json.load(sys.stdin)
77-
updating = [
78-
i['metadata']['name'] for i in data['items']
79-
if next((c['status'] for c in i['status'].get('conditions', [])
80-
if c['type'] == 'Updating'), 'False') == 'True'
81-
]
82-
print('\n'.join(updating))
83-
")
73+
UPDATING=$(echo "$MCP_JSON" | jq -r '
74+
.items[] |
75+
select(
76+
.status.conditions // [] |
77+
map(select(.type == "Updating" and .status == "True")) |
78+
length > 0
79+
) |
80+
.metadata.name
81+
')
8482
8583
if [ -z "$UPDATING" ]; then
8684
echo "All MCPs are up to date."
@@ -89,19 +87,15 @@
8987
9088
# At least one MCP is still Updating. Check for the stuck-uncordon case:
9189
# updatedMachineCount == machineCount but readyMachineCount == 0.
92-
STUCK=$(echo "$MCP_JSON" | \
93-
python3 -c "
94-
import json, sys
95-
data = json.load(sys.stdin)
96-
stuck = [
97-
i['metadata']['name'] for i in data['items']
98-
if (i['status'].get('updatedMachineCount', 0) ==
99-
i['status'].get('machineCount', 0) and
100-
i['status'].get('readyMachineCount', 0) == 0 and
101-
i['status'].get('machineCount', 0) > 0)
102-
]
103-
print('\n'.join(stuck))
104-
")
90+
STUCK=$(echo "$MCP_JSON" | jq -r '
91+
.items[] |
92+
select(
93+
.status.updatedMachineCount == .status.machineCount and
94+
.status.readyMachineCount == 0 and
95+
.status.machineCount > 0
96+
) |
97+
.metadata.name
98+
')
10599
106100
if [ -n "$STUCK" ]; then
107101
echo "Stuck MCPs detected: $STUCK -- uncordoning all nodes to break deadlock."

0 commit comments

Comments
 (0)