diff --git a/modules/python/clients/aks_machine_client.py b/modules/python/clients/aks_machine_client.py new file mode 100644 index 0000000000..2325bd9e7d --- /dev/null +++ b/modules/python/clients/aks_machine_client.py @@ -0,0 +1,270 @@ +"""AKS Machine API client. + +Subclasses ``AKSClient`` to inherit auth, ContainerServiceClient, +KubernetesClient, ``get_cluster_name``, ``get_cluster_data``, and the existing +node-pool CRUD methods. Adds raw REST methods for the Machine API which is not +yet exposed in the Azure SDK. + +Public methods (``create_machine_agentpool``, ``scale_machine``) wrap their work +in ``OperationContext``: the context is opened inside the client method, +metadata is enriched with ``op.add_metadata`` along the way, success returns +True, failures are logged and re-raised so ``OperationContext`` records them. +``MachineCRUD`` therefore stays a thin try/except wrapper. + +This is the scaffolding revision: ``create_machine_agentpool`` and the +agentpool provisioning poll are fully implemented; ``scale_machine``, its +private helpers, and the ``_get_machine_name_prefix`` naming helper raise +``NotImplementedError`` and will land in a follow-up PR. +""" +import logging +import threading +import time +from typing import Any, Dict, Optional + +import requests + +from clients.aks_client import AKSClient +from utils.logger_config import get_logger, setup_logging + +# Configure logging. +setup_logging() +logger = get_logger(__name__) +# Suppress noisy Azure SDK logs. +get_logger("azure.core.pipeline.policies.http_logging_policy").setLevel(logging.ERROR) +get_logger("azure.identity").setLevel(logging.ERROR) +get_logger("azure.core.pipeline").setLevel(logging.ERROR) +get_logger("msal").setLevel(logging.ERROR) + +_ARM_BASE = "https://management.azure.com" +_ARM_SCOPE = "https://management.azure.com/.default" +_AGENTPOOL_API_VERSION = "2024-06-02-preview" +_POLL_INTERVAL_SECONDS = 10 +# Per-request HTTP timeout is capped so that a single slow PUT/GET cannot +# consume the caller's whole ``timeout`` budget before the polling loop starts. +# The remaining budget is reserved for ``_wait_for_agentpool_provisioning``. +_PER_REQUEST_TIMEOUT_CAP = 60 + + +class AKSMachineClient(AKSClient): + """Extends ``AKSClient`` with Machine-API REST plumbing. + + Inherits ``credential``, ``subscription_id``, ``resource_group``, + ``k8s_client``, ``get_cluster_name``, ``get_cluster_data``, and the + existing ``create_node_pool`` / ``scale_node_pool`` / ``delete_node_pool`` + methods. Tests that require a baseline node pool can therefore call the + inherited methods directly without a separate client. + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._token_cache_value: Optional[str] = None + self._token_cache_expiry: float = 0.0 # unix epoch seconds + self._token_lock = threading.Lock() + # Pool TCP/TLS across calls. ``requests.Session`` is thread-safe for + # sending requests (each request creates its own urllib3 connection + # from the shared pool), so this is safe for the planned parallel + # scale path landing in PR-2. + self._session = requests.Session() + + # ---- auth + REST plumbing ---- + def _get_access_token(self) -> str: + """Cached token fetch. Re-uses token across worker threads with a 60s safety margin + before expiry. Avoids forking `az account get-access-token` per worker.""" + with self._token_lock: + if self._token_cache_value and time.time() < self._token_cache_expiry - 60: + return self._token_cache_value + tok = self.credential.get_token(_ARM_SCOPE) + self._token_cache_value = tok.token + self._token_cache_expiry = float(tok.expires_on) + return self._token_cache_value + + def make_request( + self, + method: str, + url: str, + data: Optional[Dict[str, Any]] = None, + timeout: int = 30, + ) -> requests.Response: + """Send an authenticated ARM REST request and return the raw Response. + + Callers are responsible for checking ``response.status_code`` (or + calling ``raise_for_status()``) — this method does NOT raise on HTTP + errors. Long-running Machine API operations return 200/202 with a + Location header that callers must follow. + + Uses ``self._session`` for connection pooling so TCP/TLS handshakes + are reused across polls and the planned parallel scale path. + """ + headers = { + "Authorization": f"Bearer {self._get_access_token()}", + "Content-Type": "application/json", + } + return self._session.request( + method, url, headers=headers, json=data, timeout=timeout + ) + + # ---- Machine API: agent pool provisioning ---- + def create_machine_agentpool( + self, + agentpool_name: str, + vm_size: str, + cluster_name: Optional[str] = None, + timeout: int = 300, + ) -> None: + """Convert an existing agentpool to Machines mode via PUT. + + Opens an ``OperationContext`` here, enriches metadata, returns on + success, re-raises on failure (so the context records ``success=False`` + with traceback before the exception propagates to ``MachineCRUD``). + + Args: + agentpool_name: Target agentpool name. + vm_size: VM SKU recorded in operation metadata. The PUT body itself + only sets ``mode: Machines``; the SKU is informational here so + downstream Kusto rows can attribute the agentpool to a SKU. + cluster_name: Parent AKS cluster name. Defaults to + ``self.get_cluster_name()`` (matches ``create_node_pool``). + timeout: Total wall-clock budget (seconds) for this operation. + Used as the polling deadline for ``_wait_for_agentpool_provisioning``. + The per-request HTTP timeout is capped at + ``_PER_REQUEST_TIMEOUT_CAP`` so a single slow PUT cannot consume + the whole budget before polling starts. + + Raises: + RuntimeError: PUT non-2xx, ARM-reported Failed state, or timeout. + """ + cluster_name = cluster_name or self.get_cluster_name() + metadata = { + "cluster_name": cluster_name, + "agentpool_name": agentpool_name, + "vm_size": vm_size, + } + with self._get_operation_context()( + "create_machine_agentpool", "azure", metadata, result_dir=self.result_dir + ) as op: + try: + sub = self.subscription_id + url = ( + f"{_ARM_BASE}/subscriptions/{sub}/resourceGroups/{self.resource_group}" + f"/providers/Microsoft.ContainerService/managedClusters/{cluster_name}" + f"/agentPools/{agentpool_name}?api-version={_AGENTPOOL_API_VERSION}" + ) + body = {"properties": {"mode": "Machines"}} + # Cap the PUT timeout so the bulk of ``timeout`` is preserved + # for the polling loop below. + put_timeout = min(timeout, _PER_REQUEST_TIMEOUT_CAP) + resp = self.make_request("PUT", url, data=body, timeout=put_timeout) + # ARM async PUT acceptance returns 200/201/202; the agentpool + # provisioning poll below is the real completion gate. + if resp.status_code not in (200, 201, 202): + raise RuntimeError( + f"create_machine_agentpool PUT failed: " + f"{resp.status_code} {resp.text[:500]}" + ) + if not self._wait_for_agentpool_provisioning(url, timeout): + raise RuntimeError( + f"agentpool {agentpool_name} did not reach Succeeded within {timeout}s" + ) + op.add_metadata("agentpool_info", self.get_node_pool( + agentpool_name, cluster_name).as_dict()) + op.add_metadata("cluster_info", self.get_cluster_data(cluster_name)) + except Exception as e: + logger.error(f"Failed to create machine agentpool {agentpool_name}: {e}") + raise + + def _wait_for_agentpool_provisioning(self, url: str, timeout: int) -> bool: + """Poll the agentpool URL until provisioningState is terminal. + + Polls every ``_POLL_INTERVAL_SECONDS`` (10s) using a 30s per-request HTTP + timeout. Returns True on 'Succeeded'. Returns False on 'Failed' or when + ``timeout`` seconds have elapsed since invocation. 4xx GETs are raised + immediately (e.g. 404 'agentpool not found', 401/403) since retrying + won't change the outcome; 5xx and other transient failures are logged + and retried until the deadline. By AKS contract a 200 GET on the + agentpool always includes ``properties.provisioningState``; if it is + absent on a 200 response, that is a Failed signal and we stop + immediately rather than waiting out the timeout. + """ + deadline = time.time() + timeout + while time.time() < deadline: + r = self.make_request("GET", url, timeout=30) + if r.status_code != 200: + if 400 <= r.status_code < 500: + raise RuntimeError( + f"agentpool GET {url} -> {r.status_code} {r.text[:500]}" + ) + logger.warning(f"agentpool GET {url} -> {r.status_code}") + time.sleep(_POLL_INTERVAL_SECONDS) + continue + try: + body = r.json() + except (ValueError, TypeError) as exc: + logger.warning(f"agentpool poll JSON parse failed: {exc}") + time.sleep(_POLL_INTERVAL_SECONDS) + continue + state = body.get("properties", {}).get("provisioningState") + if state == "Succeeded": + return True + if state == "Failed": + logger.error(f"agentpool provisioning failed: {body}") + return False + if state is None: + logger.error( + f"agentpool 200 GET returned without provisioningState; " + f"treating as Failed. body={body}" + ) + return False + time.sleep(_POLL_INTERVAL_SECONDS) + logger.error(f"agentpool provisioning timed out after {timeout}s") + return False + + # ---- scale path: stubbed on this scaffolding PR ---- + # The methods below preserve the public/private signatures so that + # ``MachineCRUD`` can be wired up against them in a follow-up PR; each + # raises ``NotImplementedError`` until the implementation lands. + + def _wait_for_machine_node_readiness( + self, + agentpool_name: str, + expected_count: int, + timeout: int, + baseline_count: int = 0, + ) -> Dict[str, Dict[str, Any]]: + """Wait for ``expected_count`` NEW Ready nodes; return percentile dict. + + Stubbed in this PR; implementation lands in a follow-up. + """ + raise NotImplementedError( + "_wait_for_machine_node_readiness lands in a follow-up PR" + ) + + def scale_machine( + self, + agentpool_name: str, + vm_size: str, + scale_machine_count: int, + cluster_name: Optional[str] = None, + use_batch_api: bool = False, + machine_workers: int = 1, + timeout: int = 600, + readiness_wait_timeout: int = 1200, + tags: Optional[Dict[str, str]] = None, # pylint: disable=unused-argument + ) -> bool: + """Scale a Machine-mode agentpool by creating ``scale_machine_count`` machines. + + Stubbed in this PR; implementation lands in a follow-up. + """ + raise NotImplementedError( + "scale_machine lands in a follow-up PR" + ) + + @staticmethod + def _get_machine_name_prefix(scale_machine_count: int) -> str: + """Generate the machine-name prefix for a given scale count. + + Stubbed in this PR; implementation lands in a follow-up alongside the + scale path that consumes it. + """ + raise NotImplementedError( + "_get_machine_name_prefix lands in a follow-up PR" + ) diff --git a/modules/python/tests/test_aks_machine_client.py b/modules/python/tests/test_aks_machine_client.py new file mode 100644 index 0000000000..eca4505937 --- /dev/null +++ b/modules/python/tests/test_aks_machine_client.py @@ -0,0 +1,154 @@ +#!/usr/bin/env python3 +"""Unit tests for AKSMachineClient (subclass of AKSClient). + +All public methods open an ``OperationContext`` (patched here) and write the +result file via ``Operation.save_to_file`` on context exit. Tests verify: +- success path completes without raising and enriches ``op.add_metadata`` with + the right keys +- failure path raises (so the OperationContext records ``success=False``) + +Only ``create_machine_agentpool`` has a full implementation on this scaffolding +PR; ``scale_machine`` and the ``_get_machine_name_prefix`` naming helper raise +``NotImplementedError`` and will be tested in a follow-up PR. +""" +# pylint: disable=protected-access +# Tests intentionally exercise private helpers directly; the leading underscore +# is conventional rather than semantic privacy. +import tempfile +import unittest +from unittest import mock + +from clients.aks_machine_client import AKSMachineClient + + +class TestAKSMachineClient(unittest.TestCase): + """Tests for the AKSMachineClient class.""" + + def setUp(self): + """Patch all upstream Azure SDK and OperationContext seams.""" + 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.cs_client_patcher.start() + self.mi_cred_patcher.start() + mock_k8s_class = self.k8s_client_patcher.start() + mock_get_operation_context = self.operation_context_getter_patcher.start() + self.mock_operation_context = mock.MagicMock() + mock_get_operation_context.return_value = self.mock_operation_context + + self.mock_k8s = mock_k8s_class.return_value + self.mock_operation = mock.MagicMock() + self.mock_operation_context.return_value.__enter__.return_value = ( + self.mock_operation + ) + self.mock_operation_context.return_value.__exit__.return_value = None + + # Hermetic per-test temp dir avoids cross-platform /tmp assumptions + # and parallel-run collisions. ``with`` doesn't fit the setUp/tearDown + # lifecycle, so we explicitly cleanup() in tearDown. + self._tmp_dir = tempfile.TemporaryDirectory() # pylint: disable=consider-using-with + self.test_result_dir = self._tmp_dir.name + + self.client = AKSMachineClient( + subscription_id="fake-sub", + resource_group="fake-rg", + cluster_name="fake-cluster", + use_managed_identity=True, + result_dir=self.test_result_dir, + ) + + # Stub inherited helpers that the Machine methods enrich metadata with. + self.client.get_cluster_name = mock.MagicMock(return_value="fake-cluster") + self.client.get_cluster_data = mock.MagicMock( + return_value={"name": "fake-cluster"} + ) + fake_pool = mock.MagicMock() + fake_pool.as_dict.return_value = {"name": "fake-pool"} + self.client.get_node_pool = mock.MagicMock(return_value=fake_pool) + + def tearDown(self): + mock.patch.stopall() + self._tmp_dir.cleanup() + + # ---- create_machine_agentpool ---- + + @mock.patch.object(AKSMachineClient, "_wait_for_agentpool_provisioning", + return_value=True) + @mock.patch.object(AKSMachineClient, "make_request") + def test_create_machine_agentpool_success(self, mock_make_request, _mock_wait): + """PUT 200 + Succeeded poll -> returns; metadata enriched.""" + mock_resp = mock.MagicMock() + mock_resp.status_code = 200 + mock_make_request.return_value = mock_resp + + self.client.create_machine_agentpool( + agentpool_name="apool", vm_size="Standard_D2_v3" + ) + + mock_make_request.assert_called_once() + call_args = mock_make_request.call_args + self.assertEqual(call_args.args[0], "PUT") + self.assertEqual(call_args.kwargs["data"], {"properties": {"mode": "Machines"}}) + metadata_keys = { + call.args[0] for call in self.mock_operation.add_metadata.call_args_list + } + self.assertIn("agentpool_info", metadata_keys) + self.assertIn("cluster_info", metadata_keys) + + @mock.patch.object(AKSMachineClient, "make_request") + def test_create_machine_agentpool_put_failure_raises(self, mock_make_request): + """PUT non-2xx \u2192 RuntimeError propagates out of with-block.""" + mock_resp = mock.MagicMock() + mock_resp.status_code = 500 + mock_resp.text = "boom" + mock_make_request.return_value = mock_resp + + with self.assertRaises(RuntimeError): + self.client.create_machine_agentpool( + agentpool_name="apool", vm_size="Standard_D2_v3" + ) + + @mock.patch.object(AKSMachineClient, "_wait_for_agentpool_provisioning", + return_value=False) + @mock.patch.object(AKSMachineClient, "make_request") + def test_create_machine_agentpool_provisioning_timeout_raises( + self, mock_make_request, _mock_wait + ): + """PUT OK but provisioning never reaches Succeeded \u2192 RuntimeError.""" + mock_resp = mock.MagicMock() + mock_resp.status_code = 200 + mock_make_request.return_value = mock_resp + + with self.assertRaises(RuntimeError): + self.client.create_machine_agentpool( + agentpool_name="apool", vm_size="Standard_D2_v3" + ) + + # ---- scale_machine: stubbed on this PR ---- + + def test_scale_machine_raises_not_implemented(self): + """scale_machine is a stub on this PR; subsequent PR will implement it.""" + with self.assertRaises(NotImplementedError): + self.client.scale_machine( + agentpool_name="apool", vm_size="Standard_D2_v3", + scale_machine_count=2, + ) + + # ---- _get_machine_name_prefix: stubbed on this PR ---- + + def test_get_machine_name_prefix_raises_not_implemented(self): + """_get_machine_name_prefix is a stub on this PR; lands with the scale path.""" + with self.assertRaises(NotImplementedError): + AKSMachineClient._get_machine_name_prefix(1000) + + +if __name__ == "__main__": + unittest.main()