|
1 | 1 | --- |
| 2 | +# Extract OCP installer and/or client binaries directly from the release image |
| 3 | +# using `oc adm release extract --tools` instead of the release-controller's |
| 4 | +# file-cache (openshift-release-artifacts), which has no SLA and can get stuck |
| 5 | +# indefinitely during tool extraction. |
| 6 | +# |
| 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`. |
2 | 11 | - name: Get the OCP installer and/or client binaries |
3 | 12 | vars: |
4 | | - installer_url: "{{ openshift_download_url }}/openshift-install-linux-{{ release_name }}.tar.gz" |
5 | | - client_url: "{{ openshift_download_url }}/openshift-client-linux-{{ release_name }}.tar.gz" |
| 13 | + installer_tarball: "openshift-install-linux-{{ release_name }}.tar.gz" |
| 14 | + client_tarball: "openshift-client-linux-{{ release_name }}.tar.gz" |
| 15 | + pull_secret_file: "{{ home_dir }}/pull-secret.json" |
| 16 | + bootstrap_oc_dir: "{{ home_dir }}/bootstrap-oc" |
| 17 | + bootstrap_oc_url: "{{ openshift_mirror_url }}/stable/openshift-client-linux.tar.gz" |
6 | 18 | block: |
7 | 19 | - name: Fail if release_name var is not defined |
8 | 20 | ansible.builtin.fail: |
9 | 21 | msg: "'release_name' variable must be defined and cannot be empty" |
10 | 22 | when: release_name == '' |
11 | 23 |
|
12 | | - - name: Wait for content to come up on {{ openshift_download_url }} |
13 | | - ansible.builtin.uri: |
14 | | - url: "{{ openshift_download_url }}" |
15 | | - method: GET |
16 | | - return_content: yes |
17 | | - status_code: 200 |
18 | | - body_format: json |
19 | | - register: result |
20 | | - until: result.content.find("openshift-install-linux") != -1 |
21 | | - retries: 20 |
22 | | - delay: 60 |
| 24 | + - name: Fail if openshift_release_pull_spec is not defined |
| 25 | + ansible.builtin.fail: |
| 26 | + msg: "'openshift_release_pull_spec' must be set by get_openshift_release_build_name.yml" |
| 27 | + when: openshift_release_pull_spec is not defined or openshift_release_pull_spec == '' |
| 28 | + |
| 29 | + - name: Extract pull secret from host cluster via Kubernetes API |
| 30 | + ansible.builtin.shell: | |
| 31 | + python3 << 'PYEOF' |
| 32 | + import yaml, json, base64, subprocess, os, sys, tempfile |
| 33 | +
|
| 34 | + kubeconfig_path = "{{ rhoso_kubeconfig }}" |
| 35 | + output_path = "{{ pull_secret_file }}" |
| 36 | +
|
| 37 | + with open(kubeconfig_path) as f: |
| 38 | + kc = yaml.safe_load(f) |
| 39 | +
|
| 40 | + server = kc['clusters'][0]['cluster']['server'] |
| 41 | + user = kc['users'][0]['user'] |
| 42 | +
|
| 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) |
| 50 | +
|
| 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) |
| 70 | +
|
| 71 | + if result.returncode != 0: |
| 72 | + print(f"Failed to fetch pull secret from {server}: {result.stderr}", file=sys.stderr) |
| 73 | + sys.exit(1) |
| 74 | +
|
| 75 | + data = json.loads(result.stdout) |
| 76 | + decoded = base64.b64decode(data['data']['.dockerconfigjson']).decode() |
| 77 | + auths = json.loads(decoded) |
| 78 | +
|
| 79 | + with open(output_path, 'w') as f: |
| 80 | + f.write(decoded) |
| 81 | +
|
| 82 | + print(f"Pull secret extracted: {len(auths.get('auths', {}))} registries") |
| 83 | + PYEOF |
| 84 | + register: _pull_secret_result |
| 85 | + no_log: true |
| 86 | + |
| 87 | + - name: Verify pull secret file is valid |
| 88 | + ansible.builtin.shell: >- |
| 89 | + python3 -c "import json; d=json.load(open('{{ pull_secret_file }}')); |
| 90 | + print(len(d.get('auths',{})), 'registries found')" |
| 91 | + register: _pull_secret_verify |
| 92 | + changed_when: false |
| 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 | + |
| 100 | + - name: Bootstrap oc client from {{ bootstrap_oc_url }} |
| 101 | + when: _oc_available is failed |
| 102 | + block: |
| 103 | + - name: Create bootstrap directory |
| 104 | + ansible.builtin.file: |
| 105 | + path: "{{ bootstrap_oc_dir }}" |
| 106 | + state: directory |
| 107 | + mode: u=rwx,g=rw,o=r |
| 108 | + |
| 109 | + - name: Download stable oc client from mirror |
| 110 | + ansible.builtin.unarchive: |
| 111 | + src: "{{ bootstrap_oc_url }}" |
| 112 | + dest: "{{ bootstrap_oc_dir }}" |
| 113 | + remote_src: yes |
| 114 | + register: _bootstrap_download |
| 115 | + until: _bootstrap_download is not failed |
| 116 | + retries: 3 |
| 117 | + delay: 10 |
| 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' }}" |
23 | 122 |
|
24 | 123 | - name: Create the installer directory |
25 | 124 | ansible.builtin.file: |
26 | 125 | path: "{{ home_dir }}/{{ release_name }}" |
27 | 126 | state: directory |
28 | 127 | mode: u=rwx,g=rw,o=r |
29 | 128 |
|
| 129 | + - name: Extract OCP tools from release image {{ openshift_release_pull_spec }} |
| 130 | + ansible.builtin.command: |
| 131 | + cmd: >- |
| 132 | + timeout 900 |
| 133 | + {{ _oc_bin }} adm release extract |
| 134 | + --tools |
| 135 | + --registry-config={{ pull_secret_file }} |
| 136 | + --to={{ home_dir }}/{{ release_name }} |
| 137 | + {{ openshift_release_pull_spec }} |
| 138 | + register: extract_result |
| 139 | + until: extract_result is not failed |
| 140 | + retries: 3 |
| 141 | + delay: 30 |
| 142 | + |
30 | 143 | - name: Get the installer binary and create a symlink |
31 | 144 | when: "'installer' in binaries" |
32 | 145 | block: |
33 | | - - name: Download and unarchive the installer from {{ installer_url }} |
| 146 | + - name: Unarchive the installer from {{ installer_tarball }} |
34 | 147 | ansible.builtin.unarchive: |
35 | | - src: "{{ installer_url }}" |
| 148 | + src: "{{ home_dir }}/{{ release_name }}/{{ installer_tarball }}" |
36 | 149 | dest: "{{ home_dir }}/{{ release_name }}" |
37 | 150 | remote_src: yes |
38 | | - register: result |
39 | | - until: result is not failed |
40 | | - retries: 3 |
41 | | - delay: 10 |
42 | 151 |
|
43 | 152 | - name: Create a symlink to the openshift-install binary from /usr/local/bin |
44 | 153 | ansible.builtin.file: |
|
47 | 156 | state: link |
48 | 157 | become: true |
49 | 158 |
|
50 | | - - name: Get the installer binary and create symlinks |
| 159 | + - name: Get the client binary and create symlinks |
51 | 160 | when: "'client' in binaries" |
52 | 161 | block: |
53 | | - - name: Download and unarchive the client from {{ client_url }} |
| 162 | + - name: Unarchive the client from {{ client_tarball }} |
54 | 163 | ansible.builtin.unarchive: |
55 | | - src: "{{ client_url }}" |
| 164 | + src: "{{ home_dir }}/{{ release_name }}/{{ client_tarball }}" |
56 | 165 | dest: "{{ home_dir }}/{{ release_name }}" |
57 | 166 | remote_src: yes |
58 | | - register: result |
59 | | - until: result is not failed |
60 | | - retries: 3 |
61 | | - delay: 10 |
62 | 167 |
|
63 | 168 | - name: Create a symlink to the oc binary from /usr/local/bin |
64 | 169 | ansible.builtin.file: |
|
73 | 178 | dest: /usr/bin/kubectl |
74 | 179 | state: link |
75 | 180 | become: true |
| 181 | + |
| 182 | + always: |
| 183 | + - name: Remove pull secret file |
| 184 | + ansible.builtin.file: |
| 185 | + path: "{{ pull_secret_file }}" |
| 186 | + state: absent |
| 187 | + |
| 188 | + - name: Remove bootstrap oc directory |
| 189 | + ansible.builtin.file: |
| 190 | + path: "{{ bootstrap_oc_dir }}" |
| 191 | + state: absent |
0 commit comments