11import uuid
2+ from importlib import resources
23from pathlib import Path
34
45import netifaces
1213 PodmanMount ,
1314 PodmanMountType ,
1415)
15-
16- from ._vector_template import VECTOR_CONFIG_TEMPLATE
16+ from interactem .core .nats .config import NatsMode , get_nats_config
1717
1818logger = get_logger ()
19+ VECTOR_NATS_CREDS_TARGET = "/nats.creds"
20+
1921
22+ def _load_vector_template () -> str :
23+ return resources .files (__package__ ).joinpath ("templates/vector.yaml.j2" ).read_text (encoding = "utf-8" )
2024
2125class Settings (BaseSettings ):
2226 model_config = SettingsConfigDict (env_file = ".env" , extra = "ignore" )
@@ -43,7 +47,7 @@ class Settings(BaseSettings):
4347 ALWAYS_PULL_IMAGES : bool = False
4448
4549 # Vector configuration
46- VECTOR_AGGREGATOR_ADDR : str | None = None
50+ VECTOR_ENABLED : bool = True
4751 LOG_DIR : Path = Path ("~/.interactem/logs" ).expanduser ().resolve ()
4852 VECTOR_CONFIG_PATH : Path | None = None
4953 OPERATOR_EXTRA_ENV : dict [str , str ] = Field (default_factory = dict , exclude = True )
@@ -125,26 +129,52 @@ def vector_mounts(self) -> list[PodmanMount]:
125129 target = "/etc/vector/vector.yaml" ,
126130 )
127131 log_mount = self .log_mount
132+ mounts = [config_mount ]
128133 if log_mount :
129- return [config_mount , log_mount ]
130- return [config_mount ]
134+ mounts .append (log_mount )
135+ creds_mount = self .vector_creds_mount
136+ if creds_mount :
137+ mounts .append (creds_mount )
138+ return mounts
139+
140+ @property
141+ def vector_creds_mount (self ) -> PodmanMount | None :
142+ creds_path = self .nats_creds_file
143+ if not creds_path :
144+ return None
145+ return PodmanMount (
146+ type = PodmanMountType .bind ,
147+ source = str (creds_path ),
148+ target = VECTOR_NATS_CREDS_TARGET ,
149+ )
150+
151+ @property
152+ def nats_creds_file (self ) -> Path | None :
153+ try :
154+ nats_cfg = get_nats_config ()
155+ except ValueError :
156+ return None
157+ if nats_cfg .NATS_SECURITY_MODE != NatsMode .CREDS :
158+ return None
159+ return nats_cfg .NATS_CREDS_FILE
131160
132161 def generate_vector_config (self ) -> Path | None :
133162 """Generates a vector config file and returns path to it"""
134163
135- if not self .VECTOR_AGGREGATOR_ADDR :
136- logger .warning ("VECTOR_AGGREGATOR_ADDR not set , skipping log aggregation." )
164+ if not self .VECTOR_ENABLED :
165+ logger .warning ("VECTOR_ENABLED is false , skipping log aggregation." )
137166 return None
138167
139168 if not self .LOG_DIR .exists ():
140169 raise RuntimeError (
141170 f"Log directory { self .LOG_DIR } does not exist. Should not happen."
142171 )
143- templ : Template = Template (VECTOR_CONFIG_TEMPLATE )
172+ templ : Template = Template (_load_vector_template () )
144173 vector_yaml = templ .render (
145174 logs_dir = LOGS_DIR_IN_CONTAINER ,
146175 agent_id = self .ID ,
147- vector_addr = self .VECTOR_AGGREGATOR_ADDR ,
176+ nats_url = str (self .NATS_SERVER_URL_IN_CONTAINER ),
177+ nats_creds_path = VECTOR_NATS_CREDS_TARGET ,
148178 )
149179 output_path = self .LOG_DIR / "vector.yaml"
150180 with open (output_path , "w" ) as f :
@@ -155,4 +185,3 @@ def generate_vector_config(self) -> Path | None:
155185
156186
157187cfg = Settings () # pyright: ignore[reportCallIssue]
158-
0 commit comments