Skip to content

Commit 983c5c5

Browse files
committed
feat: add restricted kubeconfig creation
Signed-off-by: Gerard Vanloo <gerard.vanloo@ibm.com>
1 parent 179e540 commit 983c5c5

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
- name: Load kubeconfig file
3+
ansible.builtin.set_fact:
4+
agent_kubeconfig: "{{ lookup('ansible.builtin.file', agent_cluster.kubeconfig) | from_yaml }}"
5+
6+
- name: Find index of current context cluster
7+
ansible.builtin.set_fact:
8+
agent_cluster_index: "{{ lookup('ansible.utils.index_of', agent_kubeconfig.clusters, 'eq', cluster, 'name') }}"
9+
vars:
10+
cluster: "{{ agent_kubeconfig['current-context'] }}"
11+
12+
- name: Generate contexts and users
13+
ansible.builtin.set_fact:
14+
agent_contexts: "{{ (agent_contexts | default([])) + [{'cluster': cluster, 'user': 'agent', 'namespace': kv.key}] }}"
15+
agent_users: "{{ (agent_users | default([])) +[{'name': 'agent', 'user': {'token': kv.value}}] }}"
16+
loop: "{{ agent_application_tokens | dict2items }}"
17+
loop_control:
18+
label: application/{{ kv.key }}
19+
loop_var: kv
20+
vars:
21+
cluster: "{{ agent_kubeconfig['current-context'] }}"
22+
when:
23+
- kv.value != ""
24+
25+
- name: Create restricted kubeconfig file in temporary directory
26+
ansible.builtin.copy:
27+
content: "{{ lookup('ansible.builtin.template', 'templates/kubeconfig.j2') | from_yaml | to_nice_yaml(indent=2) }}"
28+
dest: /tmp/restricted_config
29+
mode: "0644"
30+
vars:
31+
certificate_authority_data: "{{ agent_kubeconfig.clusters[agent_cluster_index].cluster['certificate-authority-data'] }}"
32+
cluster: "{{ agent_kubeconfig.clusters[agent_cluster_index].name }}"
33+
contexts: "{{ agent_contexts }}"
34+
server: "{{ agent_kubeconfig.clusters[agent_cluster_index].cluster.server }}"
35+
users: "{{ agent_users }}"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
apiVersion: v1
3+
kind: Config
4+
preferences: {}
5+
clusters:
6+
- cluster:
7+
server: {{ server }}
8+
certificate-authority-data: {{ certificate_authority_data }}
9+
name: {{ cluster }}
10+
contexts: {{ contexts }}
11+
current-context: {{ cluster }}
12+
users: {{ users }}

sre/roles/applications/tasks/install.yaml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@
2121
- applications_required.otel_demo is defined
2222
- applications_required.otel_demo.enabled
2323

24-
# TODO: Enable these tasks during the refactor of the e2e
25-
26-
# - name: Import Deployment Time tasks
27-
# ansible.builtin.import_tasks:
28-
# file: register_deployment_time.yaml
29-
30-
# - name: Import Deployment Failure tasks
31-
# ansible.builtin.import_tasks:
32-
# file: register_deployment_failure.yaml
33-
# when:
34-
# - (sre_bench_runner | default(false))
24+
- name: Import agents role for restricted kubeconfig generation
25+
ansible.builtin.import_role:
26+
name: agent
27+
tasks_from: generate_restricted_kubeconfig
28+
vars:
29+
agent_application_tokens:
30+
"{{ applications_helm_releases.otel_demo.namespace }}": "{{ applications_otel_demo_token | default('') }}"
31+
agent_cluster:
32+
kubeconfig: "{{ cluster.kubeconfig }}"

sre/roles/applications/tasks/install_otel_demo.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,7 @@
399399
namespace: "{{ helm_release.namespace }}"
400400
agent_cluster:
401401
kubeconfig: "{{ cluster.kubeconfig }}"
402+
403+
- name: Set token variable for telemetry access
404+
ansible.builtin.set_fact:
405+
applications_otel_demo_token: "{{ agent_token_request.result.status.token }}"

0 commit comments

Comments
 (0)