|
171 | 171 | ResourceReference = TypeVar("ResourceReference") |
172 | 172 |
|
173 | 173 |
|
| 174 | +def _get_monitoring_addon_key(cluster, addon_consts): |
| 175 | + """Resolve the monitoring addon key from the cluster's addon_profiles. |
| 176 | +
|
| 177 | + The API response may return the addon key as either "omsagent" or "omsAgent". |
| 178 | + Returns the key present in addon_profiles, or the default constant if neither is found. |
| 179 | + """ |
| 180 | + const_monitoring = addon_consts.get("CONST_MONITORING_ADDON_NAME") |
| 181 | + if cluster.addon_profiles: |
| 182 | + if const_monitoring in cluster.addon_profiles: |
| 183 | + return const_monitoring |
| 184 | + if CONST_MONITORING_ADDON_NAME_CAMELCASE in cluster.addon_profiles: |
| 185 | + return CONST_MONITORING_ADDON_NAME_CAMELCASE |
| 186 | + return const_monitoring |
| 187 | + |
| 188 | + |
174 | 189 | # pylint: disable=too-few-public-methods |
175 | 190 | class AKSPreviewManagedClusterModels(AKSManagedClusterModels): |
176 | 191 | """Store the models used in aks series of commands. |
@@ -5191,113 +5206,111 @@ def _should_create_dcra(self) -> bool: |
5191 | 5206 | params = self.context.raw_param |
5192 | 5207 | return ( |
5193 | 5208 | params.get("enable_addons") is not None or |
5194 | | - params.get("enable-azure-monitor-logs") is not None or |
| 5209 | + params.get("enable_azure_monitor_logs") is not None or |
| 5210 | + self._is_cnl_or_hlsm_changing() |
| 5211 | + ) |
| 5212 | + |
| 5213 | + def _is_cnl_or_hlsm_changing(self) -> bool: |
| 5214 | + """Return True if any CNL or High Log Scale Mode flag was provided.""" |
| 5215 | + params = self.context.raw_param |
| 5216 | + return ( |
5195 | 5217 | params.get("enable_container_network_logs") is not None or |
5196 | 5218 | params.get("enable_retina_flow_logs") is not None or |
5197 | 5219 | params.get("disable_container_network_logs") is not None or |
5198 | 5220 | params.get("disable_retina_flow_logs") is not None or |
5199 | 5221 | params.get("enable_high_log_scale_mode") is not None |
5200 | 5222 | ) |
5201 | 5223 |
|
5202 | | - # pylint: disable=too-many-locals,too-many-branches |
5203 | | - def postprocessing_after_mc_created(self, cluster: ManagedCluster) -> None: |
5204 | | - """Postprocessing performed after the cluster is created. |
| 5224 | + def _postprocess_monitoring_enable(self, cluster: ManagedCluster) -> None: |
| 5225 | + """Handle monitoring addon postprocessing for the enable case.""" |
| 5226 | + enable_msi_auth_for_monitoring = self.context.get_enable_msi_auth_for_monitoring() |
| 5227 | + if not enable_msi_auth_for_monitoring: |
| 5228 | + # add cluster spn/msi Monitoring Metrics Publisher role assignment to publish metrics to MDM |
| 5229 | + # mdm metrics is supported only in azure public cloud, so add the role assignment only in this cloud |
| 5230 | + cloud_name = self.cmd.cli_ctx.cloud.name |
| 5231 | + if cloud_name.lower() == "azurecloud": |
| 5232 | + cluster_resource_id = resource_id( |
| 5233 | + subscription=self.context.get_subscription_id(), |
| 5234 | + resource_group=self.context.get_resource_group_name(), |
| 5235 | + namespace="Microsoft.ContainerService", |
| 5236 | + type="managedClusters", |
| 5237 | + name=self.context.get_name(), |
| 5238 | + ) |
| 5239 | + self.context.external_functions.add_monitoring_role_assignment( |
| 5240 | + cluster, cluster_resource_id, self.cmd |
| 5241 | + ) |
| 5242 | + elif self._should_create_dcra(): |
| 5243 | + addon_consts = self.context.get_addon_consts() |
| 5244 | + monitoring_addon_key = _get_monitoring_addon_key(cluster, addon_consts) |
| 5245 | + self.context.external_functions.ensure_container_insights_for_monitoring( |
| 5246 | + self.cmd, |
| 5247 | + cluster.addon_profiles[monitoring_addon_key], |
| 5248 | + self.context.get_subscription_id(), |
| 5249 | + self.context.get_resource_group_name(), |
| 5250 | + self.context.get_name(), |
| 5251 | + self.context.get_location(), |
| 5252 | + remove_monitoring=False, |
| 5253 | + aad_route=self.context.get_enable_msi_auth_for_monitoring(), |
| 5254 | + create_dcr=self._is_cnl_or_hlsm_changing(), |
| 5255 | + create_dcra=True, |
| 5256 | + enable_syslog=self.context.get_enable_syslog(), |
| 5257 | + data_collection_settings=self.context.get_data_collection_settings(), |
| 5258 | + is_private_cluster=self.context.get_enable_private_cluster(), |
| 5259 | + ampls_resource_id=self.context.get_ampls_resource_id(), |
| 5260 | + enable_high_log_scale_mode=self.context.get_enable_high_log_scale_mode(), |
| 5261 | + ) |
5205 | 5262 |
|
5206 | | - :return: None |
5207 | | - """ |
5208 | | - # monitoring addon |
5209 | | - monitoring_addon_enabled = self.context.get_intermediate("monitoring_addon_enabled", default_value=False) |
5210 | | - if monitoring_addon_enabled: |
5211 | | - enable_msi_auth_for_monitoring = self.context.get_enable_msi_auth_for_monitoring() |
5212 | | - if not enable_msi_auth_for_monitoring: |
5213 | | - # add cluster spn/msi Monitoring Metrics Publisher role assignment to publish metrics to MDM |
5214 | | - # mdm metrics is supported only in azure public cloud, so add the role assignment only in this cloud |
5215 | | - cloud_name = self.cmd.cli_ctx.cloud.name |
5216 | | - if cloud_name.lower() == "azurecloud": |
5217 | | - cluster_resource_id = resource_id( |
5218 | | - subscription=self.context.get_subscription_id(), |
5219 | | - resource_group=self.context.get_resource_group_name(), |
5220 | | - namespace="Microsoft.ContainerService", |
5221 | | - type="managedClusters", |
5222 | | - name=self.context.get_name(), |
5223 | | - ) |
5224 | | - self.context.external_functions.add_monitoring_role_assignment( |
5225 | | - cluster, cluster_resource_id, self.cmd |
5226 | | - ) |
5227 | | - elif self._should_create_dcra(): |
5228 | | - # Create/update the DCR when CNL or HLSM flags change so that the DCR streams |
5229 | | - # (e.g. Microsoft-ContainerLogV2-HighScale) are kept in sync. |
5230 | | - cnl_or_hlsm_changing = ( |
5231 | | - self.context.raw_param.get("enable_container_network_logs") is not None or |
5232 | | - self.context.raw_param.get("enable_retina_flow_logs") is not None or |
5233 | | - self.context.raw_param.get("disable_container_network_logs") is not None or |
5234 | | - self.context.raw_param.get("disable_retina_flow_logs") is not None or |
5235 | | - self.context.raw_param.get("enable_high_log_scale_mode") is not None |
5236 | | - ) |
5237 | | - addon_consts = self.context.get_addon_consts() |
5238 | | - CONST_MONITORING_ADDON_NAME = addon_consts.get("CONST_MONITORING_ADDON_NAME") |
5239 | | - # The API response may return the addon key as either "omsagent" or "omsAgent" |
5240 | | - monitoring_addon_key = CONST_MONITORING_ADDON_NAME |
5241 | | - if CONST_MONITORING_ADDON_NAME not in cluster.addon_profiles and \ |
5242 | | - CONST_MONITORING_ADDON_NAME_CAMELCASE in cluster.addon_profiles: |
5243 | | - monitoring_addon_key = CONST_MONITORING_ADDON_NAME_CAMELCASE |
| 5263 | + def _postprocess_monitoring_disable(self) -> None: |
| 5264 | + """Handle monitoring addon postprocessing for the disable case.""" |
| 5265 | + addon_consts = self.context.get_addon_consts() |
| 5266 | + CONST_MONITORING_ADDON_NAME = addon_consts.get("CONST_MONITORING_ADDON_NAME") |
| 5267 | + |
| 5268 | + # Get the current cluster state to check config before it was disabled |
| 5269 | + current_cluster = self.client.get(self.context.get_resource_group_name(), self.context.get_name()) |
| 5270 | + |
| 5271 | + if (current_cluster.addon_profiles and |
| 5272 | + CONST_MONITORING_ADDON_NAME in current_cluster.addon_profiles): |
| 5273 | + |
| 5274 | + addon_profile = current_cluster.addon_profiles[CONST_MONITORING_ADDON_NAME] |
| 5275 | + |
| 5276 | + try: |
5244 | 5277 | self.context.external_functions.ensure_container_insights_for_monitoring( |
5245 | 5278 | self.cmd, |
5246 | | - cluster.addon_profiles[monitoring_addon_key], |
| 5279 | + addon_profile, |
5247 | 5280 | self.context.get_subscription_id(), |
5248 | 5281 | self.context.get_resource_group_name(), |
5249 | 5282 | self.context.get_name(), |
5250 | 5283 | self.context.get_location(), |
5251 | | - remove_monitoring=False, |
5252 | | - aad_route=self.context.get_enable_msi_auth_for_monitoring(), |
5253 | | - create_dcr=cnl_or_hlsm_changing, |
| 5284 | + remove_monitoring=True, |
| 5285 | + aad_route=True, |
| 5286 | + create_dcr=False, |
5254 | 5287 | create_dcra=True, |
5255 | | - enable_syslog=self.context.get_enable_syslog(), |
5256 | | - data_collection_settings=self.context.get_data_collection_settings(), |
5257 | | - is_private_cluster=self.context.get_enable_private_cluster(), |
5258 | | - ampls_resource_id=self.context.get_ampls_resource_id(), |
5259 | | - enable_high_log_scale_mode=self.context.get_enable_high_log_scale_mode(), |
| 5288 | + enable_syslog=False, |
| 5289 | + data_collection_settings=None, |
| 5290 | + ampls_resource_id=None, |
| 5291 | + enable_high_log_scale_mode=False |
5260 | 5292 | ) |
| 5293 | + except TypeError: |
| 5294 | + pass |
| 5295 | + |
| 5296 | + # pylint: disable=too-many-locals,too-many-branches |
| 5297 | + def postprocessing_after_mc_created(self, cluster: ManagedCluster) -> None: |
| 5298 | + """Postprocessing performed after the cluster is created. |
| 5299 | +
|
| 5300 | + :return: None |
| 5301 | + """ |
| 5302 | + # monitoring addon |
| 5303 | + monitoring_addon_enabled = self.context.get_intermediate("monitoring_addon_enabled", default_value=False) |
| 5304 | + if monitoring_addon_enabled: |
| 5305 | + self._postprocess_monitoring_enable(cluster) |
5261 | 5306 |
|
5262 | 5307 | # Handle monitoring addon postprocessing (disable case) - same logic as aks_disable_addons |
5263 | 5308 | monitoring_addon_disable_postprocessing_required = self.context.get_intermediate( |
5264 | 5309 | "monitoring_addon_disable_postprocessing_required", default_value=False |
5265 | 5310 | ) |
5266 | 5311 |
|
5267 | 5312 | if monitoring_addon_disable_postprocessing_required: |
5268 | | - addon_consts = self.context.get_addon_consts() |
5269 | | - CONST_MONITORING_ADDON_NAME = addon_consts.get("CONST_MONITORING_ADDON_NAME") |
5270 | | - |
5271 | | - # Get the current cluster state to check config before it was disabled |
5272 | | - current_cluster = self.client.get(self.context.get_resource_group_name(), self.context.get_name()) |
5273 | | - |
5274 | | - if (current_cluster.addon_profiles and |
5275 | | - CONST_MONITORING_ADDON_NAME in current_cluster.addon_profiles): |
5276 | | - |
5277 | | - # Use the current cluster addon profile for cleanup |
5278 | | - addon_profile = current_cluster.addon_profiles[CONST_MONITORING_ADDON_NAME] |
5279 | | - |
5280 | | - # Call ensure_container_insights_for_monitoring with remove_monitoring=True (same as aks_disable_addons) |
5281 | | - try: |
5282 | | - self.context.external_functions.ensure_container_insights_for_monitoring( |
5283 | | - self.cmd, |
5284 | | - addon_profile, |
5285 | | - self.context.get_subscription_id(), |
5286 | | - self.context.get_resource_group_name(), |
5287 | | - self.context.get_name(), |
5288 | | - self.context.get_location(), |
5289 | | - remove_monitoring=True, |
5290 | | - aad_route=True, |
5291 | | - create_dcr=False, |
5292 | | - create_dcra=True, |
5293 | | - enable_syslog=False, |
5294 | | - data_collection_settings=None, |
5295 | | - ampls_resource_id=None, |
5296 | | - enable_high_log_scale_mode=False |
5297 | | - ) |
5298 | | - except TypeError: |
5299 | | - # Ignore TypeError just like aks_disable_addons does |
5300 | | - pass |
| 5313 | + self._postprocess_monitoring_disable() |
5301 | 5314 |
|
5302 | 5315 | # ingress appgw addon |
5303 | 5316 | ingress_appgw_addon_enabled = self.context.get_intermediate("ingress_appgw_addon_enabled", default_value=False) |
@@ -7965,18 +7978,12 @@ def postprocessing_after_mc_created(self, cluster: ManagedCluster) -> None: |
7965 | 7978 | ) |
7966 | 7979 | if monitoring_addon_postprocessing_required: |
7967 | 7980 | addon_consts = self.context.get_addon_consts() |
7968 | | - CONST_MONITORING_ADDON_NAME = addon_consts.get("CONST_MONITORING_ADDON_NAME") |
7969 | 7981 | CONST_MONITORING_USING_AAD_MSI_AUTH = addon_consts.get("CONST_MONITORING_USING_AAD_MSI_AUTH") |
7970 | 7982 |
|
7971 | | - # The API response may return the addon key as either "omsagent" or "omsAgent" |
7972 | | - monitoring_addon_key = None |
7973 | | - if cluster.addon_profiles: |
7974 | | - if CONST_MONITORING_ADDON_NAME in cluster.addon_profiles: |
7975 | | - monitoring_addon_key = CONST_MONITORING_ADDON_NAME |
7976 | | - elif CONST_MONITORING_ADDON_NAME_CAMELCASE in cluster.addon_profiles: |
7977 | | - monitoring_addon_key = CONST_MONITORING_ADDON_NAME_CAMELCASE |
| 7983 | + monitoring_addon_key = _get_monitoring_addon_key(cluster, addon_consts) |
7978 | 7984 |
|
7979 | | - if (monitoring_addon_key and |
| 7985 | + if (cluster.addon_profiles and |
| 7986 | + monitoring_addon_key in cluster.addon_profiles and |
7980 | 7987 | cluster.addon_profiles[monitoring_addon_key].enabled): |
7981 | 7988 |
|
7982 | 7989 | # Check if MSI auth is enabled |
|
0 commit comments