Skip to content

Commit 11520e9

Browse files
authored
Merge pull request #75 from ironsource-mobile/arnold_slack
Slack Integration out of the box
2 parents 2d53179 + 6dd4012 commit 11520e9

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ tzlocal==4.2 ; python_version >= "3.9" and python_version < "3.12"
4848
urllib3==1.26.14 ; python_version >= "3.9" and python_version < "3.12"
4949
websocket-client==1.5.1 ; python_version >= "3.9" and python_version < "3.12"
5050
zipp==3.15.0 ; python_version >= "3.9" and python_version < "3.10"
51+
slack-sdk==3.23.1 ; python_version >= "3.9" and python_version < "3.12"

robusta_krr/core/models/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class Config(pd.BaseSettings):
3838
strategy: str
3939
log_to_stderr: bool
4040

41+
# Outputs Settings
42+
file_output: Optional[str] = pd.Field(None)
43+
slack_output: Optional[str] = pd.Field(None)
44+
4145
other_args: dict[str, Any]
4246

4347
# Internal

robusta_krr/core/runner.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import math
33
from concurrent.futures import ThreadPoolExecutor
44
from typing import Optional, Union
5+
import sys, os
6+
from slack_sdk import WebClient
7+
import warnings
58

69
from robusta_krr.core.abstract.strategies import ResourceRecommendation, RunResult
710
from robusta_krr.core.integrations.kubernetes import KubernetesLoader
@@ -65,6 +68,26 @@ def _process_result(self, result: Result) -> None:
6568
formatted = result.format(Formatter)
6669
self.echo("\n", no_prefix=True)
6770
self.print_result(formatted, rich=getattr(Formatter, "__rich_console__", False))
71+
if (self.config.file_output) or (self.config.slack_output):
72+
if self.config.file_output:
73+
file_name = self.config.file_output
74+
elif self.config.slack_output:
75+
file_name = self.config.slack_output
76+
with open(file_name, 'w') as target_file:
77+
sys.stdout = target_file
78+
self.print_result(formatted, rich=getattr(Formatter, "__rich_console__", False))
79+
sys.stdout = sys.stdout
80+
if (self.config.slack_output):
81+
client = WebClient(os.environ["SLACK_BOT_TOKEN"])
82+
warnings.filterwarnings("ignore", category=UserWarning)
83+
client.files_upload(
84+
channels=f'#{self.config.slack_output}',
85+
title="KRR Report",
86+
file=f'./{file_name}',
87+
initial_comment=f'Kubernetes Resource Report for {(" ".join(self.config.namespaces))}'
88+
)
89+
os.remove(file_name)
90+
6891

6992
def __get_resource_minimal(self, resource: ResourceType) -> float:
7093
if resource == ResourceType.CPU:

robusta_krr/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ def {func_name}(
129129
verbose: bool = typer.Option(False, "--verbose", "-v", help="Enable verbose mode", rich_help_panel="Logging Settings"),
130130
quiet: bool = typer.Option(False, "--quiet", "-q", help="Enable quiet mode", rich_help_panel="Logging Settings"),
131131
log_to_stderr: bool = typer.Option(False, "--logtostderr", help="Pass logs to stderr", rich_help_panel="Logging Settings"),
132+
file_output: Optional[str] = typer.Option(None, "--fileoutput", help="Print the output to a file", rich_help_panel="Output Settings"),
133+
slack_output: Optional[str] = typer.Option(None, "--slackoutput", help="Send to output to a slack channel, must have SLACK_BOT_TOKEN", rich_help_panel="Output Settings"),
132134
{strategy_settings},
133135
) -> None:
134136
'''Run KRR using the `{func_name}` strategy'''
@@ -149,6 +151,8 @@ def {func_name}(
149151
verbose=verbose,
150152
quiet=quiet,
151153
log_to_stderr=log_to_stderr,
154+
file_output=file_output,
155+
slack_output=slack_output,
152156
strategy="{func_name}",
153157
other_args={strategy_args},
154158
)

0 commit comments

Comments
 (0)