...
1from typing import TYPE_CHECKING, List, Optional, Type
2
3from ..config import Config
4from .irbasemapping import IRBaseMapping
5from .irhttpmapping import IRHTTPMapping
6from .irtcpmapping import IRTCPMapping
7
8if TYPE_CHECKING:
9 from .ir import IR # pragma: no cover
10
11
12def unique_mapping_name(aconf: Config, name: str) -> str:
13 http_mappings = aconf.get_config("mappings") or {}
14 tcp_mappings = aconf.get_config("tcpmappings") or {}
15
16 basename = name
17 counter = 0
18
19 while name in http_mappings or name in tcp_mappings:
20 name = f"{basename}-{counter}"
21 counter += 1
22
23 return name
24
25
26class MappingFactory:
27 @classmethod
28 def load_all(cls, ir: "IR", aconf: Config) -> None:
29 cls.load_config(ir, aconf, "Mapping", "mappings", IRHTTPMapping)
30 cls.load_config(ir, aconf, "TCPMapping", "tcpmappings", IRTCPMapping)
31
32 @classmethod
33 def load_config(
34 cls,
35 ir: "IR",
36 aconf: Config,
37 kind: str,
38 config_name: str,
39 mapping_class: Type[IRBaseMapping],
40 ) -> None:
41 config_info = aconf.get_config(config_name)
42
43 if not config_info:
44 return
45
46 assert len(config_info) > 0 # really rank paranoia on my part...
47
48 live_mappings: List[IRBaseMapping] = []
49
50 for config in config_info.values():
51 ir.logger.debug("IR: MappingFactory checking %s" % repr(config))
52
53 # Is this mapping already in the cache?
54 key = IRBaseMapping.make_cache_key(kind, config.name, config.namespace)
55
56 mapping: Optional[IRBaseMapping] = None
57 cached_mapping = ir.cache_fetch(key)
58
59 if cached_mapping is None:
60 # Cache miss: synthesize a new Mapping.
61 ir.logger.debug(f"IR: synthesizing Mapping for {config.name}")
62 mapping = mapping_class(ir, aconf, **config)
63 else:
64 # Cache hit. We know a priori that anything in the cache under a Mapping
65 # key must be an IRBaseMapping, but let's assert that rather than casting.
66 assert isinstance(cached_mapping, IRBaseMapping)
67 mapping = cached_mapping
68
69 if mapping:
70 ir.logger.debug(f"IR: live Mapping for {config.name}")
71 live_mappings.append(mapping)
72
73 ir.logger.debug("IR: MappingFactory checking invalidations")
74
75 for mapping in live_mappings:
76 if mapping.cache_key in ir.invalidate_groups_for:
77 group_key = mapping.group_class().key_for_id(mapping.group_id)
78 ir.logger.debug(
79 "IR: MappingFactory invalidating %s for %s", group_key, mapping.name
80 )
81 ir.cache.invalidate(group_key)
82
83 ir.logger.debug("IR: MappingFactory adding live mappings")
84
85 for mapping in live_mappings:
86 ir.logger.debug("IR: MappingFactory adding %s" % mapping.name)
87 ir.add_mapping(aconf, mapping)
88
89 ir.cache.dump("MappingFactory")
90
91 @classmethod
92 def finalize(cls, ir: "IR", aconf: Config) -> None:
93 # OK. We've created whatever IRMappings we need. Time to create the clusters
94 # they need.
95
96 ir.logger.debug("IR: MappingFactory finalizing")
97
98 for group in ir.groups.values():
99 ir.logger.debug("IR: MappingFactory finalizing group %s", group.group_id)
100 group.finalize(ir, aconf)
101 ir.logger.debug("IR: MappingFactory finalized group %s", group.group_id)
102
103 ir.logger.debug("IR: MappingFactory finalized")
View as plain text