Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding core cli api resources call #133

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1910,26 +1910,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 @@ -1942,8 +1942,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 @@ -1956,7 +1956,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 @@ -1976,12 +1975,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 @@ -1990,32 +1988,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
Loading