diff --git a/.gitignore b/.gitignore index def13cb..77c640e 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,7 @@ molecule/test-local/.kube/ testing/ # vscode settings -.vscode/ \ No newline at end of file +.vscode/ + +# pyenv ignore +.python-version diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml index bacbabf..1b6fe5b 100644 --- a/molecule/default/playbook.yml +++ b/molecule/default/playbook.yml @@ -6,6 +6,5 @@ ansible_python_interpreter: '{{ ansible_playbook_python }}' roles: - setup - - collect - import_playbook: '{{ playbook_dir }}/asserts.yml' diff --git a/molecule/test-cluster/converge.yml b/molecule/test-cluster/converge.yml index 15fb355..9faae17 100644 --- a/molecule/test-cluster/converge.yml +++ b/molecule/test-cluster/converge.yml @@ -41,36 +41,4 @@ delay: 12 retries: 10 - - name: Create the cost-mgmt-data.openshift.io/v1alpha1.CostManagementData - k8s: - state: present - namespace: '{{ namespace }}' - definition: '{{ cm_data_cr }}' - - - name: debug cost-mgmt data custom resource - ignore_errors: yes - failed_when: false - debug: - var: debug_cr - vars: - debug_cr: '{{ lookup("k8s", - kind=cm_data_cr.kind, - api_version=cm_data_cr.apiVersion, - namespace=namespace, - resource_name=cm_data_cr.metadata.name - )}}' - - - name: Wait 2m for reconciliation to complete for collect - k8s_info: - api_version: '{{ cm_data_cr.apiVersion }}' - kind: '{{ cm_data_cr.kind }}' - namespace: '{{ namespace }}' - name: '{{ cm_data_cr.metadata.name }}' - register: cm_data_cr - until: - - "'Successful' in (cm_data_cr | - json_query('resources[].status.conditions[].reason'))" - delay: 12 - retries: 10 - - import_playbook: '{{ playbook_dir }}/../default/asserts.yml' diff --git a/molecule/test-local/converge.yml b/molecule/test-local/converge.yml index 698dbf4..9fb43ad 100644 --- a/molecule/test-local/converge.yml +++ b/molecule/test-local/converge.yml @@ -43,7 +43,7 @@ src: 'mock_ingress_server.py' mode: 0666 -- name: Converge on setup +- name: Prepare converge on setup hosts: localhost connection: local vars: @@ -56,10 +56,6 @@ 'crds/cost_mgmt_cr.yaml'])) | from_yaml }}" cm_crd: "{{ lookup('file', '/'.join([deploy_dir, 'crds/cost_mgmt_crd.yaml'])) | from_yaml }}" - cm_data_crd: "{{ lookup('file', '/'.join([deploy_dir, - 'crds/cost_mgmt_data_crd.yaml'])) | from_yaml }}" - cm_data_cr: "{{ lookup('file', '/'.join([molecule_dir, - 'crds/cost_mgmt_data_cr.yaml'])) | from_yaml }}" metering_cr: "{{ lookup('file', '/'.join([molecule_dir, 'crds/metering_cr.yaml'])) | from_yaml }}" metering_crd: "{{ lookup('file', '/'.join([molecule_dir, @@ -78,6 +74,9 @@ 'crds/reporting_operator_token.yaml'])) | from_yaml }}" trusted_ca_config: "{{ lookup('file', '/'.join([molecule_dir, 'crds/trusted_ca_certmap.yaml'])) | from_yaml }}" + authentication_secret: "{{ lookup('file', '/'.join([molecule_dir, + 'crds/authentication_secret.yaml'])) | from_yaml }}" + tasks: - block: - name: Delete the Operator Deployment @@ -131,12 +130,6 @@ namespace: '{{ namespace }}' definition: '{{ cm_crd }}' - - name: Create the cost-mgmt-data.openshift.io/v1alpha1.CostManagementData crd - k8s: - state: present - namespace: '{{ namespace }}' - definition: '{{ cm_data_crd }}' - - name: Create the trusted cert ConfigMap k8s: state: present @@ -168,8 +161,8 @@ (cr | json_query('resources[].status.conditions[].reason'))" - "'Metering has not been configured.' in (cr | json_query('resources[].status.conditions[].message'))" - delay: 12 - retries: 15 + delay: 10 + retries: 12 rescue: - name: debug cr @@ -201,21 +194,7 @@ ignore_errors: yes failed_when: false command: kubectl logs - deployment/{{ definition.metadata.name }} -n {{ namespace }} -c operator - environment: - KUBECONFIG: '{{ lookup("env", "KUBECONFIG") }}' - vars: - definition: "{{ lookup('template', - '/'.join([deploy_dir, 'operator.yaml'])) | from_yaml }}" - register: log - - - debug: var=log.stdout_lines - - - name: get ansible logs - ignore_errors: yes - failed_when: false - command: kubectl logs - deployment/{{ definition.metadata.name }} -n {{ namespace }} -c ansible + deployment/{{ definition.metadata.name }} -n {{ namespace }} environment: KUBECONFIG: '{{ lookup("env", "KUBECONFIG") }}' vars: @@ -268,6 +247,12 @@ command: docker ps -aqf "ancestor=cost-mgmt.openshift.io/cost-mgmt-operator:testing" register: container_id + - name: Create the authentication Secret + k8s: + state: present + namespace: '{{ namespace }}' + definition: '{{ authentication_secret }}' + - name: Create the metering namespace k8s: api_version: v1 @@ -335,68 +320,13 @@ debug: var: reporting_token - - name: Create the cost-mgmt.openshift.io/v1alpha1.CostManagement crd - k8s: - state: present - namespace: '{{ namespace }}' - definition: '{{ cm_crd }}' - - - name: Create the cost-mgmt-data.openshift.io/v1alpha1.CostManagementData crd - k8s: - state: present - namespace: '{{ namespace }}' - definition: '{{ cm_data_crd }}' - - - name: Create the cost-mgmt.openshift.io/v1alpha1.CostManagement - k8s: - state: present - namespace: '{{ namespace }}' - definition: '{{ cm_cr }}' - - - name: Wait 2m for reconciliation to complete for setup - k8s_info: - api_version: '{{ cm_cr.apiVersion }}' - kind: '{{ cm_cr.kind }}' - namespace: '{{ namespace }}' - name: '{{ cm_cr.metadata.name }}' - register: cr - until: - - "'Successful' in (cr | - json_query('resources[].status.conditions[].reason'))" - delay: 12 - retries: 10 - rescue: - - name: debug cr - ignore_errors: yes - failed_when: false - debug: - var: debug_cr - vars: - debug_cr: '{{ lookup("k8s", - kind=cm_data_cr.kind, - api_version=cm_data_cr.apiVersion, - namespace=namespace, - resource_name=cm_data_cr.metadata.name - )}}' - - - name: debug cost-mgmt lookup - ignore_errors: yes - failed_when: false - debug: - var: deploy - vars: - deploy: '{{ lookup("k8s", - kind="Deployment", - api_version="apps/v1", - namespace=namespace - )}}' - name: get operator logs ignore_errors: yes failed_when: false command: kubectl logs - deployment/{{ definition.metadata.name }} -n {{ namespace }} -c operator + deployment/{{ definition.metadata.name }} -n {{ namespace }} environment: KUBECONFIG: '{{ lookup("env", "KUBECONFIG") }}' vars: @@ -406,23 +336,6 @@ - debug: var=log.stdout_lines - - name: get ansible logs - ignore_errors: yes - failed_when: false - command: kubectl logs - deployment/{{ definition.metadata.name }} -n {{ namespace }} -c ansible - environment: - KUBECONFIG: '{{ lookup("env", "KUBECONFIG") }}' - vars: - definition: "{{ lookup('template', - '/'.join([deploy_dir, 'operator.yaml'])) | from_yaml }}" - register: log - - - debug: var=log.stdout_lines - - - fail: - msg: "Failed on action: converge" - - name: Start server on Operator container hosts: k8s tasks: @@ -483,57 +396,44 @@ - name: start server command: docker exec -i "{{ containerName }}" bash -c 'cd /tmp/www; nohup python3 mock_ingress_server.py /dev/null 2>&1 &' -- name: Converge for collect +- name: Converge for setup hosts: localhost connection: local vars: ansible_python_interpreter: '{{ ansible_playbook_python }}' deploy_dir: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/deploy" molecule_dir: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/molecule" - cm_data_cr: "{{ lookup('file', '/'.join([deploy_dir, - 'crds/cost_mgmt_data_cr.yaml'])) | from_yaml }}" - authentication_secret: "{{ lookup('file', '/'.join([molecule_dir, - 'crds/authentication_secret.yaml'])) | from_yaml }}" REPLACE_IMAGE: cost-mgmt.openshift.io/cost-mgmt-operator:testing + cm_cr: "{{ lookup('file', '/'.join([molecule_dir, + 'crds/cost_mgmt_cr.yaml'])) | from_yaml }}" + cm_crd: "{{ lookup('file', '/'.join([deploy_dir, + 'crds/cost_mgmt_crd.yaml'])) | from_yaml }}" tasks: - block: - - name: Create the authentication Secret - k8s: - state: present - namespace: '{{ namespace }}' - definition: '{{ authentication_secret }}' - - - name: Create the cost-mgmt-data.openshift.io/v1alpha1.CostManagementData - k8s: - state: present - namespace: '{{ namespace }}' - definition: '{{ cm_data_cr }}' - - - name: debug cost-mgmt data custom resource - ignore_errors: yes - failed_when: false - debug: - var: debug_cr - vars: - debug_cr: '{{ lookup("k8s", - kind=cm_data_cr.kind, - api_version=cm_data_cr.apiVersion, - namespace=namespace, - resource_name=cm_data_cr.metadata.name - )}}' - - - name: Wait 2m for reconciliation to complete for collect - k8s_info: - api_version: '{{ cm_data_cr.apiVersion }}' - kind: '{{ cm_data_cr.kind }}' - namespace: '{{ namespace }}' - name: '{{ cm_data_cr.metadata.name }}' - register: cm_data_cr - until: - - "'Successful' in (cm_data_cr | - json_query('resources[].status.conditions[].reason'))" - delay: 12 - retries: 10 + - name: Create the cost-mgmt.openshift.io/v1alpha1.CostManagement crd + k8s: + state: present + namespace: '{{ namespace }}' + definition: '{{ cm_crd }}' + + - name: Create the cost-mgmt.openshift.io/v1alpha1.CostManagement + k8s: + state: present + namespace: '{{ namespace }}' + definition: '{{ cm_cr }}' + + - name: Wait 5m for reconciliation to complete for setup + k8s_info: + api_version: '{{ cm_cr.apiVersion }}' + kind: '{{ cm_cr.kind }}' + namespace: '{{ namespace }}' + name: '{{ cm_cr.metadata.name }}' + register: cr + until: + - "'Successful' in (cr | + json_query('resources[].status.conditions[].reason'))" + delay: 20 + retries: 15 rescue: - name: debug cr @@ -543,10 +443,10 @@ var: debug_cr vars: debug_cr: '{{ lookup("k8s", - kind=cm_data_cr.kind, - api_version=cm_data_cr.apiVersion, + kind=cm_cr.kind, + api_version=cm_cr.apiVersion, namespace=namespace, - resource_name=cm_data_cr.metadata.name + resource_name=cm_cr.metadata.name )}}' - name: debug cost-mgmt lookup @@ -565,21 +465,7 @@ ignore_errors: yes failed_when: false command: kubectl logs - deployment/{{ definition.metadata.name }} -n {{ namespace }} -c operator - environment: - KUBECONFIG: '{{ lookup("env", "KUBECONFIG") }}' - vars: - definition: "{{ lookup('template', - '/'.join([deploy_dir, 'operator.yaml'])) | from_yaml }}" - register: log - - - debug: var=log.stdout_lines - - - name: get ansible logs - ignore_errors: yes - failed_when: false - command: kubectl logs - deployment/{{ definition.metadata.name }} -n {{ namespace }} -c ansible + deployment/{{ definition.metadata.name }} -n {{ namespace }} environment: KUBECONFIG: '{{ lookup("env", "KUBECONFIG") }}' vars: @@ -589,9 +475,6 @@ - debug: var=log.stdout_lines - - fail: - msg: "Failed on action: converge" - - name: Copy tar.gz to k8s from Operator hosts: k8s tasks: diff --git a/roles/collect/defaults/main.yml b/roles/collect/defaults/main.yml deleted file mode 100644 index bea4c35..0000000 --- a/roles/collect/defaults/main.yml +++ /dev/null @@ -1,27 +0,0 @@ ---- -namespace: openshift-metering -collect_format: 'csv' -collect_manifest_uuid: '{{ 99999999999999999999 | random | to_uuid }}' -collect_archive_name: cost-mgmt -ocp_validate_cert: 'true' -ocp_cluster_id: '' -reporting_operator_token_name: '' -collect_reports: - - 'cm-openshift-usage-lookback-' - - 'cm-openshift-persistentvolumeclaim-lookback-' - - 'cm-openshift-node-labels-lookback-' -collect_download_path: '/tmp/cost-mgmt-operator-collect' -collect_delete_after: 'true' -collect_ocp_report_timeout: 60 -collect_max_csvfile_size: 99 -api_prefix: 'https://' -ingress_url: 'https://cloud.redhat.com/api/ingress/v1/upload' -authentication: 'token' -authentication_token: '' -username: '' -password: '' -cacert_path: '{{ collect_download_path }}/ca-bundle.crt' -debug: 'true' -collect_upload_wait: '{{ 2100 | random(step=10) }}' -current_month: '{{ ansible_date_time.month | int }}' -current_year: '{{ ansible_date_time.year | int }}' diff --git a/roles/setup/defaults/main.yml b/roles/setup/defaults/main.yml index d3fd11d..f6ca34a 100644 --- a/roles/setup/defaults/main.yml +++ b/roles/setup/defaults/main.yml @@ -1,8 +1,32 @@ --- -namespace: openshift-metering current_day: '{{ ansible_date_time.day | int }}' current_month: '{{ ansible_date_time.month | int }}' current_year: '{{ ansible_date_time.year | int }}' setup_template_path: '/tmp/cost-mgmt-operator-collect' setup_template_dir: "{{ lookup('password', '/dev/null chars=ascii_letters') }}" setup_delete_after: 'true' +upload_cycle_seconds: 21600 +collect_data: 'false' +collect_format: 'csv' +collect_manifest_uuid: '{{ 99999999999999999999 | random | to_uuid }}' +collect_archive_name: cost-mgmt +ocp_validate_cert: 'true' +ocp_cluster_id: '' +reporting_operator_token_name: '' +collect_reports: + - 'cm-openshift-usage-lookback-' + - 'cm-openshift-persistentvolumeclaim-lookback-' + - 'cm-openshift-node-labels-lookback-' +collect_download_path: '/tmp/cost-mgmt-operator-collect' +collect_delete_after: 'true' +collect_ocp_report_timeout: 60 +collect_max_csvfile_size: 99 +api_prefix: 'https://' +ingress_url: 'https://cloud.redhat.com/api/ingress/v1/upload' +authentication: 'token' +authentication_token: '' +username: '' +password: '' +cacert_path: '{{ collect_download_path }}/ca-bundle.crt' +debug: 'true' +collect_upload_wait: '{{ 2100 | random(step=10) }}' diff --git a/roles/collect/files/package_report.py b/roles/setup/files/package_report.py similarity index 100% rename from roles/collect/files/package_report.py rename to roles/setup/files/package_report.py diff --git a/roles/collect/files/trusted_ca_certmap.yaml b/roles/setup/files/trusted_ca_certmap.yaml similarity index 100% rename from roles/collect/files/trusted_ca_certmap.yaml rename to roles/setup/files/trusted_ca_certmap.yaml diff --git a/roles/collect/tasks/main.yml b/roles/setup/tasks/collect.yml similarity index 82% rename from roles/collect/tasks/main.yml rename to roles/setup/tasks/collect.yml index 08ca3d5..3432324 100644 --- a/roles/collect/tasks/main.yml +++ b/roles/setup/tasks/collect.yml @@ -1,22 +1,9 @@ --- -- name: Obtain cost-mgmt-setup info - community.kubernetes.k8s_info: - api_version: cost-mgmt.openshift.io/v1alpha1 - kind: CostManagement - namespace: "{{ namespace }}" - name: cost-mgmt-setup - register: cost_mgmt_setup - -- name: Fail if cost-mgmt-setup does not exist - fail: - msg: 'The cost-mgmt-setup custom resource has not been configured.' - when: not cost_mgmt_setup.resources - - name: Set cluster ID set_fact: - ocp_cluster_id: "{{ cost_mgmt_setup.resources[0].spec.clusterID }}" - when: cost_mgmt_setup.resources + ocp_cluster_id: "{{ current_cr_spec.clusterID }}" + when: current_cr_spec.clusterID - name: Check for OCP clusterID debug: msg='OCP clusterID is not defined' @@ -24,17 +11,17 @@ - name: Set validation boolean set_fact: - ocp_validate_cert: "{{ cost_mgmt_setup.resources[0].spec.validate_cert }}" - when: cost_mgmt_setup.resources + ocp_validate_cert: "{{ current_cr_spec.validate_cert }}" + when: current_cr_spec.validate_cert - name: Check for validation boolean debug: msg='HTTPS certificate validation variable is not defined; defaulting to true' - when: not cost_mgmt_setup.resources + when: not current_cr_spec.validate_cert - name: Set service account token name set_fact: - reporting_operator_token_name: "{{ cost_mgmt_setup.resources[0].spec.reporting_operator_token_name }}" - when: cost_mgmt_setup.resources + reporting_operator_token_name: "{{ current_cr_spec.reporting_operator_token_name }}" + when: current_cr_spec.reporting_operator_token_name - name: Check for service account token name debug: msg='Reporting Operator service account token name is not defined' @@ -42,36 +29,24 @@ - name: Fail if the clusterID or service token are not defined fail: - msg: 'The cost-mgmt-setup custom resource requires the clusterID and reporting_operator_token_name to be defined.' + msg: 'The CostManagement custom resource requires the clusterID and reporting_operator_token_name to be defined.' when: not ocp_cluster_id or not reporting_operator_token_name - name: Set upload_wait set_fact: - collect_upload_wait: "{{ cost_mgmt_setup.resources[0].spec.upload_wait | int }}" - when: cost_mgmt_setup.resources - ignore_errors: true - -- name: Set current_month - set_fact: - current_month: "{{ cost_mgmt_setup.resources[0].spec.current_month | string }}" - when: cost_mgmt_setup.resources + collect_upload_wait: "{{ current_cr_spec.upload_wait | int }}" + when: current_cr_spec.upload_wait ignore_errors: true - name: Format current_month string if less than 10 set_fact: - current_month: '{{ "0" + (current_month | string) }}' + current_month: '{{ "0" + (input_month | string) }}' when: - - (current_month | int) < 10 - -- name: Set current_year - set_fact: - current_year: "{{ cost_mgmt_setup.resources[0].spec.current_year | string }}" - when: cost_mgmt_setup.resources - ignore_errors: true + - (input_month | int) < 10 - name: Set monthly suffix for reports set_fact: - current_year_month: '{{ (current_year | string ) + (current_month | string) }}' + current_year_month: '{{ (input_year | string ) + (input_month | string) }}' - name: Obtain metering api info community.kubernetes.k8s_info: @@ -95,8 +70,8 @@ - name: Set authentication_secret name set_fact: - authentication_secret_name: "{{ cost_mgmt_setup.resources[0].spec.authentication_secret_name }}" - when: cost_mgmt_setup.resources + authentication_secret_name: "{{ current_cr_spec.authentication_secret_name }}" + when: current_cr_spec.authentication_secret_name - name: debug auth secret name debug: @@ -105,8 +80,8 @@ - name: Set the authentication method set_fact: - authentication: "{{ cost_mgmt_setup.resources[0].spec.authentication }}" - when: cost_mgmt_setup.resources + authentication: "{{ current_cr_spec.authentication }}" + when: current_cr_spec.authentication ignore_errors: true - name: debug auth method @@ -116,8 +91,8 @@ - name: Set the ingress URL set_fact: - ingress_url: "{{ cost_mgmt_setup.resources[0].spec.ingress_url }}" - when: cost_mgmt_setup.resources + ingress_url: "{{ current_cr_spec.ingress_url }}" + when: current_cr_spec.ingress_url ignore_errors: true - name: debug ingress URL @@ -184,11 +159,6 @@ path: "{{ cacert_path }}" register: trusted_cert -- name: debug the trusted cert - debug: - var: trusted_cert - when: debug - - name: Fail if the trusted cert does not exist fail: msg: 'Failing because the ssl certificate does not exist.' @@ -207,7 +177,7 @@ community.kubernetes.k8s: namespace: "{{ namespace }}" state: present - src: '{{ ansible_env.HOME }}/roles/collect/files/trusted_ca_certmap.yaml' + src: '{{ ansible_env.HOME }}/roles/setup/files/trusted_ca_certmap.yaml' - name: Get the trusted-ca-bundle community.kubernetes.k8s_info: @@ -357,3 +327,8 @@ with_items: - '{{ packaged_reports.stdout_lines }}' when: collect_delete_after | bool + +- include_tasks: update-status.yml + vars: + status_vars: + upload_attempt_time: "{{ ansible_date_time.iso8601 }}" diff --git a/roles/setup/tasks/main.yml b/roles/setup/tasks/main.yml index 0ef399a..db3d010 100644 --- a/roles/setup/tasks/main.yml +++ b/roles/setup/tasks/main.yml @@ -1,45 +1,135 @@ --- + +- name: print hostvars[localhost]._cost_mgmt_openshift_io_costmanagement + debug: + var: hostvars["localhost"]._cost_mgmt_openshift_io_costmanagement + when: + - hostvars is defined + - hostvars["localhost"] is defined + - hostvars["localhost"]._cost_mgmt_openshift_io_costmanagement is defined + ignore_errors: true + +- name: print _cost_mgmt_openshift_io_costmanagement_spec + debug: + var: _cost_mgmt_openshift_io_costmanagement_spec + when: + - _cost_mgmt_openshift_io_costmanagement_spec is defined + ignore_errors: true + +- name: Set current cr facts + set_fact: + current_cr: "{{ hostvars['localhost']._cost_mgmt_openshift_io_costmanagement }}" + current_cr_spec: "{{ _cost_mgmt_openshift_io_costmanagement_spec }}" + when: + - hostvars is defined + - hostvars["localhost"] is defined + - hostvars["localhost"]._cost_mgmt_openshift_io_costmanagement is defined + - _cost_mgmt_openshift_io_costmanagement_spec is defined + ignore_errors: true + +- name: print meta + debug: + var: meta + +- name: Get upload_attempt_time + set_fact: + last_upload_attempt_time: "{{ current_cr.status.upload_attempt_time }}" + current_time: "{{ ansible_date_time.iso8601 }}" + when: + - current_cr is defined + - current_cr.status is defined + - current_cr.status.upload_attempt_time is defined + ignore_errors: true + +- name: print last_upload_attempt_time + debug: + var: last_upload_attempt_time + when: last_upload_attempt_time is defined + ignore_errors: true + +- name: iso8601 to timestamp + set_fact: + last_upload_attempt_timestamp: "{{ last_upload_attempt_time | regex_replace('[T, Z]+', ' ') | trim }}" + current_timestamp: "{{ current_time | regex_replace('[T, Z]+', ' ') | trim }}" + when: last_upload_attempt_time is defined + ignore_errors: true + +- name: print last_upload_attempt_timestamp + debug: + var: last_upload_attempt_timestamp + when: last_upload_attempt_timestamp is defined + +- name: timestamp to datetime + set_fact: + last_upload_attempt_datetime: "{{ last_upload_attempt_timestamp | to_datetime }}" + current_datetime: "{{ current_timestamp | to_datetime }}" + upload_time_diff: "{{ ((current_timestamp | to_datetime) - (last_upload_attempt_timestamp | to_datetime)).total_seconds() }}" + when: last_upload_attempt_timestamp is defined + ignore_errors: true + +- name: print last_upload_attempt_datetime + debug: + var: last_upload_attempt_datetime + when: last_upload_attempt_datetime is defined + +- name: print upload_time_diff + debug: + var: upload_time_diff + when: upload_time_diff is defined + +- name: Set collect_data + set_fact: + collect_data: true + when: upload_time_diff is not defined or (( upload_time_diff | int ) > upload_cycle_seconds) + +- name: print collect_data + debug: + var: collect_data + when: collect_data is defined + - name: Search for metering resources community.kubernetes.k8s_info: api_version: v1 kind: MeteringConfig - namespace: "{{ namespace }}" + namespace: "{{ meta.namespace }}" register: metering_objects - name: Ensure metering has been configured fail: msg: 'Metering has not been configured.' - when: not metering_objects.resources + when: not metering_objects.resources or (metering_objects.resources is defined and metering_objects.resources | length == 0) -- name: Obtain cost-mgmt-setup info - community.kubernetes.k8s_info: - api_version: cost-mgmt.openshift.io/v1alpha1 - kind: CostManagement - namespace: "{{ namespace }}" - name: cost-mgmt-setup - register: cost_mgmt_setup +- name: Set upload_cycle_seconds + set_fact: + upload_cycle_seconds: "{{ current_cr_spec.upload_cycle_seconds | int }}" + when: + - current_cr_spec is defined + - current_cr_spec.upload_cycle_seconds is defined - name: Set current_day set_fact: - current_day: "{{ cost_mgmt_setup.resources[0].spec.current_day | int }}" - when: cost_mgmt_setup.resources - ignore_errors: true + current_day: "{{ current_cr_spec.current_day | int }}" + when: + - current_cr_spec is defined + - current_cr_spec.current_day is defined - name: Set current_month set_fact: - current_month: "{{ cost_mgmt_setup.resources[0].spec.current_month | int }}" - when: cost_mgmt_setup.resources - ignore_errors: true + current_month: "{{ current_cr_spec.current_month | int }}" + when: + - current_cr_spec is defined + - current_cr_spec.current_month is defined - name: Set current_year set_fact: - current_year: "{{ cost_mgmt_setup.resources[0].spec.current_year | int }}" - when: cost_mgmt_setup.resources - ignore_errors: true + current_year: "{{ current_cr_spec.current_year | int }}" + when: + - current_cr_spec is defined + - current_cr_spec.current_year is defined - name: Create datasources and reportqueries community.kubernetes.k8s: - namespace: "{{ namespace }}" + namespace: "{{ meta.namespace }}" state: present src: "{{ item }}" with_fileglob: @@ -51,6 +141,7 @@ input_month: '{{ current_month }}' input_year: '{{ current_year }}' month_delta: 0 + namespace: "{{ meta.namespace }}" state: 'present' - name: Manage next month reports @@ -59,6 +150,7 @@ input_month: '{{ current_month }}' input_year: '{{ current_year }}' month_delta: 1 + namespace: "{{ meta.namespace }}" state: 'present' when: (current_day | int) > 24 @@ -68,5 +160,17 @@ input_month: '{{ current_month }}' input_year: '{{ current_year }}' month_delta: -1 + namespace: "{{ meta.namespace }}" state: 'absent' when: (current_day | int) < 3 + +- name: Upload metric data + import_tasks: collect.yml + vars: + input_month: '{{ current_month }}' + input_year: '{{ current_year }}' + namespace: "{{ meta.namespace }}" + current_cr: "{{ current_cr }}" + current_cr_spec: "{{ current_cr_spec }}" + when: + - collect_data | bool diff --git a/roles/setup/tasks/update-status.yml b/roles/setup/tasks/update-status.yml new file mode 100644 index 0000000..7c47b71 --- /dev/null +++ b/roles/setup/tasks/update-status.yml @@ -0,0 +1,8 @@ +--- +- operator_sdk.util.k8s_status: + api_version: "{{ current_cr.apiVersion }}" + kind: "{{ current_cr.kind }}" + name: "{{ current_cr.metadata.name }}" + namespace: "{{ current_cr.metadata.namespace }}" + status: "{{ status_vars }}" + ignore_errors: yes diff --git a/watches.yaml b/watches.yaml index 3aa86d3..79b1a3a 100644 --- a/watches.yaml +++ b/watches.yaml @@ -4,10 +4,4 @@ group: cost-mgmt.openshift.io kind: CostManagement role: /opt/ansible/roles/setup - -# collect the reports -- version: v1alpha1 - group: cost-mgmt-data.openshift.io - kind: CostManagementData - role: /opt/ansible/roles/collect - reconcilePeriod: 360m + reconcilePeriod: 5m