Skip to content

Commit ed55a02

Browse files
fultonjclaude
andcommitted
Fix openshift_adm role context when kubeconfig has multiple clusters
Add configurable context parameter to openshift_adm role to explicitly target the deployed OpenShift cluster instead of relying on current-context. Changes: - Add cifmw_openshift_adm_context variable defaulting to 'admin' - Update all kubernetes.core tasks to use the specified context: - _get_nodes.yml: k8s_info task for node gathering - wait_for_cluster.yml: k8s_drain task for node management - api_cert.yml: k8s and k8s_info tasks for certificate operations - shutdown.yml: k8s_drain task for node cordoning - Add context switching for custom modules that don't support context: - approve_csr module: switch context before CSR approval - openshift_auth module: switch context before authentication - Replace static cifmw_openshift_api parameter with dynamic API server URL detection: - Add _get_api_server.yml task to retrieve URL from current context - Update URI check and authentication to use context-based URL - Fixes issue where tasks fail with 403 Forbidden or target wrong cluster when current-context points to CI cluster where user lacks permissions - Also, increase delay in wait_for_cluster.yml since waiting for 2 seconds before retrying does more harm than good in our long running CI jobs. Jira: https://issues.redhat.com/browse/OSPRH-20252 Co-Authored-By: Claude <[email protected]> Signed-off-by: John Fulton <[email protected]>
1 parent 8a5b406 commit ed55a02

File tree

8 files changed

+99
-24
lines changed

8 files changed

+99
-24
lines changed

roles/openshift_adm/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ This role requires the following parameters to be configured.
1616

1717
* `cifmw_openshift_adm_basedir` (str) Framework base directory, defaults to `cifmw_basedir` or
1818
`~/ci-framework-data`.
19-
* `cifmw_openshift_api` (str) Cluster endpoint to be used for communication.
2019
* `cifmw_openshift_user` (str) Name of the user to be used for authentication.
2120
* `cifmw_openshift_password` (str) Password of the provided user.
2221
* `cifmw_openshift_kubeconfig` (str) Absolute path to the kubeconfig file.
@@ -30,6 +29,11 @@ This role requires the following parameters to be configured.
3029
performed on the cluster.
3130
* `cifmw_openshift_adm_retry_count` (int) The maximum number of attempts to be
3231
made for a command to succeed. Default is `100`.
32+
* `cifmw_openshift_adm_context` (str) The kubeconfig context to use for cluster operations. Default is `admin`.
33+
34+
## Obsolete Parameters
35+
36+
* `cifmw_openshift_api` (str) Previously required cluster endpoint URL. Removed in favor of dynamic API server URL detection from kubeconfig context to ensure correct cluster targeting.
3337

3438
## Reference
3539

roles/openshift_adm/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ cifmw_openshift_adm_op: ""
2929
cifmw_openshift_adm_dry_run: false
3030
cifmw_openshift_adm_retry_count: 100
3131
cifmw_openshift_adm_stable_period: 3m
32+
cifmw_openshift_adm_context: admin
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
# Copyright Red Hat, Inc.
3+
# All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
18+
# Gets the API server URL from the current context in the kubeconfig
19+
20+
- name: Get current context
21+
ansible.builtin.command: |
22+
oc config current-context
23+
register: _current_context
24+
changed_when: false
25+
environment:
26+
KUBECONFIG: "{{ cifmw_openshift_kubeconfig }}"
27+
28+
- name: Get cluster name from current context
29+
ansible.builtin.command: |
30+
oc config view -o jsonpath='{.contexts[?(@.name=="{{ _current_context.stdout }}")].context.cluster}'
31+
register: _current_cluster
32+
changed_when: false
33+
environment:
34+
KUBECONFIG: "{{ cifmw_openshift_kubeconfig }}"
35+
36+
- name: Get API server URL from cluster
37+
ansible.builtin.command: |
38+
oc config view -o jsonpath='{.clusters[?(@.name=="{{ _current_cluster.stdout }}")].cluster.server}'
39+
register: _context_api_server
40+
changed_when: false
41+
environment:
42+
KUBECONFIG: "{{ cifmw_openshift_kubeconfig }}"
43+
44+
- name: Set API server URL from context
45+
ansible.builtin.set_fact:
46+
_current_api_server: "{{ _context_api_server.stdout }}"

roles/openshift_adm/tasks/_get_nodes.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
kubernetes.core.k8s_info:
55
kind: Node
66
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
7+
context: "{{ cifmw_openshift_adm_context }}"
78
validate_certs: false
89
wait_condition:
910
reason: KubeletReady

roles/openshift_adm/tasks/api_cert.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
name: "{{ item }}"
3838
state: absent
3939
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
40+
context: "{{ cifmw_openshift_adm_context }}"
4041
validate_certs: false
4142
loop:
4243
- csr-signer-signer
@@ -60,6 +61,7 @@
6061
namespace: openshift-kube-controller-manager-operator
6162
name: csr-signer-signer
6263
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
64+
context: "{{ cifmw_openshift_adm_context }}"
6365
validate_certs: false
6466
register: _api_cert
6567

roles/openshift_adm/tasks/main.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
that:
2121
- cifmw_basedir is defined
2222
- cifmw_path is defined
23-
- cifmw_openshift_api is defined
2423
- cifmw_openshift_user is defined
2524
- cifmw_openshift_password is defined
2625
- cifmw_openshift_kubeconfig is defined

roles/openshift_adm/tasks/shutdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
name: "{{ item }}"
5858
state: cordon
5959
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
60+
context: "{{ cifmw_openshift_adm_context }}"
6061
validate_certs: false
6162
loop: "{{ _node_names }}"
6263

roles/openshift_adm/tasks/wait_for_cluster.yml

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@
1818
# We would wait till forbidden error is received. It indicates the endpoint
1919
# is reachable.
2020

21+
- name: Get API server URL from current context
22+
ansible.builtin.include_tasks: _get_api_server.yml
23+
2124
- name: Wait until the OCP API endpoint is reachable.
2225
ansible.builtin.uri:
23-
url: "{{ cifmw_openshift_api }}"
26+
url: "{{ _current_api_server }}"
2427
return_content: true
2528
validate_certs: false
2629
status_code: 403
2730
register: ocp_api_result
2831
until: ocp_api_result.status == 403
2932
retries: "{{ cifmw_openshift_adm_retry_count }}"
30-
delay: 5
33+
delay: 30
3134

3235
- name: Get nodes list
3336
ansible.builtin.import_tasks: _get_nodes.yml
@@ -39,25 +42,32 @@
3942
name: "{{ item }}"
4043
state: uncordon
4144
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
45+
context: "{{ cifmw_openshift_adm_context }}"
4246
validate_certs: false
4347
loop: "{{ _nodes.resources | map(attribute='metadata.name') | list }}"
4448
register: _node_status
4549
until: _node_status.result is defined
4650
retries: "{{ cifmw_openshift_adm_retry_count }}"
47-
delay: 5
51+
delay: 30
4852

4953
- name: Check for pending certificate approval.
5054
when:
5155
- _openshift_adm_check_cert_approve | default(false) | bool
52-
register: _approve_csr
53-
approve_csr:
54-
k8s_config: "{{ cifmw_openshift_kubeconfig }}"
55-
retries: 30
56-
delay: 10
57-
until:
58-
- _approve_csr is defined
59-
- _approve_csr.rc is defined
60-
- _approve_csr.rc == 0
56+
block:
57+
- name: Set current context to admin for CSR approval
58+
ansible.builtin.shell: |
59+
KUBECONFIG="{{ cifmw_openshift_kubeconfig }}" oc config use-context "{{ cifmw_openshift_adm_context }}"
60+
61+
- name: Approve pending certificate requests
62+
register: _approve_csr
63+
approve_csr:
64+
k8s_config: "{{ cifmw_openshift_kubeconfig }}"
65+
retries: 10
66+
delay: 30
67+
until:
68+
- _approve_csr is defined
69+
- _approve_csr.rc is defined
70+
- _approve_csr.rc == 0
6171

6272
- name: Wait until the OpenShift cluster is stable.
6373
environment:
@@ -68,13 +78,24 @@
6878
oc adm wait-for-stable-cluster --minimum-stable-period=5s --timeout=30m
6979
7080
- name: Wait until OCP login succeeds.
71-
community.okd.openshift_auth:
72-
host: "{{ cifmw_openshift_api }}"
73-
password: "{{ cifmw_openshift_password }}"
74-
state: present
75-
username: "{{ cifmw_openshift_user }}"
76-
validate_certs: false
77-
register: _oc_login_result
78-
until: _oc_login_result.k8s_auth is defined
79-
retries: "{{ cifmw_openshift_adm_retry_count }}"
80-
delay: 2
81+
block:
82+
- name: Ensure admin context is set for login
83+
ansible.builtin.shell: |
84+
KUBECONFIG="{{ cifmw_openshift_kubeconfig }}" oc config use-context "{{ cifmw_openshift_adm_context }}"
85+
86+
# Re-get API server URL since admin context may point to a different
87+
# cluster than the initial context used for reachability check above
88+
- name: Get API server URL from admin context
89+
ansible.builtin.include_tasks: _get_api_server.yml
90+
91+
- name: Authenticate to OpenShift cluster
92+
community.okd.openshift_auth:
93+
host: "{{ _current_api_server }}"
94+
password: "{{ cifmw_openshift_password }}"
95+
state: present
96+
username: "{{ cifmw_openshift_user }}"
97+
validate_certs: false
98+
register: _oc_login_result
99+
until: _oc_login_result.k8s_auth is defined
100+
retries: "{{ cifmw_openshift_adm_retry_count }}"
101+
delay: 30

0 commit comments

Comments
 (0)