Skip to content

Commit

Permalink
adding core cli api resources call (#133)
Browse files Browse the repository at this point in the history
Signed-off-by: Paige Patton <[email protected]>
  • Loading branch information
paigerube14 authored Jan 14, 2025
1 parent 9de100b commit 1a782da
Showing 1 changed file with 22 additions and 43 deletions.
65 changes: 22 additions & 43 deletions src/krkn_lib/k8s/krkn_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1926,26 +1926,26 @@ def get_kubernetes_core_objects_count(
:param objects: list of the kinds that must be counted
:return: a dictionary of Kinds and the number of objects counted
"""
api_client = self.api_client
resources = self.get_api_resources_by_group("", "v1")

result = dict[str, int]()

for resource in resources.resources:
if resource.kind in objects:
if api_client:
try:
try:
resources = self.cli.get_api_resources()
for resource in resources.resources:
if resource.kind in objects:
if self.api_client:
path_params: Dict[str, str] = {}
query_params: List[str] = []
header_params: Dict[str, str] = {}
auth_settings = ["BearerToken"]
header_params["Accept"] = (
api_client.select_header_accept(
self.api_client.select_header_accept(
["application/json"]
)
)

path = f"/api/{api_version}/{resource.name}"
(data) = api_client.call_api(
(data) = self.api_client.call_api(
path,
"GET",
path_params,
Expand All @@ -1958,8 +1958,8 @@ def get_kubernetes_core_objects_count(
json_obj = ast.literal_eval(data[0])
count = len(json_obj["items"])
result[resource.kind] = count
except ApiException:
pass
except ApiException:
pass
return result

def get_kubernetes_custom_objects_count(
Expand All @@ -1972,7 +1972,6 @@ def get_kubernetes_custom_objects_count(
:param objects: list of Kinds that must be counted
:return: a dictionary of Kinds and number of objects counted
"""
custom_object_api = client.CustomObjectsApi(self.api_client)
groups = client.ApisApi(self.api_client).get_api_versions().groups
result = dict[str, int]()
for api in groups:
Expand All @@ -1992,12 +1991,11 @@ def get_kubernetes_custom_objects_count(
)
for resource in data.resources:
if resource.kind in objects:
custom_resource = (
custom_object_api.list_cluster_custom_object(
group=api.name,
version=api.preferred_version.version,
plural=resource.name,
)
cust_cli = self.custom_object_client
custom_resource = cust_cli.list_cluster_custom_object(
group=api.name,
version=api.preferred_version.version,
plural=resource.name,
)
result[resource.kind] = len(custom_resource["items"])

Expand All @@ -2006,32 +2004,13 @@ def get_kubernetes_custom_objects_count(
return result

def get_api_resources_by_group(self, group, version):
api_client = self.api_client
if api_client:
try:
path_params: Dict[str, str] = {}
query_params: List[str] = []
header_params: Dict[str, str] = {}
auth_settings = ["BearerToken"]
header_params["Accept"] = api_client.select_header_accept(
["application/json"]
)

path = f"/apis/{group}/{version}"
if group == "":
path = f"/api/{version}"
(data) = api_client.call_api(
path,
"GET",
path_params,
query_params,
header_params,
response_type="V1APIResourceList",
auth_settings=auth_settings,
)
return data[0]
except Exception:
pass
try:
api_response = self.custom_object_client.get_api_resources(
group, version
)
return api_response
except Exception:
pass

return None

Expand Down

0 comments on commit 1a782da

Please sign in to comment.