Skip to content

Commit c12deac

Browse files
committed
address comments
1 parent 1cd59de commit c12deac

File tree

5 files changed

+42
-29
lines changed

5 files changed

+42
-29
lines changed

src/aks-agent/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Run in non-interactive batch mode
105105

106106
.. code-block:: bash
107107
108-
az aks agent "Diagnose networking issues" --no-interactive --max-steps 15 --model azure/my-gpt4.1-deployment
108+
az aks agent "Diagnose networking issues" --no-interactive --name MyManagedCluster --resource-group MyResourceGroup --model azure/my-gpt4.1-deployment
109109
110110
Clean up the AKS agent
111111
-----------------------

src/aks-agent/azext_aks_agent/_help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
- name: Ask about pod issues in the cluster with last configured model
5757
text: |-
5858
az aks agent "Why are my pods not starting?" --resource-group myResourceGroup --name myAKSCluster
59-
- name: Check AKS agent deployment status
59+
- name: Check AKS agent deployment status
6060
text: |-
6161
az aks agent --status --resource-group myResourceGroup --name myAKSCluster
6262
- name: Ask about pod issues in the cluster with Azure OpenAI

src/aks-agent/azext_aks_agent/agent/aks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_aks_credentials(
3939
kubeconfig_path = _get_kubeconfig_file_path(resource_group_name, cluster_name)
4040

4141
# Ensure the kubeconfig file exists and write kubeconfig to it
42-
with os.fdopen(os.open(kubeconfig_path, os.O_CREAT | os.O_WRONLY, 0o600), 'wt') as f:
42+
with os.fdopen(os.open(kubeconfig_path, os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0o600), 'wt') as f:
4343
f.write(kubeconfig)
4444

4545
logger.info("Kubeconfig downloaded successfully to: %s", kubeconfig_path)
@@ -64,7 +64,7 @@ def _get_kubeconfig_file_path( # pylint: disable=unused-argument
6464
if ex.errno != errno.EEXIST:
6565
raise
6666

67-
kubeconfig_filename = f"kubeconfig-{cluster_name}"
67+
kubeconfig_filename = f"kubeconfig-{resource_group_name}-{cluster_name}"
6868
kubeconfig_path = os.path.join(kubeconfig_dir, kubeconfig_filename)
6969

7070
return kubeconfig_path

src/aks-agent/azext_aks_agent/custom.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def aks_agent_init(cmd,
174174

175175
except Exception as e:
176176
console.print(f"❌ Error during initialization: {str(e)}", style=ERROR_COLOR)
177-
logger.error("Agent initialization failed: %s", e)
177+
raise AzCLIError(f"Agent initialization failed: {str(e)}")
178178

179179

180180
def _get_existing_cluster_role(aks_agent_manager):
@@ -431,10 +431,9 @@ def aks_agent_cleanup(
431431
console.print(
432432
"❌ Cleanup failed. Please run 'az aks agent --status' to verify cleanup completion.", style=ERROR_COLOR)
433433

434+
434435
# pylint: disable=unused-argument
435436
# pylint: disable=too-many-locals
436-
437-
438437
def aks_agent(
439438
cmd,
440439
client,

src/aks-agent/azext_aks_agent/tests/latest/test_custom_init.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@ def test_init_new_deployment_no_llm_config(
7373
mock_prompt_managed_identity.assert_called_once()
7474
mock_agent_manager.deploy_agent.assert_called_once()
7575

76+
@patch('azext_aks_agent.custom._get_existing_cluster_role')
7677
@patch('azext_aks_agent.custom._setup_and_create_llm_config')
7778
@patch('azext_aks_agent.custom.get_aks_credentials')
7879
@patch('azext_aks_agent.custom.AKSAgentManager')
7980
@patch('azext_aks_agent.custom.get_subscription_id')
8081
@patch('azext_aks_agent.custom.get_console')
8182
def test_init_existing_llm_config_user_skips_update(
8283
self, mock_get_console, mock_get_subscription_id, mock_aks_manager_class,
83-
mock_get_aks_creds, mock_setup_llm):
84-
"""Test initialization when LLM config exists and user skips update."""
84+
mock_get_aks_creds, mock_setup_llm, mock_get_cluster_role):
85+
"""Test initialization raises AzCLIError when LLM config exists, user skips update, but cluster role not found."""
8586
# Setup mocks
8687
mock_console = MagicMock()
8788
mock_get_console.return_value = mock_console
@@ -98,22 +99,29 @@ def test_init_existing_llm_config_user_skips_update(
9899
mock_agent_manager.managed_identity_client_id = ""
99100
mock_aks_manager_class.return_value = mock_agent_manager
100101

101-
# Execute
102-
aks_agent_init(self.mock_cmd, self.mock_client, self.resource_group, self.cluster_name)
102+
mock_get_cluster_role.return_value = None # Cannot find cluster role
103+
104+
# Execute - should raise AzCLIError with wrapped message
105+
with self.assertRaises(AzCLIError) as cm:
106+
aks_agent_init(self.mock_cmd, self.mock_client, self.resource_group, self.cluster_name)
103107

104108
# Assert
105109
mock_agent_manager.check_llm_config_exists.assert_called_once()
106110
mock_setup_llm.assert_not_called()
111+
# Verify the error message contains the wrapped format
112+
self.assertIn("Agent initialization failed:", str(cm.exception))
113+
self.assertIn("Could not determine existing cluster role", str(cm.exception))
107114

115+
@patch('azext_aks_agent.custom._get_existing_cluster_role')
108116
@patch('azext_aks_agent.custom._setup_and_create_llm_config')
109117
@patch('azext_aks_agent.custom.get_aks_credentials')
110118
@patch('azext_aks_agent.custom.AKSAgentManager')
111119
@patch('azext_aks_agent.custom.get_subscription_id')
112120
@patch('azext_aks_agent.custom.get_console')
113121
def test_init_existing_llm_config_user_updates(
114122
self, mock_get_console, mock_get_subscription_id, mock_aks_manager_class,
115-
mock_get_aks_creds, mock_setup_llm):
116-
"""Test initialization when LLM config exists and user chooses to update."""
123+
mock_get_aks_creds, mock_setup_llm, mock_get_cluster_role):
124+
"""Test initialization raises AzCLIError when LLM config exists, user updates, but cluster role not found."""
117125
# Setup mocks
118126
mock_console = MagicMock()
119127
mock_get_console.return_value = mock_console
@@ -130,12 +138,18 @@ def test_init_existing_llm_config_user_updates(
130138
mock_agent_manager.managed_identity_client_id = ""
131139
mock_aks_manager_class.return_value = mock_agent_manager
132140

133-
# Execute
134-
aks_agent_init(self.mock_cmd, self.mock_client, self.resource_group, self.cluster_name)
141+
mock_get_cluster_role.return_value = None # Cannot find cluster role
142+
143+
# Execute - should raise AzCLIError with wrapped message
144+
with self.assertRaises(AzCLIError) as cm:
145+
aks_agent_init(self.mock_cmd, self.mock_client, self.resource_group, self.cluster_name)
135146

136147
# Assert
137148
mock_agent_manager.check_llm_config_exists.assert_called_once()
138149
mock_setup_llm.assert_called_once_with(mock_console, mock_agent_manager)
150+
# Verify the error message contains the wrapped format
151+
self.assertIn("Agent initialization failed:", str(cm.exception))
152+
self.assertIn("Could not determine existing cluster role", str(cm.exception))
139153

140154
@patch('azext_aks_agent.custom._get_existing_cluster_role')
141155
@patch('azext_aks_agent.custom._display_cluster_role_rules')
@@ -226,7 +240,7 @@ def test_init_with_custom_cluster_role(
226240
def test_init_deployment_failure_logs_error(
227241
self, mock_get_console, mock_get_subscription_id, mock_aks_manager_class,
228242
mock_get_aks_creds, mock_setup_llm):
229-
"""Test initialization logs error when deployment fails."""
243+
"""Test initialization raises AzCLIError when deployment fails."""
230244
# Setup mocks
231245
mock_console = MagicMock()
232246
mock_get_console.return_value = mock_console
@@ -239,13 +253,13 @@ def test_init_deployment_failure_logs_error(
239253
mock_agent_manager.deploy_agent.return_value = (False, "Deployment failed")
240254
mock_aks_manager_class.return_value = mock_agent_manager
241255

242-
# Execute - should not raise, but handle the error internally
243-
aks_agent_init(self.mock_cmd, self.mock_client, self.resource_group, self.cluster_name)
256+
# Execute - should raise AzCLIError with wrapped message
257+
with self.assertRaises(AzCLIError) as cm:
258+
aks_agent_init(self.mock_cmd, self.mock_client, self.resource_group, self.cluster_name)
244259

245-
# Verify error message was printed to console
246-
error_calls = [call for call in mock_console.print.call_args_list
247-
if 'Failed to deploy agent' in str(call)]
248-
self.assertTrue(len(error_calls) > 0, "Expected error message about deployment failure to be printed")
260+
# Verify the error message contains the wrapped format
261+
self.assertIn("Agent initialization failed:", str(cm.exception))
262+
self.assertIn("Failed to deploy agent", str(cm.exception))
249263

250264
@patch('azext_aks_agent.custom._get_existing_cluster_role')
251265
@patch('azext_aks_agent.custom._setup_and_create_llm_config')
@@ -256,7 +270,7 @@ def test_init_deployment_failure_logs_error(
256270
def test_init_deployed_no_cluster_role_logs_error(
257271
self, mock_get_console, mock_get_subscription_id, mock_aks_manager_class,
258272
mock_get_aks_creds, mock_setup_llm, mock_get_cluster_role):
259-
"""Test initialization logs error when deployed but cannot determine cluster role."""
273+
"""Test initialization raises AzCLIError when deployed but cannot determine cluster role."""
260274
# Setup mocks
261275
mock_console = MagicMock()
262276
mock_get_console.return_value = mock_console
@@ -271,13 +285,13 @@ def test_init_deployed_no_cluster_role_logs_error(
271285

272286
mock_get_cluster_role.return_value = None # Cannot find cluster role
273287

274-
# Execute - should not raise, but handle the error internally
275-
aks_agent_init(self.mock_cmd, self.mock_client, self.resource_group, self.cluster_name)
288+
# Execute - should raise AzCLIError with wrapped message
289+
with self.assertRaises(AzCLIError) as cm:
290+
aks_agent_init(self.mock_cmd, self.mock_client, self.resource_group, self.cluster_name)
276291

277-
# Verify error message was printed to console
278-
error_calls = [call for call in mock_console.print.call_args_list
279-
if 'Could not determine existing cluster role' in str(call)]
280-
self.assertTrue(len(error_calls) > 0, "Expected error message about cluster role to be printed")
292+
# Verify the error message contains the wrapped format
293+
self.assertIn("Agent initialization failed:", str(cm.exception))
294+
self.assertIn("Could not determine existing cluster role", str(cm.exception))
281295

282296
@patch('azext_aks_agent.custom._prompt_managed_identity_configuration')
283297
@patch('azext_aks_agent.custom._prompt_cluster_role_configuration')

0 commit comments

Comments
 (0)