Skip to content

Commit 4e4e035

Browse files
author
Martin Jackson
committed
Provide mechanism to discover clustergroup files. Use it to discover sscsi workload auth elements from managed clustergroups
1 parent dab5ce6 commit 4e4e035

14 files changed

Lines changed: 642 additions & 136 deletions

playbooks/list_clustergroups.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
# Discover values-<clustergroup>.yaml|yml under pattern_dir.
3+
# Resolves pattern_dir like pattern_settings (extra var pattern_dir, env PATTERN_DIR, cwd).
4+
- name: List pattern clustergroup value stems
5+
hosts: localhost
6+
connection: local
7+
gather_facts: false
8+
become: false
9+
roles:
10+
- pattern_settings
11+
- role: clustergroup_discovery
12+
tasks:
13+
- name: Report clustergroup discovery
14+
ansible.builtin.debug:
15+
msg:
16+
pattern_dir: "{{ pattern_dir }}"
17+
main_clustergroup: "{{ main_clustergroup }}"
18+
managed_clustergroup_names: "{{ managed_clustergroup_names }}"
19+
clustergroup_names: "{{ clustergroup_names }}"
20+
clustergroup_load_order: "{{ clustergroup_load_order }}"
21+
clustergroup_file_entries: "{{ clustergroup_file_entries }}"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
# Parse every top-level values-<clustergroup>.yaml|yml into clustergroup_documents (stem -> root).
3+
# Use for migration tooling or inspection; SS CSI merge uses the same discovery role internally.
4+
- name: Parse pattern clustergroup values files
5+
hosts: localhost
6+
connection: local
7+
gather_facts: false
8+
become: false
9+
roles:
10+
- pattern_settings
11+
- role: clustergroup_discovery
12+
vars:
13+
clustergroup_discovery_parse_documents: true
14+
tasks:
15+
- name: Summarize parsed clustergroup documents
16+
ansible.builtin.debug:
17+
msg:
18+
pattern_dir: "{{ pattern_dir }}"
19+
main_clustergroup: "{{ main_clustergroup }}"
20+
managed_clustergroup_names: "{{ managed_clustergroup_names }}"
21+
stems_parsed: "{{ clustergroup_documents | default({}) | dict2items | map(attribute='key') | sort | list }}"
22+
document_count: "{{ clustergroup_documents | default({}) | length }}"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
# When true, slurp and parse each resolved clustergroup file into clustergroup_documents (stem -> root mapping)
3+
clustergroup_discovery_parse_documents: false
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
galaxy_info:
3+
author: rhvp
4+
description: >-
5+
Resolve main clustergroup from values-global, read managedClusterGroups from the main
6+
values file, then optionally parse existing values-<stem> files for those stems.
7+
license: Apache-2.0
8+
min_ansible_version: "2.14"
9+
galaxy_tags:
10+
- openshift
11+
- gitops
12+
dependencies: []
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
# Discover clustergroups in use: main from values-global, managed from main file's clusterGroup.managedClusterGroups.
3+
# Sets: clustergroup_names (sorted stems), managed_clustergroup_names (sorted, excludes main),
4+
# clustergroup_load_order (main first, then managed sorted — SS CSI merge precedence),
5+
# clustergroup_file_entries ({name, path} only when values-<stem>.yaml|yml exists),
6+
# clustergroup_documents (optional, stem -> parsed YAML root).
7+
8+
- name: Resolve pattern_dir for clustergroup discovery
9+
ansible.builtin.include_tasks: ../pattern_settings/tasks/resolve_overrides.yml
10+
when: (pattern_dir | default('', true) | string | trim | length) == 0
11+
12+
- name: Fail when pattern_dir is empty after resolve
13+
ansible.builtin.fail:
14+
msg: >-
15+
pattern_dir is required (extra var pattern_dir, env PATTERN_DIR, or cwd with values-global.yaml).
16+
when: (pattern_dir | default('', true) | string | trim | length) == 0
17+
18+
- name: Resolve main clustergroup stem from facts or values-global.yaml
19+
ansible.builtin.set_fact:
20+
_clustergroup_discovery_main_stem: >-
21+
{{
22+
(
23+
(main_clustergroupname | default(main_clustergroup | default('', true), true) | string | trim | length) > 0
24+
)
25+
| ternary(
26+
main_clustergroupname | default(main_clustergroup, true) | string | trim,
27+
(
28+
lookup('file', (pattern_dir | string | trim) ~ '/values-global.yaml')
29+
| from_yaml
30+
).main.clusterGroupName | string | trim
31+
)
32+
}}
33+
34+
- name: Fail when main clusterGroupName cannot be resolved
35+
ansible.builtin.fail:
36+
msg: >-
37+
Could not resolve main clustergroup (values-global.yaml missing .main.clusterGroupName or empty).
38+
when: (_clustergroup_discovery_main_stem | string | trim | length) == 0
39+
40+
- name: Stat main clustergroup values file (yaml)
41+
ansible.builtin.stat:
42+
path: "{{ pattern_dir | string | trim }}/values-{{ _clustergroup_discovery_main_stem }}.yaml"
43+
register: _clustergroup_discovery_main_stat_yaml
44+
45+
- name: Stat main clustergroup values file (yml)
46+
ansible.builtin.stat:
47+
path: "{{ pattern_dir | string | trim }}/values-{{ _clustergroup_discovery_main_stem }}.yml"
48+
register: _clustergroup_discovery_main_stat_yml
49+
when: not (_clustergroup_discovery_main_stat_yaml.stat.exists | default(false))
50+
51+
- name: Set path to main clustergroup values file when present
52+
ansible.builtin.set_fact:
53+
_clustergroup_main_values_path: "{{ pattern_dir | string | trim }}/values-{{ _clustergroup_discovery_main_stem }}.yaml"
54+
when: _clustergroup_discovery_main_stat_yaml.stat.exists | default(false)
55+
56+
- name: Set path to main clustergroup values file when only yml exists
57+
ansible.builtin.set_fact:
58+
_clustergroup_main_values_path: "{{ pattern_dir | string | trim }}/values-{{ _clustergroup_discovery_main_stem }}.yml"
59+
when:
60+
- _clustergroup_main_values_path is not defined
61+
- _clustergroup_discovery_main_stat_yml is defined
62+
- _clustergroup_discovery_main_stat_yml.stat.exists | default(false)
63+
64+
- name: Load parsed root from main clustergroup values file
65+
ansible.builtin.set_fact:
66+
_clustergroup_main_root: "{{ lookup('file', _clustergroup_main_values_path) | from_yaml }}"
67+
when: _clustergroup_main_values_path is defined
68+
69+
- name: Default empty main clustergroup root when file is absent
70+
ansible.builtin.set_fact:
71+
_clustergroup_main_root: {}
72+
when: _clustergroup_main_values_path is not defined
73+
74+
- name: Collect managed clustergroup names from main file managedClusterGroups
75+
ansible.builtin.set_fact:
76+
managed_clustergroup_names: "{{ managed_clustergroup_names | default([]) + [_cgd_mcg_name] }}"
77+
vars:
78+
_cgd_mcg_name: "{{ (item.value.name | default(item.key, true)) | string | trim }}"
79+
loop: "{{ (_clustergroup_main_root.clusterGroup | default({})).managedClusterGroups | default({}) | dict2items }}"
80+
loop_control:
81+
label: "{{ _cgd_mcg_name }}"
82+
when:
83+
- _clustergroup_main_root is mapping
84+
- (_clustergroup_main_root.clusterGroup | default({})).managedClusterGroups is defined
85+
- ((_clustergroup_main_root.clusterGroup | default({})).managedClusterGroups | default({})) is mapping
86+
87+
- name: Finalize managed clustergroup names list
88+
ansible.builtin.set_fact:
89+
managed_clustergroup_names: "{{ managed_clustergroup_names | default([]) | unique | sort }}"
90+
91+
- name: Set clustergroup load order (main first so managed values files override for SS CSI merge)
92+
ansible.builtin.set_fact:
93+
clustergroup_load_order: >-
94+
{{
95+
(
96+
[_clustergroup_discovery_main_stem]
97+
+ (managed_clustergroup_names | reject('equalto', _clustergroup_discovery_main_stem) | list)
98+
) | unique | list
99+
}}
100+
101+
- name: Set sorted clustergroup names (all stems in use)
102+
ansible.builtin.set_fact:
103+
clustergroup_names: "{{ clustergroup_load_order | sort }}"
104+
105+
- name: Build clustergroup_file_entries for stems that have a local values file
106+
ansible.builtin.include_tasks: resolve_clustergroup_file_path.yml
107+
loop: "{{ clustergroup_load_order }}"
108+
loop_control:
109+
loop_var: clustergroup_discovery_stem
110+
111+
- name: Default empty clustergroup file entries
112+
ansible.builtin.set_fact:
113+
clustergroup_file_entries: []
114+
when: clustergroup_file_entries is not defined
115+
116+
- name: Parse each resolved clustergroup values file when requested
117+
ansible.builtin.include_tasks: parse_documents.yml
118+
when: clustergroup_discovery_parse_documents | default(false) | bool
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
- name: Parse clustergroup values YAML into clustergroup_documents
3+
ansible.builtin.set_fact:
4+
clustergroup_documents: "{{ clustergroup_documents | default({}) | combine({item.name: (lookup('file', item.path) | from_yaml)}) }}"
5+
loop: "{{ clustergroup_file_entries }}"
6+
loop_control:
7+
label: "{{ item.name }}"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
# loop_var: clustergroup_discovery_stem — append {name, path} to clustergroup_file_entries when file exists.
3+
4+
- name: Stat values file for stem {{ clustergroup_discovery_stem }} (yaml)
5+
ansible.builtin.stat:
6+
path: "{{ pattern_dir | string | trim }}/values-{{ clustergroup_discovery_stem | string | trim }}.yaml"
7+
register: _clustergroup_discovery_stem_stat_yaml
8+
9+
- name: Stat values file for stem {{ clustergroup_discovery_stem }} (yml)
10+
ansible.builtin.stat:
11+
path: "{{ pattern_dir | string | trim }}/values-{{ clustergroup_discovery_stem | string | trim }}.yml"
12+
register: _clustergroup_discovery_stem_stat_yml
13+
14+
- name: Record clustergroup file entry for {{ clustergroup_discovery_stem }} (prefer yaml)
15+
ansible.builtin.set_fact:
16+
clustergroup_file_entries: "{{ clustergroup_file_entries | default([]) + [_entry] }}"
17+
vars:
18+
_entry:
19+
name: "{{ clustergroup_discovery_stem | string | trim }}"
20+
path: "{{ pattern_dir | string | trim }}/values-{{ clustergroup_discovery_stem | string | trim }}.yaml"
21+
when: _clustergroup_discovery_stem_stat_yaml.stat.exists | default(false)
22+
23+
- name: Record clustergroup file entry for {{ clustergroup_discovery_stem }} (yml fallback)
24+
ansible.builtin.set_fact:
25+
clustergroup_file_entries: "{{ clustergroup_file_entries | default([]) + [_entry] }}"
26+
vars:
27+
_entry:
28+
name: "{{ clustergroup_discovery_stem | string | trim }}"
29+
path: "{{ pattern_dir | string | trim }}/values-{{ clustergroup_discovery_stem | string | trim }}.yml"
30+
when:
31+
- not (_clustergroup_discovery_stem_stat_yaml.stat.exists | default(false))
32+
- _clustergroup_discovery_stem_stat_yml.stat.exists | default(false)

roles/vault_utils/defaults/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ vault_csi_role_ttl: "15m"
7979
# namespace defaults to the application namespace; cluster defaults to hub for hub apps, or to
8080
# managedClusterGroup.name (else the group YAML key) for applications declared under managedClusterGroups.
8181
vault_ss_csi_from_applications: true
82+
# When true, SS CSI loads ConfigMap/file per clustergroup stem and merges applications +
83+
# managedClusterGroups (main stem first, then others alphabetically; later files override).
84+
# When false, only the main clustergroup document is loaded (legacy behavior).
85+
vault_ss_csi_aggregate_clustergroup_sources: true
8286
# Prefer merged clustergroup values from an in-cluster ConfigMap (reflects GitOps overrides).
8387
vault_ss_csi_clustergroup_values_from_configmap: true
8488
# Namespace containing the clustergroup values ConfigMap (OpenShift GitOps default).
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
# loop_var: cg_collect_stem — collect ssCsiWorkloadAuth from clusterGroup.applications for that stem only.
3+
# Main stem defaults cluster to hub; other stems default to the stem name (managed clustergroup file).
4+
5+
- name: Collect SS CSI rows from clusterGroup.applications for stem {{ cg_collect_stem }}
6+
ansible.builtin.include_tasks: vault_ss_csi_collect_one_application.yaml
7+
loop: >-
8+
{{
9+
((_vault_ss_csi_apps_by_stem | default({}))[cg_collect_stem] | default({}))
10+
| dict2items
11+
| selectattr('value.ssCsiWorkloadAuth', 'defined')
12+
| list
13+
}}
14+
loop_control:
15+
loop_var: outer_item
16+
vars:
17+
ss_csi_cluster_default_for_app: >-
18+
{{
19+
'hub'
20+
if (
21+
(cg_collect_stem | string | trim)
22+
== (main_clustergroupname | default(main_clustergroup | default('', true), true) | string | trim)
23+
)
24+
else (cg_collect_stem | string | trim)
25+
}}

0 commit comments

Comments
 (0)