Skip to content

Commit

Permalink
KrknTelemetryOpenshift refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Tullio Sebastiani <[email protected]>

KrknTelemetryOpenshift refactoring

Signed-off-by: Tullio Sebastiani <[email protected]>

refactoring

Signed-off-by: Tullio Sebastiani <[email protected]>
  • Loading branch information
tsebastiani committed Sep 13, 2024
1 parent 2154bde commit f516611
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/krkn_lib/k8s/krkn_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2143,19 +2143,19 @@ def get_nodes_infos(self) -> (list[NodeInfo], list[Taint]):
node_info.instance_type = "unknown"

if node_type_infra_label in node.metadata.labels.keys():
node_info.nodes_type= "infra"
node_info.nodes_type = "infra"
elif node_type_worker_label in node.metadata.labels.keys():
node_info.nodes_type= "worker"
node_info.nodes_type = "worker"
elif node_type_master_label in node.metadata.labels.keys():
node_info.nodes_type= "master"
node_info.nodes_type = "master"
elif node_type_workload_label in node.metadata.labels.keys():
node_info.nodes_type= "workload"
node_info.nodes_type = "workload"
elif (
node_type_application_label in node.metadata.labels.keys()
):
node_info.nodes_type= "application"
node_info.nodes_type = "application"
else:
node_info.nodes_type= "unknown"
node_info.nodes_type = "unknown"

node_info.architecture = node.status.node_info.architecture
node_info.architecture = node.status.node_info.architecture
Expand Down
50 changes: 39 additions & 11 deletions src/krkn_lib/telemetry/k8s/krkn_telemetry_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,56 @@


class KrknTelemetryKubernetes:
kubecli: KrknKubernetes = None
__kubecli: KrknKubernetes = None
__krkn_telemetry_config: dict[str, any] = None
__telemetry_request_id: str = ""
safe_logger: SafeLogger = None
default_telemetry_group = "default"
krkn_telemetry_config: dict[str, any] = None

def __init__(
self,
safe_logger: SafeLogger,
lib_kubernetes: KrknKubernetes,
krkn_telemetry_config: dict[str, any] = None,
telemetry_request_id: str = "",
):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
urllib3.disable_warnings(DeprecationWarning)
warnings.filterwarnings(
action="ignore", message="unclosed", category=ResourceWarning
)
self.kubecli = lib_kubernetes
self.__kubecli = lib_kubernetes
self.safe_logger = safe_logger
self.__telemetry_request_id = telemetry_request_id
if not krkn_telemetry_config:
self.krkn_telemetry_config = {}
self.__krkn_telemetry_config = {}
else:
self.krkn_telemetry_config = krkn_telemetry_config
self.__krkn_telemetry_config = krkn_telemetry_config

def get_lib_kubernetes(self) -> KrknKubernetes:
"""
Returns the instance of KrknKubernetes
:return: a KrknKubernetes instance
"""
return self.__kubecli

def get_telemetry_config(self) -> dict[str, any]:
"""
Returns the telemetry config section from config.yaml
:return: the telemetry config section
"""
return self.__krkn_telemetry_config

def get_telemetry_request_id(self) -> str:
"""
Gets the telemetry request id that represents the path
on S3 where the telemetry data is uploaded
:return: the krkn telemetry id
"""
return self.__telemetry_request_id

def collect_cluster_metadata(self, chaos_telemetry: ChaosRunTelemetry):
"""
Expand All @@ -61,7 +89,7 @@ def collect_cluster_metadata(self, chaos_telemetry: ChaosRunTelemetry):
self.safe_logger.info("collecting telemetry data, please wait....")

chaos_telemetry.kubernetes_objects_count = (
self.kubecli.get_all_kubernetes_object_count(
self.__kubecli.get_all_kubernetes_object_count(
[
"Deployment",
"Pod",
Expand All @@ -72,9 +100,9 @@ def collect_cluster_metadata(self, chaos_telemetry: ChaosRunTelemetry):
]
)
)
node_infos, taints = self.kubecli.get_nodes_infos()
node_infos, taints = self.__kubecli.get_nodes_infos()
chaos_telemetry.node_summary_infos = node_infos
chaos_telemetry.cluster_version = self.kubecli.get_version()
chaos_telemetry.cluster_version = self.__kubecli.get_version()
chaos_telemetry.node_taints = taints
for info in node_infos:
chaos_telemetry.total_node_count += info.count
Expand Down Expand Up @@ -217,7 +245,7 @@ def get_prometheus_pod_data(
if not prometheus_backup:
return file_list

prometheus_pod = self.kubecli.get_pod_info(
prometheus_pod = self.__kubecli.get_pod_info(
prometheus_pod_name, prometheus_namespace
)
if not prometheus_pod:
Expand All @@ -234,7 +262,7 @@ def get_prometheus_pod_data(
target_path = "/prometheus/wal"

try:
file_list = self.kubecli.archive_and_get_path_from_pod(
file_list = self.__kubecli.archive_and_get_path_from_pod(
prometheus_pod_name,
prometheus_container_name,
prometheus_namespace,
Expand Down Expand Up @@ -591,7 +619,7 @@ def put_cluster_events(
if len(exceptions) > 0:
raise Exception(", ".join(exceptions))

events_file = self.kubecli.collect_cluster_events(
events_file = self.__kubecli.collect_cluster_events(
start_timestamp,
end_timestamp,
str(get_localzone()),
Expand Down
18 changes: 14 additions & 4 deletions src/krkn_lib/telemetry/ocp/krkn_telemetry_openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@
import os
import threading
from queue import Queue

from krkn_lib.models.telemetry import ChaosRunTelemetry
from krkn_lib.ocp import KrknOpenshift
from krkn_lib.telemetry.k8s import KrknTelemetryKubernetes
from krkn_lib.utils import SafeLogger


class KrknTelemetryOpenshift(KrknTelemetryKubernetes):
ocpcli: KrknOpenshift
__ocpcli: KrknOpenshift
__telemetry_id: str = None

def __init__(
self,
safe_logger: SafeLogger,
lib_openshift: KrknOpenshift,
telemetry_request_id: str = "",
krkn_telemetry_config: dict[str, any] = None,
):
super().__init__(
safe_logger=safe_logger,
lib_kubernetes=lib_openshift,
krkn_telemetry_config=krkn_telemetry_config,
telemetry_request_id=telemetry_request_id,
)
self.ocpcli = lib_openshift
self.__ocpcli = lib_openshift

def get_lib_ocp(self) -> KrknOpenshift:
"""
Returns the instance of KrknOpenshift
:return: a KrknOpenshift instance
"""
return self.__ocpcli

def get_ocp_prometheus_data(
self,
Expand Down Expand Up @@ -164,7 +174,7 @@ def put_ocp_logs(
archive_path = self.ocpcli.collect_filter_archive_ocp_logs(
workdir,
dst_dir,
self.kubecli.get_kubeconfig_path(),
self.__kubecli.get_kubeconfig_path(),
start_timestamp,
end_timestamp,
logs_filter_patterns,
Expand Down

0 comments on commit f516611

Please sign in to comment.