Skip to content

Commit 3b9ebde

Browse files
scripts: add an abstraction for updating the configs. Will be used to update configs dynamically.
1 parent d647187 commit 3b9ebde

File tree

4 files changed

+69
-22
lines changed

4 files changed

+69
-22
lines changed

scripts/prod/restart_all_nodes_together.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import urllib.request
88
from update_config_and_restart_nodes_lib import (
99
ApolloArgsParserBuilder,
10+
ConstConfigValuesUpdater,
1011
NamespaceAndInstructionArgs,
1112
RestartStrategy,
1213
Service,
@@ -137,7 +138,7 @@ def main():
137138
)
138139

139140
update_config_and_restart_nodes(
140-
config_overrides,
141+
ConstConfigValuesUpdater(config_overrides),
141142
namespace_and_instruction_args,
142143
Service.Core,
143144
restarter,

scripts/prod/set_node_revert_mode.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import urllib.parse
77
from update_config_and_restart_nodes_lib import (
88
ApolloArgsParserBuilder,
9+
ConstConfigValuesUpdater,
910
NamespaceAndInstructionArgs,
1011
RestartStrategy,
1112
Service,
@@ -65,7 +66,7 @@ def set_revert_mode(
6566
)
6667

6768
update_config_and_restart_nodes(
68-
config_overrides,
69+
ConstConfigValuesUpdater(config_overrides),
6970
namespace_and_instruction_args,
7071
Service.Core,
7172
restarter,

scripts/prod/update_config_and_restart_nodes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from update_config_and_restart_nodes_lib import (
99
ApolloArgsParserBuilder,
1010
Colors,
11+
ConstConfigValuesUpdater,
1112
RestartStrategy,
1213
NamespaceAndInstructionArgs,
1314
Service,
@@ -165,7 +166,10 @@ def main():
165166
)
166167

167168
update_config_and_restart_nodes(
168-
config_overrides, namespace_and_instruction_args, args.service, restarter
169+
ConstConfigValuesUpdater(config_overrides),
170+
namespace_and_instruction_args,
171+
args.service,
172+
restarter,
169173
)
170174

171175

scripts/prod/update_config_and_restart_nodes_lib.py

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,63 @@ def get_context_list_from_args(
274274
]
275275

276276

277+
class ConfigValuesUpdater(ABC):
278+
"""Abstract class for updating configuration values for different service instances."""
279+
280+
def get_updated_config(self, orig_config_yaml: str, instance_index: int) -> str:
281+
"""Get updated configuration YAML for a specific instance.
282+
283+
Args:
284+
orig_config_yaml: Original configuration as YAML string
285+
instance_index: Index of the instance to update configuration for
286+
287+
Returns:
288+
Updated configuration as YAML string
289+
"""
290+
config, config_data = parse_config_from_yaml(orig_config_yaml)
291+
updated_config_data = self.get_updated_config_for_instance(config_data, instance_index)
292+
return serialize_config_to_yaml(config, updated_config_data)
293+
294+
@abstractmethod
295+
def get_updated_config_for_instance(
296+
self, config_data: dict[str, any], instance_index: int
297+
) -> dict[str, any]:
298+
"""Get updated configuration data for a specific instance.
299+
300+
Args:
301+
config_data: Current configuration data dictionary
302+
instance_index: Index of the instance to update configuration for
303+
304+
Returns:
305+
Updated configuration data dictionary
306+
"""
307+
pass
308+
309+
310+
class ConstConfigValuesUpdater(ConfigValuesUpdater):
311+
"""Concrete implementation that applies constant configuration overrides."""
312+
313+
def __init__(self, config_overrides: dict[str, any]):
314+
"""Initialize with configuration overrides.
315+
316+
Args:
317+
config_overrides: Dictionary of configuration keys and values to override
318+
"""
319+
self.config_overrides = config_overrides
320+
321+
def get_updated_config_for_instance(
322+
self, config_data: dict[str, any], instance_index: int
323+
) -> dict[str, any]:
324+
"""Apply the same configuration overrides to the config data for each instance."""
325+
updated_config = config_data.copy()
326+
327+
for key, value in self.config_overrides.items():
328+
print_colored(f" Overriding config: {key} = {value}")
329+
updated_config[key] = value
330+
331+
return updated_config
332+
333+
277334
class ServiceRestarter(ABC):
278335
"""Abstract class for restarting service instances."""
279336

@@ -541,22 +598,6 @@ def represent_literal_str(dumper, data):
541598
return result
542599

543600

544-
def update_config_values(
545-
config_content: str,
546-
config_overrides: dict[str, Any] = None,
547-
) -> str:
548-
"""Update configuration values in the YAML content and return the updated YAML"""
549-
# Parse the configuration
550-
config, config_data = parse_config_from_yaml(config_content)
551-
552-
for key, value in config_overrides.items():
553-
print_colored(f" Overriding config: {key} = {value}")
554-
config_data[key] = value
555-
556-
# Serialize back to YAML
557-
return serialize_config_to_yaml(config, config_data)
558-
559-
560601
def normalize_config(config_content: str) -> str:
561602
"""Normalize configuration by parsing and re-serializing without changes.
562603
@@ -633,12 +674,12 @@ def apply_configmap(
633674

634675

635676
def update_config_and_restart_nodes(
636-
config_overrides: dict[str, Any],
677+
config_values_updater: ConfigValuesUpdater,
637678
namespace_and_instruction_args: NamespaceAndInstructionArgs,
638679
service: Service,
639680
restarter: ServiceRestarter,
640681
) -> None:
641-
assert config_overrides is not None, "config_overrides must be provided"
682+
assert config_values_updater is not None, "config_values_updater must be provided"
642683
assert namespace_and_instruction_args.namespace_list is not None, "namespaces must be provided"
643684

644685
if not namespace_and_instruction_args.cluster_list:
@@ -669,7 +710,7 @@ def update_config_and_restart_nodes(
669710
)
670711

671712
# Update config
672-
updated_config = update_config_values(original_config, config_overrides)
713+
updated_config = config_values_updater.get_updated_config(original_config, index)
673714

674715
# Store configs
675716
configs.append({"original": original_config, "updated": updated_config})

0 commit comments

Comments
 (0)