From 59a5596d0186975064ac0217edf4af0acdb47ce0 Mon Sep 17 00:00:00 2001 From: Anson Qian Date: Fri, 16 Jan 2026 16:06:41 -0500 Subject: [PATCH] fix: when applying Kubernetes manifests, resources should be updated when they already exist (409 conflict) rather than skipped. --- modules/python/clients/kubernetes_client.py | 179 +++++++++++++++++- .../tests/clients/test_kubernetes_client.py | 21 +- 2 files changed, 192 insertions(+), 8 deletions(-) diff --git a/modules/python/clients/kubernetes_client.py b/modules/python/clients/kubernetes_client.py index 779be53c0a..21574331bc 100644 --- a/modules/python/clients/kubernetes_client.py +++ b/modules/python/clients/kubernetes_client.py @@ -1263,13 +1263,188 @@ def _apply_single_manifest(self, manifest, namespace=None): logger.warning("Unsupported resource kind: %s. Skipping...", kind) except client.rest.ApiException as e: - if e.status == 409: # Resource already exists + if e.status == 409: # Resource already exists, update it instead resource_name = manifest.get('metadata', {}).get('name') - logger.info("Resource %s/%s already exists, skipping creation", + logger.info("Resource %s/%s already exists, updating it", kind, resource_name) + self._update_single_manifest(manifest, namespace) else: raise Exception(f"Error creating {kind}: {str(e)}") from e + def _update_single_manifest(self, manifest, namespace=None): + """ + Update an existing Kubernetes manifest using the appropriate API client. + Uses strategic merge patch to update the resource. + + :param manifest: Dictionary representing a Kubernetes resource + :param namespace: Optional namespace to override the manifest namespace + :return: None + """ + try: + kind = manifest.get("kind") + # Use provided namespace or fall back to manifest namespace + namespace = namespace or manifest.get("metadata", {}).get("namespace") + name = manifest.get("metadata", {}).get("name") + logger.info("Updating manifest %s %s in namespace %s", kind, name, namespace) + + if kind == "Deployment": + if namespace: + self.app.patch_namespaced_deployment(name=name, namespace=namespace, body=manifest) + else: + raise ValueError("Deployment requires a namespace") + elif kind == "DaemonSet": + if namespace: + self.app.patch_namespaced_daemon_set(name=name, namespace=namespace, body=manifest) + else: + raise ValueError("DaemonSet requires a namespace") + elif kind == "StatefulSet": + if namespace: + self.app.patch_namespaced_stateful_set(name=name, namespace=namespace, body=manifest) + else: + raise ValueError("StatefulSet requires a namespace") + elif kind == "Service": + if namespace: + self.api.patch_namespaced_service(name=name, namespace=namespace, body=manifest) + else: + raise ValueError("Service requires a namespace") + elif kind == "ConfigMap": + if namespace: + self.api.patch_namespaced_config_map(name=name, namespace=namespace, body=manifest) + else: + raise ValueError("ConfigMap requires a namespace") + elif kind == "Secret": + if namespace: + self.api.patch_namespaced_secret(name=name, namespace=namespace, body=manifest) + else: + raise ValueError("Secret requires a namespace") + elif kind == "ServiceAccount": + if namespace: + self.api.patch_namespaced_service_account(name=name, namespace=namespace, body=manifest) + else: + raise ValueError("ServiceAccount requires a namespace") + elif kind == "ClusterRole": + # ClusterRole is cluster-scoped + rbac_api = client.RbacAuthorizationV1Api() + rbac_api.patch_cluster_role(name=name, body=manifest) + elif kind == "ClusterRoleBinding": + # ClusterRoleBinding is cluster-scoped + rbac_api = client.RbacAuthorizationV1Api() + rbac_api.patch_cluster_role_binding(name=name, body=manifest) + elif kind == "Role": + if namespace: + rbac_api = client.RbacAuthorizationV1Api() + rbac_api.patch_namespaced_role(name=name, namespace=namespace, body=manifest) + else: + raise ValueError("Role requires a namespace") + elif kind == "RoleBinding": + if namespace: + rbac_api = client.RbacAuthorizationV1Api() + rbac_api.patch_namespaced_role_binding(name=name, namespace=namespace, body=manifest) + else: + raise ValueError("RoleBinding requires a namespace") + elif kind == "Namespace": + # Namespace is cluster-scoped + self.api.patch_namespace(name=name, body=manifest) + elif kind == "CustomResourceDefinition": + # CustomResourceDefinition is cluster-scoped + apiextensions_api = client.ApiextensionsV1Api() + apiextensions_api.patch_custom_resource_definition(name=name, body=manifest) + elif kind == "FlowSchema": + # FlowSchema is cluster-scoped (part of flow control API) + # Skip FlowSchemas that reference 'exempt' PriorityLevelConfiguration + priority_level_ref = manifest.get("spec", {}).get("priorityLevelConfiguration", {}).get("name") + if priority_level_ref == "exempt": + logger.warning( + "Skipping update of FlowSchema %s that references exempt PriorityLevelConfiguration", + name + ) + return + flowcontrol_api = client.FlowcontrolApiserverV1Api() + flowcontrol_api.patch_flow_schema(name=name, body=manifest) + elif kind == "Stage": + # Stage is a custom resource from KWOK, handle as custom resource + api_version = manifest.get("apiVersion", "") + group, version = api_version.split("/") if "/" in api_version else ("", api_version) + custom_api = client.CustomObjectsApi() + custom_api.patch_cluster_custom_object( + group=group, + version=version, + plural="stages", + name=name, + body=manifest + ) + elif kind == "MPIJob": + # MPIJob is a custom resource from Kubeflow MPI Operator + api_version = manifest.get("apiVersion", "") + group, version = api_version.split("/") if "/" in api_version else ("", api_version) + custom_api = client.CustomObjectsApi() + if namespace: + custom_api.patch_namespaced_custom_object( + group=group, + version=version, + namespace=namespace, + plural="mpijobs", + name=name, + body=manifest + ) + else: + raise ValueError("MPIJob requires a namespace") + elif kind == "NodeFeatureRule": + # NodeFeatureRule is a custom resource from Node Feature Discovery (NFD) + api_version = manifest.get("apiVersion", "") + group, version = api_version.split("/") if "/" in api_version else ("", api_version) + custom_api = client.CustomObjectsApi() + # NodeFeatureRule is cluster-scoped + custom_api.patch_cluster_custom_object( + group=group, + version=version, + plural="nodefeaturerules", + name=name, + body=manifest + ) + elif kind == "NicClusterPolicy": + # NicClusterPolicy is a custom resource from NVIDIA Network Operator + api_version = manifest.get("apiVersion", "") + group, version = api_version.split("/") if "/" in api_version else ("", api_version) + custom_api = client.CustomObjectsApi() + # NicClusterPolicy is cluster-scoped + custom_api.patch_cluster_custom_object( + group=group, + version=version, + plural="nicclusterpolicies", + name=name, + body=manifest + ) + elif kind == "ResourceSlice": + # ResourceSlice is a cluster-scoped resource for Dynamic Resource Allocation (DRA) + api_version = manifest.get("apiVersion", "") + group, version = api_version.split("/") if "/" in api_version else ("", api_version) + custom_api = client.CustomObjectsApi() + custom_api.patch_cluster_custom_object( + group=group, + version=version, + plural="resourceslices", + name=name, + body=manifest + ) + elif kind == "DeviceClass": + # DeviceClass is a cluster-scoped resource for Dynamic Resource Allocation (DRA) + api_version = manifest.get("apiVersion", "") + group, version = api_version.split("/") if "/" in api_version else ("", api_version) + custom_api = client.CustomObjectsApi() + custom_api.patch_cluster_custom_object( + group=group, + version=version, + plural="deviceclasses", + name=name, + body=manifest + ) + else: + logger.warning("Unsupported resource kind for update: %s. Skipping...", kind) + + except Exception as e: + raise Exception(f"Error updating {kind}: {str(e)}") from e + def _delete_single_manifest(self, manifest, ignore_not_found: bool = True, namespace: Optional[str] = None): """ Delete a single Kubernetes manifest using the appropriate API client. diff --git a/modules/python/tests/clients/test_kubernetes_client.py b/modules/python/tests/clients/test_kubernetes_client.py index 89801a530a..5118d41e61 100644 --- a/modules/python/tests/clients/test_kubernetes_client.py +++ b/modules/python/tests/clients/test_kubernetes_client.py @@ -2723,7 +2723,8 @@ def test_apply_single_manifest_unsupported_kind(self, mock_logger): "Unsupported resource kind: %s. Skipping...", "UnsupportedResource") @patch('kubernetes.client.CoreV1Api.create_namespace') - def test_apply_single_manifest_resource_already_exists(self, mock_create_namespace): + @patch('clients.kubernetes_client.KubernetesClient._update_single_manifest') + def test_apply_single_manifest_resource_already_exists(self, mock_update_single, mock_create_namespace): """Test _apply_single_manifest when resource already exists (409 conflict).""" manifest = { "apiVersion": "v1", @@ -2735,15 +2736,17 @@ def test_apply_single_manifest_resource_already_exists(self, mock_create_namespa api_exception = ApiException(status=409, reason="Conflict") mock_create_namespace.side_effect = api_exception - # Should not raise an exception, just log and continue + # Should not raise an exception, should call _update_single_manifest instead with patch('clients.kubernetes_client.logger') as mock_logger: # pylint: disable=protected-access self.client._apply_single_manifest(manifest) expected_calls = [ mock.call("Applying manifest %s %s in namespace %s", "Namespace", "existing-namespace", None), - mock.call("Resource %s/%s already exists, skipping creation", "Namespace", "existing-namespace") + mock.call("Resource %s/%s already exists, updating it", "Namespace", "existing-namespace") ] mock_logger.info.assert_has_calls(expected_calls) + # Verify that _update_single_manifest was called with the correct arguments + mock_update_single.assert_called_once_with(manifest, None) @patch('kubernetes.client.CoreV1Api.create_namespace') def test_apply_single_manifest_api_exception_non_409(self, mock_create_namespace): @@ -3089,15 +3092,21 @@ def test_apply_manifest_from_file_api_exception_409_conflict(self, _mock_open_fi with patch.object(self.client, 'api') as mock_api: mock_api.create_namespaced_service.side_effect = api_exception - # Should not raise exception, just log info + # Should not raise exception, should call patch to update with patch('clients.kubernetes_client.logger') as mock_logger: self.client.apply_manifest_from_file(manifest_path="/path/to/service.yaml") - # Verify info was logged about resource already existing + # Verify info was logged about resource being updated mock_logger.info.assert_any_call( - "Resource %s/%s already exists, skipping creation", + "Resource %s/%s already exists, updating it", "Service", "test-service" ) + # Verify patch was called to update the service + mock_api.patch_namespaced_service.assert_called_once_with( + name="test-service", + namespace="test-namespace", + body=manifest_dict + ) @patch('os.path.isfile') @patch('yaml.safe_load_all')