Skip to content

Commit

Permalink
adding core cli api resources call
Browse files Browse the repository at this point in the history
  • Loading branch information
paigerube14 committed Oct 31, 2024
1 parent 91edc32 commit d4f6789
Showing 1 changed file with 21 additions and 38 deletions.
59 changes: 21 additions & 38 deletions src/krkn_lib/k8s/krkn_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,26 +1912,27 @@ 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 @@ -1944,9 +1945,10 @@ 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(
self, objects: list[str]
Expand All @@ -1958,10 +1960,10 @@ 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:
print('groups ' + str(api))
versions = []
for v in api.versions:
name = ""
Expand All @@ -1979,7 +1981,7 @@ 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(
self.custom_object_client.list_cluster_custom_object(
group=api.name,
version=api.preferred_version.version,
plural=resource.name,
Expand All @@ -1992,32 +1994,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)
print('api resource ' + str(api_response))
return api_response
except Exception:
print('exception')
pass

return None

Expand Down

0 comments on commit d4f6789

Please sign in to comment.