|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from core.configs.pod import PodTemplateSpecConfig |
| 5 | +from core.configs.statefulset import StatefulSetConfig, StatefulSetSpecConfig |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class OvnConfig: |
| 10 | + """OVN configuration for bandwidth shaping and subnet attachment.""" |
| 11 | + # Bandwidth shaping (in Mbps) |
| 12 | + ingress_rate: Optional[int] = None |
| 13 | + egress_rate: Optional[int] = None |
| 14 | + # Subnet attachment |
| 15 | + logical_switch: Optional[str] = None |
| 16 | + |
| 17 | + def to_annotations(self) -> dict: |
| 18 | + """Convert configuration to OVN annotations dict.""" |
| 19 | + annotations = {} |
| 20 | + |
| 21 | + if self.ingress_rate is not None: |
| 22 | + annotations["ovn.kubernetes.io/ingress_rate"] = str(self.ingress_rate) |
| 23 | + if self.egress_rate is not None: |
| 24 | + annotations["ovn.kubernetes.io/egress_rate"] = str(self.egress_rate) |
| 25 | + if self.logical_switch is not None: |
| 26 | + annotations["ovn.kubernetes.io/logical_switch"] = self.logical_switch |
| 27 | + |
| 28 | + return annotations |
| 29 | + |
| 30 | + |
| 31 | +def apply_ovn_pod_template( |
| 32 | + config: PodTemplateSpecConfig, |
| 33 | + ovn_config: OvnConfig, |
| 34 | +): |
| 35 | + """Apply OVN annotations to PodTemplateSpecConfig.""" |
| 36 | + for key, value in ovn_config.to_annotations().items(): |
| 37 | + config.with_annotation(key, value, overwrite=True) |
| 38 | + |
| 39 | + |
| 40 | +def apply_ovn_statefulset_spec( |
| 41 | + config: StatefulSetSpecConfig, |
| 42 | + ovn_config: OvnConfig, |
| 43 | +): |
| 44 | + """Apply OVN annotations to StatefulSetSpecConfig.""" |
| 45 | + apply_ovn_pod_template(config.pod_template_spec_config, ovn_config) |
| 46 | + |
| 47 | + |
| 48 | +def apply_ovn_statefulset( |
| 49 | + config: StatefulSetConfig, |
| 50 | + ovn_config: OvnConfig, |
| 51 | +): |
| 52 | + """Apply OVN annotations to StatefulSetConfig.""" |
| 53 | + apply_ovn_statefulset_spec(config.stateful_set_spec, ovn_config) |
| 54 | + |
| 55 | + |
| 56 | +#bandwidth configurations |
| 57 | +def apply_bandwidth_limit( |
| 58 | + config: StatefulSetConfig | StatefulSetSpecConfig | PodTemplateSpecConfig, |
| 59 | + ingress_mbps: int, |
| 60 | + egress_mbps: Optional[int] = None, |
| 61 | +): |
| 62 | + if egress_mbps is None: |
| 63 | + egress_mbps = ingress_mbps |
| 64 | + |
| 65 | + ovn_config = OvnConfig(ingress_rate=ingress_mbps, egress_rate=egress_mbps) |
| 66 | + |
| 67 | + if isinstance(config, StatefulSetConfig): |
| 68 | + apply_ovn_statefulset(config, ovn_config) |
| 69 | + elif isinstance(config, StatefulSetSpecConfig): |
| 70 | + apply_ovn_statefulset_spec(config, ovn_config) |
| 71 | + elif isinstance(config, PodTemplateSpecConfig): |
| 72 | + apply_ovn_pod_template(config, ovn_config) |
| 73 | + else: |
| 74 | + raise TypeError(f"Unsupported config type: {type(config)}") |
| 75 | + |
| 76 | + |
| 77 | +def apply_logical_switch( |
| 78 | + config: StatefulSetConfig | StatefulSetSpecConfig | PodTemplateSpecConfig, |
| 79 | + logical_switch: str, |
| 80 | +): |
| 81 | + ovn_config = OvnConfig(logical_switch=logical_switch) |
| 82 | + |
| 83 | + if isinstance(config, StatefulSetConfig): |
| 84 | + apply_ovn_statefulset(config, ovn_config) |
| 85 | + elif isinstance(config, StatefulSetSpecConfig): |
| 86 | + apply_ovn_statefulset_spec(config, ovn_config) |
| 87 | + elif isinstance(config, PodTemplateSpecConfig): |
| 88 | + apply_ovn_pod_template(config, ovn_config) |
| 89 | + else: |
| 90 | + raise TypeError(f"Unsupported config type: {type(config)}") |
0 commit comments