From e3d785b0b1b414da6b784f9e4163ed17b478eff3 Mon Sep 17 00:00:00 2001 From: kpavic Date: Mon, 8 Mar 2021 14:20:39 +0100 Subject: [PATCH] Add a playbook for parsing collected image pull events * Add the DPTP shell script for collecting pull events * Add the option to use pre-collected image pull events * Add the playbook for CVP usage --- parse-collected-images.yml | 18 +++++++++++ .../collect_modified_images/defaults/main.yml | 1 + ...onal-operators-cvp-common-test-commands.sh | 13 ++++++++ roles/collect_modified_images/tasks/main.yml | 30 ++++++++++++------- 4 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 parse-collected-images.yml create mode 100644 roles/collect_modified_images/files/optional-operators-cvp-common-test-commands.sh diff --git a/parse-collected-images.yml b/parse-collected-images.yml new file mode 100644 index 000000000..66154bfce --- /dev/null +++ b/parse-collected-images.yml @@ -0,0 +1,18 @@ +--- +- name: "Parse the collected image pull events" + hosts: all + become: false + gather_facts: false + + vars: + run_upstream: false + operator_work_dir: "/home/jenkins/agent/test-operator" + work_dir: "{{ lookup('env', 'WORKSPACE') }}" + collect_pull_events: false + + tasks: + - name: "Parse the collected image pull events" + include_role: + name: collect_modified_images + vars: + events_file_path: "{{ work_dir }}/image_pull_events.json" \ No newline at end of file diff --git a/roles/collect_modified_images/defaults/main.yml b/roles/collect_modified_images/defaults/main.yml index fa8fe9833..f8ab382ef 100644 --- a/roles/collect_modified_images/defaults/main.yml +++ b/roles/collect_modified_images/defaults/main.yml @@ -1,3 +1,4 @@ pulled_images: [] events_file_path: /tmp/events.json deployment_start_time: 1970-01-01T00:00:00Z +collect_pull_events: true \ No newline at end of file diff --git a/roles/collect_modified_images/files/optional-operators-cvp-common-test-commands.sh b/roles/collect_modified_images/files/optional-operators-cvp-common-test-commands.sh new file mode 100644 index 000000000..011b50838 --- /dev/null +++ b/roles/collect_modified_images/files/optional-operators-cvp-common-test-commands.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -o nounset +set -o errexit +set -o pipefail + +# Collect all image pull events from the testing cluster +# The output will contain the timestamp and the event message +# in a comma-separated value format + +PULL_EVENTS_ART="${ARTIFACT_DIR}/image_pull_events.csv" +echo "Collecting all image pull events from the testing cluster in ${PULL_EVENTS_ART}" +oc get events --all-namespaces --field-selector reason==Pulling -o go-template='{{range .items}}{{.lastTimestamp}},{{.message}}{{"\n"}}{{end}}' > "${PULL_EVENTS_ART}" \ No newline at end of file diff --git a/roles/collect_modified_images/tasks/main.yml b/roles/collect_modified_images/tasks/main.yml index 85692878d..22cf9ba41 100644 --- a/roles/collect_modified_images/tasks/main.yml +++ b/roles/collect_modified_images/tasks/main.yml @@ -13,25 +13,33 @@ # should be run immediately after the deployment, there should be no need to filter # images pulled after the deployment. +- name: "Set the output CSV file location" + set_fact: + image_pull_events: "{{ work_dir }}/image_pull_events.csv" + when: image_pull_events is not defined - -- name: "Generate events.json" - shell: "{{ oc_bin_path }} {% raw %}get events --all-namespaces --field-selector reason==Pulling -o go-template='{{range .items}}{{.lastTimestamp}},{{.message}}{{\"\\n\"}}{{end}}'{% endraw %}" +- name: "Collect the image pull events from the cluster in a CSV format" + shell: "{{ lookup('file', 'optional-operators-cvp-common-test-commands.sh') }}" environment: KUBECONFIG: "{{ kubeconfig_path }}" - register: events + ARTIFACT_DIR: "{{ work_dir }}" + when: collect_pull_events|bool + +- name: "Read the resulting file and split it by lines" + set_fact: + image_pull_events_lines: "{{ lookup('file', image_pull_events).splitlines() }}" -- name: "get list of images in events.json" +- name: "Filter the list of images in the image_pull_events.csv by timestamp" include_tasks: parse_image_names.yml with_items: - - "{{ events.stdout_lines }}" + - "{{ image_pull_events_lines }}" -- name: "Print images" +- name: "Print filtered images" debug: msg: "pulled images: {{ pulled_images }}" -- name: "Save images to file" - module: copy - content: "{{ pulled_images | to_json }}" - dest: "{{ events_file_path }}" +- name: "Save images to an output json file" + copy: + content: "{{ pulled_images | to_json }}" + dest: "{{ events_file_path }}" delegate_to: localhost