Skip to content

Commit

Permalink
Merge pull request #75 from ironsource-mobile/arnold_slack
Browse files Browse the repository at this point in the history
Slack Integration out of the box
  • Loading branch information
LeaveMyYard authored Jul 27, 2023
2 parents 2d53179 + 6dd4012 commit 11520e9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ tzlocal==4.2 ; python_version >= "3.9" and python_version < "3.12"
urllib3==1.26.14 ; python_version >= "3.9" and python_version < "3.12"
websocket-client==1.5.1 ; python_version >= "3.9" and python_version < "3.12"
zipp==3.15.0 ; python_version >= "3.9" and python_version < "3.10"
slack-sdk==3.23.1 ; python_version >= "3.9" and python_version < "3.12"
4 changes: 4 additions & 0 deletions robusta_krr/core/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Config(pd.BaseSettings):
strategy: str
log_to_stderr: bool

# Outputs Settings
file_output: Optional[str] = pd.Field(None)
slack_output: Optional[str] = pd.Field(None)

other_args: dict[str, Any]

# Internal
Expand Down
23 changes: 23 additions & 0 deletions robusta_krr/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import math
from concurrent.futures import ThreadPoolExecutor
from typing import Optional, Union
import sys, os
from slack_sdk import WebClient
import warnings

from robusta_krr.core.abstract.strategies import ResourceRecommendation, RunResult
from robusta_krr.core.integrations.kubernetes import KubernetesLoader
Expand Down Expand Up @@ -65,6 +68,26 @@ def _process_result(self, result: Result) -> None:
formatted = result.format(Formatter)
self.echo("\n", no_prefix=True)
self.print_result(formatted, rich=getattr(Formatter, "__rich_console__", False))
if (self.config.file_output) or (self.config.slack_output):
if self.config.file_output:
file_name = self.config.file_output
elif self.config.slack_output:
file_name = self.config.slack_output
with open(file_name, 'w') as target_file:
sys.stdout = target_file
self.print_result(formatted, rich=getattr(Formatter, "__rich_console__", False))
sys.stdout = sys.stdout
if (self.config.slack_output):
client = WebClient(os.environ["SLACK_BOT_TOKEN"])
warnings.filterwarnings("ignore", category=UserWarning)
client.files_upload(
channels=f'#{self.config.slack_output}',
title="KRR Report",
file=f'./{file_name}',
initial_comment=f'Kubernetes Resource Report for {(" ".join(self.config.namespaces))}'
)
os.remove(file_name)


def __get_resource_minimal(self, resource: ResourceType) -> float:
if resource == ResourceType.CPU:
Expand Down
4 changes: 4 additions & 0 deletions robusta_krr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ def {func_name}(
verbose: bool = typer.Option(False, "--verbose", "-v", help="Enable verbose mode", rich_help_panel="Logging Settings"),
quiet: bool = typer.Option(False, "--quiet", "-q", help="Enable quiet mode", rich_help_panel="Logging Settings"),
log_to_stderr: bool = typer.Option(False, "--logtostderr", help="Pass logs to stderr", rich_help_panel="Logging Settings"),
file_output: Optional[str] = typer.Option(None, "--fileoutput", help="Print the output to a file", rich_help_panel="Output Settings"),
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"),
{strategy_settings},
) -> None:
'''Run KRR using the `{func_name}` strategy'''
Expand All @@ -149,6 +151,8 @@ def {func_name}(
verbose=verbose,
quiet=quiet,
log_to_stderr=log_to_stderr,
file_output=file_output,
slack_output=slack_output,
strategy="{func_name}",
other_args={strategy_args},
)
Expand Down

0 comments on commit 11520e9

Please sign in to comment.