Skip to content

Commit 8548bec

Browse files
Use tempfile for certs, add auth type check, skip bootstrap if oc exists, add 900s timeout per extract attempt
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 6c996c3 commit 8548bec

1 file changed

Lines changed: 44 additions & 26 deletions

File tree

collection/tools/roles/tools_get_openshift_release/tasks/get_openshift_release_binaries.yml

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
# file-cache (openshift-release-artifacts), which has no SLA and can get stuck
55
# indefinitely during tool extraction.
66
#
7-
# Since the shiftstackclient pod starts with no `oc` binary, we bootstrap one
8-
# from mirror.openshift.com, extract the pull secret from the host cluster via
9-
# the Kubernetes API (using the kubeconfig's client certificate), then use
10-
# `oc adm release extract --tools` to get the version-matched binaries.
7+
# The pull secret is extracted from the host cluster via the Kubernetes API
8+
# using the kubeconfig's client certificate. If `oc` is not already present
9+
# in the pod (cold-start), a stable client is bootstrapped from
10+
# mirror.openshift.com before running `oc adm release extract --tools`.
1111
- name: Get the OCP installer and/or client binaries
1212
vars:
1313
installer_tarball: "openshift-install-linux-{{ release_name }}.tar.gz"
@@ -29,7 +29,7 @@
2929
- name: Extract pull secret from host cluster via Kubernetes API
3030
ansible.builtin.shell: |
3131
python3 << 'PYEOF'
32-
import yaml, json, base64, subprocess, os, sys
32+
import yaml, json, base64, subprocess, os, sys, tempfile
3333
3434
kubeconfig_path = "{{ rhoso_kubeconfig }}"
3535
output_path = "{{ pull_secret_file }}"
@@ -40,27 +40,33 @@
4040
server = kc['clusters'][0]['cluster']['server']
4141
user = kc['users'][0]['user']
4242
43-
ca_path = '/tmp/k8s-ca.crt'
44-
cert_path = '/tmp/k8s-client.crt'
45-
key_path = '/tmp/k8s-client.key'
46-
47-
with open(ca_path, 'wb') as f:
48-
f.write(base64.b64decode(kc['clusters'][0]['cluster']['certificate-authority-data']))
49-
with open(cert_path, 'wb') as f:
50-
f.write(base64.b64decode(user['client-certificate-data']))
51-
with open(key_path, 'wb') as f:
52-
f.write(base64.b64decode(user['client-key-data']))
53-
54-
result = subprocess.run([
55-
'curl', '-s', '--fail',
56-
'--cacert', ca_path,
57-
'--cert', cert_path,
58-
'--key', key_path,
59-
f'{server}/api/v1/namespaces/openshift-config/secrets/pull-secret'
60-
], capture_output=True, text=True)
43+
try:
44+
cert_data = user['client-certificate-data']
45+
key_data = user['client-key-data']
46+
except KeyError:
47+
print(f"rhoso_kubeconfig must use client-certificate auth, "
48+
f"found auth keys: {list(user.keys())}", file=sys.stderr)
49+
sys.exit(1)
6150
62-
for f in [ca_path, cert_path, key_path]:
63-
os.remove(f)
51+
with tempfile.TemporaryDirectory() as tmpdir:
52+
ca_path = os.path.join(tmpdir, 'ca.crt')
53+
cert_path = os.path.join(tmpdir, 'client.crt')
54+
key_path = os.path.join(tmpdir, 'client.key')
55+
56+
with open(ca_path, 'wb') as f:
57+
f.write(base64.b64decode(kc['clusters'][0]['cluster']['certificate-authority-data']))
58+
with open(cert_path, 'wb') as f:
59+
f.write(base64.b64decode(cert_data))
60+
with open(key_path, 'wb') as f:
61+
f.write(base64.b64decode(key_data))
62+
63+
result = subprocess.run([
64+
'curl', '-s', '--fail',
65+
'--cacert', ca_path,
66+
'--cert', cert_path,
67+
'--key', key_path,
68+
f'{server}/api/v1/namespaces/openshift-config/secrets/pull-secret'
69+
], capture_output=True, text=True)
6470
6571
if result.returncode != 0:
6672
print(f"Failed to fetch pull secret from {server}: {result.stderr}", file=sys.stderr)
@@ -85,7 +91,14 @@
8591
register: _pull_secret_verify
8692
changed_when: false
8793

94+
- name: Check if oc is already available
95+
ansible.builtin.command: which oc
96+
register: _oc_available
97+
ignore_errors: true
98+
changed_when: false
99+
88100
- name: Bootstrap oc client from {{ bootstrap_oc_url }}
101+
when: _oc_available is failed
89102
block:
90103
- name: Create bootstrap directory
91104
ansible.builtin.file:
@@ -103,6 +116,10 @@
103116
retries: 3
104117
delay: 10
105118

119+
- name: Set oc binary path
120+
ansible.builtin.set_fact:
121+
_oc_bin: "{{ (bootstrap_oc_dir + '/oc') if _oc_available is failed else 'oc' }}"
122+
106123
- name: Create the installer directory
107124
ansible.builtin.file:
108125
path: "{{ home_dir }}/{{ release_name }}"
@@ -112,7 +129,8 @@
112129
- name: Extract OCP tools from release image {{ openshift_release_pull_spec }}
113130
ansible.builtin.command:
114131
cmd: >-
115-
{{ bootstrap_oc_dir }}/oc adm release extract
132+
timeout 900
133+
{{ _oc_bin }} adm release extract
116134
--tools
117135
--registry-config={{ pull_secret_file }}
118136
--to={{ home_dir }}/{{ release_name }}

0 commit comments

Comments
 (0)