|
167 | 167 | ResourceReference = TypeVar("ResourceReference") |
168 | 168 |
|
169 | 169 |
|
| 170 | +def _get_monitoring_addon_key(cluster, addon_consts): |
| 171 | + """Resolve the monitoring addon key from the cluster's addon_profiles. |
| 172 | +
|
| 173 | + The API response may return the addon key as either "omsagent" or "omsAgent". |
| 174 | + Returns the key present in addon_profiles, or the default constant if neither is found. |
| 175 | + """ |
| 176 | + const_monitoring = addon_consts.get("CONST_MONITORING_ADDON_NAME") |
| 177 | + if cluster.addon_profiles: |
| 178 | + if const_monitoring in cluster.addon_profiles: |
| 179 | + return const_monitoring |
| 180 | + if CONST_MONITORING_ADDON_NAME_CAMELCASE in cluster.addon_profiles: |
| 181 | + return CONST_MONITORING_ADDON_NAME_CAMELCASE |
| 182 | + return const_monitoring |
| 183 | + |
| 184 | + |
170 | 185 | # pylint: disable=too-few-public-methods |
171 | 186 | class AKSPreviewManagedClusterModels(AKSManagedClusterModels): |
172 | 187 | """Store the models used in aks series of commands. |
@@ -5102,113 +5117,111 @@ def _should_create_dcra(self) -> bool: |
5102 | 5117 | params = self.context.raw_param |
5103 | 5118 | return ( |
5104 | 5119 | params.get("enable_addons") is not None or |
5105 | | - params.get("enable-azure-monitor-logs") is not None or |
| 5120 | + params.get("enable_azure_monitor_logs") is not None or |
| 5121 | + self._is_cnl_or_hlsm_changing() |
| 5122 | + ) |
| 5123 | + |
| 5124 | + def _is_cnl_or_hlsm_changing(self) -> bool: |
| 5125 | + """Return True if any CNL or High Log Scale Mode flag was provided.""" |
| 5126 | + params = self.context.raw_param |
| 5127 | + return ( |
5106 | 5128 | params.get("enable_container_network_logs") is not None or |
5107 | 5129 | params.get("enable_retina_flow_logs") is not None or |
5108 | 5130 | params.get("disable_container_network_logs") is not None or |
5109 | 5131 | params.get("disable_retina_flow_logs") is not None or |
5110 | 5132 | params.get("enable_high_log_scale_mode") is not None |
5111 | 5133 | ) |
5112 | 5134 |
|
5113 | | - # pylint: disable=too-many-locals,too-many-branches |
5114 | | - def postprocessing_after_mc_created(self, cluster: ManagedCluster) -> None: |
5115 | | - """Postprocessing performed after the cluster is created. |
| 5135 | + def _postprocess_monitoring_enable(self, cluster: ManagedCluster) -> None: |
| 5136 | + """Handle monitoring addon postprocessing for the enable case.""" |
| 5137 | + enable_msi_auth_for_monitoring = self.context.get_enable_msi_auth_for_monitoring() |
| 5138 | + if not enable_msi_auth_for_monitoring: |
| 5139 | + # add cluster spn/msi Monitoring Metrics Publisher role assignment to publish metrics to MDM |
| 5140 | + # mdm metrics is supported only in azure public cloud, so add the role assignment only in this cloud |
| 5141 | + cloud_name = self.cmd.cli_ctx.cloud.name |
| 5142 | + if cloud_name.lower() == "azurecloud": |
| 5143 | + cluster_resource_id = resource_id( |
| 5144 | + subscription=self.context.get_subscription_id(), |
| 5145 | + resource_group=self.context.get_resource_group_name(), |
| 5146 | + namespace="Microsoft.ContainerService", |
| 5147 | + type="managedClusters", |
| 5148 | + name=self.context.get_name(), |
| 5149 | + ) |
| 5150 | + self.context.external_functions.add_monitoring_role_assignment( |
| 5151 | + cluster, cluster_resource_id, self.cmd |
| 5152 | + ) |
| 5153 | + elif self._should_create_dcra(): |
| 5154 | + addon_consts = self.context.get_addon_consts() |
| 5155 | + monitoring_addon_key = _get_monitoring_addon_key(cluster, addon_consts) |
| 5156 | + self.context.external_functions.ensure_container_insights_for_monitoring( |
| 5157 | + self.cmd, |
| 5158 | + cluster.addon_profiles[monitoring_addon_key], |
| 5159 | + self.context.get_subscription_id(), |
| 5160 | + self.context.get_resource_group_name(), |
| 5161 | + self.context.get_name(), |
| 5162 | + self.context.get_location(), |
| 5163 | + remove_monitoring=False, |
| 5164 | + aad_route=self.context.get_enable_msi_auth_for_monitoring(), |
| 5165 | + create_dcr=self._is_cnl_or_hlsm_changing(), |
| 5166 | + create_dcra=True, |
| 5167 | + enable_syslog=self.context.get_enable_syslog(), |
| 5168 | + data_collection_settings=self.context.get_data_collection_settings(), |
| 5169 | + is_private_cluster=self.context.get_enable_private_cluster(), |
| 5170 | + ampls_resource_id=self.context.get_ampls_resource_id(), |
| 5171 | + enable_high_log_scale_mode=self.context.get_enable_high_log_scale_mode(), |
| 5172 | + ) |
5116 | 5173 |
|
5117 | | - :return: None |
5118 | | - """ |
5119 | | - # monitoring addon |
5120 | | - monitoring_addon_enabled = self.context.get_intermediate("monitoring_addon_enabled", default_value=False) |
5121 | | - if monitoring_addon_enabled: |
5122 | | - enable_msi_auth_for_monitoring = self.context.get_enable_msi_auth_for_monitoring() |
5123 | | - if not enable_msi_auth_for_monitoring: |
5124 | | - # add cluster spn/msi Monitoring Metrics Publisher role assignment to publish metrics to MDM |
5125 | | - # mdm metrics is supported only in azure public cloud, so add the role assignment only in this cloud |
5126 | | - cloud_name = self.cmd.cli_ctx.cloud.name |
5127 | | - if cloud_name.lower() == "azurecloud": |
5128 | | - cluster_resource_id = resource_id( |
5129 | | - subscription=self.context.get_subscription_id(), |
5130 | | - resource_group=self.context.get_resource_group_name(), |
5131 | | - namespace="Microsoft.ContainerService", |
5132 | | - type="managedClusters", |
5133 | | - name=self.context.get_name(), |
5134 | | - ) |
5135 | | - self.context.external_functions.add_monitoring_role_assignment( |
5136 | | - cluster, cluster_resource_id, self.cmd |
5137 | | - ) |
5138 | | - elif self._should_create_dcra(): |
5139 | | - # Create/update the DCR when CNL or HLSM flags change so that the DCR streams |
5140 | | - # (e.g. Microsoft-ContainerLogV2-HighScale) are kept in sync. |
5141 | | - cnl_or_hlsm_changing = ( |
5142 | | - self.context.raw_param.get("enable_container_network_logs") is not None or |
5143 | | - self.context.raw_param.get("enable_retina_flow_logs") is not None or |
5144 | | - self.context.raw_param.get("disable_container_network_logs") is not None or |
5145 | | - self.context.raw_param.get("disable_retina_flow_logs") is not None or |
5146 | | - self.context.raw_param.get("enable_high_log_scale_mode") is not None |
5147 | | - ) |
5148 | | - addon_consts = self.context.get_addon_consts() |
5149 | | - CONST_MONITORING_ADDON_NAME = addon_consts.get("CONST_MONITORING_ADDON_NAME") |
5150 | | - # The API response may return the addon key as either "omsagent" or "omsAgent" |
5151 | | - monitoring_addon_key = CONST_MONITORING_ADDON_NAME |
5152 | | - if CONST_MONITORING_ADDON_NAME not in cluster.addon_profiles and \ |
5153 | | - CONST_MONITORING_ADDON_NAME_CAMELCASE in cluster.addon_profiles: |
5154 | | - monitoring_addon_key = CONST_MONITORING_ADDON_NAME_CAMELCASE |
| 5174 | + def _postprocess_monitoring_disable(self) -> None: |
| 5175 | + """Handle monitoring addon postprocessing for the disable case.""" |
| 5176 | + addon_consts = self.context.get_addon_consts() |
| 5177 | + CONST_MONITORING_ADDON_NAME = addon_consts.get("CONST_MONITORING_ADDON_NAME") |
| 5178 | + |
| 5179 | + # Get the current cluster state to check config before it was disabled |
| 5180 | + current_cluster = self.client.get(self.context.get_resource_group_name(), self.context.get_name()) |
| 5181 | + |
| 5182 | + if (current_cluster.addon_profiles and |
| 5183 | + CONST_MONITORING_ADDON_NAME in current_cluster.addon_profiles): |
| 5184 | + |
| 5185 | + addon_profile = current_cluster.addon_profiles[CONST_MONITORING_ADDON_NAME] |
| 5186 | + |
| 5187 | + try: |
5155 | 5188 | self.context.external_functions.ensure_container_insights_for_monitoring( |
5156 | 5189 | self.cmd, |
5157 | | - cluster.addon_profiles[monitoring_addon_key], |
| 5190 | + addon_profile, |
5158 | 5191 | self.context.get_subscription_id(), |
5159 | 5192 | self.context.get_resource_group_name(), |
5160 | 5193 | self.context.get_name(), |
5161 | 5194 | self.context.get_location(), |
5162 | | - remove_monitoring=False, |
5163 | | - aad_route=self.context.get_enable_msi_auth_for_monitoring(), |
5164 | | - create_dcr=cnl_or_hlsm_changing, |
| 5195 | + remove_monitoring=True, |
| 5196 | + aad_route=True, |
| 5197 | + create_dcr=False, |
5165 | 5198 | create_dcra=True, |
5166 | | - enable_syslog=self.context.get_enable_syslog(), |
5167 | | - data_collection_settings=self.context.get_data_collection_settings(), |
5168 | | - is_private_cluster=self.context.get_enable_private_cluster(), |
5169 | | - ampls_resource_id=self.context.get_ampls_resource_id(), |
5170 | | - enable_high_log_scale_mode=self.context.get_enable_high_log_scale_mode(), |
| 5199 | + enable_syslog=False, |
| 5200 | + data_collection_settings=None, |
| 5201 | + ampls_resource_id=None, |
| 5202 | + enable_high_log_scale_mode=False |
5171 | 5203 | ) |
| 5204 | + except TypeError: |
| 5205 | + pass |
| 5206 | + |
| 5207 | + # pylint: disable=too-many-locals,too-many-branches |
| 5208 | + def postprocessing_after_mc_created(self, cluster: ManagedCluster) -> None: |
| 5209 | + """Postprocessing performed after the cluster is created. |
| 5210 | +
|
| 5211 | + :return: None |
| 5212 | + """ |
| 5213 | + # monitoring addon |
| 5214 | + monitoring_addon_enabled = self.context.get_intermediate("monitoring_addon_enabled", default_value=False) |
| 5215 | + if monitoring_addon_enabled: |
| 5216 | + self._postprocess_monitoring_enable(cluster) |
5172 | 5217 |
|
5173 | 5218 | # Handle monitoring addon postprocessing (disable case) - same logic as aks_disable_addons |
5174 | 5219 | monitoring_addon_disable_postprocessing_required = self.context.get_intermediate( |
5175 | 5220 | "monitoring_addon_disable_postprocessing_required", default_value=False |
5176 | 5221 | ) |
5177 | 5222 |
|
5178 | 5223 | if monitoring_addon_disable_postprocessing_required: |
5179 | | - addon_consts = self.context.get_addon_consts() |
5180 | | - CONST_MONITORING_ADDON_NAME = addon_consts.get("CONST_MONITORING_ADDON_NAME") |
5181 | | - |
5182 | | - # Get the current cluster state to check config before it was disabled |
5183 | | - current_cluster = self.client.get(self.context.get_resource_group_name(), self.context.get_name()) |
5184 | | - |
5185 | | - if (current_cluster.addon_profiles and |
5186 | | - CONST_MONITORING_ADDON_NAME in current_cluster.addon_profiles): |
5187 | | - |
5188 | | - # Use the current cluster addon profile for cleanup |
5189 | | - addon_profile = current_cluster.addon_profiles[CONST_MONITORING_ADDON_NAME] |
5190 | | - |
5191 | | - # Call ensure_container_insights_for_monitoring with remove_monitoring=True (same as aks_disable_addons) |
5192 | | - try: |
5193 | | - self.context.external_functions.ensure_container_insights_for_monitoring( |
5194 | | - self.cmd, |
5195 | | - addon_profile, |
5196 | | - self.context.get_subscription_id(), |
5197 | | - self.context.get_resource_group_name(), |
5198 | | - self.context.get_name(), |
5199 | | - self.context.get_location(), |
5200 | | - remove_monitoring=True, |
5201 | | - aad_route=True, |
5202 | | - create_dcr=False, |
5203 | | - create_dcra=True, |
5204 | | - enable_syslog=False, |
5205 | | - data_collection_settings=None, |
5206 | | - ampls_resource_id=None, |
5207 | | - enable_high_log_scale_mode=False |
5208 | | - ) |
5209 | | - except TypeError: |
5210 | | - # Ignore TypeError just like aks_disable_addons does |
5211 | | - pass |
| 5224 | + self._postprocess_monitoring_disable() |
5212 | 5225 |
|
5213 | 5226 | # ingress appgw addon |
5214 | 5227 | ingress_appgw_addon_enabled = self.context.get_intermediate("ingress_appgw_addon_enabled", default_value=False) |
@@ -7804,18 +7817,12 @@ def postprocessing_after_mc_created(self, cluster: ManagedCluster) -> None: |
7804 | 7817 | ) |
7805 | 7818 | if monitoring_addon_postprocessing_required: |
7806 | 7819 | addon_consts = self.context.get_addon_consts() |
7807 | | - CONST_MONITORING_ADDON_NAME = addon_consts.get("CONST_MONITORING_ADDON_NAME") |
7808 | 7820 | CONST_MONITORING_USING_AAD_MSI_AUTH = addon_consts.get("CONST_MONITORING_USING_AAD_MSI_AUTH") |
7809 | 7821 |
|
7810 | | - # The API response may return the addon key as either "omsagent" or "omsAgent" |
7811 | | - monitoring_addon_key = None |
7812 | | - if cluster.addon_profiles: |
7813 | | - if CONST_MONITORING_ADDON_NAME in cluster.addon_profiles: |
7814 | | - monitoring_addon_key = CONST_MONITORING_ADDON_NAME |
7815 | | - elif CONST_MONITORING_ADDON_NAME_CAMELCASE in cluster.addon_profiles: |
7816 | | - monitoring_addon_key = CONST_MONITORING_ADDON_NAME_CAMELCASE |
| 7822 | + monitoring_addon_key = _get_monitoring_addon_key(cluster, addon_consts) |
7817 | 7823 |
|
7818 | | - if (monitoring_addon_key and |
| 7824 | + if (cluster.addon_profiles and |
| 7825 | + monitoring_addon_key in cluster.addon_profiles and |
7819 | 7826 | cluster.addon_profiles[monitoring_addon_key].enabled): |
7820 | 7827 |
|
7821 | 7828 | # Check if MSI auth is enabled |
|
0 commit comments