From ae21f317858133958348bcbe8202f0f1d2a28722 Mon Sep 17 00:00:00 2001 From: Jan Gutter Date: Tue, 1 Aug 2023 14:09:01 +0100 Subject: [PATCH] etcd: deduplicate environments for containers * etcd service containers usually have a set of environment parameters required to boot the container. * The short-lived etcd bootstrap containers pass extra ETCD_INITIAL_* environment variables, but still need to pass the ones that the service containers use. * This uses ansible's `combine` filter to cut down on the duplication. * This is intended to be just a straightforward refactor. Change-Id: I04e95f92a8f365553afd618d58b99de595d48312 --- ansible/roles/etcd/defaults/main.yml | 57 ++++++++++++------- .../roles/etcd/tasks/bootstrap_cluster.yml | 21 +------ .../roles/etcd/tasks/bootstrap_services.yml | 21 +------ 3 files changed, 40 insertions(+), 59 deletions(-) diff --git a/ansible/roles/etcd/defaults/main.yml b/ansible/roles/etcd/defaults/main.yml index 2421f16faf..c19bd2f328 100644 --- a/ansible/roles/etcd/defaults/main.yml +++ b/ansible/roles/etcd/defaults/main.yml @@ -4,25 +4,7 @@ etcd_services: container_name: etcd group: etcd enabled: true - environment: - # KOLLA_BOOTSTRAP_STATUS is used to indicate whether the container should - # be recreated. Otherwise the kolla_container task doesn't detect that the - # environment has changed if variables are removed. - KOLLA_BOOTSTRAP_STATUS: "bootstrap completed" - ETCDCTL_API: "3" - ETCDCTL_ENDPOINTS: "{{ etcd_client_internal_endpoint }}" - ETCDCTL_WRITE_OUT: "json" - ETCD_DATA_DIR: "/var/lib/etcd" - ETCD_NAME: "{{ ansible_facts.hostname }}" - ETCD_ADVERTISE_CLIENT_URLS: "{{ etcd_client_internal_endpoint }}" - ETCD_LISTEN_CLIENT_URLS: "{{ etcd_client_internal_endpoint }}" - ETCD_LISTEN_PEER_URLS: "{{ etcd_peer_internal_endpoint }}" - ETCD_OUT_FILE: "/var/log/kolla/etcd/etcd.log" - KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" - ETCD_CERT_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-cert.pem{% endif %}" - ETCD_KEY_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-key.pem{% endif %}" - ETCD_PEER_CERT_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-cert.pem{% endif %}" - ETCD_PEER_KEY_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-key.pem{% endif %}" + environment: "{{ etcd_base_environment | combine(etcd_default_environment) }}" image: "{{ etcd_image_full }}" volumes: "{{ etcd_default_volumes + etcd_extra_volumes }}" dimensions: "{{ etcd_dimensions }}" @@ -34,6 +16,43 @@ etcd_services: port: "{{ etcd_client_port }}" tls_backend: "{{ etcd_enable_tls | bool }}" +#################### +# Environment +#################### +etcd_base_environment: + # KOLLA_BOOTSTRAP_STATUS is used to indicate whether the container should + # be recreated. Otherwise the kolla_container task doesn't detect that the + # environment has changed if variables are removed. + KOLLA_BOOTSTRAP_STATUS: "undefined" + ETCDCTL_API: "3" + ETCDCTL_ENDPOINTS: "{{ etcd_client_internal_endpoint }}" + ETCDCTL_WRITE_OUT: "json" + ETCD_DATA_DIR: "/var/lib/etcd" + ETCD_NAME: "{{ ansible_facts.hostname }}" + ETCD_ADVERTISE_CLIENT_URLS: "{{ etcd_client_internal_endpoint }}" + ETCD_LISTEN_CLIENT_URLS: "{{ etcd_client_internal_endpoint }}" + ETCD_LISTEN_PEER_URLS: "{{ etcd_peer_internal_endpoint }}" + ETCD_OUT_FILE: "/var/log/kolla/etcd/etcd.log" + KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" + ETCD_CERT_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-cert.pem{% endif %}" + ETCD_KEY_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-key.pem{% endif %}" + ETCD_PEER_CERT_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-cert.pem{% endif %}" + ETCD_PEER_KEY_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-key.pem{% endif %}" +etcd_default_environment: + KOLLA_BOOTSTRAP_STATUS: "bootstrap completed" +etcd_bootstrap_service_environment: + KOLLA_BOOTSTRAP_STATUS: "bootstrap service" + ETCD_INITIAL_CLUSTER_STATE: "existing" + ETCD_INITIAL_ADVERTISE_PEER_URLS: "{{ etcd_peer_internal_endpoint }}" + ETCD_INITIAL_CLUSTER_TOKEN: "{{ etcd_cluster_token }}" + ETCD_INITIAL_CLUSTER: "{% for host in groups['etcd_had_volume_True'] %}{{ hostvars[host].ansible_facts.hostname }}={{ etcd_protocol }}://{{ 'api' | kolla_address(host) | put_address_in_context('url') }}:{{ etcd_peer_port }},{% endfor %}{{ ansible_facts.hostname }}={{ etcd_protocol }}://{{ 'api' | kolla_address(inventory_hostname) | put_address_in_context('url') }}:{{ etcd_peer_port }}" +etcd_bootstrap_cluster_environment: + KOLLA_BOOTSTRAP_STATUS: "bootstrap cluster" + ETCD_INITIAL_CLUSTER_STATE: "new" + ETCD_INITIAL_ADVERTISE_PEER_URLS: "{{ etcd_peer_internal_endpoint }}" + ETCD_INITIAL_CLUSTER_TOKEN: "{{ etcd_cluster_token }}" + ETCD_INITIAL_CLUSTER: "{% for host in groups['etcd'] %}{{ hostvars[host].ansible_facts.hostname }}={{ etcd_protocol }}://{{ 'api' | kolla_address(host) | put_address_in_context('url') }}:{{ etcd_peer_port }}{% if not loop.last %},{% endif %}{% endfor %}" + #################### # Docker #################### diff --git a/ansible/roles/etcd/tasks/bootstrap_cluster.yml b/ansible/roles/etcd/tasks/bootstrap_cluster.yml index 5c627f9e5d..bf9bbfee9b 100644 --- a/ansible/roles/etcd/tasks/bootstrap_cluster.yml +++ b/ansible/roles/etcd/tasks/bootstrap_cluster.yml @@ -7,26 +7,7 @@ kolla_container: action: "start_container" common_options: "{{ docker_common_options }}" - environment: - KOLLA_BOOTSTRAP_STATUS: "bootstrap cluster" - ETCD_INITIAL_CLUSTER_STATE: "new" - ETCD_INITIAL_ADVERTISE_PEER_URLS: "{{ etcd_peer_internal_endpoint }}" - ETCD_INITIAL_CLUSTER_TOKEN: "{{ etcd_cluster_token }}" - ETCD_INITIAL_CLUSTER: "{% for host in groups['etcd'] %}{{ hostvars[host].ansible_facts.hostname }}={{ etcd_protocol }}://{{ 'api' | kolla_address(host) | put_address_in_context('url') }}:{{ etcd_peer_port }}{% if not loop.last %},{% endif %}{% endfor %}" - ETCDCTL_API: "3" - ETCDCTL_ENDPOINTS: "{{ etcd_client_internal_endpoint }}" - ETCDCTL_WRITE_OUT: "json" - ETCD_DATA_DIR: "/var/lib/etcd" - ETCD_NAME: "{{ ansible_facts.hostname }}" - ETCD_ADVERTISE_CLIENT_URLS: "{{ etcd_client_internal_endpoint }}" - ETCD_LISTEN_CLIENT_URLS: "{{ etcd_client_internal_endpoint }}" - ETCD_LISTEN_PEER_URLS: "{{ etcd_peer_internal_endpoint }}" - ETCD_OUT_FILE: "/var/log/kolla/etcd/etcd.log" - KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" - ETCD_CERT_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-cert.pem{% endif %}" - ETCD_KEY_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-key.pem{% endif %}" - ETCD_PEER_CERT_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-cert.pem{% endif %}" - ETCD_PEER_KEY_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-key.pem{% endif %}" + environment: "{{ etcd_base_environment | combine(etcd_bootstrap_cluster_environment) }}" image: "{{ service.image }}" name: "{{ service.container_name }}" volumes: "{{ service.volumes }}" diff --git a/ansible/roles/etcd/tasks/bootstrap_services.yml b/ansible/roles/etcd/tasks/bootstrap_services.yml index 05e18c4971..3630a38d10 100644 --- a/ansible/roles/etcd/tasks/bootstrap_services.yml +++ b/ansible/roles/etcd/tasks/bootstrap_services.yml @@ -18,26 +18,7 @@ kolla_container: action: "start_container" common_options: "{{ docker_common_options }}" - environment: - KOLLA_BOOTSTRAP_STATUS: "bootstrap service" - ETCD_INITIAL_CLUSTER_STATE: "existing" - ETCD_INITIAL_ADVERTISE_PEER_URLS: "{{ etcd_peer_internal_endpoint }}" - ETCD_INITIAL_CLUSTER_TOKEN: "{{ etcd_cluster_token }}" - ETCD_INITIAL_CLUSTER: "{% for host in groups['etcd_had_volume_True'] %}{{ hostvars[host].ansible_facts.hostname }}={{ etcd_protocol }}://{{ 'api' | kolla_address(host) | put_address_in_context('url') }}:{{ etcd_peer_port }},{% endfor %}{{ ansible_facts.hostname }}={{ etcd_protocol }}://{{ 'api' | kolla_address(inventory_hostname) | put_address_in_context('url') }}:{{ etcd_peer_port }}" - ETCDCTL_API: "3" - ETCDCTL_ENDPOINTS: "{{ etcd_client_internal_endpoint }}" - ETCDCTL_WRITE_OUT: "json" - ETCD_DATA_DIR: "/var/lib/etcd" - ETCD_NAME: "{{ ansible_facts.hostname }}" - ETCD_ADVERTISE_CLIENT_URLS: "{{ etcd_client_internal_endpoint }}" - ETCD_LISTEN_CLIENT_URLS: "{{ etcd_client_internal_endpoint }}" - ETCD_LISTEN_PEER_URLS: "{{ etcd_peer_internal_endpoint }}" - ETCD_OUT_FILE: "/var/log/kolla/etcd/etcd.log" - KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" - ETCD_CERT_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-cert.pem{% endif %}" - ETCD_KEY_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-key.pem{% endif %}" - ETCD_PEER_CERT_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-cert.pem{% endif %}" - ETCD_PEER_KEY_FILE: "{% if etcd_enable_tls | bool %}/etc/etcd/certs/etcd-key.pem{% endif %}" + environment: "{{ etcd_base_environment | combine(etcd_bootstrap_service_environment) }}" image: "{{ service.image }}" name: "{{ service.container_name }}" volumes: "{{ service.volumes }}"