Skip to content

Commit

Permalink
found another issue that ExtensionsV1Api was deprecated and removed i…
Browse files Browse the repository at this point in the history
…n favor of AppsV1Api for deployment operations
  • Loading branch information
djivey committed Jan 9, 2025
1 parent ea13fa5 commit 2c5b1c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
20 changes: 10 additions & 10 deletions src/saltext/kubernetes/modules/kubernetesmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,15 @@ def deployments(namespace="default", **kwargs):
"""
cfg = _setup_conn(**kwargs)
try:
api_instance = kubernetes.client.ExtensionsV1Api()
api_instance = kubernetes.client.AppsV1Api()
api_response = api_instance.list_namespaced_deployment(namespace)

return [dep["metadata"]["name"] for dep in api_response.to_dict().get("items")]
except (ApiException, HTTPError) as exc:
if isinstance(exc, ApiException) and exc.status == 404:
return None
else:
log.exception("Exception when calling ExtensionsV1Api->list_namespaced_deployment")
log.exception("Exception when calling AppsV1Api->list_namespaced_deployment")
raise CommandExecutionError(exc)
finally:
_cleanup(**cfg)
Expand Down Expand Up @@ -579,15 +579,15 @@ def show_deployment(name, namespace="default", **kwargs):
"""
cfg = _setup_conn(**kwargs)
try:
api_instance = kubernetes.client.ExtensionsV1Api()
api_instance = kubernetes.client.AppsV1Api()
api_response = api_instance.read_namespaced_deployment(name, namespace)

return api_response.to_dict()
except (ApiException, HTTPError) as exc:
if isinstance(exc, ApiException) and exc.status == 404:
return None
else:
log.exception("Exception when calling ExtensionsV1Api->read_namespaced_deployment")
log.exception("Exception when calling AppsV1Api->read_namespaced_deployment")
raise CommandExecutionError(exc)
finally:
_cleanup(**cfg)
Expand Down Expand Up @@ -750,7 +750,7 @@ def delete_deployment(name, namespace="default", **kwargs):
body = kubernetes.client.V1DeleteOptions(orphan_dependents=True)

try:
api_instance = kubernetes.client.ExtensionsV1Api()
api_instance = kubernetes.client.AppsV1Api()
api_response = api_instance.delete_namespaced_deployment(
name=name, namespace=namespace, body=body
)
Expand Down Expand Up @@ -783,7 +783,7 @@ def delete_deployment(name, namespace="default", **kwargs):
if isinstance(exc, ApiException) and exc.status == 404:
return None
else:
log.exception("Exception when calling ExtensionsV1Api->delete_namespaced_deployment")
log.exception("Exception when calling AppsV1Api->delete_namespaced_deployment")
raise CommandExecutionError(exc)
finally:
_cleanup(**cfg)
Expand Down Expand Up @@ -962,15 +962,15 @@ def create_deployment(name, namespace, metadata, spec, source, template, saltenv
cfg = _setup_conn(**kwargs)

try:
api_instance = kubernetes.client.ExtensionsV1Api()
api_instance = kubernetes.client.AppsV1Api()
api_response = api_instance.create_namespaced_deployment(namespace, body)

return api_response.to_dict()
except (ApiException, HTTPError) as exc:
if isinstance(exc, ApiException) and exc.status == 404:
return None
else:
log.exception("Exception when calling ExtensionsV1Api->create_namespaced_deployment")
log.exception("Exception when calling AppsV1Api->create_namespaced_deployment")
raise CommandExecutionError(exc)
finally:
_cleanup(**cfg)
Expand Down Expand Up @@ -1208,15 +1208,15 @@ def replace_deployment(
cfg = _setup_conn(**kwargs)

try:
api_instance = kubernetes.client.ExtensionsV1Api()
api_instance = kubernetes.client.AppsV1Api()
api_response = api_instance.replace_namespaced_deployment(name, namespace, body)

return api_response.to_dict()
except (ApiException, HTTPError) as exc:
if isinstance(exc, ApiException) and exc.status == 404:
return None
else:
log.exception("Exception when calling ExtensionsV1Api->replace_namespaced_deployment")
log.exception("Exception when calling AppsV1Api->replace_namespaced_deployment")
raise CommandExecutionError(exc)
finally:
_cleanup(**cfg)
Expand Down
18 changes: 6 additions & 12 deletions tests/unit/modules/test_kubernetesmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_deployments():
:return:
"""
with mock_kubernetes_library() as mock_kubernetes_lib:
mock_kubernetes_lib.client.ExtensionsV1Api.return_value = Mock(
mock_kubernetes_lib.client.AppsV1Api.return_value = Mock(
**{
"list_namespaced_deployment.return_value.to_dict.return_value": {
"items": [{"metadata": {"name": "mock_deployment_name"}}]
Expand All @@ -83,11 +83,7 @@ def test_deployments():
)
assert kubernetes.deployments() == ["mock_deployment_name"]
# py#int: disable=E1120
assert (
kubernetes.kubernetes.client.ExtensionsV1Api()
.list_namespaced_deployment()
.to_dict.called
)
assert kubernetes.kubernetes.client.AppsV1Api().list_namespaced_deployment().to_dict.called


def test_services():
Expand Down Expand Up @@ -134,12 +130,12 @@ def test_delete_deployments():
"saltext.kubernetes.modules.kubernetesmod.show_deployment", Mock(return_value=None)
):
mock_kubernetes_lib.client.V1DeleteOptions = Mock(return_value="")
mock_kubernetes_lib.client.ExtensionsV1Api.return_value = Mock(
mock_kubernetes_lib.client.AppsV1Api.return_value = Mock(
**{"delete_namespaced_deployment.return_value.to_dict.return_value": {"code": ""}}
)
assert kubernetes.delete_deployment("test") == {"code": 200}
assert (
kubernetes.kubernetes.client.ExtensionsV1Api()
kubernetes.kubernetes.client.AppsV1Api()
.delete_namespaced_deployment()
.to_dict.called
)
Expand All @@ -151,14 +147,12 @@ def test_create_deployments():
:return:
"""
with mock_kubernetes_library() as mock_kubernetes_lib:
mock_kubernetes_lib.client.ExtensionsV1Api.return_value = Mock(
mock_kubernetes_lib.client.AppsV1Api.return_value = Mock(
**{"create_namespaced_deployment.return_value.to_dict.return_value": {}}
)
assert kubernetes.create_deployment("test", "default", {}, {}, None, None, None) == {}
assert (
kubernetes.kubernetes.client.ExtensionsV1Api()
.create_namespaced_deployment()
.to_dict.called
kubernetes.kubernetes.client.AppsV1Api().create_namespaced_deployment().to_dict.called
)


Expand Down

0 comments on commit 2c5b1c3

Please sign in to comment.