|
9 | 9 | from urllib.parse import urlparse |
10 | 10 | import json |
11 | 11 | import requests |
| 12 | +import copy |
12 | 13 | import subprocess |
13 | 14 | from concurrent.futures import ThreadPoolExecutor |
14 | 15 |
|
|
127 | 128 |
|
128 | 129 | from ._ssh_utils import (SSH_DEFAULT_ENCODING, DebugWebSocketConnection, read_debug_ssh) |
129 | 130 |
|
130 | | -from ._utils import connected_env_check_cert_name_availability, get_oryx_run_image_tags, patchable_check, get_pack_exec_path, is_docker_running, parse_build_env_vars, env_has_managed_identity |
| 131 | +from ._utils import (connected_env_check_cert_name_availability, get_oryx_run_image_tags, patchable_check, |
| 132 | + get_pack_exec_path, is_docker_running, parse_build_env_vars, env_has_managed_identity) |
| 133 | + |
| 134 | +from ._arc_utils import (get_core_dns_deployment, get_core_dns_configmap, backup_custom_core_dns_configmap, |
| 135 | + replace_configmap, replace_deployment, delete_configmap, patch_coredns, |
| 136 | + create_folder, create_sub_folder, |
| 137 | + check_kube_connection, create_kube_client) |
131 | 138 |
|
132 | 139 | from ._constants import (CONTAINER_APPS_RP, |
133 | 140 | NAME_INVALID, NAME_ALREADY_EXISTS, ACR_IMAGE_SUFFIX, DEV_POSTGRES_IMAGE, DEV_POSTGRES_SERVICE_TYPE, |
|
136 | 143 | DEV_QDRANT_CONTAINER_NAME, DEV_QDRANT_SERVICE_TYPE, DEV_WEAVIATE_IMAGE, DEV_WEAVIATE_CONTAINER_NAME, DEV_WEAVIATE_SERVICE_TYPE, |
137 | 144 | DEV_MILVUS_IMAGE, DEV_MILVUS_CONTAINER_NAME, DEV_MILVUS_SERVICE_TYPE, DEV_SERVICE_LIST, CONTAINER_APPS_SDK_MODELS, BLOB_STORAGE_TOKEN_STORE_SECRET_SETTING_NAME, |
138 | 145 | DAPR_SUPPORTED_STATESTORE_DEV_SERVICE_LIST, DAPR_SUPPORTED_PUBSUB_DEV_SERVICE_LIST, |
139 | | - JAVA_COMPONENT_CONFIG, JAVA_COMPONENT_EUREKA, JAVA_COMPONENT_ADMIN, JAVA_COMPONENT_NACOS, JAVA_COMPONENT_GATEWAY, DOTNET_COMPONENT_RESOURCE_TYPE) |
| 146 | + JAVA_COMPONENT_CONFIG, JAVA_COMPONENT_EUREKA, JAVA_COMPONENT_ADMIN, JAVA_COMPONENT_NACOS, JAVA_COMPONENT_GATEWAY, DOTNET_COMPONENT_RESOURCE_TYPE, |
| 147 | + CUSTOM_CORE_DNS, CORE_DNS, KUBE_SYSTEM) |
140 | 148 |
|
141 | 149 |
|
142 | 150 | logger = get_logger(__name__) |
@@ -2064,6 +2072,105 @@ def connected_env_remove_storage(cmd, storage_name, name, resource_group_name, n |
2064 | 2072 | handle_raw_exception(e) |
2065 | 2073 |
|
2066 | 2074 |
|
| 2075 | +def setup_core_dns(cmd, distro=None, kube_config=None, kube_context=None, skip_ssl_verification=False): |
| 2076 | + # Checking the connection to kubernetes cluster. |
| 2077 | + check_kube_connection(kube_config, kube_context, skip_ssl_verification) |
| 2078 | + |
| 2079 | + # create a local path to store the original and the changed deployment and core dns configmap. |
| 2080 | + time_stamp = time.strftime("%Y-%m-%d-%H.%M.%S") |
| 2081 | + |
| 2082 | + parent_folder, folder_status, error = create_folder("setup-core-dns", time_stamp) |
| 2083 | + if not folder_status: |
| 2084 | + raise ValidationError(error) |
| 2085 | + |
| 2086 | + original_folder, folder_status, error = create_sub_folder(parent_folder, "original") |
| 2087 | + if not folder_status: |
| 2088 | + raise ValidationError(error) |
| 2089 | + |
| 2090 | + kube_client = create_kube_client(kube_config, kube_context, skip_ssl_verification) |
| 2091 | + |
| 2092 | + # backup original deployment and configmap |
| 2093 | + logger.info("Backup existing coredns deployment and configmap") |
| 2094 | + original_coredns_deployment = get_core_dns_deployment(kube_client, original_folder) |
| 2095 | + coredns_deployment = copy.deepcopy(original_coredns_deployment) |
| 2096 | + |
| 2097 | + original_coredns_configmap = get_core_dns_configmap(kube_client, original_folder) |
| 2098 | + coredns_configmap = copy.deepcopy(original_coredns_configmap) |
| 2099 | + |
| 2100 | + volumes = coredns_deployment.spec.template.spec.volumes |
| 2101 | + if volumes is None: |
| 2102 | + raise ValidationError('Unexpected Volumes in coredns deployment, Volumes not found') |
| 2103 | + |
| 2104 | + volume_mounts = coredns_deployment.spec.template.spec.containers[0].volume_mounts |
| 2105 | + if volume_mounts is None: |
| 2106 | + raise ValidationError('Unexpected Volume mounts in coredns deployment, VolumeMounts not found') |
| 2107 | + |
| 2108 | + coredns_configmap_volume_set = False |
| 2109 | + custom_coredns_configmap_volume_set = False |
| 2110 | + custom_coredns_configmap_volume_mounted = False |
| 2111 | + |
| 2112 | + for volume in volumes: |
| 2113 | + if volume.config_map is not None: |
| 2114 | + if volume.config_map.name == CORE_DNS: |
| 2115 | + for mount in volume_mounts: |
| 2116 | + if mount.name is not None and mount.name == volume.name: |
| 2117 | + coredns_configmap_volume_set = True |
| 2118 | + break |
| 2119 | + elif volume.config_map.name == CUSTOM_CORE_DNS: |
| 2120 | + custom_coredns_configmap_volume_set = True |
| 2121 | + for mount in volume_mounts: |
| 2122 | + if mount.name is not None and mount.name == volume.name: |
| 2123 | + custom_coredns_configmap_volume_mounted = True |
| 2124 | + break |
| 2125 | + |
| 2126 | + if not coredns_configmap_volume_set: |
| 2127 | + raise ValidationError("Cannot find volume and volume mounts for core dns config map") |
| 2128 | + |
| 2129 | + original_custom_core_dns_configmap = backup_custom_core_dns_configmap(kube_client, original_folder) |
| 2130 | + |
| 2131 | + new_filepath_with_timestamp, folder_status, error = create_sub_folder(parent_folder, "new") |
| 2132 | + if not folder_status: |
| 2133 | + raise ValidationError(error) |
| 2134 | + |
| 2135 | + try: |
| 2136 | + patch_coredns(kube_client, coredns_configmap, coredns_deployment, new_filepath_with_timestamp, |
| 2137 | + original_custom_core_dns_configmap is not None, not custom_coredns_configmap_volume_set, not custom_coredns_configmap_volume_mounted) |
| 2138 | + except Exception as e: |
| 2139 | + logger.error(f"Failed to setup custom coredns. {e}") |
| 2140 | + logger.info("Start to reverted coredns") |
| 2141 | + replace_succeeded = False |
| 2142 | + retry_count = 0 |
| 2143 | + while not replace_succeeded and retry_count < 10: |
| 2144 | + logger.info(f"Retry the revert operation with retry count {retry_count}") |
| 2145 | + |
| 2146 | + try: |
| 2147 | + logger.info("Start to reverted coredns configmap") |
| 2148 | + latest_core_dns_configmap = get_core_dns_configmap(kube_client) |
| 2149 | + latest_core_dns_configmap.data = original_coredns_configmap.data |
| 2150 | + |
| 2151 | + replace_configmap(CORE_DNS, KUBE_SYSTEM, kube_client, latest_core_dns_configmap) |
| 2152 | + logger.info("Reverted coredns configmap successfully") |
| 2153 | + |
| 2154 | + logger.info("Start to reverted coredns deployment") |
| 2155 | + latest_core_dns_deployment = get_core_dns_deployment(kube_client) |
| 2156 | + latest_core_dns_deployment.spec.template.spec = original_coredns_deployment.spec.template.spec |
| 2157 | + |
| 2158 | + replace_deployment(CORE_DNS, KUBE_SYSTEM, kube_client, latest_core_dns_deployment) |
| 2159 | + logger.info("Reverted coredns deployment successfully") |
| 2160 | + |
| 2161 | + if original_custom_core_dns_configmap is None: |
| 2162 | + delete_configmap(CUSTOM_CORE_DNS, KUBE_SYSTEM, kube_client) |
| 2163 | + replace_succeeded = True |
| 2164 | + except Exception as revertEx: |
| 2165 | + logger.warning(f"Failed to revert coredns configmap or deployment {revertEx}") |
| 2166 | + retry_count = retry_count + 1 |
| 2167 | + time.sleep(2) |
| 2168 | + |
| 2169 | + if not replace_succeeded: |
| 2170 | + logger.error(f"Failed to revert the deployment and configuration. " |
| 2171 | + f"You can get the original coredns config and deployment from {original_folder}") |
| 2172 | + |
| 2173 | + |
2067 | 2174 | def init_dapr_components(cmd, resource_group_name, environment_name, statestore="redis", pubsub="redis"): |
2068 | 2175 | _validate_subscription_registered(cmd, CONTAINER_APPS_RP) |
2069 | 2176 |
|
|
0 commit comments