From 0908166bf3c34f2bdffcc8ab24923a4a076705bf Mon Sep 17 00:00:00 2001 From: Meni Yakove <441263+myakove@users.noreply.github.com> Date: Thu, 19 Dec 2024 16:04:03 +0200 Subject: [PATCH] Deprecate `privileged_client`, use client. (#2242) * Deprecate `privileged_client`, use client. * Use warn for DeprecationWarning * Fix pod execute client * Fix _kube_v1_api --- ocp_resources/datavolume.py | 5 +---- ocp_resources/endpoint_slice.py | 3 --- ocp_resources/endpoints.py | 3 --- ocp_resources/image_stream.py | 2 -- ocp_resources/job.py | 3 --- ocp_resources/lease.py | 3 --- ocp_resources/ocs_initialization.py | 2 -- ocp_resources/pod.py | 2 +- ocp_resources/resource.py | 17 ++++++++++++----- ocp_resources/restore.py | 2 -- ocp_resources/virtual_machine.py | 3 --- ocp_resources/virtual_machine_import.py | 3 --- ocp_resources/virtual_machine_instance.py | 8 +++----- 13 files changed, 17 insertions(+), 39 deletions(-) diff --git a/ocp_resources/datavolume.py b/ocp_resources/datavolume.py index 3415fb11b8..d088a7510e 100644 --- a/ocp_resources/datavolume.py +++ b/ocp_resources/datavolume.py @@ -88,7 +88,6 @@ def __init__( bind_immediate_annotation=None, preallocation=None, teardown=True, - privileged_client=None, yaml_file=None, delete_timeout=TIMEOUT_4MINUTES, api_name="pvc", @@ -119,7 +118,6 @@ def __init__( should be bound immediately. preallocation (bool, default: None): preallocate disk space. teardown (bool, default: True): Indicates if this resource would need to be deleted. - privileged_client (DynamicClient, default: None): Instance of Dynamic client yaml_file (yaml, default: None): yaml file for the resource. delete_timeout (int, default: 4 minutes): timeout associated with delete action. api_name (str, default: "pvc"): api used for DV, pvc/storage @@ -130,7 +128,6 @@ def __init__( namespace=namespace, client=client, teardown=teardown, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, @@ -230,7 +227,7 @@ def wait(self, timeout=TIMEOUT_10MINUTES, failure_timeout=TIMEOUT_2MINUTES, wait @property def pvc(self): return PersistentVolumeClaim( - client=self.privileged_client or self.client, + client=self.client, name=self.name, namespace=self.namespace, ) diff --git a/ocp_resources/endpoint_slice.py b/ocp_resources/endpoint_slice.py index baa94c3184..ae740be341 100644 --- a/ocp_resources/endpoint_slice.py +++ b/ocp_resources/endpoint_slice.py @@ -19,7 +19,6 @@ def __init__( endpoints=None, ports=None, teardown=True, - privileged_client=None, yaml_file=None, delete_timeout=TIMEOUT_4MINUTES, **kwargs, @@ -33,7 +32,6 @@ def __init__( endpoints (list): List of unique endpoints in this slice ports (list, optional): List of port numbers available on the related ip addresses teardown (bool): Indicates if the resource should be torn down at the end - privileged_client (DynamicClient): Privileged client for api calls yaml_file (str): yaml file for the resource. delete_timeout (int): timeout associated with delete action """ @@ -42,7 +40,6 @@ def __init__( namespace=namespace, client=client, teardown=teardown, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, diff --git a/ocp_resources/endpoints.py b/ocp_resources/endpoints.py index df644854cf..3f698aa1ab 100644 --- a/ocp_resources/endpoints.py +++ b/ocp_resources/endpoints.py @@ -18,7 +18,6 @@ def __init__( addresses=None, ports=None, teardown=True, - privileged_client=None, yaml_file=None, delete_timeout=TIMEOUT_4MINUTES, **kwargs, @@ -31,7 +30,6 @@ def __init__( addresses (list): List of ip addresses which offers the related ports that are marked as ready ports (list): List of port numbers available on the related ip addresses teardown (bool): Indicates if the resource should be torn down at the end - privileged_client (DynamicClient): Privileged client for api calls yaml_file (str): yaml file for the resource. delete_timeout (int): timeout associated with delete action """ @@ -40,7 +38,6 @@ def __init__( namespace=namespace, client=client, teardown=teardown, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, diff --git a/ocp_resources/image_stream.py b/ocp_resources/image_stream.py index 8ebd7e6075..d1dad30f78 100644 --- a/ocp_resources/image_stream.py +++ b/ocp_resources/image_stream.py @@ -18,7 +18,6 @@ def __init__( lookup_policy=False, tags=None, teardown=True, - privileged_client=None, yaml_file=None, delete_timeout=TIMEOUT_4MINUTES, **kwargs, @@ -28,7 +27,6 @@ def __init__( namespace=namespace, client=client, teardown=teardown, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, diff --git a/ocp_resources/job.py b/ocp_resources/job.py index baea883fe1..a5c338fc21 100644 --- a/ocp_resources/job.py +++ b/ocp_resources/job.py @@ -13,7 +13,6 @@ class Job(NamespacedResource): namespace (str): Namespace name. client (DynamicClient): Dynamic client for connecting to a remote cluster. teardown (bool): Indicates if this resource would need to be deleted. - privileged_client (DynamicClient): Instance of Dynamic client. yaml_file (str): Yaml file for the resource. delete_timeout (int): Timeout associated with delete action. backoff_limit (int): The number of retries for a job. @@ -37,7 +36,6 @@ def __init__( namespace=None, client=None, teardown=True, - privileged_client=None, yaml_file=None, delete_timeout=TIMEOUT_4MINUTES, backoff_limit=None, @@ -52,7 +50,6 @@ def __init__( namespace=namespace, client=client, teardown=teardown, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, diff --git a/ocp_resources/lease.py b/ocp_resources/lease.py index b898e659dc..f92fa40e99 100644 --- a/ocp_resources/lease.py +++ b/ocp_resources/lease.py @@ -16,7 +16,6 @@ def __init__( namespace=None, client=None, teardown=True, - privileged_client=None, yaml_file=None, delete_timeout=TIMEOUT_4MINUTES, holder_identity=None, @@ -37,7 +36,6 @@ def __init__( lease_transitions (int, optional): number of transitions of a lease between holders. acquire_time (time, optional): when the current lease was acquired renew_time (time, optional): when current holder of the lease has last updated it - privileged_client (DynamicClient): Privileged client for api calls yaml_file (str): yaml file for the resource. delete_timeout (int): timeout associated with delete action """ @@ -46,7 +44,6 @@ def __init__( namespace=namespace, client=client, teardown=teardown, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, diff --git a/ocp_resources/ocs_initialization.py b/ocp_resources/ocs_initialization.py index de64c8b79d..d501fe88bf 100644 --- a/ocp_resources/ocs_initialization.py +++ b/ocp_resources/ocs_initialization.py @@ -11,7 +11,6 @@ def __init__( namespace=None, client=None, teardown=False, - privileged_client=None, yaml_file=None, delete_timeout=TIMEOUT_4MINUTES, enable_ceph_tools=None, @@ -33,7 +32,6 @@ def __init__( namespace=namespace, client=client, teardown=teardown, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, diff --git a/ocp_resources/pod.py b/ocp_resources/pod.py index 02bc479792..877fbf3ebe 100644 --- a/ocp_resources/pod.py +++ b/ocp_resources/pod.py @@ -546,7 +546,7 @@ def node(self) -> Node: node_name = self.instance.spec.nodeName assert node_name, f"Node not found for pod {self.name}" return Node( - client=self.privileged_client or self.client, + client=self.client, name=node_name, ) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index f2e48666cb..6f2e19d94c 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -5,6 +5,8 @@ import copy import json +from warnings import warn + import os import re import sys @@ -499,13 +501,20 @@ def __init__( kind_dict (dict): dict which represents the resource object wait_for_resource (bool): Waits for the resource to be created """ + if privileged_client: + warn( + "privileged_client is deprecated and will be removed in the future. Use client instead.", + DeprecationWarning, + stacklevel=2, + ) + if yaml_file and kind_dict: raise ValueError("yaml_file and resource_dict are mutually exclusive") self.name = name self.teardown = teardown self.timeout = timeout - self.privileged_client = privileged_client + self.privileged_client = client self.yaml_file = yaml_file self.kind_dict = kind_dict self.delete_timeout = delete_timeout @@ -513,7 +522,7 @@ def __init__( self.node_selector = node_selector self.node_selector_labels = node_selector_labels self.config_file = config_file - if not isinstance(config_file, str): + if not isinstance(self.config_file, str): # If we pass config_file which isn't a string, get_client will fail and it will be very hard to know why. # Better fail here and let the user know. raise ValueError("config_file must be a string") @@ -1121,7 +1130,7 @@ def api_request(self, method: str, action: str, url: str, **params: Any) -> Dict data(dict): response data """ - client: DynamicClient = self.privileged_client or self.client + client: DynamicClient = self.client response = client.client.request( method=method, url=f"{url}/{action}", @@ -1313,7 +1322,6 @@ def __init__( yaml_file: str = "", delete_timeout: int = TIMEOUT_4MINUTES, client: DynamicClient | None = None, - privileged_client: DynamicClient | None = None, ensure_exists: bool = False, **kwargs: Any, ): @@ -1322,7 +1330,6 @@ def __init__( client=client, teardown=teardown, timeout=timeout, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, diff --git a/ocp_resources/restore.py b/ocp_resources/restore.py index a8c6ec3057..b8fe07ba0c 100644 --- a/ocp_resources/restore.py +++ b/ocp_resources/restore.py @@ -18,7 +18,6 @@ def __init__( backup_name=None, client=None, teardown=False, - privileged_client=None, yaml_file=None, **kwargs, ): @@ -27,7 +26,6 @@ def __init__( namespace=namespace, client=client, teardown=teardown, - privileged_client=privileged_client, yaml_file=yaml_file, **kwargs, ) diff --git a/ocp_resources/virtual_machine.py b/ocp_resources/virtual_machine.py index 032738888a..8e714aba99 100644 --- a/ocp_resources/virtual_machine.py +++ b/ocp_resources/virtual_machine.py @@ -47,7 +47,6 @@ def __init__( client=None, body=None, teardown=True, - privileged_client=None, yaml_file=None, delete_timeout=TIMEOUT_4MINUTES, **kwargs, @@ -57,7 +56,6 @@ def __init__( namespace=namespace, client=client, teardown=teardown, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, @@ -142,7 +140,6 @@ def vmi(self): client=self.client, name=self.name, namespace=self.namespace, - privileged_client=self.privileged_client or self.client, ) @property diff --git a/ocp_resources/virtual_machine_import.py b/ocp_resources/virtual_machine_import.py index c760e5376c..309e06a4ec 100644 --- a/ocp_resources/virtual_machine_import.py +++ b/ocp_resources/virtual_machine_import.py @@ -103,7 +103,6 @@ def __init__( resource_mapping_namespace=None, warm=False, finalize_date=None, - privileged_client=None, yaml_file=None, delete_timeout=TIMEOUT_4MINUTES, **kwargs, @@ -113,7 +112,6 @@ def __init__( namespace=namespace, client=client, teardown=teardown, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, @@ -139,7 +137,6 @@ def vm(self): name=self.target_vm_name, namespace=self.namespace, client=self.client, - privileged_client=self.privileged_client or self.client, ) def to_dict(self) -> None: diff --git a/ocp_resources/virtual_machine_instance.py b/ocp_resources/virtual_machine_instance.py index 594e31a717..fdf1e1b710 100644 --- a/ocp_resources/virtual_machine_instance.py +++ b/ocp_resources/virtual_machine_instance.py @@ -26,7 +26,6 @@ def __init__( name=None, namespace=None, client=None, - privileged_client=None, yaml_file=None, delete_timeout=TIMEOUT_4MINUTES, **kwargs, @@ -35,7 +34,6 @@ def __init__( name=name, namespace=namespace, client=client, - privileged_client=privileged_client, yaml_file=yaml_file, delete_timeout=delete_timeout, **kwargs, @@ -70,7 +68,7 @@ def interfaces(self): def virt_launcher_pod(self): pods = list( Pod.get( - dyn_client=self.privileged_client or self.client, + dyn_client=self.client, namespace=self.namespace, label_selector=f"kubevirt.io=virt-launcher,kubevirt.io/created-by={self.instance.metadata.uid}", ) @@ -92,7 +90,7 @@ def virt_launcher_pod(self): def virt_handler_pod(self): pods = list( Pod.get( - dyn_client=self.privileged_client or self.client, + dyn_client=self.client, label_selector="kubevirt.io=virt-handler", ) ) @@ -189,7 +187,7 @@ def node(self): Node: Node """ return Node( - client=self.privileged_client or self.client, + client=self.client, name=self.instance.status.nodeName, )