summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-02-24 15:59:43 +0200
committerПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-02-24 15:59:43 +0200
commit2998e2781c99fe63185a90f1591451c3593d9aed (patch)
tree61ed435330fb656130c856c2fd3923ae58c9d5b0
parent26b0cf460167f5c6462b39b551ff4f138f86682c (diff)
Improve logs, fix recommendations gathering
-rw-r--r--robusta_krr/core/formatters.py3
-rw-r--r--robusta_krr/core/objects.py3
-rw-r--r--robusta_krr/core/runner.py33
-rw-r--r--robusta_krr/core/strategies.py3
-rw-r--r--robusta_krr/utils/configurable.py15
-rw-r--r--robusta_krr/utils/logo.py11
6 files changed, 46 insertions, 22 deletions
diff --git a/robusta_krr/core/formatters.py b/robusta_krr/core/formatters.py
index 3f7549c..50ec90b 100644
--- a/robusta_krr/core/formatters.py
+++ b/robusta_krr/core/formatters.py
@@ -16,6 +16,9 @@ class BaseFormatter(abc.ABC):
__display_name__: str
+ def __str__(self) -> str:
+ return self.__display_name__.title()
+
@abc.abstractmethod
def format(self, result: Result) -> str:
"""Format the result.
diff --git a/robusta_krr/core/objects.py b/robusta_krr/core/objects.py
index 370a588..e374b3c 100644
--- a/robusta_krr/core/objects.py
+++ b/robusta_krr/core/objects.py
@@ -8,3 +8,6 @@ class K8sObjectData(pd.BaseModel):
def __str__(self) -> str:
return f"{self.kind}/{self.namespace}/{self.name}"
+
+ def __hash__(self) -> int:
+ return hash(str(self))
diff --git a/robusta_krr/core/runner.py b/robusta_krr/core/runner.py
index 86816e9..37150e2 100644
--- a/robusta_krr/core/runner.py
+++ b/robusta_krr/core/runner.py
@@ -1,4 +1,5 @@
import asyncio
+import itertools
from robusta_krr.core.config import Config
from robusta_krr.core.kubernetes import KubernetesLoader
@@ -8,6 +9,7 @@ from robusta_krr.core.result import ResourceAllocations, ResourceScan, ResourceT
from robusta_krr.core.strategies import ResourceRecommendation
from robusta_krr.utils.configurable import Configurable
from robusta_krr.utils.version import get_version
+from robusta_krr.utils.logo import ASCII_LOGO
class Runner(Configurable):
@@ -18,7 +20,11 @@ class Runner(Configurable):
self._strategy = self.config.create_strategy()
def _greet(self) -> None:
- self.echo(f"Running Robusta's KRR (Kubernetes Resource Recommender) {get_version()}")
+ self.echo(ASCII_LOGO, no_prefix=True)
+ self.echo(f"Running Robusta's KRR (Kubernetes Resource Recommender) {get_version()}", no_prefix=True)
+ self.echo(f"Using strategy: {self._strategy}", no_prefix=True)
+ self.echo(f"Using formatter: {self.config.format}", no_prefix=True)
+ self.echo(no_prefix=True)
def _process_result(self, result: Result) -> None:
formatted = result.format(self.config.format)
@@ -41,28 +47,21 @@ class Runner(Configurable):
# But keep in mind that numpy calcluations will not block the GIL
return await asyncio.to_thread(self._strategy.run, data, object, resource)
- async def _gather_objects_recommendations_for_resource(
- self, objects: list[K8sObjectData], resource: ResourceType
- ) -> list[ResourceRecommendation]:
- return await asyncio.gather(*[self._calculate_object_recommendations(obj, resource) for obj in objects])
-
async def _gather_objects_recommendations(self, objects: list[K8sObjectData]) -> list[ResourceAllocations]:
- per_resource_recommendations = await asyncio.gather(
- *[self._gather_objects_recommendations_for_resource(objects, resource) for resource in ResourceType]
+ recommendations: list[ResourceRecommendation] = await asyncio.gather(
+ *[
+ self._calculate_object_recommendations(object, resource)
+ for object, resource in itertools.product(objects, ResourceType)
+ ]
)
+ recommendations_dict = dict(zip(itertools.product(objects, ResourceType), recommendations))
return [
ResourceAllocations(
- requests={
- resource_type: recommendations[i].request
- for resource_type, recommendations in zip(ResourceType, per_resource_recommendations)
- },
- limits={
- resource_type: recommendations[i].limit
- for resource_type, recommendations in zip(ResourceType, per_resource_recommendations)
- },
+ requests={resource: recommendations_dict[object, resource].request for resource in ResourceType},
+ limits={resource: recommendations_dict[object, resource].limit for resource in ResourceType},
)
- for i, _ in enumerate(objects)
+ for object in objects
]
async def _collect_result(self) -> Result:
diff --git a/robusta_krr/core/strategies.py b/robusta_krr/core/strategies.py
index 8c6130d..ebc23b4 100644
--- a/robusta_krr/core/strategies.py
+++ b/robusta_krr/core/strategies.py
@@ -35,6 +35,9 @@ class BaseStrategy(abc.ABC, Generic[_StrategySettings]):
def __init__(self, settings: _StrategySettings):
self.settings = settings
+ def __str__(self) -> str:
+ return self.__display_name__.title()
+
@abc.abstractmethod
def run(
self, history_data: HistoryData, object_data: K8sObjectData, resource_type: ResourceType
diff --git a/robusta_krr/utils/configurable.py b/robusta_krr/utils/configurable.py
index 870dede..c45d436 100644
--- a/robusta_krr/utils/configurable.py
+++ b/robusta_krr/utils/configurable.py
@@ -1,4 +1,5 @@
import typer
+from rich import print
from robusta_krr.core.config import Config
@@ -12,19 +13,23 @@ class Configurable:
def __init__(self, config: Config) -> None:
self.config = config
- def echo(self, message: str) -> None:
+ @staticmethod
+ def __add_prefix(text: str, prefix: str, /, no_prefix: bool) -> str:
+ return f"{prefix} {text}" if not no_prefix else text
+
+ def echo(self, message: str = "", *, no_prefix: bool = False) -> None:
"""
Echoes a message to the user.
If quiet mode is enabled, the message will not be echoed
"""
- if not self.config.quiet:
- typer.echo(message)
+ if not self.config.quiet and self.config.verbose:
+ print(self.__add_prefix(message, "[bold green][INFO][/bold green]", no_prefix=no_prefix))
- def debug(self, message: str) -> None:
+ def debug(self, message: str = "") -> None:
"""
Echoes a message to the user if verbose mode is enabled
"""
if self.config.verbose:
- typer.echo(message)
+ print(self.__add_prefix(message, "[bold green][DEBUG][/bold green]", no_prefix=False))
diff --git a/robusta_krr/utils/logo.py b/robusta_krr/utils/logo.py
new file mode 100644
index 0000000..a6b9641
--- /dev/null
+++ b/robusta_krr/utils/logo.py
@@ -0,0 +1,11 @@
+ASCII_LOGO = r"""
+[bold magenta]
+ _____ _ _ _ _______ _____
+ | __ \ | | | | | |/ / __ \| __ \
+ | |__) |___ | |__ _ _ ___| |_ __ _ | ' /| |__) | |__) |
+ | _ // _ \| '_ \| | | / __| __/ _` | | < | _ /| _ /
+ | | \ \ (_) | |_) | |_| \__ \ || (_| | | . \| | \ \| | \ \
+ |_| \_\___/|_.__/ \__,_|___/\__\__,_| |_|\_\_| \_\_| \_\
+[/bold magenta]
+
+"""