33# Licensed under the MIT License. See License.txt in the project root for license information.
44# --------------------------------------------------------------------------------------------
55
6- import json
76from knack .log import get_logger
87from knack .util import CLIError
98from azure .cli .core .azclierror import ArgumentUsageError
4241logger = get_logger (__name__ )
4342
4443
45- def enable_addons (cmd ,
46- client ,
47- resource_group_name ,
48- name ,
49- addons ,
50- check_enabled = True ,
51- workspace_resource_id = None ,
52- subnet_name = None ,
53- appgw_name = None ,
54- appgw_subnet_prefix = None ,
55- appgw_subnet_cidr = None ,
56- appgw_id = None ,
57- appgw_subnet_id = None ,
58- appgw_watch_namespace = None ,
59- enable_sgxquotehelper = False ,
60- enable_secret_rotation = False ,
61- rotation_poll_interval = None ,
62- no_wait = False ,
63- dns_zone_resource_id = None ,
64- dns_zone_resource_ids = None ,
65- enable_msi_auth_for_monitoring = True ,
66- enable_syslog = False ,
67- data_collection_settings = None ):
44+ # pylint: disable=too-many-locals
45+ def enable_addons (
46+ cmd ,
47+ client ,
48+ resource_group_name ,
49+ name ,
50+ addons ,
51+ check_enabled = True ,
52+ workspace_resource_id = None ,
53+ subnet_name = None ,
54+ appgw_name = None ,
55+ appgw_subnet_prefix = None ,
56+ appgw_subnet_cidr = None ,
57+ appgw_id = None ,
58+ appgw_subnet_id = None ,
59+ appgw_watch_namespace = None ,
60+ enable_sgxquotehelper = False ,
61+ enable_secret_rotation = False ,
62+ rotation_poll_interval = None ,
63+ no_wait = False ,
64+ dns_zone_resource_id = None ,
65+ dns_zone_resource_ids = None ,
66+ enable_msi_auth_for_monitoring = True ,
67+ enable_syslog = False ,
68+ data_collection_settings = None
69+ ):
6870 instance = client .get (resource_group_name , name )
6971 # this is overwritten by _update_addons(), so the value needs to be recorded here
7072 msi_auth = False
@@ -74,18 +76,33 @@ def enable_addons(cmd,
7476 enable_msi_auth_for_monitoring = False
7577
7678 subscription_id = get_subscription_id (cmd .cli_ctx )
77- instance = update_addons (cmd , instance , subscription_id , resource_group_name , name , addons , enable = True ,
78- check_enabled = check_enabled ,
79- workspace_resource_id = workspace_resource_id ,
80- enable_msi_auth_for_monitoring = enable_msi_auth_for_monitoring , subnet_name = subnet_name ,
81- appgw_name = appgw_name , appgw_subnet_prefix = appgw_subnet_prefix ,
82- appgw_subnet_cidr = appgw_subnet_cidr , appgw_id = appgw_id , appgw_subnet_id = appgw_subnet_id ,
83- appgw_watch_namespace = appgw_watch_namespace ,
84- enable_sgxquotehelper = enable_sgxquotehelper ,
85- enable_secret_rotation = enable_secret_rotation , rotation_poll_interval = rotation_poll_interval , no_wait = no_wait ,
86- dns_zone_resource_id = dns_zone_resource_id , dns_zone_resource_ids = dns_zone_resource_ids ,
87- enable_syslog = enable_syslog ,
88- data_collection_settings = data_collection_settings )
79+ instance = update_addons (
80+ cmd ,
81+ instance ,
82+ subscription_id ,
83+ resource_group_name ,
84+ name ,
85+ addons ,
86+ enable = True ,
87+ check_enabled = check_enabled ,
88+ workspace_resource_id = workspace_resource_id ,
89+ enable_msi_auth_for_monitoring = enable_msi_auth_for_monitoring ,
90+ subnet_name = subnet_name ,
91+ appgw_name = appgw_name ,
92+ appgw_subnet_prefix = appgw_subnet_prefix ,
93+ appgw_subnet_cidr = appgw_subnet_cidr ,
94+ appgw_id = appgw_id ,
95+ appgw_subnet_id = appgw_subnet_id ,
96+ appgw_watch_namespace = appgw_watch_namespace ,
97+ enable_sgxquotehelper = enable_sgxquotehelper ,
98+ enable_secret_rotation = enable_secret_rotation ,
99+ rotation_poll_interval = rotation_poll_interval ,
100+ no_wait = no_wait ,
101+ dns_zone_resource_id = dns_zone_resource_id ,
102+ dns_zone_resource_ids = dns_zone_resource_ids ,
103+ enable_syslog = enable_syslog ,
104+ data_collection_settings = data_collection_settings ,
105+ )
89106
90107 if CONST_MONITORING_ADDON_NAME in instance .addon_profiles and instance .addon_profiles [
91108 CONST_MONITORING_ADDON_NAME ].enabled :
@@ -95,21 +112,20 @@ def enable_addons(cmd,
95112 if not msi_auth :
96113 raise ArgumentUsageError (
97114 "--enable-msi-auth-for-monitoring can not be used on clusters with service principal auth." )
98- else :
99- # create a Data Collection Rule (DCR) and associate it with the cluster
100- ensure_container_insights_for_monitoring (
101- cmd ,
102- instance .addon_profiles [CONST_MONITORING_ADDON_NAME ],
103- subscription_id ,
104- resource_group_name ,
105- name ,
106- instance .location ,
107- aad_route = True ,
108- create_dcr = True ,
109- create_dcra = True ,
110- enable_syslog = enable_syslog ,
111- data_collection_settings = data_collection_settings
112- )
115+ # create a Data Collection Rule (DCR) and associate it with the cluster
116+ ensure_container_insights_for_monitoring (
117+ cmd ,
118+ instance .addon_profiles [CONST_MONITORING_ADDON_NAME ],
119+ subscription_id ,
120+ resource_group_name ,
121+ name ,
122+ instance .location ,
123+ aad_route = True ,
124+ create_dcr = True ,
125+ create_dcra = True ,
126+ enable_syslog = enable_syslog ,
127+ data_collection_settings = data_collection_settings
128+ )
113129 else :
114130 # monitoring addon will use legacy path
115131 if enable_syslog :
@@ -163,31 +179,34 @@ def enable_addons(cmd,
163179 return result
164180
165181
166- def update_addons (cmd , # pylint: disable=too-many-branches,too-many-statements
167- instance ,
168- subscription_id ,
169- resource_group_name ,
170- name ,
171- addons ,
172- enable ,
173- check_enabled = True ,
174- workspace_resource_id = None ,
175- enable_msi_auth_for_monitoring = True ,
176- subnet_name = None ,
177- appgw_name = None ,
178- appgw_subnet_prefix = None ,
179- appgw_subnet_cidr = None ,
180- appgw_id = None ,
181- appgw_subnet_id = None ,
182- appgw_watch_namespace = None ,
183- enable_sgxquotehelper = False ,
184- enable_secret_rotation = False ,
185- rotation_poll_interval = None ,
186- dns_zone_resource_id = None ,
187- dns_zone_resource_ids = None ,
188- no_wait = False , # pylint: disable=unused-argument
189- enable_syslog = False ,
190- data_collection_settings = None ):
182+ # pylint: disable=too-many-locals, too-many-branches, too-many-statements
183+ def update_addons (
184+ cmd ,
185+ instance ,
186+ subscription_id ,
187+ resource_group_name ,
188+ name ,
189+ addons ,
190+ enable ,
191+ check_enabled = True ,
192+ workspace_resource_id = None ,
193+ enable_msi_auth_for_monitoring = True ,
194+ subnet_name = None ,
195+ appgw_name = None ,
196+ appgw_subnet_prefix = None ,
197+ appgw_subnet_cidr = None ,
198+ appgw_id = None ,
199+ appgw_subnet_id = None ,
200+ appgw_watch_namespace = None ,
201+ enable_sgxquotehelper = False ,
202+ enable_secret_rotation = False ,
203+ rotation_poll_interval = None ,
204+ dns_zone_resource_id = None ,
205+ dns_zone_resource_ids = None ,
206+ no_wait = False , # pylint: disable=unused-argument
207+ enable_syslog = False , # pylint: disable=unused-argument
208+ data_collection_settings = None , # pylint: disable=unused-argument
209+ ):
191210 # parse the comma-separated addons argument
192211 addon_args = addons .split (',' )
193212
@@ -242,7 +261,7 @@ def update_addons(cmd, # pylint: disable=too-many-branches,too-many-statements
242261 continue
243262
244263 if addon_arg not in ADDONS :
245- raise CLIError ("Invalid addon name: {}." . format ( addon_arg ) )
264+ raise CLIError (f "Invalid addon name: { addon_arg } ." )
246265 addon = ADDONS [addon_arg ]
247266 if addon == CONST_VIRTUAL_NODE_ADDON_NAME :
248267 # only linux is supported for now, in the future this will be a user flag
@@ -274,13 +293,22 @@ def update_addons(cmd, # pylint: disable=too-many-branches,too-many-statements
274293
275294 cloud_name = cmd .cli_ctx .cloud .name
276295 if enable_msi_auth_for_monitoring and (cloud_name .lower () == 'ussec' or cloud_name .lower () == 'usnat' ):
277- if instance .identity is not None and instance .identity .type is not None and instance .identity .type == "userassigned" :
278- logger .warning ("--enable_msi_auth_for_monitoring is not supported in %s cloud and continuing monitoring enablement without this flag." , cloud_name )
296+ if (
297+ instance .identity is not None and
298+ instance .identity .type is not None and
299+ instance .identity .type == "userassigned"
300+ ):
301+ logger .warning (
302+ "--enable_msi_auth_for_monitoring is not supported in %s cloud and continuing "
303+ "monitoring enablement without this flag." , cloud_name
304+ )
279305 enable_msi_auth_for_monitoring = False
280306
281307 addon_profile .config = {
282308 logAnalyticsConstName : workspace_resource_id }
283- addon_profile .config [CONST_MONITORING_USING_AAD_MSI_AUTH ] = "true" if enable_msi_auth_for_monitoring else "false"
309+ addon_profile .config [CONST_MONITORING_USING_AAD_MSI_AUTH ] = (
310+ "true" if enable_msi_auth_for_monitoring else "false"
311+ )
284312 elif addon == (CONST_VIRTUAL_NODE_ADDON_NAME + os_type ):
285313 if addon_profile .enabled and check_enabled :
286314 raise CLIError ('The virtual-node addon is already enabled for this managed cluster.\n '
@@ -333,10 +361,11 @@ def update_addons(cmd, # pylint: disable=too-many-branches,too-many-statements
333361 elif addon == CONST_AZURE_KEYVAULT_SECRETS_PROVIDER_ADDON_NAME :
334362 if addon_profile .enabled and check_enabled :
335363 raise CLIError (
336- 'The azure-keyvault-secrets-provider addon is already enabled for this managed cluster.\n '
337- 'To change azure-keyvault-secrets-provider configuration, run '
338- f'"az aks disable-addons -a azure-keyvault-secrets-provider -n { name } -g { resource_group_name } " '
339- 'before enabling it again.' )
364+ "The azure-keyvault-secrets-provider addon is already enabled for this managed cluster.\n "
365+ "To change azure-keyvault-secrets-provider configuration, run "
366+ '"az aks disable-addons -a azure-keyvault-secrets-provider '
367+ f'-n { name } -g { resource_group_name } " before enabling it again.'
368+ )
340369 addon_profile = ManagedClusterAddonProfile (
341370 enabled = True , config = {CONST_SECRET_ROTATION_ENABLED : "false" , CONST_ROTATION_POLL_INTERVAL : "2m" })
342371 if enable_secret_rotation :
@@ -352,7 +381,8 @@ def update_addons(cmd, # pylint: disable=too-many-branches,too-many-statements
352381 enabled = False )
353382 else :
354383 raise CLIError (
355- "The addon {} is not installed." .format (addon ))
384+ f"The addon { addon } is not installed."
385+ )
356386 addon_profiles [addon ].config = None
357387 addon_profiles [addon ].enabled = enable
358388
0 commit comments