Skip to content

Commit

Permalink
Merge pull request #583 from redhatci/rem_ztp_by_ref
Browse files Browse the repository at this point in the history
Add delete-ztp-by-reference action
  • Loading branch information
betoredhat authored Mar 6, 2025
2 parents dacb81c + 01e1c27 commit 3e25159
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
35 changes: 34 additions & 1 deletion roles/acm_spoke_mgmt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The following variable controls the action that can be performed within this rol

Name | Type | Required | Default | Description
---------------------------- | ------ | -------- | -------------------------------------------------- | ------------------------------------------------------------
asm_action | string | yes | - | Action to be performed. Accepted values are `detach` and `attach`.
asm_action | string | yes | - | Action to be performed. Accepted values are `detach`, `attach`, and `delete-ztp-by-ref`.

## Detach a spoke cluster

Expand Down Expand Up @@ -73,4 +73,37 @@ asm_cluster_name | string | yes | -
asm_action: "attach"
asm_cluster_kubeconfig_path: "/path/to/spoke/kubeconfig"
asm_cluster_name: "mycluster"

## Remove ZTP ArgoCD resources

This action allows to remove the ArgoCD resources used to deploy a ZTP cluster. This could remove all the created resources in cascading. The role locates the ArgoCD applications used to create a cluster using the GitOps repository a source branch as references.

Two resources are deleted by this role.

- Application - Cluster
- Application - Policies

### Requirements

* A `KUBECONFIG` environment variable pointing to the management cluster's kubeconfig file.

### Role Variables

Name | Type | Required | Default | Description
---------------------------- | ------ | -------- | -------------------------------------------------- | ------------------------------------------------------------
asm_source_repo | string | yes | - | GitOps repository that was used to deploy the ZTP cluster
asm_target_revision | string | yes | - | Branch used to deploy the ZTP cluster
asm_delete_ztp_resources | boolean| yes | true | Deletes the ArgoCD applications and all the related cluster deployments resources

### Example

```yaml
- name: Remove a ZTP managed cluster from ArgoCD
ansible.builtin.include_role:
name: redhatci.ocp.acm_spoke_mgmt
vars:
asm_action: "delete-ztp-by-ref"
asm_cluster_kubeconfig_path: "/path/to/spoke/kubeconfig"
asm_source_repo: "http://<gitops-repository>/gituser/gitops"
asm_target_revision: main
```
2 changes: 2 additions & 0 deletions roles/acm_spoke_mgmt/defaults/main.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
---
asm_delete_ztp_resources: true
...
73 changes: 73 additions & 0 deletions roles/acm_spoke_mgmt/tasks/delete-ztp-by-ref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
- name: Assert the required variables are defined
ansible.builtin.assert:
that:
- asm_source_repo is defined
- asm_source_repo | length > 0
- asm_target_revision is defined
- asm_target_revision | length > 0

- name: Get all ArgoCD Applications
kubernetes.core.k8s_info:
api_version: argoproj.io/v1alpha1
kind: Application
namespace: openshift-gitops
register: _asm_all_apps
no_log: true

- name: Delete ArgoCD Applications
vars:
app_sites: "{{ _asm_all_apps.resources | selectattr('spec.source.repoURL', 'equalto', asm_source_repo)
| selectattr('spec.source.path', 'equalto', 'sites')
| selectattr('spec.source.targetRevision', 'equalto', asm_target_revision)
| map(attribute='metadata.name') | list }}"
app_policies: "{{ _asm_all_apps.resources | selectattr('spec.source.repoURL', 'equalto', asm_source_repo)
| selectattr('spec.source.path', 'equalto', 'policies')
| selectattr('spec.source.targetRevision', 'equalto', asm_target_revision)
| map(attribute='metadata.name') | list }}"
apps_list: "{{ app_sites + app_policies | default([]) }}"
app_ns: openshift-gitops
when:
- apps_list | length > 0
block:
- name: Patch ArgoCD application finalizers
kubernetes.core.k8s:
api_version: argoproj.io/v1alpha1
kind: Application
name: "{{ app }}"
namespace: "{{ app_ns }}"
definition:
metadata:
finalizers:
- "resources-finalizer.argocd.argoproj.io"
loop: "{{ apps_list }}"
loop_control:
loop_var: app
when:
- asm_delete_ztp_resources | bool

- name: Delete Argo Applications
kubernetes.core.k8s:
state: absent
api_version: argoproj.io/v1alpha1
kind: Application
name: "{{ app }}"
namespace: "{{ app_ns }}"
loop: "{{ apps_list }}"
loop_control:
loop_var: app

- name: Wait for ArgoCD application deletion
kubernetes.core.k8s_info:
api_version: argoproj.io/v1alpha1
kind: Application
name: "{{ app }}"
namespace: "{{ app_ns }}"
register: _asm_app_status
until: _asm_app_status.resources | length == 0
retries: 120
delay: 10
loop: "{{ apps_list }}"
loop_control:
loop_var: app
...
5 changes: 5 additions & 0 deletions roles/acm_spoke_mgmt/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
asm_actions:
- detach
- attach
- delete-ztp-by-ref
ansible.builtin.assert:
that:
- asm_action | lower in asm_actions
Expand All @@ -20,4 +21,8 @@
when:
- asm_action == 'attach'

- name: Delete a ZTP deployment by references
ansible.builtin.include_tasks: delete-ztp-by-ref.yaml
when:
- asm_action == 'delete-ztp-by-ref'
...

0 comments on commit 3e25159

Please sign in to comment.