|
4 | 4 | # file-cache (openshift-release-artifacts), which has no SLA and can get stuck |
5 | 5 | # indefinitely during tool extraction. |
6 | 6 | # |
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`. |
11 | 11 | - name: Get the OCP installer and/or client binaries |
12 | 12 | vars: |
13 | 13 | installer_tarball: "openshift-install-linux-{{ release_name }}.tar.gz" |
|
29 | 29 | - name: Extract pull secret from host cluster via Kubernetes API |
30 | 30 | ansible.builtin.shell: | |
31 | 31 | python3 << 'PYEOF' |
32 | | - import yaml, json, base64, subprocess, os, sys |
| 32 | + import yaml, json, base64, subprocess, os, sys, tempfile |
33 | 33 |
|
34 | 34 | kubeconfig_path = "{{ rhoso_kubeconfig }}" |
35 | 35 | output_path = "{{ pull_secret_file }}" |
|
40 | 40 | server = kc['clusters'][0]['cluster']['server'] |
41 | 41 | user = kc['users'][0]['user'] |
42 | 42 |
|
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) |
61 | 50 |
|
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) |
64 | 70 |
|
65 | 71 | if result.returncode != 0: |
66 | 72 | print(f"Failed to fetch pull secret from {server}: {result.stderr}", file=sys.stderr) |
|
85 | 91 | register: _pull_secret_verify |
86 | 92 | changed_when: false |
87 | 93 |
|
| 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 | + |
88 | 100 | - name: Bootstrap oc client from {{ bootstrap_oc_url }} |
| 101 | + when: _oc_available is failed |
89 | 102 | block: |
90 | 103 | - name: Create bootstrap directory |
91 | 104 | ansible.builtin.file: |
|
103 | 116 | retries: 3 |
104 | 117 | delay: 10 |
105 | 118 |
|
| 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 | + |
106 | 123 | - name: Create the installer directory |
107 | 124 | ansible.builtin.file: |
108 | 125 | path: "{{ home_dir }}/{{ release_name }}" |
|
112 | 129 | - name: Extract OCP tools from release image {{ openshift_release_pull_spec }} |
113 | 130 | ansible.builtin.command: |
114 | 131 | cmd: >- |
115 | | - {{ bootstrap_oc_dir }}/oc adm release extract |
| 132 | + timeout 900 |
| 133 | + {{ _oc_bin }} adm release extract |
116 | 134 | --tools |
117 | 135 | --registry-config={{ pull_secret_file }} |
118 | 136 | --to={{ home_dir }}/{{ release_name }} |
|
0 commit comments