2024-07-31 17:13:39 -07:00
|
|
|
import os
|
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2024-09-18 20:03:26 -07:00
|
|
|
import yaml
|
2024-07-31 17:13:39 -07:00
|
|
|
|
|
|
|
|
ENVOY_CONFIG_TEMPLATE_FILE = os.getenv('ENVOY_CONFIG_TEMPLATE_FILE', 'envoy.template.yaml')
|
2024-09-17 08:56:47 -07:00
|
|
|
BOLT_CONFIG_FILE = os.getenv('BOLT_CONFIG_FILE', 'bolt_config.yaml')
|
2024-07-31 17:13:39 -07:00
|
|
|
ENVOY_CONFIG_FILE_RENDERED = os.getenv('ENVOY_CONFIG_FILE_RENDERED', '/usr/src/app/out/envoy.yaml')
|
|
|
|
|
|
|
|
|
|
env = Environment(loader=FileSystemLoader('./'))
|
|
|
|
|
template = env.get_template('envoy.template.yaml')
|
|
|
|
|
|
2024-09-17 08:47:35 -07:00
|
|
|
with open(BOLT_CONFIG_FILE, 'r') as file:
|
2024-07-31 17:13:39 -07:00
|
|
|
katanemo_config = file.read()
|
|
|
|
|
|
2024-09-18 20:03:26 -07:00
|
|
|
config_yaml = yaml.safe_load(katanemo_config)
|
|
|
|
|
|
|
|
|
|
inferred_clusters = {}
|
|
|
|
|
|
|
|
|
|
for prompt_target in config_yaml["prompt_targets"]:
|
|
|
|
|
cluster = prompt_target.get("endpoint", {}).get("cluster", "")
|
|
|
|
|
if cluster not in inferred_clusters:
|
|
|
|
|
inferred_clusters[cluster] = {
|
|
|
|
|
"name": cluster,
|
|
|
|
|
"address": cluster,
|
|
|
|
|
"port": 80, # default port
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print(inferred_clusters)
|
|
|
|
|
|
|
|
|
|
clusters = config_yaml.get("clusters", {})
|
|
|
|
|
|
|
|
|
|
# override the inferred clusters with the ones defined in the config
|
|
|
|
|
for name, cluster in clusters.items():
|
|
|
|
|
if name in inferred_clusters:
|
|
|
|
|
print("updating cluster", cluster)
|
|
|
|
|
inferred_clusters[name].update(cluster)
|
|
|
|
|
else:
|
|
|
|
|
inferred_clusters[name] = cluster
|
|
|
|
|
|
|
|
|
|
print("updated clusters", inferred_clusters)
|
|
|
|
|
|
2024-07-31 17:13:39 -07:00
|
|
|
data = {
|
2024-09-18 20:03:26 -07:00
|
|
|
'katanemo_config': katanemo_config,
|
|
|
|
|
'arch_clusters': inferred_clusters
|
2024-07-31 17:13:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rendered = template.render(data)
|
|
|
|
|
print(rendered)
|
|
|
|
|
print(ENVOY_CONFIG_FILE_RENDERED)
|
|
|
|
|
with open(ENVOY_CONFIG_FILE_RENDERED, 'w') as file:
|
|
|
|
|
file.write(rendered)
|