From d6910f01deb3fa41b2a7662d1d5eb4a7d1b5c50f Mon Sep 17 00:00:00 2001 From: Piotr Milewski Date: Thu, 26 Mar 2026 18:05:41 +0100 Subject: Add YAML support to kolla_set_configs engine Following the PTG agreement, this commit extends the container configuration engine (set_configs.py) to support YAML format while maintaining backward compatibility with JSON. - Integrated 'yaml' library as a universal parser for both JSON and YAML formats (YAML is a superset of JSON) - Updated 'load_config' to search for 'config.json', 'config.yaml' - Added 'python3-yaml' (Debian) and 'python3-pyyaml' (RHEL) as required base packages in Dockerfile.j2 This change improves configuration readability and allows for easier maintenance while ensuring no breakage for existing JSON-based deployments. Change-Id: Icfb52b8f94e1036d5952b160c5921348d1427fda Signed-off-by: Piotr Milewski --- docker/base/Dockerfile.j2 | 2 ++ docker/base/set_configs.py | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) (limited to 'docker/base') diff --git a/docker/base/Dockerfile.j2 b/docker/base/Dockerfile.j2 index a4255f98c..3825a2f79 100644 --- a/docker/base/Dockerfile.j2 +++ b/docker/base/Dockerfile.j2 @@ -138,6 +138,7 @@ RUN {{ macros.install_packages(base_centos_yum_repo_packages | customizable("cen 'procps-ng', 'python3', 'python3-pip', + 'python3-pyyaml', 'socat', 'sudo', 'tar', @@ -230,6 +231,7 @@ COPY apt_preferences /etc/apt/preferences.d/kolla-custom 'procps', 'python3', 'python3-pip', + 'python3-yaml', 'socat', 'sudo', 'tgt', diff --git a/docker/base/set_configs.py b/docker/base/set_configs.py index 1a80bd877..542537e4c 100644 --- a/docker/base/set_configs.py +++ b/docker/base/set_configs.py @@ -23,6 +23,7 @@ import re import shutil import stat import sys +import yaml # TODO(rhallisey): add docstring. @@ -298,13 +299,33 @@ def validate_source(data): def load_config(): def load_from_file(): - config_file = '/var/lib/kolla/config_files/config.json' - LOG.info("Loading config file at %s", config_file) + yaml_config_file = '/var/lib/kolla/config_files/config.yaml' + json_config_file = '/var/lib/kolla/config_files/config.json' - # Attempt to read config file - with open(config_file) as f: + if os.path.exists(yaml_config_file): + config_file = yaml_config_file + LOG.info("Loading config file at %s", config_file) try: - return json.load(f) + with open(config_file) as f: + config = yaml.safe_load(f) + + if config is None: + raise InvalidConfig( + "Invalid yaml file found at %s: " + "file is empty" % config_file) + return config + except yaml.YAMLError: + raise InvalidConfig( + "Invalid yaml file found at %s" % config_file) + except IOError as e: + raise InvalidConfig( + "Could not read file %s: %r" % (config_file, e)) + else: + config_file = json_config_file + LOG.info("Loading config file at %s", config_file) + try: + with open(config_file) as f: + return json.load(f) except ValueError: raise InvalidConfig( "Invalid json file found at %s" % config_file) @@ -631,7 +652,7 @@ def execute_config_check(config): """ state = get_defaults_state() - # Build a set of all current destination paths from config.json + # Build a set of all current destination paths from the config # If the destination is a directory, we append the # basename of the source current_dests = { @@ -642,7 +663,7 @@ def execute_config_check(config): } # Detect any paths that are present in the state file but - # missing from config.json. + # missing from the config file. # These would be either restored (if state[dest] has a backup) # or removed (if dest is null) removed_dests = [ @@ -653,7 +674,7 @@ def execute_config_check(config): if removed_dests: raise StateMismatch( f"The following config files are tracked in state but missing " - f"from config.json. " + f"from the config file. " f"They would be restored or removed: {sorted(removed_dests)}" ) -- cgit v1.2.3