From deaf79ba8d9550cc12ff32d335b620a232424e18 Mon Sep 17 00:00:00 2001 From: SurajGudaji Date: Tue, 5 Nov 2024 15:20:03 +0530 Subject: [PATCH] Create Playbook for for developing and testing sample helm based operator using operator-sdk Signed-off-by: SurajGudaji --- examples/ocp_operator_api-vars.yml | 10 +++ playbooks/main.yml | 3 + playbooks/ocp-operator-api.yml | 8 ++ playbooks/roles/ocp-operator-api/README.md | 59 +++++++++++++ .../roles/ocp-operator-api/defaults/main.yaml | 11 +++ .../roles/ocp-operator-api/tasks/main.yaml | 88 +++++++++++++++++++ .../roles/ocp-operator-api/vars/main.yaml | 3 + 7 files changed, 182 insertions(+) create mode 100644 examples/ocp_operator_api-vars.yml create mode 100644 playbooks/ocp-operator-api.yml create mode 100644 playbooks/roles/ocp-operator-api/README.md create mode 100644 playbooks/roles/ocp-operator-api/defaults/main.yaml create mode 100644 playbooks/roles/ocp-operator-api/tasks/main.yaml create mode 100644 playbooks/roles/ocp-operator-api/vars/main.yaml diff --git a/examples/ocp_operator_api-vars.yml b/examples/ocp_operator_api-vars.yml new file mode 100644 index 00000000..74fa4729 --- /dev/null +++ b/examples/ocp_operator_api-vars.yml @@ -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 }}" diff --git a/playbooks/main.yml b/playbooks/main.yml index 27127676..9de23558 100644 --- a/playbooks/main.yml +++ b/playbooks/main.yml @@ -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 diff --git a/playbooks/ocp-operator-api.yml b/playbooks/ocp-operator-api.yml new file mode 100644 index 00000000..097ea6c6 --- /dev/null +++ b/playbooks/ocp-operator-api.yml @@ -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 + \ No newline at end of file diff --git a/playbooks/roles/ocp-operator-api/README.md b/playbooks/roles/ocp-operator-api/README.md new file mode 100644 index 00000000..d0c85f5d --- /dev/null +++ b/playbooks/roles/ocp-operator-api/README.md @@ -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 +------------------ + +Suraj.Gudaji1@ibm.com diff --git a/playbooks/roles/ocp-operator-api/defaults/main.yaml b/playbooks/roles/ocp-operator-api/defaults/main.yaml new file mode 100644 index 00000000..6feab6ff --- /dev/null +++ b/playbooks/roles/ocp-operator-api/defaults/main.yaml @@ -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 }}" diff --git a/playbooks/roles/ocp-operator-api/tasks/main.yaml b/playbooks/roles/ocp-operator-api/tasks/main.yaml new file mode 100644 index 00000000..7624287b --- /dev/null +++ b/playbooks/roles/ocp-operator-api/tasks/main.yaml @@ -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 diff --git a/playbooks/roles/ocp-operator-api/vars/main.yaml b/playbooks/roles/ocp-operator-api/vars/main.yaml new file mode 100644 index 00000000..733394d2 --- /dev/null +++ b/playbooks/roles/ocp-operator-api/vars/main.yaml @@ -0,0 +1,3 @@ +operator_api_env: + PATH: "{{ ansible_env.PATH }}:/usr/local/bin/operator-sdk" + KUBECONFIG: "{{ ansible_env.HOME }}/.kube/config"