From c4262e738fac8da5cd012763b7239f6e123cb8ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Radoi?= Date: Fri, 22 Nov 2024 15:27:02 +0100 Subject: [PATCH] [DPE-5764] handle config as dict (#4) This PR improves the handling of config properties. Instead of using a list of strings, the config is loaded from a yaml-file and handled as a dict. This allows for controlling all config properties as part of the code while making it easier to add or update config properties. It also provides a cleaner approach. Key changes: - move config properties to a separate file - load config properties from yaml to dict instead of from string to list of strings - `config_properties` now returns a string instead of `list[str]` which makes it easier to write to a file --- src/managers/config.py | 76 +++++++------------------------ src/managers/config/etcd.conf.yml | 24 ++++++++++ 2 files changed, 41 insertions(+), 59 deletions(-) create mode 100644 src/managers/config/etcd.conf.yml diff --git a/src/managers/config.py b/src/managers/config.py index 99aeafb..898ba3e 100644 --- a/src/managers/config.py +++ b/src/managers/config.py @@ -5,7 +5,9 @@ """Manager for handling configuration building + writing.""" import logging +from pathlib import Path +import yaml from ops.model import ConfigData from core.cluster import ClusterState @@ -14,52 +16,7 @@ logger = logging.getLogger(__name__) -DEFAULT_PROPERTIES = """ -initial-cluster-token: 'etcd-cluster' -snapshot-count: 10000 -heartbeat-interval: 100 -election-timeout: 1000 -quota-backend-bytes: 0 -max-snapshots: 5 -max-wals: 5 -strict-reconfig-check: false -enable-pprof: true -proxy: 'off' -proxy-failure-wait: 5000 -proxy-refresh-interval: 30000 -proxy-dial-timeout: 1000 -proxy-write-timeout: 5000 -proxy-read-timeout: 0 -force-new-cluster: false -auto-compaction-mode: periodic -auto-compaction-retention: "1" -""" - -# these config properties are not used at the moment -# they are only listed here for completeness -TLS_PROPERTIES = """ -client-transport-security: - cert-file: - # Path to the client server TLS key file. - key-file: - client-cert-auth: false - trusted-ca-file: - auto-tls: false -peer-transport-security: - cert-file: - key-file: - client-cert-auth: false - trusted-ca-file: - auto-tls: false - allowed-cn: - allowed-hostname: -cipher-suites: [ - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 -] -tls-min-version: 'TLS1.2' -tls-max-version: 'TLS1.3' -""" +WORKING_DIR = Path(__file__).absolute().parent class ConfigManager: @@ -77,28 +34,29 @@ def __init__( self.config_file = CONFIG_FILE @property - def config_properties(self) -> list[str]: + def config_properties(self) -> str: """Assemble the config properties. Returns: List of properties to be written to the config file. """ - properties = [ - f"name: {self.state.unit_server.member_name}", - f"initial-advertise-peer-urls: {self.state.unit_server.peer_url}", - f"initial-cluster-state: {self.state.cluster.initial_cluster_state}", - f"listen-peer-urls: {self.state.unit_server.peer_url}", - f"listen-client-urls: {self.state.unit_server.client_url}", - f"advertise-client-urls: {self.state.unit_server.client_url}", - f"initial-cluster: {self._get_cluster_endpoints()}", - ] + DEFAULT_PROPERTIES.split("\n") - - return properties + with open(f"{WORKING_DIR}/config/etcd.conf.yml") as config: + config_properties = yaml.safe_load(config) + + config_properties["name"] = self.state.unit_server.member_name + config_properties["initial-advertise-peer-urls"] = self.state.unit_server.peer_url + config_properties["initial-cluster-state"] = self.state.cluster.initial_cluster_state + config_properties["listen-peer-urls"] = self.state.unit_server.peer_url + config_properties["listen-client-urls"] = self.state.unit_server.client_url + config_properties["advertise-client-urls"] = self.state.unit_server.client_url + config_properties["initial-cluster"] = self._get_cluster_endpoints() + + return yaml.safe_dump(config_properties) def set_config_properties(self) -> None: """Write the config properties to the config file.""" self.workload.write_file( - content="\n".join(self.config_properties), + content=self.config_properties, file=self.config_file, ) diff --git a/src/managers/config/etcd.conf.yml b/src/managers/config/etcd.conf.yml new file mode 100644 index 0000000..c31fa93 --- /dev/null +++ b/src/managers/config/etcd.conf.yml @@ -0,0 +1,24 @@ +initial-cluster-token: 'etcd-cluster' +snapshot-count: 10000 +heartbeat-interval: 100 +election-timeout: 1000 +quota-backend-bytes: 0 +max-snapshots: 5 +max-wals: 5 +strict-reconfig-check: false +enable-pprof: true +proxy: 'off' +proxy-failure-wait: 5000 +proxy-refresh-interval: 30000 +proxy-dial-timeout: 1000 +proxy-write-timeout: 5000 +proxy-read-timeout: 0 +force-new-cluster: false +auto-compaction-mode: periodic +auto-compaction-retention: "1" +cipher-suites: [ + TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, + TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 +] +tls-min-version: 'TLS1.2' +tls-max-version: 'TLS1.3' \ No newline at end of file