Skip to content

Commit

Permalink
Add cluster-compare role
Browse files Browse the repository at this point in the history
  • Loading branch information
betoredhat committed Jan 9, 2025
1 parent e2ac103 commit b43a359
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Name | Description
[redhatci.ocp.catalog_source](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/catalog_source/README.md) | A Role to deploy an OLM-based CatalogSource
[redhatci.ocp.chart_verifier](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/chart_verifier/README.md) | Executes the [chart-verifier](https://github.com/redhat-certification/chart-verifier) tool.
[redhatci.ocp.check_resource](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/check_resource/README.md) | Role to wait for the deployment of a given resource
[redhatci.ocp.cluster_compare](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/cluster_compare/README.md) | A role to facilitate the comparison of Kubernetes cluster configurations by using the kube-compare tool.
[redhatci.ocp.configure_ztp_gitops_apps](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/configure_ztp_gitops_apps/README.md) | Connects the OpenShift Gitops Operator to a remote Git repository to pull the SiteConfig and PolicyGenTemplate manifests.
[redhatci.ocp.configure_ztp_gitops_repo](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/configure_ztp_gitops_repo/README.md) | Pushes the SiteConfig and PolicyGenTemplate manifests to the Git repository, thus triggering the Spoke Cluster deployment process.
[redhatci.ocp.conserver](https://github.com/redhatci/ansible-collection-redhatci-ocp/blob/main/roles/conserver/README.md) | Setup of [conserver](https://www.conserver.com/) to log serial console.
Expand Down
37 changes: 37 additions & 0 deletions roles/cluster_compare/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Cluster Compare

A role to facilitate the comparison of Kubernetes cluster configurations by using the kube-compare tool. It leverages reference metadata and templates from specified container or repositories.

Features

- Clones or pulls reference metadata and templates from containers or Git repositories.
- Executes the kube-compare tool for detailed cluster configuration analysis.
- Supports both container-based and source-based workflows.

## Requirements

- Ensure a valid KUBECONFIG file is accessible. The KUBECONFIG file path must be provided as an environment variable.

## Variables

| Variable Name | Default Value | Description |
|-------------------------------------- |----------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------|
| `cc_reference_container_source` | "" | Pullspec for the container that holds the reference `metadata.yaml` and templates. |
| `cc_reference_repository` | "" | Git repository to clone for obtaining the reference `metadata.yaml` and templates. |
| `cc_reference_branch` | "main" | Branch of the reference repository to clone for obtaining the reference. |
| `cc_reference_metadata_yaml_location` | "/path/to/metadata.yaml" | Path from the reference repository root or container root to the reference `metadata.yaml`. |
| `cc_compare_container_source` | "registry-proxy.engineering.redhat.com/rh-osbs/openshift-kube-compare-artifacts:v4.18" | Pullspec for the kube-compare tool container. Leave empty to build from source. |
| `cc_compare_container_executable` | "/usr/share/openshift/linux_amd64/kube-compare.rhel8" | Path within the container to extract the `kubectl-compare_cluster` executable. |
| `cc_compare_repository` | "https://github.com/openshift/kube-compare.git" | URL of the cluster-compare command source code repository to build from source. |
| `cc_compare_branch` | "main" | Branch of the compare repository used when building from source. |
| `cc_report_creator_version` | "latest" | Version of reports-creator to install, must match a tag or branch in the `kube-compare` repository. |
| `cc_logging` | true | Controls task's log output. |

Notes:
- Main work is for upstream version, some draft work for containers base is in place

This is still a WIP:
- Get reference manifest according cluster version
- Copy results to log_dir
- Complete the integration with DOA
- Work on support for containers base comparsion
11 changes: 11 additions & 0 deletions roles/cluster_compare/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
cc_reference_container_source: "quay.io/openshift-kni/ztp-site-generator:4.18"
cc_reference_repository: ""
cc_reference_branch: "main"
cc_reference_metadata_yaml_location: "/home/ztp/reference"
cc_compare_container_source: "registry-proxy.engineering.redhat.com/rh-osbs/openshift-kube-compare-artifacts:v4.18"
cc_compare_container_executable: "/usr/share/openshift/linux_amd64/kube-compare.rhel8"
cc_report_creator_version: "latest"
cc_ocp_supported: 4.14
cc_logging: true
...
70 changes: 70 additions & 0 deletions roles/cluster_compare/tasks/containers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---

# - name: Extract cluster-compare tool from container
# when: cc_compare_container_source | length > 0
# block:
# - name: Extract cluster-compare tool from container
# ansible.builtin.shell: |
# podman pull {{ cc_compare_container_source }}
# podman rm -fi ofe-cc-kca
# podman cp $(podman create --name ofe-cc-kca {{ cc_compare_container_source }}):{{ cc_compare_container_executable }} kubectl-cluster_compare
# podman rm ofe-cc-kca
# args:
# chdir: "{{ cc_work_dir }}"

# - name: Remove temporary container
# ansible.builtin.command:
# cmd: "podman rm -fi ofe-cc-kca"

# - name: Extract reference metadata from container
# when: cc_reference_container_source | length > 0
# block:
# - name: Pull container image
# containers.podman.podman_image:
# name: "{{ cc_reference_container_source }}"

# - name: Extract metadata from container
# ansible.builtin.shell: |
# podman rm -f ofe-cc-ref
# podman cp $(podman create --name ofe-cc-ref {{ cc_reference_container_source }}):{{ cc_reference_metadata_yaml_location }} {{ cc_work_dir }}/bin/

# # - name: Clone reference metadata from Git
# # when: cc_reference_container_source | length == 0
# # block:
# # - name: Ensure reference directory is cleaned
# # ansible.builtin.file:
# # path: "{{ cc_reference_dir }}"
# # state: absent

# # - name: Clone reference repository
# # ansible.builtin.git:
# # repo: "{{ cc_reference_repository }}"
# # dest: "{{ cc_reference_dir }}"
# # version: "{{ cc_reference_branch }}"

# - name: Run cluster-compare command
# ansible.builtin.shell: >
# ./bin/kubectl-cluster_compare -v
# -r reference/metadata.yaml
# -o json > {{ cc_compare_output_file }}
# args:
# chdir: "{{ cc_work_dir }}"
# ignore_errors: true
# register: compare_status

# - name: Generate human-readable cluster-compare report
# when: compare_status.rc == 1
# ansible.builtin.shell: >
# kubectl-cluster_compare
# -r reference/metadata.yaml
# 2>/dev/null || true
# args:
# chdir: "{{ cc_work_dir }}"

# - name: Generate comparison report
# ansible.builtin.shell: >
# report-creator -j {{ cc_compare_output_file }}
# -o {{ cc_report_name }}
# args:
# chdir: "{{ cc_work_dir }}"
...
26 changes: 26 additions & 0 deletions roles/cluster_compare/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
- name: Test with Upstream
ansible.builtin.include_tasks: validations.yml

- name: Create working directory
ansible.builtin.tempfile:
state: directory
prefix: cluster_compare
register: _cc_tmp_dir

- name: Set work directory path
ansible.builtin.set_fact:
cc_work_dir: "{{ _cc_tmp_dir.path }}"

- name: Test with Upstream
ansible.builtin.include_tasks: upstream.yml

# - name: Test with Upstream
# ansible.builtin.include_tasks: containers.yml

- name: Remove working directory
ansible.builtin.file:
path: "{{ cc_work_dir }}"
state: absent
when: cc_work_dir is defined
...
90 changes: 90 additions & 0 deletions roles/cluster_compare/tasks/upstream.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
- name: Get the latest tools release tag
ansible.builtin.uri:
url: "https://api.github.com/repos/openshift/kube-compare/releases/latest"
method: GET
return_content: true
status_code: 200
register: _cc_github_response
no_log: "{{ cc_logging }}"

- name: Download and extract binary files
vars:
cc_version: "{{ _cc_github_response.json.tag_name | default('v0.6.0', true) }}"
cc_base_repo: https://github.com/openshift/kube-compare/releases/download
cc_bins:
- kube-compare_addon_tools_linux_amd64.tar.gz
- kube-compare_linux_amd64.tar.gz
ansible.builtin.unarchive:
src: "{{ cc_base_repo }}/{{ cc_version }}/{{ binary }}"
dest: "{{ cc_work_dir }}"
remote_src: true
mode: '0755'
register: _cc_extract
retries: 1
delay: 10
until: _cc_extract is not failed
loop_control:
loop_var: binary
loop: "{{ cc_bins }}"
no_log: "{{ cc_logging }}"

- name: Get cluster version
community.kubernetes.k8s_info:
api: config.openshift.io/v1
kind: ClusterVersion
name: version
register: _cc_cluster_version
no_log: "{{ cc_logging }}"

- name: Set cluster version
vars:
current_ver_query: "history[?state=='Completed'] | [0].version"
full_ver: "{{ _cc_cluster_version.resources[0].status | json_query(current_ver_query) }}"
current_ver: "{{ full_ver | regex_search('^\\d+\\.\\d+') }}"
ansible.builtin.set_fact:
cc_ocp_version: "{{ current_ver }}"

- name: Validate if the Cluster Version is supported
ansible.builtin.fail:
msg: "OCP version must be >= {{ cc_ocp_supported }} to run cluster compare"
when: cc_ocp_version is version( cc_ocp_supported , "<")

- name: Run cluster compare using manifest for current OCP {{ cc_ocp_version }}
vars:
cc_reference:
"https://raw.githubusercontent.com/openshift-kni/telco-reference/refs/heads/release-{{ cc_ocp_version }}/\
telco-core/configuration/reference-crs-kube-compare/metadata.yaml"
block:
- name: Run cluster-compare for using manifest for OCP-{{ cc_ocp_version }}
ansible.builtin.shell: >
./kubectl-cluster_compare -v
-r {{ cc_reference }}
-o json > {{ cc_reports_dir }}/{{ cc_compare_output_file }}
args:
chdir: "{{ cc_work_dir }}"
ignore_errors: true
register: _cc_cmd_output
changed_when: _cc_cmd_output.rc != 0

- name: Generate human-readable cluster-compare report
ansible.builtin.shell: >
./kubectl-cluster_compare
-r {{ cc_reference }}
> {{ cc_reports_dir }}/{{ cc_compare_human_file }}
2>/dev/null || true
args:
chdir: "{{ cc_work_dir }}"
register: _cc_cmd_output
changed_when: _cc_cmd_output.rc != 0
when: _cc_cmd_output.rc == 1

- name: Generate JUnit report
ansible.builtin.shell: >
./report-creator -j {{ cc_reports_dir }}/{{ cc_compare_output_file }}
-o {{ cc_reports_dir }}/{{ cc_report_name }}
args:
chdir: "{{ cc_work_dir }}"
register: _cc_cmd_output
changed_when: _cc_cmd_output.rc != 0
...
17 changes: 17 additions & 0 deletions roles/cluster_compare/tasks/validations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: Ensure report directory is defined
ansible.builtin.fail:
msg: "The variable 'cc_reports_dir' is not defined."
when: cc_reports_dir is not defined

- name: Check if the directory exists
ansible.builtin.stat:
path: "{{ cc_reports_dir }}"
register: _cc_dir_stat
when: cc_reports_dir is defined

- name: Fail if the report directory does not exist
ansible.builtin.fail:
msg: "The directory to save the reports '{{ cc_reports_dir }}' does not exist."
when: cc_reports_dir is defined and not _cc_dir_stat.stat.exists
...
11 changes: 11 additions & 0 deletions roles/cluster_compare/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
cc_command_directory: "cluster-compare"
cc_go_installation_dir: "go"
cc_go_path: "gopath"
cc_reference_dir: "reference"
cc_report_name: "cluster-compare-report.xml"
cc_compare_output_file: "cluster-compare.json"
cc_compare_human_file: "cluster-compare.txt"
cc_spoke_cluster_name_param: "${params.SPOKE_CLUSTER_NAME}"
cc_kubeconfig: "${HOME}/clusterconfigs/${cc_spoke_cluster_name}/auth/kubeconfig"
...

0 comments on commit b43a359

Please sign in to comment.