Skip to content

Commit

Permalink
Create Playbook for for developing and testing sample helm based oper…
Browse files Browse the repository at this point in the history
…ator using operator-sdk

Signed-off-by: SurajGudaji <[email protected]>
  • Loading branch information
SurajGudaji committed Nov 15, 2024
1 parent edfe340 commit deaf79b
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/ocp_operator_api-vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---

op_api_role_enable: false
build_operator_image: true
deploy_operator: false
OPERATOR_SDK_VERSION: "1.37.0"
op_api_dir: "/tmp/operator-api"
ocp_version: "418"
quay_username: ""
HELM_OP_IMAGE: "quay.io/{{quay_username}}/nginx-operator-helm:{{ ocp_version }}"
3 changes: 3 additions & 0 deletions playbooks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,6 @@

- import_playbook: ocp-etcd-rotate-encryption-key.yml
when: ocp_etcd_key_rotation_enable is defined and ocp_etcd_key_rotation_enable

- import_playbook: ocp-operator-api.yml
when: op_api_role_enable is defined and op_api_role_enable
8 changes: 8 additions & 0 deletions playbooks/ocp-operator-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Validate Operator development and installation
hosts: bastion
tasks:
- name: Validate Operator development and installation
include_role:
name: ocp-operator-api

59 changes: 59 additions & 0 deletions playbooks/roles/ocp-operator-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
ocp-cluster-observability-operator
=========

This role is used to validate operator development using operator-sdk.

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

- OCP 4.x healthy cluster on PowerVS.


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

| Variable | Required | Default | Comments |
|--------------------------------------------|----------|--------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|
| op_api_role_enable | no | false | Set it to true to run this playbook |
| op_cleanup | no | false | Set it to true to do operator cleanup. |
| build_operator_image | no | true | Set it to true to build the operator image |
| deploy_operator | no | false | Set it to true to deploy the operators |
| OPERATOR_SDK_VERSION | no | "1.37.0" | Version of operator-sdk to be installed. |
| op_api_dir | no | "/tmp/operator-api" | Directory where the operator workspace are intialized. |
| ocp_version | yes | | Openshift version on which this validation is run |
| quay_username | yes | | quay account username where the operator images will be pushed |




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

- Login to a quay.io account
```
docker login -u='' -p='' quay.io
```

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

```
---
- name: Validate Operator development and installation
hosts: bastion
tasks:
- name: Validate Operator development and installation
include_role:
name: ocp-operator-api
```


License
-------

See LICENCE.txt

Author Information
------------------

[email protected]
11 changes: 11 additions & 0 deletions playbooks/roles/ocp-operator-api/defaults/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---

op_api_role_enable: false
build_operator_image: true
deploy_operator: false
op_cleanup: false
OPERATOR_SDK_VERSION: "1.37.0"
op_api_dir: "/tmp/operator-api"
ocp_version: "418"
quay_username: ""
HELM_OP_IMAGE: "quay.io/{{quay_username}}/nginx-operator-helm:{{ ocp_version }}"
88 changes: 88 additions & 0 deletions playbooks/roles/ocp-operator-api/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---

- name: Check if cluster operators and nodes are healthy
include_role:
name: check-cluster-health

- name: Download operator-sdk
get_url:
url: "https://github.com/operator-framework/operator-sdk/releases/download/v{{ OPERATOR_SDK_VERSION }}/operator-sdk_linux_ppc64le"
dest: /usr/local/bin/operator-sdk
mode: '0755'

- name: Install required packages
package:
name:
- docker
state: present

- name: Build and push operator images to quay.io
block:
- name: Create working directory for helm based nginx operator
ansible.builtin.file:
path: "{{ op_api_dir }}/helm-op"
state: directory
mode: '0755'

- name: Intialize the helm based nginx operator project
command: operator-sdk init --plugins=helm --domain=helm.com --group=helm --version=v1 --kind=Nginx
args:
chdir: "{{ op_api_dir }}/helm-op"

- name: Build and push Nginx-helm operator image
command: make docker-build docker-push IMG="{{ HELM_OP_IMAGE }}"
args:
chdir: "{{ op_api_dir }}/helm-op"
when: build_operator_image

- name: Deploy and Validate operator
block:
- name: Deploy helm based nginx operator
command: make deploy IMG="{{ HELM_OP_IMAGE }}"
args:
chdir: "{{ op_api_dir }}/helm-op"

- name: Verify helm based nginx operator
shell: oc get pods -n helm-op-system --no-headers | grep -v "Running\|Completed" | wc -l
register: helm_nginx_pods
until: helm_nginx_pods.stdout|int == 0 and helm_nginx_pods.stderr == ""
retries: 10
delay: 10

- name: Create a instance of Nginx resource
command: oc create -f "{{ op_api_dir }}/helm-op/config/samples/helm_v1_nginx.yaml"
args:
chdir: "{{ op_api_dir }}/helm-op"

- name: Verify if instance of nginx operator is created sucessfully
shell: oc get pods --no-headers | grep -v "Running\|Completed" | grep nginx-sample |wc -l
register: nginx_instance_pods
until: nginx_instance_pods.stdout|int == 0 and nginx_instance_pods.stderr == ""
retries: 10
delay: 10
when: deploy_operator

- name: Cleanup
block:
- name: Delete Nginx instance
command: oc delete -f "{{ op_api_dir }}/helm-op/config/samples/helm_v1_nginx.yaml"
args:
chdir: "{{ op_api_dir }}/helm-op"

- name: Verify Nginx instance is deleted
shell: oc get Nginx nginx-sample --no-headers | wc -l
register: nginx_instance
until: nginx_instance.stdout|int == 0 and nginx_instance.stderr == ""
retries: 10
delay: 10

- name: Operator cleanup
command: make undeploy IMG="{{ HELM_OP_IMAGE }}"
args:
chdir: "{{ op_api_dir }}/helm-op"

- name: Delete the workspace
file:
path: "{{ op_api_dir }}"
state: "absent"
when: op_cleanup
3 changes: 3 additions & 0 deletions playbooks/roles/ocp-operator-api/vars/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
operator_api_env:
PATH: "{{ ansible_env.PATH }}:/usr/local/bin/operator-sdk"
KUBECONFIG: "{{ ansible_env.HOME }}/.kube/config"

0 comments on commit deaf79b

Please sign in to comment.