Skip to content

Commit

Permalink
get the virtualmachine number (#124)
Browse files Browse the repository at this point in the history
conditional value



lint

Signed-off-by: Tullio Sebastiani <[email protected]>
  • Loading branch information
tsebastiani authored Oct 2, 2024
1 parent a7d1cc7 commit c9ac4f8
Show file tree
Hide file tree
Showing 4 changed files with 1,949 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/krkn_lib/elastic/krkn_elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def upload_metrics_to_elasticsearch(
and isinstance(metric["value"], float)
and isinstance(metric["name"], str)
):

result = self.push_metric(
ElasticMetric(
run_uuid=run_uuid,
Expand Down
38 changes: 38 additions & 0 deletions src/krkn_lib/telemetry/ocp/krkn_telemetry_openshift.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import datetime
import json
import logging
import os
import threading
from queue import Queue
Expand Down Expand Up @@ -63,6 +65,11 @@ def collect_cluster_metadata(self, chaos_telemetry: ChaosRunTelemetry):
chaos_telemetry.network_plugins = (
self.ocpcli.get_cluster_network_plugins()
)
vm_number = self.get_vm_number()
if vm_number > 0:
chaos_telemetry.kubernetes_objects_count[
"VirtualMachineInstance"
] = vm_number

def put_ocp_logs(
self,
Expand Down Expand Up @@ -209,3 +216,34 @@ def put_ocp_logs(
worker.start()
queue.join()
self.safe_logger.info("ocp logs successfully uploaded")

def get_vm_number(self) -> int:
api_client = self.ocpcli.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 = "/apis/kubevirt.io/v1/virtualmachineinstances"
(data) = api_client.call_api(
path,
"GET",
path_params,
query_params,
header_params,
response_type="str",
auth_settings=auth_settings,
)
if data[1] != 200:
return 0

json_obj = json.loads(data[0])
return len(json_obj["items"])
except Exception as e:
logging.error(f"failed to parse virtualmachines API: {e}")
return 0
16 changes: 16 additions & 0 deletions src/krkn_lib/tests/test_krkn_telemetry_openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import os
import uuid

from jinja2 import Environment, FileSystemLoader

from krkn_lib.ocp import KrknOpenshift
from krkn_lib.telemetry.ocp import KrknTelemetryOpenshift
from krkn_lib.tests import BaseTest
from krkn_lib.utils import SafeLogger
from unittest.mock import MagicMock


class KrknTelemetryOpenshiftTests(BaseTest):
Expand Down Expand Up @@ -49,3 +52,16 @@ def _test_put_ocp_logs(self):
int(ten_minutes_ago.timestamp()),
int(ten_minutes_fwd.timestamp()),
)

def test_get_vm_infos(self):
environment = Environment(loader=FileSystemLoader("src/testdata/"))
template = environment.get_template("virtualization_api_result.j2")
api_result = template.render()
krkn_ocp_mock = MagicMock()
krkn_ocp_mock.kubeconfig_path = "~/.kube/config"
krkn_ocp_mock.api_client.call_api.return_value = [api_result, 200]
safe_logger = SafeLogger()
krkn_telemetry_ocp = KrknTelemetryOpenshift(safe_logger, krkn_ocp_mock)
self.assertEqual(krkn_telemetry_ocp.get_vm_number(), 3)
krkn_ocp_mock.api_client.call_api.return_value = [None, 404]
self.assertEqual(krkn_telemetry_ocp.get_vm_number(), 0)
Loading

0 comments on commit c9ac4f8

Please sign in to comment.