|
5 | 5 |
|
6 | 6 | import base64 |
7 | 7 | import os |
| 8 | +from azure.cli.core.util import get_file_json |
8 | 9 | from types import SimpleNamespace |
9 | 10 | from typing import Dict, TypeVar, Union, List |
10 | 11 |
|
@@ -810,6 +811,30 @@ def get_disable_fips_image(self) -> bool: |
810 | 811 | # read the original value passed by the command |
811 | 812 | return self.raw_param.get("disable_fips_image") |
812 | 813 |
|
| 814 | + def get_localdns_config(self): |
| 815 | + return self.raw_param.get("localdns_config") |
| 816 | + |
| 817 | + def get_localdns_profile(self): |
| 818 | + """ |
| 819 | + Returns the local DNS profile dict if set, or None. |
| 820 | + Only supports loading from --localdns-config (JSON file). |
| 821 | + Assumes the input is always a string filename. |
| 822 | + """ |
| 823 | + config = self.get_localdns_config() |
| 824 | + if config: |
| 825 | + if not isinstance(config, str) or not os.path.isfile(config): |
| 826 | + raise InvalidArgumentValueError( |
| 827 | + f"{config} is not a valid file, or not accessible." |
| 828 | + ) |
| 829 | + profile = get_file_json(config) |
| 830 | + if not isinstance(profile, dict): |
| 831 | + raise InvalidArgumentValueError( |
| 832 | + f"Error reading local DNS config from {config}. " |
| 833 | + "Please provide a valid JSON file." |
| 834 | + ) |
| 835 | + return profile |
| 836 | + return None |
| 837 | + |
813 | 838 |
|
814 | 839 | class AKSPreviewAgentPoolAddDecorator(AKSAgentPoolAddDecorator): |
815 | 840 | def __init__( |
@@ -1099,6 +1124,50 @@ def set_up_managed_system_mode(self, agentpool: AgentPool) -> AgentPool: |
1099 | 1124 |
|
1100 | 1125 | return agentpool |
1101 | 1126 |
|
| 1127 | + def set_up_localdns_profile(self, agentpool: AgentPool) -> AgentPool: |
| 1128 | + """Set up local DNS profile for the AgentPool object if provided via --localdns-config.""" |
| 1129 | + self._ensure_agentpool(agentpool) |
| 1130 | + localdns_profile = self.context.get_localdns_profile() |
| 1131 | + if localdns_profile is not None: |
| 1132 | + kube_dns_overrides = {} |
| 1133 | + vnet_dns_overrides = {} |
| 1134 | + |
| 1135 | + def build_override(override_dict): |
| 1136 | + camel_to_snake_case = { |
| 1137 | + "queryLogging": "query_logging", |
| 1138 | + "protocol": "protocol", |
| 1139 | + "forwardDestination": "forward_destination", |
| 1140 | + "forwardPolicy": "forward_policy", |
| 1141 | + "maxConcurrent": "max_concurrent", |
| 1142 | + "cacheDurationInSeconds": "cache_duration_in_seconds", |
| 1143 | + "serveStaleDurationInSeconds": "serve_stale_duration_in_seconds", |
| 1144 | + "serveStale": "serve_stale", |
| 1145 | + } |
| 1146 | + valid_keys = set(camel_to_snake_case.values()) |
| 1147 | + filtered = {} |
| 1148 | + for k, v in override_dict.items(): |
| 1149 | + if k in camel_to_snake_case: |
| 1150 | + filtered[camel_to_snake_case[k]] = v |
| 1151 | + elif k in valid_keys: |
| 1152 | + filtered[k] = v |
| 1153 | + return self.models.LocalDNSOverride(**filtered) |
| 1154 | + |
| 1155 | + # Build kubeDNSOverrides and vnetDNSOverrides from the localdns_profile |
| 1156 | + kube_overrides = localdns_profile.get("kubeDNSOverrides") |
| 1157 | + for key, value in kube_overrides.items(): |
| 1158 | + kube_dns_overrides[key] = build_override(value) |
| 1159 | + |
| 1160 | + vnet_overrides = localdns_profile.get("vnetDNSOverrides") |
| 1161 | + for key, value in vnet_overrides.items(): |
| 1162 | + vnet_dns_overrides[key] = build_override(value) |
| 1163 | + |
| 1164 | + agentpool.local_dns_profile = self.models.LocalDNSProfile( |
| 1165 | + mode=localdns_profile.get("mode"), |
| 1166 | + kube_dns_overrides=kube_dns_overrides, |
| 1167 | + vnet_dns_overrides=vnet_dns_overrides, |
| 1168 | + ) |
| 1169 | + return agentpool |
| 1170 | + |
1102 | 1171 | def construct_agentpool_profile_preview(self) -> AgentPool: |
1103 | 1172 | """The overall controller used to construct the preview AgentPool profile. |
1104 | 1173 |
|
@@ -1151,6 +1220,8 @@ def construct_agentpool_profile_preview(self) -> AgentPool: |
1151 | 1220 | agentpool = self.set_up_agentpool_gateway_profile(agentpool) |
1152 | 1221 | # set up virtual machines profile |
1153 | 1222 | agentpool = self.set_up_virtual_machines_profile(agentpool) |
| 1223 | + # set up local DNS profile |
| 1224 | + agentpool = self.set_up_localdns_profile(agentpool) |
1154 | 1225 | # DO NOT MOVE: keep this at the bottom, restore defaults |
1155 | 1226 | agentpool = self._restore_defaults_in_agentpool(agentpool) |
1156 | 1227 | return agentpool |
@@ -1343,6 +1414,50 @@ def update_fips_image(self, agentpool: AgentPool) -> AgentPool: |
1343 | 1414 |
|
1344 | 1415 | return agentpool |
1345 | 1416 |
|
| 1417 | + def update_localdns_profile(self, agentpool: AgentPool) -> AgentPool: |
| 1418 | + """Update local DNS profile for the AgentPool object if provided via --localdns-config.""" |
| 1419 | + self._ensure_agentpool(agentpool) |
| 1420 | + localdns_profile = self.context.get_localdns_profile() |
| 1421 | + if localdns_profile is not None: |
| 1422 | + kube_dns_overrides = {} |
| 1423 | + vnet_dns_overrides = {} |
| 1424 | + |
| 1425 | + def build_override(override_dict): |
| 1426 | + camel_to_snake_case = { |
| 1427 | + "queryLogging": "query_logging", |
| 1428 | + "protocol": "protocol", |
| 1429 | + "forwardDestination": "forward_destination", |
| 1430 | + "forwardPolicy": "forward_policy", |
| 1431 | + "maxConcurrent": "max_concurrent", |
| 1432 | + "cacheDurationInSeconds": "cache_duration_in_seconds", |
| 1433 | + "serveStaleDurationInSeconds": "serve_stale_duration_in_seconds", |
| 1434 | + "serveStale": "serve_stale", |
| 1435 | + } |
| 1436 | + valid_keys = set(camel_to_snake_case.values()) |
| 1437 | + filtered = {} |
| 1438 | + for k, v in override_dict.items(): |
| 1439 | + if k in camel_to_snake_case: |
| 1440 | + filtered[camel_to_snake_case[k]] = v |
| 1441 | + elif k in valid_keys: |
| 1442 | + filtered[k] = v |
| 1443 | + return self.models.LocalDNSOverride(**filtered) |
| 1444 | + |
| 1445 | + # Build kubeDNSOverrides and vnetDNSOverrides from the localdns_profile |
| 1446 | + kube_overrides = localdns_profile.get("kubeDNSOverrides") |
| 1447 | + for key, value in kube_overrides.items(): |
| 1448 | + kube_dns_overrides[key] = build_override(value) |
| 1449 | + |
| 1450 | + vnet_overrides = localdns_profile.get("vnetDNSOverrides") |
| 1451 | + for key, value in vnet_overrides.items(): |
| 1452 | + vnet_dns_overrides[key] = build_override(value) |
| 1453 | + |
| 1454 | + agentpool.local_dns_profile = self.models.LocalDNSProfile( |
| 1455 | + mode=localdns_profile.get("mode"), |
| 1456 | + kube_dns_overrides=kube_dns_overrides, |
| 1457 | + vnet_dns_overrides=vnet_dns_overrides, |
| 1458 | + ) |
| 1459 | + return agentpool |
| 1460 | + |
1346 | 1461 | def update_agentpool_profile_preview(self, agentpools: List[AgentPool] = None) -> AgentPool: |
1347 | 1462 | """The overall controller used to update the preview AgentPool profile. |
1348 | 1463 |
|
@@ -1387,6 +1502,9 @@ def update_agentpool_profile_preview(self, agentpools: List[AgentPool] = None) - |
1387 | 1502 | # update ssh access |
1388 | 1503 | agentpool = self.update_ssh_access(agentpool) |
1389 | 1504 |
|
| 1505 | + # update local DNS profile |
| 1506 | + agentpool = self.update_localdns_profile(agentpool) |
| 1507 | + |
1390 | 1508 | return agentpool |
1391 | 1509 |
|
1392 | 1510 | def update_upgrade_settings(self, agentpool: AgentPool) -> AgentPool: |
|
0 commit comments