Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SA-6478] Add owner label as a prefix while creating integration #165

Merged
merged 60 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
397baf5
Add owner label as a prefix while creating integration
John2020-cyber Dec 13, 2024
5a9749c
fix
John2020-cyber Dec 13, 2024
f859b3b
update
John2020-cyber Dec 31, 2024
76ca029
update
John2020-cyber Dec 31, 2024
bb6b879
update
John2020-cyber Dec 31, 2024
216a83c
update
John2020-cyber Dec 31, 2024
d80869f
update
John2020-cyber Dec 31, 2024
316825f
update
John2020-cyber Dec 31, 2024
5bba315
update
John2020-cyber Dec 31, 2024
bb371a9
update
John2020-cyber Jan 7, 2025
a68b36f
update
John2020-cyber Jan 7, 2025
4f4ba58
update
John2020-cyber Jan 7, 2025
6993d79
update
John2020-cyber Jan 7, 2025
04d450a
update
John2020-cyber Jan 7, 2025
fb5a916
update
John2020-cyber Jan 7, 2025
1155d1a
update
John2020-cyber Jan 7, 2025
ba41590
update
John2020-cyber Jan 7, 2025
ee54ea8
update
John2020-cyber Jan 7, 2025
755288b
update
John2020-cyber Jan 7, 2025
f1f85c9
update
John2020-cyber Jan 7, 2025
6497252
update
John2020-cyber Jan 7, 2025
dddfba0
update
John2020-cyber Jan 7, 2025
ad85cee
update
John2020-cyber Jan 7, 2025
83b7b1e
update
John2020-cyber Jan 7, 2025
50b22c2
update
John2020-cyber Jan 7, 2025
a707fc0
update
John2020-cyber Jan 7, 2025
ac800a4
update
John2020-cyber Jan 7, 2025
9796f90
update
John2020-cyber Jan 7, 2025
b3dd087
update
John2020-cyber Jan 8, 2025
70f8d42
update
John2020-cyber Jan 8, 2025
99accb5
update
John2020-cyber Jan 8, 2025
58dbff6
update
John2020-cyber Jan 8, 2025
3ca8b4c
update
John2020-cyber Jan 8, 2025
609e850
update
John2020-cyber Jan 8, 2025
dd486ea
update
John2020-cyber Jan 8, 2025
7f09d37
update
John2020-cyber Jan 8, 2025
7696fdc
update
John2020-cyber Jan 8, 2025
e6f93dd
update
John2020-cyber Jan 8, 2025
313564e
update
John2020-cyber Jan 8, 2025
d6bac6d
update
John2020-cyber Jan 8, 2025
f720a42
update
John2020-cyber Jan 8, 2025
ef8b6e1
update
John2020-cyber Jan 8, 2025
31b81b3
update
John2020-cyber Jan 8, 2025
d9f8f67
update
John2020-cyber Jan 8, 2025
aa1615c
update
John2020-cyber Jan 8, 2025
bbbda93
update
John2020-cyber Jan 8, 2025
14ed456
update
John2020-cyber Jan 8, 2025
c5d63b3
update
John2020-cyber Jan 8, 2025
8671912
update
John2020-cyber Jan 8, 2025
2882936
update
John2020-cyber Jan 8, 2025
da6e35d
update
John2020-cyber Jan 8, 2025
73e5efc
update
John2020-cyber Jan 8, 2025
ed95730
update
John2020-cyber Jan 8, 2025
1bfc635
update
John2020-cyber Jan 8, 2025
4aa724f
update
John2020-cyber Jan 8, 2025
656b17a
update
John2020-cyber Jan 8, 2025
957073f
update
John2020-cyber Jan 8, 2025
b605b5d
update
John2020-cyber Jan 8, 2025
89cb104
add comments
John2020-cyber Jan 9, 2025
bf4aa96
update
John2020-cyber Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,37 @@
kind: ManagedCluster
register: managed_clusters_raw

- name: Extract list of ManagedCluster CRs
# This initializes the managed_cluster var as it has been used at other places in the playbook
- name: Initialize managed_clusters as an empty list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comments on each code block why you need this , like first initialize list then extract names of managed clusters .. currently code is not very understandable

ansible.builtin.set_fact:
managed_clusters: "{{ managed_clusters | default([]) + [{'name': item.metadata.name}] }}"
managed_clusters: []

# This will extract all the names from the ManagedCluster CR
- name: Extract ManagedCluster names with prefix
ansible.builtin.set_fact:
managed_cluster_names: "{{ managed_cluster_names | default([]) + [item.metadata.name] }}"
loop: "{{ managed_clusters_raw.resources }}"
loop_control:
loop_var: item

# This will extract all the owner from the labels of ManagedCluster CR
- name: Extract ManagedCluster owners with prefix
ansible.builtin.set_fact:
managed_cluster_owners: "{{ managed_cluster_owners | default([]) + [item.metadata.labels.owner | default('unknown')] }}"
loop: "{{ managed_clusters_raw.resources }}"
loop_control:
loop_var: item

- name: Determine which ManagedCluster CR doesn't have integrations
# This combines both the name and owner as [managed_cluster_names]-[managed_cluster_owners] and saves it in var managed_cluster
- name: Combine managed_cluster_names and managed_cluster_owners into a list of strings
ansible.builtin.set_fact:
create_integration_for: "{{ managed_clusters | rejectattr('name', 'in', existing_integration_names) | list }}"
managed_clusters: >-
{{
managed_cluster_names
| zip(managed_cluster_owners)
| map('join', '-')
| list
}}

- name: Fetch current status of Config CR
kubernetes.core.k8s_info:
Expand All @@ -57,13 +78,10 @@
ansible.builtin.set_fact:
previous_managed_clusters: "{{ config_cr.resources[0].status.managedClusters | default([]) }}"

- name: Extract current ManagedCluster names
ansible.builtin.set_fact:
current_managed_clusters: "{{ managed_clusters | map(attribute='name') | list }}"

# Compares the Status field of Config CR with exsiting managed clusters in the clusters and then detemines which integrations to delete.
- name: Determine integrations to delete in Grafana Cloud
ansible.builtin.set_fact:
delete_integration_for: "{{ previous_managed_clusters | difference(current_managed_clusters) }}"
delete_integration_for: "{{ previous_managed_clusters | difference(managed_clusters) }}"

- name: Delete integrations and dashboards when there are integrations to delete
when: delete_integration_for | length > 0
Expand Down Expand Up @@ -120,6 +138,7 @@
loop_control:
loop_var: item

# This clears out the status field which was having existing managedCluster list
- name: Removing old ManagedCluster List
operator_sdk.util.k8s_status:
api_version: grafanacloud.stakater.com/v1alpha1
Expand All @@ -136,14 +155,28 @@
message: "Clears old managed clusters"
when: delete_integration_for

# This removes any duplications if found and stores the mangedCluster list with correct format [managed_cluster_name]-[managed_cluster_owner]
- name: Remove deleted integrations from managedClusters
ansible.builtin.set_fact:
updated_managed_clusters: >-
{{
config_cr.resources[0].status.managedClusters
| map('regex_replace', '^name: ', '')
| map('regex_replace', '-owner: ', '-')
| difference(delete_integration_for)
| unique
| list
}}
when: delete_integration_for | length > 0

- name: Update CR status for IntegrationsDeleted
operator_sdk.util.k8s_status:
api_version: grafanacloud.stakater.com/v1alpha1
kind: Config
name: "{{ cr_name }}"
namespace: "{{ cr_namespace }}"
status:
managedClusters: "{{ current_managed_clusters }}"
managedClusters: "{{ updated_managed_clusters }}"
conditions:
- lastTransitionTime: "{{ ansible_date_time.iso8601 }}"
status: "True"
Expand All @@ -160,7 +193,7 @@
kind: ManifestWork
metadata:
name: "{{ item.name }}-manifestwork-grafana-oncall"
namespace: "{{ item.name }}"
namespace: "{{ item.name | regex_replace('^(.+)-[^-]+$', '\\1') }}"
spec:
workload:
manifests:
Expand All @@ -169,11 +202,10 @@
metadata:
name: alertmanager-main
namespace: openshift-monitoring
loop: "{{ integrations_to_delete }}" # Ensure you have a loop here
loop: "{{ integrations_to_delete }}"
loop_control:
label: "{{ item.name }}"
when:
- delete_integration_for | length > 0
when: delete_integration_for | length > 0
register: manifestwork_deletion_results

- name: End play if any integrations failed or were skipped
Expand Down
Loading
Loading