Skip to content

Commit

Permalink
[DPE-5764] handle config as dict (#4)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
reneradoi authored Nov 22, 2024
1 parent 8ab4111 commit c4262e7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 59 deletions.
76 changes: 17 additions & 59 deletions src/managers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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,
)

Expand Down
24 changes: 24 additions & 0 deletions src/managers/config/etcd.conf.yml
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit c4262e7

Please sign in to comment.