From 39ef816f5e8659194a6922e18acdb7723a7e5f21 Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Thu, 15 Apr 2021 23:49:31 -0400 Subject: [PATCH 1/4] Updated logic to avoid crash during upgrades --- .../tasks/database_configuration.yml | 24 ++++ .../{initialize.yml => initialize_django.yml} | 0 roles/installer/tasks/main.yml | 46 +------- roles/installer/tasks/migrate_data.yml | 7 +- .../tasks/resources_configuration.yml | 107 ++++++++++++++++++ .../templates/tower_postgres.yaml.j2 | 2 +- roles/installer/vars/main.yml | 1 + 7 files changed, 141 insertions(+), 46 deletions(-) rename roles/installer/tasks/{initialize.yml => initialize_django.yml} (100%) create mode 100644 roles/installer/tasks/resources_configuration.yml diff --git a/roles/installer/tasks/database_configuration.yml b/roles/installer/tasks/database_configuration.yml index a79f55c4..af40c024 100644 --- a/roles/installer/tasks/database_configuration.yml +++ b/roles/installer/tasks/database_configuration.yml @@ -64,12 +64,36 @@ set_fact: pg_config: '{{ _generated_pg_config_resources["resources"] | default([]) | length | ternary(_generated_pg_config_resources, _pg_config) }}' +- name: Check if PostgreSQL statefuset is present + k8s_info: + api_version: v1 + kind: StatefulSet + namespace: '{{ meta.namespace }}' + name: '{{ meta.name }}-postgres' + register: _postgres_statefulset_result + +- name: Set manual_statefulset_upgrade to false since PostgreSQL statefulset does not exist yet + set_fact: + manual_statefulset_upgrade: False + when: not _postgres_statefulset_result['resources'] | length + +- name: Determine if PostgreSQL statefulset requires manual upgrade + set_fact: + manual_statefulset_upgrade: '{{ _postgres_statefulset_result["resources"][0]["metadata"]["labels"]["app.kubernetes.io/managed-by"] | default("") | ternary("False", "True") }}' + when: manual_statefulset_upgrade is not defined + +- name: Show help message since PostgreSQL statefulset was created by a legacy version + debug: + var: manual_upgrade_msg + when: manual_statefulset_upgrade|bool + - name: Create Database if no database is specified k8s: apply: true definition: "{{ lookup('template', 'tower_postgres.yaml.j2') }}" when: - pg_config['resources'][0]['data']['type'] | default('') | b64decode == 'managed' + - not manual_statefulset_upgrade|bool - name: Store Database Configuration set_fact: diff --git a/roles/installer/tasks/initialize.yml b/roles/installer/tasks/initialize_django.yml similarity index 100% rename from roles/installer/tasks/initialize.yml rename to roles/installer/tasks/initialize_django.yml diff --git a/roles/installer/tasks/main.yml b/roles/installer/tasks/main.yml index 1d2bc7c5..9fad33b9 100644 --- a/roles/installer/tasks/main.yml +++ b/roles/installer/tasks/main.yml @@ -43,47 +43,8 @@ - tower_ingress_type | lower == 'route' - tower_route_tls_secret != '' -- name: Ensure configured instance resources exist in the cluster. - k8s: - apply: yes - definition: "{{ lookup('template', item) | from_yaml_all | list }}" - with_items: - - tower_config.yaml.j2 - -- name: Apply Resources - k8s: - apply: yes - definition: "{{ lookup('template', item + '.yaml.j2') }}" - register: tower_deployment_result - loop: - - 'tower_app_credentials' - - 'tower_service_account' - - 'tower_persistent' - - 'tower_deployment' - - 'tower_service' - - 'tower_ingress' - -- name: Get the resource pod information. - k8s_info: - kind: Pod - namespace: '{{ meta.namespace }}' - label_selectors: - - "app.kubernetes.io/name={{ meta.name }}" - - "app.kubernetes.io/managed-by=awx-operator" - - "app.kubernetes.io/component=awx" - register: tower_pods - until: "tower_pods['resources'][0]['status']['phase'] == 'Running'" - delay: 5 - retries: 60 - -- name: Set the resource pod name as a variable. - set_fact: - tower_pod_name: "{{ tower_pods['resources'][0]['metadata']['name'] }}" - -- name: Verify the resource pod name is populated. - assert: - that: tower_pod_name != '' - fail_msg: "Could not find the tower pod's name." +- name: Include resources configuration tasks + include_tasks: resources_configuration.yml - name: Check for pending migrations k8s_exec: @@ -107,7 +68,8 @@ - database_check is defined - (database_check.stdout|trim) != '0' -- include_tasks: initialize.yml +- name: Initialize Django + include_tasks: initialize_django.yml - name: Update status variables include_tasks: update_status.yml diff --git a/roles/installer/tasks/migrate_data.yml b/roles/installer/tasks/migrate_data.yml index cd619af0..da5067c0 100644 --- a/roles/installer/tasks/migrate_data.yml +++ b/roles/installer/tasks/migrate_data.yml @@ -12,10 +12,11 @@ k8s_info: kind: Pod namespace: '{{ meta.namespace }}' - label_selectors: - - "app.kubernetes.io/name={{ meta.name }}-postgres" + name: '{{ meta.name }}-postgres-0' # using name to keep compatibility + field_selectors: + - status.phase=Running register: postgres_pod - until: "postgres_pod['resources'][0]['status']['phase'] == 'Running'" + until: postgres_pod['resources'] | length delay: 5 retries: 60 diff --git a/roles/installer/tasks/resources_configuration.yml b/roles/installer/tasks/resources_configuration.yml new file mode 100644 index 00000000..90818953 --- /dev/null +++ b/roles/installer/tasks/resources_configuration.yml @@ -0,0 +1,107 @@ +--- + +- name: Check if deployment is present + k8s_info: + api_version: v1 + kind: Deployment + namespace: '{{ meta.namespace }}' + name: '{{ meta.name }}' + register: _deployment_result + +- name: Set manual_deployment_upgrade to false since deployment does not exist yet + set_fact: + manual_deployment_upgrade: False + when: not _deployment_result['resources'] | length + +- name: Determine if deployment requires manual upgrade + set_fact: + manual_deployment_upgrade: '{{ _deployment_result["resources"][0]["metadata"]["labels"]["app.kubernetes.io/managed-by"] | default("") | ternary("False", "True") }}' + when: manual_deployment_upgrade is not defined + +- name: Determine pod label_selector to be used + set_fact: + tower_pod_label_selector: '{{ manual_deployment_upgrade|bool | default("false"|bool)| ternary("app={{ deployment_type }}", "app.kubernetes.io/name={{ meta.name }}") }}' + +- name: Show help message since deployment was created by a legacy version + debug: + var: manual_upgrade_msg + when: manual_deployment_upgrade|bool + +- name: Get the current resource pod information. + k8s_info: + api_version: v1 + kind: Pod + namespace: '{{ meta.namespace }}' + label_selectors: + - '{{ tower_pod_label_selector }}' + field_selectors: + - status.phase=Running + register: tower_pods + changed_when: tower_pods["resources"] | length + +- name: Set the resource pod name as a variable. + set_fact: + tower_pod_name: "{{ tower_pods['resources'][0]['metadata']['name'] | default('') }}" + +- name: Apply Resources + k8s: + apply: yes + definition: "{{ lookup('template', item + '.yaml.j2') }}" + wait: yes + register: tower_resources_result + loop: + - 'tower_config' + - 'tower_app_credentials' + - 'tower_service_account' + - 'tower_persistent' + - 'tower_service' + - 'tower_ingress' + when: not manual_deployment_upgrade|bool + +- name: Apply deployment resources + k8s: + apply: yes + definition: "{{ lookup('template', 'tower_deployment.yaml.j2') | from_yaml }}" + wait: yes + register: tower_deployment_result + when: not manual_deployment_upgrade|bool + +- block: + - name: Delete pod to reload configuration + k8s: + api_version: v1 + state: absent + kind: Pod + namespace: '{{ meta.namespace }}' + name: '{{ tower_pod_name }}' + wait: yes + when: + - tower_resources_result.changed + - tower_pod_name | length + + - name: Get the new resource pod information after updating resource. + k8s_info: + kind: Pod + namespace: '{{ meta.namespace }}' + label_selectors: + - '{{ tower_pod_label_selector }}' + field_selectors: + - status.phase=Running + register: _new_pod + until: + - _new_pod['resources'] | length + - _new_pod['resources'][0]['metadata']['name'] != tower_pod_name + delay: 5 + retries: 60 + when: not manual_deployment_upgrade|bool + + - name: Update new resource pod name as a variable. + set_fact: + tower_pod_name: '{{ _new_pod["resources"][0]["metadata"]["name"] }}' + when: + - tower_resources_result.changed or tower_deployment_result.changed + +- name: Verify the resource pod name is populated. + assert: + that: tower_pod_name != '' + fail_msg: "Could not find the tower pod's name." diff --git a/roles/installer/templates/tower_postgres.yaml.j2 b/roles/installer/templates/tower_postgres.yaml.j2 index 18d2ae96..f60cf29c 100644 --- a/roles/installer/templates/tower_postgres.yaml.j2 +++ b/roles/installer/templates/tower_postgres.yaml.j2 @@ -1,6 +1,6 @@ # Postgres StatefulSet. --- -apiVersion: v1 +apiVersion: apps/v1 kind: StatefulSet metadata: name: '{{ meta.name }}-postgres' diff --git a/roles/installer/vars/main.yml b/roles/installer/vars/main.yml index b454ee74..469912e3 100644 --- a/roles/installer/vars/main.yml +++ b/roles/installer/vars/main.yml @@ -3,3 +3,4 @@ postgres_initdb_args: '--auth-host=scram-sha-256' postgres_host_auth_method: 'scram-sha-256' ldap_cacert_ca_crt: '' tower_projects_existing_claim: '' +manual_upgrade_msg: 'Manual Upgrade is required, please refer to the upgrade docs' From 09652056b74a61db652e35b09d940d7ee7845fe4 Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Tue, 20 Apr 2021 00:23:10 -0400 Subject: [PATCH 2/4] Added initial upgrade documentation --- README.md | 12 ++++++++++++ roles/installer/vars/main.yml | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f9fd0624..822d2193 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ An [Ansible AWX](https://github.com/ansible/awx) operator for Kubernetes built w * [LDAP Certificate Authority](#ldap-certificate-authority) * [Persisting Projects Directory](#persisting-projects-directory) * [Custom Volume and Volume Mount Options](#custom-volume-and-volume-mount-options) + * [Upgrade Notes](#upgrade-notes) + * [From Older Versions](#from-older-versions) * [Development](#development) * [Testing](#testing) * [Testing in Docker](#testing-in-docker) @@ -477,6 +479,16 @@ Example spec file for volumes and volume mounts > :warning: **Volume and VolumeMount names cannot contain underscores(_)** +## Upgrade Notes + +### From Older Versions + +For `AWX` instances created by the `awx-operator<=0.0.7` a manual upgrade will be required. On version `0.0.8` a new set of `labels` were introduced to the PostgreSQL `statefulset` (when applied) and the AWX `deployment`. Since `selector.matchLabels` are `ready-only` by the Kubernetes API, the resource must be deleted and recreated. + +The `awx-operator` to avoid deleting data from environments which the `persistence` layer is not configured to retain the data, it will be up to the system administrator to perform such operator. For instances created from `awx-operator>=0.0.8`, no manual intervetion shall be necessary. + +For example, if you are running a `managed` PostgreSQL configured instance and have the cluster storage layer to maintain the `PV` storage area upon the `statefulset` deletion, you could just delete the `awx` resource and recreate it on the newer version. + ## Development ### Testing diff --git a/roles/installer/vars/main.yml b/roles/installer/vars/main.yml index 469912e3..cd78fd7a 100644 --- a/roles/installer/vars/main.yml +++ b/roles/installer/vars/main.yml @@ -3,4 +3,4 @@ postgres_initdb_args: '--auth-host=scram-sha-256' postgres_host_auth_method: 'scram-sha-256' ldap_cacert_ca_crt: '' tower_projects_existing_claim: '' -manual_upgrade_msg: 'Manual Upgrade is required, please refer to the upgrade docs' +manual_upgrade_msg: '{{ meta.name }} requires a manual upgrade. Please refer to the upgrade documentation at https://github.com/ansible/awx-operator#upgrade-notes' From 597356f317db66badb4233bf488692ce6f0746c8 Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Wed, 21 Apr 2021 14:58:21 -0400 Subject: [PATCH 3/4] Handles deleting and recreating statefulset and deployment when needed --- README.md | 6 +-- .../tasks/check_and_scale_down_deployment.yml | 19 ++++++++ .../tasks/database_configuration.yml | 30 +++++++------ roles/installer/tasks/migrate_data.yml | 18 +------- .../tasks/resources_configuration.yml | 44 +++++++++---------- roles/installer/vars/main.yml | 1 - 6 files changed, 61 insertions(+), 57 deletions(-) create mode 100644 roles/installer/tasks/check_and_scale_down_deployment.yml diff --git a/README.md b/README.md index 822d2193..85d8ee4a 100644 --- a/README.md +++ b/README.md @@ -483,11 +483,9 @@ Example spec file for volumes and volume mounts ### From Older Versions -For `AWX` instances created by the `awx-operator<=0.0.7` a manual upgrade will be required. On version `0.0.8` a new set of `labels` were introduced to the PostgreSQL `statefulset` (when applied) and the AWX `deployment`. Since `selector.matchLabels` are `ready-only` by the Kubernetes API, the resource must be deleted and recreated. +For `AWX` instances created by the `awx-operator<0.0.8`, it is required both PostgreSQL `statefulset` and AWX `deployment` resources to be deleted and recreated. This is required due to new labels added on both resources and the requirement of the Kubernetes API which enforces `selector.matchLabels` attributes to be `ready-only`. -The `awx-operator` to avoid deleting data from environments which the `persistence` layer is not configured to retain the data, it will be up to the system administrator to perform such operator. For instances created from `awx-operator>=0.0.8`, no manual intervetion shall be necessary. - -For example, if you are running a `managed` PostgreSQL configured instance and have the cluster storage layer to maintain the `PV` storage area upon the `statefulset` deletion, you could just delete the `awx` resource and recreate it on the newer version. +The `awx-operator` will handle the upgrading both resources. Note that just the `statefulset` and `deployment` will be recreated. Therefore, any `persistent volume` used on any of these 2 resources, **shall not be deleted**. ## Development diff --git a/roles/installer/tasks/check_and_scale_down_deployment.yml b/roles/installer/tasks/check_and_scale_down_deployment.yml new file mode 100644 index 00000000..cab7af23 --- /dev/null +++ b/roles/installer/tasks/check_and_scale_down_deployment.yml @@ -0,0 +1,19 @@ +--- + +- name: Check for presence of Deployment + k8s_info: + api_version: v1 + kind: Deployment + name: "{{ meta.name }}" + namespace: "{{ meta.namespace }}" + register: tower_deployment + +- name: Scale down Deployment for migration + k8s_scale: + api_version: v1 + kind: Deployment + name: "{{ meta.name }}" + namespace: "{{ meta.namespace }}" + replicas: 0 + wait: yes + when: tower_deployment['resources'] | length diff --git a/roles/installer/tasks/database_configuration.yml b/roles/installer/tasks/database_configuration.yml index af40c024..7065cfe4 100644 --- a/roles/installer/tasks/database_configuration.yml +++ b/roles/installer/tasks/database_configuration.yml @@ -72,28 +72,32 @@ name: '{{ meta.name }}-postgres' register: _postgres_statefulset_result -- name: Set manual_statefulset_upgrade to false since PostgreSQL statefulset does not exist yet +- name: Determine if PostgreSQL statefulset must be recreated set_fact: - manual_statefulset_upgrade: False - when: not _postgres_statefulset_result['resources'] | length + # yamllint disable-line rule:line-length + recreate_statefulset: '{{ _postgres_statefulset_result["resources"][0]["metadata"]["labels"]["app.kubernetes.io/managed-by"] | default("") | ternary("False", "True") }}' # noqa 204 + when: _postgres_statefulset_result['resources'] | length -- name: Determine if PostgreSQL statefulset requires manual upgrade - set_fact: - manual_statefulset_upgrade: '{{ _postgres_statefulset_result["resources"][0]["metadata"]["labels"]["app.kubernetes.io/managed-by"] | default("") | ternary("False", "True") }}' - when: manual_statefulset_upgrade is not defined +- block: + - name: Check and scale down deployment + include_tasks: check_and_scale_down_deployment.yml -- name: Show help message since PostgreSQL statefulset was created by a legacy version - debug: - var: manual_upgrade_msg - when: manual_statefulset_upgrade|bool + - name: Delete PostgreSQL statefulset to ensure expected labels + k8s: + state: absent + api_version: v1 + kind: StatefulSet + namespace: '{{ meta.namespace }}' + name: '{{ meta.name }}-postgres' + wait: yes + when: recreate_statefulset is defined and recreate_statefulset|bool - name: Create Database if no database is specified k8s: apply: true definition: "{{ lookup('template', 'tower_postgres.yaml.j2') }}" when: - - pg_config['resources'][0]['data']['type'] | default('') | b64decode == 'managed' - - not manual_statefulset_upgrade|bool + - pg_config['resources'][0]['data']['type'] | default('') | b64decode == 'managed' or recreate_statefulset|bool - name: Store Database Configuration set_fact: diff --git a/roles/installer/tasks/migrate_data.yml b/roles/installer/tasks/migrate_data.yml index da5067c0..966c8d14 100644 --- a/roles/installer/tasks/migrate_data.yml +++ b/roles/installer/tasks/migrate_data.yml @@ -24,22 +24,8 @@ set_fact: postgres_pod_name: "{{ postgres_pod['resources'][0]['metadata']['name'] }}" -- name: Check for presence of Deployment - k8s_info: - api_version: v1 - kind: Deployment - name: "{{ meta.name }}" - namespace: "{{ meta.namespace }}" - register: tower_deployment - -- name: Scale down Deployment for migration - k8s_scale: - api_version: v1 - kind: Deployment - name: "{{ meta.name }}" - namespace: "{{ meta.namespace }}" - replicas: 0 - when: tower_deployment['resources'] | length +- name: Check and scale down deployment + include_tasks: check_and_scale_down_deployment.yml - name: Set pg_dump command set_fact: diff --git a/roles/installer/tasks/resources_configuration.yml b/roles/installer/tasks/resources_configuration.yml index 90818953..5e5412ea 100644 --- a/roles/installer/tasks/resources_configuration.yml +++ b/roles/installer/tasks/resources_configuration.yml @@ -8,24 +8,11 @@ name: '{{ meta.name }}' register: _deployment_result -- name: Set manual_deployment_upgrade to false since deployment does not exist yet +- name: Determine if deployment must be recreated set_fact: - manual_deployment_upgrade: False - when: not _deployment_result['resources'] | length - -- name: Determine if deployment requires manual upgrade - set_fact: - manual_deployment_upgrade: '{{ _deployment_result["resources"][0]["metadata"]["labels"]["app.kubernetes.io/managed-by"] | default("") | ternary("False", "True") }}' - when: manual_deployment_upgrade is not defined - -- name: Determine pod label_selector to be used - set_fact: - tower_pod_label_selector: '{{ manual_deployment_upgrade|bool | default("false"|bool)| ternary("app={{ deployment_type }}", "app.kubernetes.io/name={{ meta.name }}") }}' - -- name: Show help message since deployment was created by a legacy version - debug: - var: manual_upgrade_msg - when: manual_deployment_upgrade|bool + # yamllint disable-line rule:line-length + recreate_deployment: '{{ _deployment_result["resources"][0]["metadata"]["labels"]["app.kubernetes.io/managed-by"] | default("") | ternary("False", "True") }}' # noqa 204 + when: _deployment_result['resources'] | length - name: Get the current resource pod information. k8s_info: @@ -33,7 +20,9 @@ kind: Pod namespace: '{{ meta.namespace }}' label_selectors: - - '{{ tower_pod_label_selector }}' + - "app.kubernetes.io/name={{ meta.name }}" + - "app.kubernetes.io/managed-by=awx-operator" + - "app.kubernetes.io/component=awx" field_selectors: - status.phase=Running register: tower_pods @@ -43,6 +32,16 @@ set_fact: tower_pod_name: "{{ tower_pods['resources'][0]['metadata']['name'] | default('') }}" +- name: Delete deployment to ensure expected labels + k8s: + state: absent + api_version: v1 + kind: Deployment + namespace: '{{ meta.namespace }}' + name: '{{ meta.name }}' + wait: yes + when: recreate_deployment is defined and recreate_deployment|bool + - name: Apply Resources k8s: apply: yes @@ -56,7 +55,6 @@ - 'tower_persistent' - 'tower_service' - 'tower_ingress' - when: not manual_deployment_upgrade|bool - name: Apply deployment resources k8s: @@ -64,10 +62,9 @@ definition: "{{ lookup('template', 'tower_deployment.yaml.j2') | from_yaml }}" wait: yes register: tower_deployment_result - when: not manual_deployment_upgrade|bool - block: - - name: Delete pod to reload configuration + - name: Delete pod to reload a resource configuration k8s: api_version: v1 state: absent @@ -84,7 +81,9 @@ kind: Pod namespace: '{{ meta.namespace }}' label_selectors: - - '{{ tower_pod_label_selector }}' + - "app.kubernetes.io/name={{ meta.name }}" + - "app.kubernetes.io/managed-by=awx-operator" + - "app.kubernetes.io/component=awx" field_selectors: - status.phase=Running register: _new_pod @@ -93,7 +92,6 @@ - _new_pod['resources'][0]['metadata']['name'] != tower_pod_name delay: 5 retries: 60 - when: not manual_deployment_upgrade|bool - name: Update new resource pod name as a variable. set_fact: diff --git a/roles/installer/vars/main.yml b/roles/installer/vars/main.yml index cd78fd7a..b454ee74 100644 --- a/roles/installer/vars/main.yml +++ b/roles/installer/vars/main.yml @@ -3,4 +3,3 @@ postgres_initdb_args: '--auth-host=scram-sha-256' postgres_host_auth_method: 'scram-sha-256' ldap_cacert_ca_crt: '' tower_projects_existing_claim: '' -manual_upgrade_msg: '{{ meta.name }} requires a manual upgrade. Please refer to the upgrade documentation at https://github.com/ansible/awx-operator#upgrade-notes' From 46fca2af5afd709e9d1ddd2f21e8c760563ce3cb Mon Sep 17 00:00:00 2001 From: Marcelo Moreira de Mello Date: Wed, 21 Apr 2021 15:41:18 -0400 Subject: [PATCH 4/4] updated --- README.md | 9 ------ .../tasks/check_and_scale_down_deployment.yml | 19 ------------ .../tasks/database_configuration.yml | 30 +------------------ roles/installer/tasks/migrate_data.yml | 19 ++++++++++-- .../tasks/resources_configuration.yml | 25 ---------------- 5 files changed, 18 insertions(+), 84 deletions(-) delete mode 100644 roles/installer/tasks/check_and_scale_down_deployment.yml diff --git a/README.md b/README.md index c3092d51..5f2d675f 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,6 @@ An [Ansible AWX](https://github.com/ansible/awx) operator for Kubernetes built w * [LDAP Certificate Authority](#ldap-certificate-authority) * [Persisting Projects Directory](#persisting-projects-directory) * [Custom Volume and Volume Mount Options](#custom-volume-and-volume-mount-options) - * [Upgrade Notes](#upgrade-notes) - * [From Older Versions](#from-older-versions) * [Exporting Environment Variables to Containers](#exporting-environment-variables-to-containers) * [Development](#development) * [Testing](#testing) @@ -482,13 +480,6 @@ Example spec file for volumes and volume mounts > :warning: **Volume and VolumeMount names cannot contain underscores(_)** -## Upgrade Notes - -### From Older Versions - -For `AWX` instances created by the `awx-operator<0.0.8`, it is required both PostgreSQL `statefulset` and AWX `deployment` resources to be deleted and recreated. This is required due to new labels added on both resources and the requirement of the Kubernetes API which enforces `selector.matchLabels` attributes to be `ready-only`. - -The `awx-operator` will handle the upgrading both resources. Note that just the `statefulset` and `deployment` will be recreated. Therefore, any `persistent volume` used on any of these 2 resources, **shall not be deleted**. #### Exporting Environment Variables to Containers If you need to export custom environment variables to your containers. diff --git a/roles/installer/tasks/check_and_scale_down_deployment.yml b/roles/installer/tasks/check_and_scale_down_deployment.yml deleted file mode 100644 index cab7af23..00000000 --- a/roles/installer/tasks/check_and_scale_down_deployment.yml +++ /dev/null @@ -1,19 +0,0 @@ ---- - -- name: Check for presence of Deployment - k8s_info: - api_version: v1 - kind: Deployment - name: "{{ meta.name }}" - namespace: "{{ meta.namespace }}" - register: tower_deployment - -- name: Scale down Deployment for migration - k8s_scale: - api_version: v1 - kind: Deployment - name: "{{ meta.name }}" - namespace: "{{ meta.namespace }}" - replicas: 0 - wait: yes - when: tower_deployment['resources'] | length diff --git a/roles/installer/tasks/database_configuration.yml b/roles/installer/tasks/database_configuration.yml index 7065cfe4..a79f55c4 100644 --- a/roles/installer/tasks/database_configuration.yml +++ b/roles/installer/tasks/database_configuration.yml @@ -64,40 +64,12 @@ set_fact: pg_config: '{{ _generated_pg_config_resources["resources"] | default([]) | length | ternary(_generated_pg_config_resources, _pg_config) }}' -- name: Check if PostgreSQL statefuset is present - k8s_info: - api_version: v1 - kind: StatefulSet - namespace: '{{ meta.namespace }}' - name: '{{ meta.name }}-postgres' - register: _postgres_statefulset_result - -- name: Determine if PostgreSQL statefulset must be recreated - set_fact: - # yamllint disable-line rule:line-length - recreate_statefulset: '{{ _postgres_statefulset_result["resources"][0]["metadata"]["labels"]["app.kubernetes.io/managed-by"] | default("") | ternary("False", "True") }}' # noqa 204 - when: _postgres_statefulset_result['resources'] | length - -- block: - - name: Check and scale down deployment - include_tasks: check_and_scale_down_deployment.yml - - - name: Delete PostgreSQL statefulset to ensure expected labels - k8s: - state: absent - api_version: v1 - kind: StatefulSet - namespace: '{{ meta.namespace }}' - name: '{{ meta.name }}-postgres' - wait: yes - when: recreate_statefulset is defined and recreate_statefulset|bool - - name: Create Database if no database is specified k8s: apply: true definition: "{{ lookup('template', 'tower_postgres.yaml.j2') }}" when: - - pg_config['resources'][0]['data']['type'] | default('') | b64decode == 'managed' or recreate_statefulset|bool + - pg_config['resources'][0]['data']['type'] | default('') | b64decode == 'managed' - name: Store Database Configuration set_fact: diff --git a/roles/installer/tasks/migrate_data.yml b/roles/installer/tasks/migrate_data.yml index 966c8d14..63ec4c75 100644 --- a/roles/installer/tasks/migrate_data.yml +++ b/roles/installer/tasks/migrate_data.yml @@ -24,8 +24,23 @@ set_fact: postgres_pod_name: "{{ postgres_pod['resources'][0]['metadata']['name'] }}" -- name: Check and scale down deployment - include_tasks: check_and_scale_down_deployment.yml +- name: Check for presence of Deployment + k8s_info: + api_version: v1 + kind: Deployment + name: "{{ meta.name }}" + namespace: "{{ meta.namespace }}" + register: tower_deployment + +- name: Scale down Deployment for migration + k8s_scale: + api_version: v1 + kind: Deployment + name: "{{ meta.name }}" + namespace: "{{ meta.namespace }}" + replicas: 0 + wait: yes + when: tower_deployment['resources'] | length - name: Set pg_dump command set_fact: diff --git a/roles/installer/tasks/resources_configuration.yml b/roles/installer/tasks/resources_configuration.yml index 5e5412ea..703191bc 100644 --- a/roles/installer/tasks/resources_configuration.yml +++ b/roles/installer/tasks/resources_configuration.yml @@ -1,19 +1,5 @@ --- -- name: Check if deployment is present - k8s_info: - api_version: v1 - kind: Deployment - namespace: '{{ meta.namespace }}' - name: '{{ meta.name }}' - register: _deployment_result - -- name: Determine if deployment must be recreated - set_fact: - # yamllint disable-line rule:line-length - recreate_deployment: '{{ _deployment_result["resources"][0]["metadata"]["labels"]["app.kubernetes.io/managed-by"] | default("") | ternary("False", "True") }}' # noqa 204 - when: _deployment_result['resources'] | length - - name: Get the current resource pod information. k8s_info: api_version: v1 @@ -26,22 +12,11 @@ field_selectors: - status.phase=Running register: tower_pods - changed_when: tower_pods["resources"] | length - name: Set the resource pod name as a variable. set_fact: tower_pod_name: "{{ tower_pods['resources'][0]['metadata']['name'] | default('') }}" -- name: Delete deployment to ensure expected labels - k8s: - state: absent - api_version: v1 - kind: Deployment - namespace: '{{ meta.namespace }}' - name: '{{ meta.name }}' - wait: yes - when: recreate_deployment is defined and recreate_deployment|bool - - name: Apply Resources k8s: apply: yes