Skip to content

Commit b281597

Browse files
author
Alexandre Feblot
committed
Use f-strings, rename variables, add doc strings
1 parent ecf984d commit b281597

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div align="center">
1515
<h1 align="center">Kubernetes Resource Recommendations Based on Historical Data</h1>
1616
<h2 align="center">Get recommendations based on your existing data in Prometheus/Coralogix/Thanos/Mimir and more!</h2>
17-
<p align="center">
17+
<p align="center">
1818
<a href="#installation"><strong>Installation</strong></a>
1919
.
2020
<a href="#how-krr-works"><strong>How KRR works</strong></a>
@@ -202,7 +202,7 @@ Apart from running KRR as a CLI tool you can also run KRR inside your cluster. W
202202

203203
<img src="./images/ui_recommendation.png">
204204

205-
You can also run KRR in-cluster as a Kubernetes Job, if you don't want to view results easily in a <a href="https://platform.robusta.dev/signup/?benefits=krr&utm_source=github&utm_medium=krr-readme&utm_content=in-cluster-ui">UI</a>.
205+
You can also run KRR in-cluster as a Kubernetes Job, if you don't want to view results easily in a <a href="https://platform.robusta.dev/signup/?benefits=krr&utm_source=github&utm_medium=krr-readme&utm_content=in-cluster-ui">UI</a>.
206206

207207
```
208208
kubectl apply -f https://raw.githubusercontent.com/robusta-dev/krr/refs/heads/main/docs/krr-in-cluster/krr-in-cluster-job.yaml
@@ -403,12 +403,12 @@ If you need help, contact us on Slack, email, or by opening a GitHub issue.
403403
<details>
404404
<summary>VCluster</summary>
405405

406-
KRR Support VCluster software when Prometheus is outside of the VCluster (on physical cluster or centralized). Because of VCluster pod renamming, you need to provide :
406+
KRR supports VCluster software when Prometheus is outside of the VCluster (on physical cluster or centralized). Because of VCluster pod renaming, you need to provide :
407407

408408
- `vcluster-namespace` : The namespace on physical cluster where VCluster is
409409
- `vcluster-name` : The name of your VCluster (set during VCluster deployment)
410-
411-
Other parameter like namespace selector, pod selector etc work as expected.
410+
411+
Other parameters like namespace selector, pod selector etc work as expected.
412412

413413
```sh
414414
krr simple --vcluster-name my-vcluster-name --vcluster-namespace my-vcluster-namespace

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,3 @@ urllib3==1.26.19 ; python_version >= "3.9" and python_full_version < "3.13"
5454
websocket-client==1.7.0 ; python_version >= "3.9" and python_full_version < "3.13"
5555
zipp==3.19.2 ; python_version >= "3.9" and python_version < "3.13"
5656
tenacity==9.0.0 ; python_version >= "3.9" and python_version < "3.13"
57-
hashlib

robusta_krr/core/integrations/prometheus/metrics/base.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import asyncio
55
import datetime
66
import enum
7+
import hashlib
78
from concurrent.futures import ThreadPoolExecutor
89
from functools import reduce
910
from typing import Any, Optional, TypedDict
@@ -18,7 +19,6 @@
1819
from robusta_krr.core.models.config import settings
1920
from robusta_krr.core.models.objects import K8sObjectData
2021

21-
import hashlib
2222

2323
class PrometheusSeries(TypedDict):
2424
metric: dict[str, Any]
@@ -261,24 +261,42 @@ def combine_batches(self, results: list[PodsTimeData]) -> PodsTimeData:
261261

262262
return reduce(lambda x, y: x | y, results, {})
263263

264-
## Vcluster
265-
def get_vcluster_pod_real_name(self, vcluster_pod_name, vcluster_pod_namespace) -> str:
264+
## Vcluster
265+
def get_vcluster_pod_real_name(self, pod_name: str, pod_namespace: str) -> str:
266+
"""
267+
Returns the pod name on the (host) cluster, which is different from the pod name in the VCluster.
268+
When not in a VCluster, just returns the pod name as is.
269+
270+
Args:
271+
pod_name (string): The pod name in the cluster krr connected to
272+
pod_namespace (string): The pod namespace in the cluster krr connected to
273+
274+
Returns:
275+
string: the pod name in the host cluster.
276+
"""
277+
266278
if settings.vcluster_name is None:
267-
return vcluster_pod_name
279+
return pod_name
268280
else:
269-
x = '-x-'
270-
new_name = vcluster_pod_name + x + vcluster_pod_namespace + x + settings.vcluster_name
271-
result_sha256 = hashlib.sha256(new_name.encode()).hexdigest()
272-
273-
if len(new_name) > 63:
274-
shortened_name = new_name[:52] + '-' + result_sha256[:10]
275-
return shortened_name
276-
else:
277-
return new_name
278-
279-
280-
def get_pod_namespace(self, object_namespace) -> str:
281+
host_pod_name = f"{pod_name}-x-{pod_namespace}-x-{settings.vcluster_name}"
282+
if len(host_pod_name) > 63:
283+
host_pod_name_sha256 = hashlib.sha256(host_pod_name.encode()).hexdigest()
284+
host_pod_name = f"{host_pod_name[:52]}-{host_pod_name_sha256[:10]}"
285+
return host_pod_name
286+
287+
def get_pod_namespace(self, pod_namespace: str) -> str:
288+
"""
289+
Returns the pod namespace on the (host) cluster, which is different from the pod namespace in the VCluster.
290+
When not in a VCluster, just returns the pod namespace as is.
291+
292+
Args:
293+
pod_namespace (string): The pod namespace in the cluster krr connected to
294+
295+
Returns:
296+
string: the pod namepace in the host cluster.
297+
"""
298+
281299
if settings.vcluster_namespace is None:
282-
return object_namespace
300+
return pod_namespace
283301
else:
284-
return settings.vcluster_namespace
302+
return settings.vcluster_namespace

0 commit comments

Comments
 (0)