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

Automation code for Cost management operator #81

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 12 additions & 1 deletion examples/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,15 @@ coo_namespace: "openshift-observability-operator"
coo_catalogsource_name: "coo-catalog"
coo_catalogsource_image: "brew.registry.redhat.io/rh-osbs/iib:760399"
coo_channel: "development"

Pavan-Sadhana3 marked this conversation as resolved.
Show resolved Hide resolved

# ocp-cost_management vars
cost_management_enabled: false
cost_management_directory: "/tmp/compliance"
cost_management_catalogsource_image: "" # "brew.registry.redhat.io/rh-osbs/iib:327237"
cost_management_upgrade_channel: "stable"
cost_management_e2e: false
cost_management_e2e_github_repo: ""
cost_management_e2e_github_branch: ""
cost_management_github_username: ""
cost_management_github_token: ""
cost_management_cleanup: true
Pavan-Sadhana3 marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 16 additions & 0 deletions examples/ocp_cost_management_vars.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
# ocp-cmo vars
cost_management_enabled: false
cost_management_directory: "/tmp/compliance"
cost_management_catalogsource_image: "" # "brew.registry.redhat.io/rh-osbs/iib:327237"
cost_management_upgrade_channel: "stable"
cost_management_e2e: false
cost_management_e2e_github_repo: ""
cost_management_e2e_github_branch: ""
cost_management_github_username: ""
cost_management_github_token: ""
cost_management_cleanup: true
cost_management_setup: false
cost_management_test: false
openshift_console_password: ""

7 changes: 7 additions & 0 deletions playbooks/ocp-cost-management.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Installation of the Compliance Operator and run e2e
hosts: bastion
tasks:
- name: Deploy Cost Management Operator
include_role:
name: ocp-cost-management
Pavan-Sadhana3 marked this conversation as resolved.
Show resolved Hide resolved
103 changes: 103 additions & 0 deletions playbooks/roles/ocp-cost-management/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
ocp-cost-management-operator
=========

This role is used to deploy Cost Management Operator.

Requirements
------------

- OCP 4.x healthy cluster on PowerVS.
- Need to connect to Sgnvpn and Redhat VPN on your machine.

Role Variables
--------------

| Variable | Required | Default | Comments |
|--------------------------------------------|----------|--------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|
| cost_management_enabled | yes | false | |
| cost_management_directory | yes | /tmp/compliance | cost management temporary directory |
| cost_management_catalogsource_image | yes | "" | index image build id |
| cost_management_upgrade_channel | no | stable | channel |
| cost_management_e2e | yes | false | |
| cost_management_e2e_github_repo | yes |"" | |
| cost_management_e2e_github_branch | yes |master | | |
| cost_management_github_username | yes |"" | |
| cost_management_github_token | yes |"" | |
| cost_management_cleanup | yes | true | to run cleanup playbook |
| cost_management_setup | yes | false | to run cost management setup |
| cost_management_test | yes | false | to run cost management test |
| openshift_console_password | yes | "" | oc login command password |

Copy link
Contributor

@SurajGudaji SurajGudaji Oct 22, 2024

Choose a reason for hiding this comment

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

add default values used for all the variables specified in README file

Copy link
Author

Choose a reason for hiding this comment

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

done


Dependencies
------------

- None


Example Playbook
----------------

```
---
- name: Installation of the cost management operator and run e2e
hosts: bastion
roles:
- ocp-cost-management
```

Execution process
-----------------

there are 4 playbooks available on tasks folder
- main.yml
- cmo-setup.yml
- cmo-test.yml
- cmo-cleanup.yml

**var filepath:**
examples/ocp_cost_management_vars.yaml

**main.yml**
This is the main yaml file which is used to install,verify and validate cost managemenent operator.

**cmo-setup.yml**
This file is responsible for install iqe cost management plugin and dependencies on your machine,
inorder to execute this we have to enable the cost_management_setup in var file.
`cost_management_setup: true`

**cmo-test.yml**
After plugin installation is successful,we have to execute this file to run tests.
inorder to execute this we have to enable the cost_management_test in var file.
`cost_management_test: true`
Also we have to pass the kubeadmin password to this variable `openshift_console_password` in var file.

**cmo-cleanup.yml**
This file is use to cleanup all resources involved with this operator.
inorder to execute this we have to enable the cost_management_cleanup in var file.
`cost_management_cleanup: true`

Run Playbook
------------

To set up variables for specific playbook make use of the sample file at examples/<playbook_vars.yaml>. Modify the values as per your cluster. For more information refer to examples.

```
cp examples/ocp_cost_management_vars.yaml
```
Once you have configured the vars & inventory file, run the specific playbook using:

```
ansible-playbook -i inventory -e @ocp_cost_management_vars.yaml playbooks/main.yml
```


License
-------

See LICENCE.txt

Author Information
------------------
Pavan-Sadhana3 marked this conversation as resolved.
Show resolved Hide resolved

[email protected]
15 changes: 15 additions & 0 deletions playbooks/roles/ocp-cost-management/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
# defaults file for playbooks/roles/ocp-cost-management
cost_management_enabled: false
cost_management_directory: "/tmp/costmanagement"
cost_management_catalogsource_image: "" # "brew.registry.redhat.io/rh-osbs/iib:327237"
cost_management_upgrade_channel: "stable"
cost_management_e2e: false
cost_management_e2e_github_repo: ""
cost_management_e2e_github_branch: "master"
cost_management_github_username: ""
cost_management_github_token: ""
cost_management_cleanup: true
cost_management_setup: false
cost_management_test: false
openshift_console_password: ""
16 changes: 16 additions & 0 deletions playbooks/roles/ocp-cost-management/files/run-e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
re_run_count=0
max_re_runs=1
echo -e "\ne2e summary:" > $COST_MANAGEMENT_WORKDIR/e2e-summary.txt
cat "${COST_MANAGEMENT_WORKDIR}/e2e-output.txt" | tail -n 1 >> $COST_MANAGEMENT_WORKDIR/e2e-summary.txt
sed -e 's/\"/\\"/g;s/.*/\"&\"/' "${COST_MANAGEMENT_WORKDIR}/e2e-output.txt" | awk '/Failing tests:/,EOF' | tail -n +3 | head -n -2 > $COST_MANAGEMENT_WORKDIR/failed_tests.txt
while [ $re_run_count -lt $max_re_runs ] && [ "0" -ne "$(wc -l < "${COST_MANAGEMENT_WORKDIR}/failed_tests.txt")" ]
do
re_run_count=$(( $re_run_count + 1 ))
./bin/extended-platform-tests run -f $COST_MANAGEMENT_WORKDIR/failed_tests.txt -o $COST_MANAGEMENT_WORKDIR/e2e-re-run-output-${re_run_count}.txt
sed -e 's/\"/\\"/g;s/.*/\"&\"/' "${COST_MANAGEMENT_WORKDIR}/e2e-re-run-output-${re_run_count}.txt" | awk '/Failing tests:/,EOF' | tail -n +3 | head -n -2 > $COST_MANAGEMENT_WORKDIR/failed_tests.txt
done
if [ $re_run_count -ne 0 ]; then
echo -e "\n\ne2e summary after re-running:" >> $COST_MANAGEMENT_WORKDIR/e2e-summary.txt
cat $COST_MANAGEMENT_WORKDIR/failed_tests.txt >> $COST_MANAGEMENT_WORKDIR/e2e-summary.txt
cat "${COST_MANAGEMENT_WORKDIR}/e2e-re-run-output-${re_run_count}.txt" | tail -n 2 >> $COST_MANAGEMENT_WORKDIR/e2e-summary.txt
fi
141 changes: 141 additions & 0 deletions playbooks/roles/ocp-cost-management/tasks/cmo-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---
# this file is used for cleaning up the compliance operator resources

- name: Check if ScanSettingBinding exists
shell: oc get ScanSettingBinding -n costmanagement-metrics-operator --no-headers | awk '{print $1}'
register: scan_setting_binding

- name: Delete the ScanSettingBinding if exists
kubernetes.core.k8s:
api_version: compliance.openshift.io/v1alpha1
state: absent
kind: ScanSettingBinding
name: "{{ item }}"
namespace: costmanagement-metrics-operator
loop: "{{ scan_setting_binding.stdout_lines }}"
when: scan_setting_binding.stdout_lines|length > 0

- name: Check if ScanSetting exists
shell: oc get ScanSetting -n costmanagement-metrics-operator --no-headers | awk '{print $1}'
register: scan_setting

- name: Delete the ScanSetting if exists
kubernetes.core.k8s:
api_version: compliance.openshift.io/v1alpha1
state: absent
kind: ScanSetting
name: "{{ item }}"
namespace: costmanagement-metrics-operator
loop: "{{ scan_setting.stdout_lines }}"
when: scan_setting.stdout_lines|length > 0

- name: Check if ComplianceSuite exists
shell: oc get ComplianceSuite -n costmanagement-metrics-operator --no-headers | awk '{print $1}'
register: compliance_suite

- name: Delete the ComplianceSuite if exists
kubernetes.core.k8s:
api_version: compliance.openshift.io/v1alpha1
state: absent
kind: ComplianceSuite
name: "{{ item }}"
namespace: costmanagement-metrics-operator
loop: "{{ compliance_suite.stdout_lines }}"
when: compliance_suite.stdout_lines|length > 0

- name: Get the name of cluster service version for Compliance Operator
shell: oc get csv -n costmanagement-metrics-operator --no-headers | awk '{ if ($1 ~ /^compliance-operator/) print $1 }'
register: compliance_csv

- name: Delete the profile bundle if exists
kubernetes.core.k8s:
api_version: compliance.openshift.io/v1alpha1
state: absent
kind: ProfileBundle
name: "{{ item }}"
namespace: costmanagement-metrics-operator
loop:
- "ocp4"
- "rhcos4"
when: compliance_csv.stdout|length > 0

- name: Get the subscription name
shell: oc get subscription -n costmanagement-metrics-operator --no-headers | awk '{print $1}'
register: compliance_subscription

- name: Delete the subscription if exists
kubernetes.core.k8s:
api_version: operators.coreos.com/v1alpha1
state: absent
kind: Subscription
name: "{{ compliance_subscription.stdout }}"
namespace: costmanagement-metrics-operator
when: compliance_subscription.stdout|length > 0

- name: Get the operator group name
shell: oc get operatorgroup -n costmanagement-metrics-operator --no-headers | awk '{print $1}'
register: compliance_operator_group

- name: Delete the operator group if exists
kubernetes.core.k8s:
api_version: operators.coreos.com/v1
state: absent
kind: OperatorGroup
name: "{{ compliance_operator_group.stdout }}"
namespace: costmanagement-metrics-operator
when: compliance_operator_group.stdout|length > 0

- name: Delete the cluster service version if exists
kubernetes.core.k8s:
api_version: operators.coreos.com/v1alpha1
state: absent
kind: ClusterServiceVersion
name: "{{ compliance_csv.stdout }}"
namespace: costmanagement-metrics-operator
when: compliance_csv.stdout|length > 0

- name: Check if all the pods has been deleted
shell: oc get pods -n costmanagement-metrics-operator --no-headers
register: compliance_pods
until: compliance_pods.stdout|int == 0
retries: 15
delay: 60

- name: Get custom resource definitions
shell: oc get crd | awk '{print $1}'| grep compliance
register: compliance_crds
ignore_errors: true

- name: Delete the custom resource definition if exists
shell: oc delete crd {{ item }}
loop: "{{ compliance_crds.stdout_lines }}"
when: compliance_crds.stdout_lines|length > 0

- name: Check if all the custom resource definitions has been deleted
shell: oc get crd | awk '{print $1}'| grep compliance
register: compliance_crds
failed_when: compliance_crds.stdout_lines|length != 0

- name: Delete the Cost Management Operator if exists
kubernetes.core.k8s:
state: absent
kind: Operator
name: cost-management-operator.costmanagement-metrics-operator

- name: Delete the namespace if exists
kubernetes.core.k8s:
state: absent
kind: Namespace
name: costmanagement-metrics-operator

- name: Check if the namespace gets deleted
shell: oc get namespace --no-headers | grep compliance | wc -l
register: compliance_namespace
until: compliance_namespace.stdout|int == 0
retries: 15
delay: 30

- name: Delete the workplace
file:
state: absent
path: "{{ cost_management_directory }}"
59 changes: 59 additions & 0 deletions playbooks/roles/ocp-cost-management/tasks/cmo-setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
- name: Setup and Test Cost Management Metrics Operator
tasks:
- name: Create workspace directory
file:
path: "~/iqe-workspace-dev"
state: directory

- name: Clone IQE Core repository
git:
repo: "[email protected]:insights-qe/iqe-core.git"
dest: "~/iqe-workspace-dev/iqe-core"

- name: Create Python virtual environment
command: python3 -m venv .iqe-venv
args:
chdir: "~/iqe-workspace-dev/iqe-core"

- name: Activate virtual environment and install dependencies
shell: |
source .iqe-venv/bin/activate
pip install -U pip setuptools wheel
args:
chdir: "~/iqe-workspace-dev/iqe-core"

- name: Set PIP global index URL
shell: |
pip config set global.index-url https://repository.engineering.redhat.com/nexus/repository/insights-qe/simple --site
pip config set global.index https://repository.engineering.redhat.com/nexus/repository/insights-qe-search/pypi --site
args:
chdir: "~/iqe-workspace-dev/iqe-core"

- name: Install IQE core with constraints
shell: |
source .iqe-venv/bin/activate
pip install -e . -c iqe/data/all_external_constraints.linux.python3.11.txt
args:
chdir: "~/iqe-workspace-dev/iqe-core"

- name: Create plugin directory
file:
path: "~/iqe-workspace-dev/iqe-cost-management-plugin"
state: directory

- name: Check if IQE Cost Management Plugin is already cloned
stat:
path: ~/iqe-workspace-dev/iqe-cost-management-plugin/.git
register: git_repo

- name: Clone IQE Cost Management Plugin with SSL verification disabled
shell: git -c http.sslVerify=false clone https://gitlab.cee.redhat.com/insights-qe/iqe-cost-management-plugin ~/iqe-workspace-dev/iqe-cost-management-plugin
when: not git_repo.stat.exists

- name: Install IQE Cost Management Plugin
shell: |
source .iqe-venv/bin/activate
iqe plugin install --editable .
args:
chdir: "~/iqe-workspace-dev/iqe-cost-management-plugin"
Loading