Skip to content

Commit 652940c

Browse files
authored
feat: client will be mandatory in next major release; update code to support the change (#2623)
* add deprecation warning for client being mandatory * use FutureWarning * add client to get and add todos * move to prepare client over dyn_client and add client in places where dyn_client is used * update example and remove wrong return from docstring
1 parent 1543a41 commit 652940c

File tree

7 files changed

+122
-64
lines changed

7 files changed

+122
-64
lines changed

examples/pods.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Query to get Pods (resource) in the connected cluster with label of ``label_example=example``.
77
# Returns a ``generator`` of the resource - ``pod``
8-
for pod in Pod.get(dyn_client=client, label_selector="label_example=example"):
8+
for pod in Pod.get(client=client, label_selector="label_example=example"):
99
# We can also get the Node that the ``pod`` is running on:
1010
node = pod.node # Return instance of Node()
1111
# Get pod logs

mcp_server/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def list_resources(
232232
kwargs["limit"] = limit
233233

234234
resources = []
235-
for resource in resource_class.get(dyn_client=client, **kwargs):
235+
for resource in resource_class.get(client=client, **kwargs):
236236
resources.append(format_resource_info(resource))
237237

238238
return {

ocp_resources/event.py

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import warnings
2+
from collections.abc import Generator
3+
from typing import Any
4+
5+
from kubernetes.dynamic import DynamicClient
16
from simple_logger.logger import get_logger
27

38
LOGGER = get_logger(name=__name__)
@@ -10,21 +15,41 @@ class Event:
1015

1116
api_version = "v1"
1217

18+
# TODO: remove once `client` is mandatory
19+
@staticmethod
20+
def _resolve_client(
21+
client: DynamicClient | None,
22+
dyn_client: DynamicClient | None,
23+
) -> DynamicClient:
24+
"""Resolve client from new or deprecated parameter with deprecation warning."""
25+
if client is None and dyn_client is not None:
26+
warnings.warn(
27+
"`dyn_client` arg will be renamed to `client` and will be mandatory in the next major release.",
28+
FutureWarning,
29+
stacklevel=3, # Adjusted for helper function call
30+
)
31+
32+
resolved = client or dyn_client
33+
assert resolved is not None, "Either 'client' or 'dyn_client' must be provided"
34+
return resolved
35+
1336
@classmethod
1437
def get(
1538
cls,
16-
dyn_client,
17-
namespace=None,
18-
name=None,
19-
label_selector=None,
20-
field_selector=None,
21-
resource_version=None,
22-
timeout=None,
23-
):
39+
client: DynamicClient | None = None, # TODO: make mandatory in the next major release
40+
dyn_client: DynamicClient | None = None, # TODO: remove in the next major release
41+
namespace: str | None = None,
42+
name: str | None = None,
43+
label_selector: str | None = None,
44+
field_selector: str | None = None,
45+
resource_version: str | None = None,
46+
timeout: int | None = None,
47+
) -> Generator[Any, None, None]:
2448
"""
2549
get - retrieves K8s events.
2650
2751
Args:
52+
client (DynamicClient): K8s client
2853
dyn_client (DynamicClient): K8s client
2954
namespace (str): event namespace
3055
name (str): event name
@@ -46,6 +71,7 @@ def get(
4671
):
4772
print(event.object)
4873
"""
74+
_client = cls._resolve_client(client, dyn_client)
4975

5076
LOGGER.info("Reading events")
5177
LOGGER.debug(
@@ -54,7 +80,7 @@ def get(
5480
f" resource_version={resource_version}, timeout={timeout}"
5581
)
5682

57-
event_listener = dyn_client.resources.get(api_version=cls.api_version, kind=cls.__name__)
83+
event_listener = _client.resources.get(api_version=cls.api_version, kind=cls.__name__)
5884
yield from event_listener.watch(
5985
namespace=namespace,
6086
name=name,
@@ -67,19 +93,21 @@ def get(
6793
@classmethod
6894
def delete_events(
6995
cls,
70-
dyn_client,
71-
namespace=None,
72-
name=None,
73-
label_selector=None,
74-
field_selector=None,
75-
resource_version=None,
76-
timeout=None,
77-
):
96+
client: DynamicClient | None = None, # TODO: make mandatory in the next major release
97+
dyn_client: DynamicClient | None = None, # TODO: remove in the next major release
98+
namespace: str | None = None,
99+
name: str | None = None,
100+
label_selector: str | None = None,
101+
field_selector: str | None = None,
102+
resource_version: str | None = None,
103+
timeout: int | None = None,
104+
) -> None:
78105
"""
79106
delete_events - delete K8s events. For example, to cleanup events before test, in order to not get old
80107
events in the test, in order to prevent false positive test.
81108
82109
Args:
110+
client (DynamicClient): K8s client
83111
dyn_client (DynamicClient): K8s client
84112
namespace (str): event namespace
85113
name (str): event name
@@ -89,21 +117,20 @@ def delete_events(
89117
resource_version (str): filter events by their resource's version
90118
timeout (int): timeout in seconds
91119
92-
Returns
93-
list: event objects
94-
95-
example: deleting all the event with a reason of "AnEventReason", from "my-namespace" namespace
96-
97-
def delete_events_before_test(default_client):
98-
Event.delete_events(default_client, namespace=my-namespace, field_selector="reason=AnEventReason")
120+
example: deleting all the events with a reason of "AnEventReason", from "my-namespace" namespace
121+
def delete_events_before_test(client):
122+
Event.delete_events(client=client, namespace="my-namespace", field_selector="reason=AnEventReason")
99123
"""
124+
_client = cls._resolve_client(client, dyn_client)
125+
100126
LOGGER.info("Deleting events")
101127
LOGGER.debug(
102128
f"delete_events parameters: namespace={namespace}, name={name},"
103129
f" label_selector={label_selector}, field_selector='{field_selector}',"
104130
f" resource_version={resource_version}, timeout={timeout}"
105131
)
106-
dyn_client.resources.get(api_version=cls.api_version, kind=cls.__name__).delete(
132+
133+
_client.resources.get(api_version=cls.api_version, kind=cls.__name__).delete(
107134
namespace=namespace,
108135
name=name,
109136
label_selector=label_selector,

ocp_resources/node_network_configuration_policy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ def __init__(
111111
def _nodes(self):
112112
if self.node_selector:
113113
return list(
114-
Node.get(dyn_client=self.client, name=self.node_selector[f"{self.ApiGroup.KUBERNETES_IO}/hostname"])
114+
Node.get(client=self.client, name=self.node_selector[f"{self.ApiGroup.KUBERNETES_IO}/hostname"])
115115
)
116116
if self.node_selector_labels:
117117
node_labels = ",".join([
118118
f"{label_key}={label_value}" for label_key, label_value in self.node_selector_labels.items()
119119
])
120-
return list(Node.get(dyn_client=self.client, label_selector=node_labels))
120+
return list(Node.get(client=self.client, label_selector=node_labels))
121121

122122
def set_interface(self, interface):
123123
if not self.res:
@@ -441,7 +441,7 @@ def wait_for_status_success(self):
441441
@property
442442
def nnces(self):
443443
nnces = []
444-
for nnce in NodeNetworkConfigurationEnactment.get(dyn_client=self.client):
444+
for nnce in NodeNetworkConfigurationEnactment.get(client=self.client):
445445
if nnce.name.endswith(f".{self.name}"):
446446
nnces.append(nnce)
447447
return nnces

0 commit comments

Comments
 (0)