From 7c18f04c7f66ff6de6253fc5b33e95535697a813 Mon Sep 17 00:00:00 2001 From: Diamond Powell <32712461+diamondpowell@users.noreply.github.com> Date: Tue, 1 Sep 2026 12:37:36 -0400 Subject: [PATCH 1/4] Refactor AKS client credential configuration --- modules/python/clients/aks_client.py | 27 +++------ modules/python/crud/azure/machine_crud.py | 3 + modules/python/crud/azure/node_pool_crud.py | 2 + .../python/tests/clients/test_aks_client.py | 17 +++--- .../tests/crud/test_azure_node_pool_crud.py | 23 +++++++- .../python/tests/test_aks_machine_client.py | 10 +--- modules/python/tests/test_machine_crud.py | 20 ++++++- modules/python/tests/utils/test_azure_auth.py | 56 +++++++++++++++++++ modules/python/utils/azure_auth.py | 22 ++++++++ 9 files changed, 143 insertions(+), 37 deletions(-) create mode 100644 modules/python/tests/utils/test_azure_auth.py create mode 100644 modules/python/utils/azure_auth.py diff --git a/modules/python/clients/aks_client.py b/modules/python/clients/aks_client.py index b64b2ac463..e41c95df57 100644 --- a/modules/python/clients/aks_client.py +++ b/modules/python/clients/aks_client.py @@ -3,8 +3,7 @@ This module provides a client for interacting with Azure Kubernetes Service (AKS), focusing specifically on node pool operations (create, scale, delete). -It handles authentication with Azure services using Managed Identity -or other authentication methods provided by DefaultAzureCredential. +It uses an Azure credential supplied by the caller. The client also validates node readiness after operations using Kubernetes API. @@ -22,7 +21,6 @@ from azure.core.exceptions import HttpResponseError, ResourceNotFoundError from azure.core.pipeline.policies import RetryPolicy, RetryMode from azure.core.pipeline.transport import RequestsTransport -from azure.identity import DefaultAzureCredential, ManagedIdentityCredential from azure.mgmt.containerservice import ContainerServiceClient # Local imports @@ -49,8 +47,8 @@ class AKSClient: """ Client for Azure Kubernetes Service (AKS) operations. - This client handles authentication with Azure services and provides - methods for managing AKS node pools (create, scale, delete). + This client uses an injected Azure credential and provides methods for + managing AKS node pools (create, scale, delete). It also validates node readiness using Kubernetes API. """ @@ -104,7 +102,7 @@ def __init__( subscription_id: Optional[str] = None, resource_group: Optional[str] = None, cluster_name: Optional[str] = None, - use_managed_identity: bool = False, + credential=None, kube_config_file: Optional[str] = os.path.expanduser("~/.kube/config"), result_dir: Optional[str] = None, operation_timeout_minutes: int = 10, # Timeout for each step in seconds @@ -118,8 +116,7 @@ def __init__( resource_group: The Azure resource group containing the AKS cluster. cluster_name: The name of the AKS cluster. If not provided, will try to get the first cluster in the resource group. - use_managed_identity: Whether to use managed identity for authentication. - If False, will fall back to DefaultAzureCredential. + credential: The required Azure credential used to authenticate with AKS. kube_config_file: Path to the kubeconfig file for Kubernetes authentication. """ # Get subscription ID from environment if not provided @@ -134,17 +131,9 @@ def __init__( self.vm_size = None # Initialize vm_size attribute # Set up authentication - if use_managed_identity: - mi_client_id = os.getenv("AZURE_MI_ID") - if mi_client_id: - logger.info("Using Managed Identity with client ID for authentication") - self.credential = ManagedIdentityCredential(client_id=mi_client_id) - else: - logger.info("Using default Managed Identity for authentication") - self.credential = ManagedIdentityCredential() - else: - logger.info("Using DefaultAzureCredential for authentication") - self.credential = DefaultAzureCredential() + if credential is None: + raise ValueError("Azure credential is required.") + self.credential = credential # Set up retry policy retry_policy = RetryPolicy( retry_mode=RetryMode.Exponential, diff --git a/modules/python/crud/azure/machine_crud.py b/modules/python/crud/azure/machine_crud.py index 9af47779e4..87f430d1f5 100644 --- a/modules/python/crud/azure/machine_crud.py +++ b/modules/python/crud/azure/machine_crud.py @@ -16,6 +16,8 @@ from clients.aks_machine_client import AKSMachineClient from utils.logger_config import get_logger, setup_logging +from utils.azure_auth import configure_credential + setup_logging() logger = get_logger(__name__) @@ -40,6 +42,7 @@ def __init__(self, resource_group, kube_config_file=None, result_dir=None, kube_config_file=kube_config_file, result_dir=result_dir, operation_timeout_minutes=max(1, step_timeout // 60), + credential=configure_credential(), ) self.cluster_name = self.aks_client.get_cluster_name() self.result_dir = result_dir diff --git a/modules/python/crud/azure/node_pool_crud.py b/modules/python/crud/azure/node_pool_crud.py index 998299f0cb..03f3f36635 100644 --- a/modules/python/crud/azure/node_pool_crud.py +++ b/modules/python/crud/azure/node_pool_crud.py @@ -13,6 +13,7 @@ from clients.aks_client import AKSClient from utils.logger_config import get_logger, setup_logging +from utils.azure_auth import configure_credential # Configure logging setup_logging() @@ -61,6 +62,7 @@ def __init__( kube_config_file=kube_config_file, result_dir=result_dir, operation_timeout_minutes=step_timeout / 60, # Convert seconds to minutes + credential=configure_credential(), ) if not self.aks_client: diff --git a/modules/python/tests/clients/test_aks_client.py b/modules/python/tests/clients/test_aks_client.py index e0ff39a3db..8a12c5eaa0 100644 --- a/modules/python/tests/clients/test_aks_client.py +++ b/modules/python/tests/clients/test_aks_client.py @@ -17,9 +17,6 @@ def setUp(self): """Set up test environment""" # Create patches self.cs_client_patcher = mock.patch("clients.aks_client.ContainerServiceClient") - self.mi_cred_patcher = mock.patch( - "clients.aks_client.ManagedIdentityCredential" - ) self.k8s_client_patcher = mock.patch("clients.aks_client.KubernetesClient") # Mock the dynamic import of OperationContext self.operation_context_patcher = mock.patch( @@ -28,12 +25,11 @@ def setUp(self): # Start patches mock_cs_client = self.cs_client_patcher.start() - mock_mi_cred = self.mi_cred_patcher.start() mock_k8s_client = self.k8s_client_patcher.start() self.mock_operation_context = self.operation_context_patcher.start() # Setup mock credential - self.mock_credential = mock_mi_cred.return_value + self.mock_credential = mock.MagicMock() # Setup mock container service client self.mock_aks_client = mock_cs_client.return_value @@ -61,7 +57,7 @@ def setUp(self): subscription_id="fake-subscription-id", resource_group="fake-resource-group", cluster_name="fake-cluster", - use_managed_identity=True, + credential=self.mock_credential, result_dir=self.test_result_dir, ) @@ -88,9 +84,16 @@ def tearDown(self): # Stop patches self.operation_context_patcher.stop() self.cs_client_patcher.stop() - self.mi_cred_patcher.stop() self.k8s_client_patcher.stop() + def test_init_requires_credential(self): + """Test that initialization requires an injected Azure credential.""" + with self.assertRaisesRegex(ValueError, "Azure credential is required"): + AKSClient( + subscription_id="fake-subscription-id", + resource_group="fake-resource-group", + ) + def test_get_cluster_name_provided(self): """Test get_cluster_name when name is already provided""" self.assertEqual(self.aks_client.get_cluster_name(), "fake-cluster") diff --git a/modules/python/tests/crud/test_azure_node_pool_crud.py b/modules/python/tests/crud/test_azure_node_pool_crud.py index 0889877c79..afa0734652 100644 --- a/modules/python/tests/crud/test_azure_node_pool_crud.py +++ b/modules/python/tests/crud/test_azure_node_pool_crud.py @@ -16,10 +16,17 @@ def setUp(self): """Set up test environment""" # Setup mock AKSClient self.aks_client_patcher = mock.patch("crud.azure.node_pool_crud.AKSClient") - mock_aks_client_cls = self.aks_client_patcher.start() - self.mock_aks_client = mock_aks_client_cls.return_value + self.mock_aks_client_cls = self.aks_client_patcher.start() + self.mock_aks_client = self.mock_aks_client_cls.return_value self.mock_aks_client.get_cluster_name.return_value = "fake-cluster" + self.credential_patcher = mock.patch( + "crud.azure.node_pool_crud.configure_credential" + ) + self.mock_configure_credential = self.credential_patcher.start() + self.mock_credential = mock.MagicMock() + self.mock_configure_credential.return_value = self.mock_credential + # Create test directory for result files self.test_result_dir = "/tmp/test_results" os.makedirs(self.test_result_dir, exist_ok=True) @@ -34,6 +41,7 @@ def setUp(self): def tearDown(self): """Clean up after tests""" # Stop patches + self.credential_patcher.stop() self.aks_client_patcher.stop() try: @@ -41,6 +49,17 @@ def tearDown(self): except OSError: pass + def test_init_injects_configured_credential(self): + """Test that the configured credential is passed to AKSClient.""" + self.mock_configure_credential.assert_called_once_with() + self.mock_aks_client_cls.assert_called_once_with( + resource_group="fake-resource-group", + kube_config_file=None, + result_dir=self.test_result_dir, + operation_timeout_minutes=10.0, + credential=self.mock_credential, + ) + def test_create_node_pool_success(self): """Test successful node pool creation""" # Setup diff --git a/modules/python/tests/test_aks_machine_client.py b/modules/python/tests/test_aks_machine_client.py index fb3f34e5e2..7692124304 100644 --- a/modules/python/tests/test_aks_machine_client.py +++ b/modules/python/tests/test_aks_machine_client.py @@ -18,16 +18,12 @@ def setUp(self): self.cs_client_patcher = mock.patch( "clients.aks_client.ContainerServiceClient" ) - self.mi_cred_patcher = mock.patch( - "clients.aks_client.ManagedIdentityCredential" - ) self.k8s_client_patcher = mock.patch("clients.aks_client.KubernetesClient") self.operation_context_getter_patcher = mock.patch( "clients.aks_machine_client.AKSMachineClient._get_operation_context" ) - + self.mock_credential = mock.MagicMock() self.cs_client_patcher.start() - self.mi_cred_patcher.start() self.mock_k8s_class = self.k8s_client_patcher.start() mock_get_operation_context = self.operation_context_getter_patcher.start() self.mock_operation_context = mock.MagicMock() @@ -46,7 +42,7 @@ def setUp(self): subscription_id="fake-sub", resource_group="fake-rg", cluster_name="fake-cluster", - use_managed_identity=True, + credential=self.mock_credential, result_dir=self.test_result_dir, ) @@ -634,7 +630,7 @@ def test_init_requires_k8s_client(self): AKSMachineClient( subscription_id="fake-sub", resource_group="fake-rg", - use_managed_identity=True, + credential=self.mock_credential, result_dir=self.test_result_dir, ) diff --git a/modules/python/tests/test_machine_crud.py b/modules/python/tests/test_machine_crud.py index 70ccc0f51e..b647d4278e 100644 --- a/modules/python/tests/test_machine_crud.py +++ b/modules/python/tests/test_machine_crud.py @@ -17,10 +17,17 @@ def setUp(self): self.client_patcher = mock.patch( "crud.azure.machine_crud.AKSMachineClient" ) - mock_client_class = self.client_patcher.start() - self.mock_client = mock_client_class.return_value + self.mock_client_class = self.client_patcher.start() + self.mock_client = self.mock_client_class.return_value self.mock_client.get_cluster_name.return_value = "fake-cluster" + self.credential_patcher = mock.patch( + "crud.azure.machine_crud.configure_credential" + ) + self.mock_configure_credential = self.credential_patcher.start() + self.mock_credential = mock.MagicMock() + self.mock_configure_credential.return_value = self.mock_credential + # Import lazily so the patch above is in effect. from crud.azure.machine_crud import MachineCRUD # pylint: disable=import-outside-toplevel self.crud = MachineCRUD( @@ -31,12 +38,21 @@ def setUp(self): ) def tearDown(self): + self.credential_patcher.stop() self.client_patcher.stop() def test_init_captures_cluster_name(self): self.assertEqual(self.crud.cluster_name, "fake-cluster") self.assertEqual(self.crud.result_dir, "/tmp/test_results") self.assertEqual(self.crud.step_timeout, 900) + self.mock_configure_credential.assert_called_once_with() + self.mock_client_class.assert_called_once_with( + resource_group="fake-rg", + kube_config_file=None, + result_dir="/tmp/test_results", + operation_timeout_minutes=15, + credential=self.mock_credential, + ) # ---- create_machine_agentpool ---- diff --git a/modules/python/tests/utils/test_azure_auth.py b/modules/python/tests/utils/test_azure_auth.py new file mode 100644 index 0000000000..f076a3a60e --- /dev/null +++ b/modules/python/tests/utils/test_azure_auth.py @@ -0,0 +1,56 @@ +"""Tests for Azure credential configuration.""" + +import unittest +from unittest import mock + +from utils.azure_auth import configure_credential + + +class TestConfigureCredential(unittest.TestCase): + """Tests for configure_credential""" + + @mock.patch("utils.azure_auth.DefaultAzureCredential") + def test_configure_default_credential(self, mock_default_credential): + credential = configure_credential() + + mock_default_credential.assert_called_once_with() + self.assertIs(credential, mock_default_credential.return_value) + + @mock.patch("utils.azure_auth.DefaultAzureCredential") + def test_configure_default_credential_excludes_managed_identity( + self, mock_default_credential + ): + credential = configure_credential(default_credential_exclude_mi=True) + + mock_default_credential.assert_called_once_with( + exclude_managed_identity_credential=True + ) + self.assertIs(credential, mock_default_credential.return_value) + + @mock.patch.dict("os.environ", {}, clear=True) + @mock.patch("utils.azure_auth.ManagedIdentityCredential") + def test_configure_system_assigned_managed_identity( + self, mock_managed_identity_credential + ): + credential = configure_credential(use_managed_identity=True) + + mock_managed_identity_credential.assert_called_once_with() + self.assertIs(credential, mock_managed_identity_credential.return_value) + + @mock.patch.dict( + "os.environ", {"AZURE_MI_ID": "fake-managed-identity-id"}, clear=True + ) + @mock.patch("utils.azure_auth.ManagedIdentityCredential") + def test_configure_user_assigned_managed_identity( + self, mock_managed_identity_credential + ): + credential = configure_credential(use_managed_identity=True) + + mock_managed_identity_credential.assert_called_once_with( + client_id="fake-managed-identity-id" + ) + self.assertIs(credential, mock_managed_identity_credential.return_value) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/modules/python/utils/azure_auth.py b/modules/python/utils/azure_auth.py new file mode 100644 index 0000000000..b102999d12 --- /dev/null +++ b/modules/python/utils/azure_auth.py @@ -0,0 +1,22 @@ +"""Azure credential configuration.""" + +import os +from azure.identity import DefaultAzureCredential, ManagedIdentityCredential + + +def configure_credential( + use_managed_identity=False, + default_credential_exclude_mi=False, +): + """Create the Azure credential requested by the caller.""" + if use_managed_identity: + managed_identity_client_id = os.getenv("AZURE_MI_ID") + if managed_identity_client_id: + return ManagedIdentityCredential(client_id=managed_identity_client_id) + return ManagedIdentityCredential() + + if default_credential_exclude_mi: + return DefaultAzureCredential( + exclude_managed_identity_credential=True + ) + return DefaultAzureCredential() \ No newline at end of file From 036204141349800c46dd68bd84e6e0bf2edb1f5e Mon Sep 17 00:00:00 2001 From: Diamond Powell <32712461+diamondpowell@users.noreply.github.com> Date: Tue, 1 Sep 2026 12:46:41 -0400 Subject: [PATCH 2/4] Fix credential file formatting --- modules/python/tests/utils/test_azure_auth.py | 2 +- modules/python/utils/azure_auth.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/python/tests/utils/test_azure_auth.py b/modules/python/tests/utils/test_azure_auth.py index f076a3a60e..66acc005ce 100644 --- a/modules/python/tests/utils/test_azure_auth.py +++ b/modules/python/tests/utils/test_azure_auth.py @@ -53,4 +53,4 @@ def test_configure_user_assigned_managed_identity( if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() diff --git a/modules/python/utils/azure_auth.py b/modules/python/utils/azure_auth.py index b102999d12..edea7e6e7f 100644 --- a/modules/python/utils/azure_auth.py +++ b/modules/python/utils/azure_auth.py @@ -19,4 +19,4 @@ def configure_credential( return DefaultAzureCredential( exclude_managed_identity_credential=True ) - return DefaultAzureCredential() \ No newline at end of file + return DefaultAzureCredential() From b994bb6f826ed65586c655689d05bfd4025880d6 Mon Sep 17 00:00:00 2001 From: Diamond Powell <32712461+diamondpowell@users.noreply.github.com> Date: Tue, 1 Sep 2026 13:19:03 -0400 Subject: [PATCH 3/4] Configure credential refactor pipeline tests --- pipelines/system/new-pipeline-test.yml | 75 ++++++++++++++++++++------ 1 file changed, 59 insertions(+), 16 deletions(-) diff --git a/pipelines/system/new-pipeline-test.yml b/pipelines/system/new-pipeline-test.yml index 2f864af31c..8c4e82609e 100644 --- a/pipelines/system/new-pipeline-test.yml +++ b/pipelines/system/new-pipeline-test.yml @@ -4,25 +4,68 @@ pool: name: 'AKS-Telescope-Airlock' variables: - SCENARIO_TYPE: - SCENARIO_NAME: + SCENARIO_TYPE: perf-eval stages: - - stage: # format: [_]+ (e.g. azure_eastus2, aws_eastus_westus) + - stage: azure_australiaeast_node_pool_crud dependsOn: [] + variables: + SCENARIO_NAME: k8s-gpu-cluster-crud + AZURE_SERVICE_CONNECTION: Azure (42) + AZURE_SUBSCRIPTION_ID: c0d4b923-b5ea-4f8f-9b56-5390a9bf2248 jobs: - - template: /jobs/competitive-test.yml # must keep as is + - template: /jobs/competitive-test.yml parameters: - cloud: # e.g. azure, aws - regions: # list of regions - - region1 # e.g. eastus2 - topology: # e.g. cluster-autoscaler - engine: # e.g. clusterloader2 - matrix: # list of test parameters to customize the provisioned resources - : - : - : - max_parallel: # required - credential_type: service_connection # required + cloud: azure + regions: + - australiaeast + topology: k8s-crud-gpu + engine: crud + matrix: + node_pool_crud_standard: + VM_SIZE: Standard_D4s_v3 + CREATE_NODE_COUNT: 1 + SCALE_NODE_COUNT: 2 + SCALE_STEP_SIZE: 1 + POOL_NAME: testpool + STEP_TIME_OUT: 600 + GPU_NODE_POOL: "" + ENABLE_MANAGED_GPU: "false" + GPU_INSTANCE_PROFILE: "" + GPU_MIG_STRATEGY: "" + STEP_WAIT_TIME: 30 + COUNT: 1 + REPLICAS: 2 + COMPLETIONS: 1 + MANIFEST_DIR: "" + max_parallel: 1 + credential_type: service_connection ssh_key_enabled: false - timeout_in_minutes: 60 # if not specified, default is 60 + timeout_in_minutes: 60 + + - stage: azure_canadacentral_machine_crud + dependsOn: [] + variables: + SCENARIO_NAME: k8s-cluster-crud-machine + AZURE_SERVICE_CONNECTION: Azure-for-Telescope-Large-Cluster-Sub + AZURE_SUBSCRIPTION_ID: 854c9ddb-fe9e-4aea-8d58-99ed88282881 + jobs: + - template: /jobs/competitive-test.yml + parameters: + cloud: azure + regions: + - canadacentral + topology: k8s-cluster-crud-machine + engine: machine + matrix: + machine_crud_small_scale_10: + VM_SIZE: Standard_D2_v3 + POOL_NAME: smallpool1 + SCALE_MACHINE_COUNT: 10 + MACHINE_WORKERS: 5 + USE_BATCH_API: "false" + STEP_TIME_OUT: 900 + max_parallel: 1 + credential_type: service_connection + ssh_key_enabled: false + timeout_in_minutes: 360 From 6a2332d44db7de61320e67895c85b526a63a6184 Mon Sep 17 00:00:00 2001 From: Diamond Powell <32712461+diamondpowell@users.noreply.github.com> Date: Tue, 1 Sep 2026 15:01:58 -0400 Subject: [PATCH 4/4] Revert credential refactor pipeline test config --- pipelines/system/new-pipeline-test.yml | 75 ++++++-------------------- 1 file changed, 16 insertions(+), 59 deletions(-) diff --git a/pipelines/system/new-pipeline-test.yml b/pipelines/system/new-pipeline-test.yml index 8c4e82609e..2f864af31c 100644 --- a/pipelines/system/new-pipeline-test.yml +++ b/pipelines/system/new-pipeline-test.yml @@ -4,68 +4,25 @@ pool: name: 'AKS-Telescope-Airlock' variables: - SCENARIO_TYPE: perf-eval + SCENARIO_TYPE: + SCENARIO_NAME: stages: - - stage: azure_australiaeast_node_pool_crud + - stage: # format: [_]+ (e.g. azure_eastus2, aws_eastus_westus) dependsOn: [] - variables: - SCENARIO_NAME: k8s-gpu-cluster-crud - AZURE_SERVICE_CONNECTION: Azure (42) - AZURE_SUBSCRIPTION_ID: c0d4b923-b5ea-4f8f-9b56-5390a9bf2248 jobs: - - template: /jobs/competitive-test.yml + - template: /jobs/competitive-test.yml # must keep as is parameters: - cloud: azure - regions: - - australiaeast - topology: k8s-crud-gpu - engine: crud - matrix: - node_pool_crud_standard: - VM_SIZE: Standard_D4s_v3 - CREATE_NODE_COUNT: 1 - SCALE_NODE_COUNT: 2 - SCALE_STEP_SIZE: 1 - POOL_NAME: testpool - STEP_TIME_OUT: 600 - GPU_NODE_POOL: "" - ENABLE_MANAGED_GPU: "false" - GPU_INSTANCE_PROFILE: "" - GPU_MIG_STRATEGY: "" - STEP_WAIT_TIME: 30 - COUNT: 1 - REPLICAS: 2 - COMPLETIONS: 1 - MANIFEST_DIR: "" - max_parallel: 1 - credential_type: service_connection + cloud: # e.g. azure, aws + regions: # list of regions + - region1 # e.g. eastus2 + topology: # e.g. cluster-autoscaler + engine: # e.g. clusterloader2 + matrix: # list of test parameters to customize the provisioned resources + : + : + : + max_parallel: # required + credential_type: service_connection # required ssh_key_enabled: false - timeout_in_minutes: 60 - - - stage: azure_canadacentral_machine_crud - dependsOn: [] - variables: - SCENARIO_NAME: k8s-cluster-crud-machine - AZURE_SERVICE_CONNECTION: Azure-for-Telescope-Large-Cluster-Sub - AZURE_SUBSCRIPTION_ID: 854c9ddb-fe9e-4aea-8d58-99ed88282881 - jobs: - - template: /jobs/competitive-test.yml - parameters: - cloud: azure - regions: - - canadacentral - topology: k8s-cluster-crud-machine - engine: machine - matrix: - machine_crud_small_scale_10: - VM_SIZE: Standard_D2_v3 - POOL_NAME: smallpool1 - SCALE_MACHINE_COUNT: 10 - MACHINE_WORKERS: 5 - USE_BATCH_API: "false" - STEP_TIME_OUT: 900 - max_parallel: 1 - credential_type: service_connection - ssh_key_enabled: false - timeout_in_minutes: 360 + timeout_in_minutes: 60 # if not specified, default is 60