Skip to content

Commit 02db54e

Browse files
committed
fix python issue and slack bug
1 parent 79680ee commit 02db54e

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ plugins = "numpy.typing.mypy_plugin,pydantic.mypy"
2323
krr = "robusta_krr.main:run"
2424

2525
[tool.poetry.dependencies]
26-
python = ">=3.9,<=3.12.3"
26+
python = ">=3.9,<=3.12.9"
2727
typer = { extras = ["all"], version = "^0.7.0" }
2828
pydantic = "^1.10.7"
2929
kubernetes = "^26.1.0"

robusta_krr/core/runner.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from prometrix import PrometheusNotFound
1212
from rich.console import Console
1313
from slack_sdk import WebClient
14+
from slack_sdk.errors import SlackApiError
1415
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
1516
import requests
1617
import json
@@ -135,14 +136,50 @@ def _process_result(self, result: Result) -> None:
135136
if settings.slack_output:
136137
client = WebClient(os.environ["SLACK_BOT_TOKEN"])
137138
warnings.filterwarnings("ignore", category=UserWarning)
138-
client.files_upload(
139-
channels=f"#{settings.slack_output}",
139+
140+
# Resolve channel name to ID if needed (files_upload_v2 requires channel ID)
141+
channel_id = self._resolve_slack_channel_id(client, settings.slack_output)
142+
143+
client.files_upload_v2(
144+
channels=channel_id,
140145
title="KRR Report",
141146
file=f"./{file_name}",
142147
initial_comment=f'Kubernetes Resource Report for {(" ".join(settings.namespaces))}',
143148
)
144149
os.remove(file_name)
145150

151+
def _resolve_slack_channel_id(self, client: WebClient, channel_input: str) -> str:
152+
"""Resolve channel name to ID if needed. files_upload_v2 requires channel ID."""
153+
# If it already looks like a channel ID (starts with C), return as-is
154+
if channel_input.startswith('C'):
155+
return channel_input
156+
157+
# Remove # if present
158+
channel_name = channel_input.lstrip('#')
159+
160+
# Search through all pages of channels
161+
cursor = None
162+
while True:
163+
response = client.conversations_list(
164+
types="public_channel,private_channel",
165+
cursor=cursor
166+
)
167+
168+
# Check channels on this page
169+
for channel in response.get("channels", []):
170+
if channel.get("name") == channel_name:
171+
return channel["id"]
172+
173+
# Move to next page if available
174+
cursor = response.get("response_metadata", {}).get("next_cursor")
175+
if not cursor:
176+
break
177+
178+
raise ValueError(
179+
f"Channel '{channel_input}' not found. "
180+
f"Ensure: 1) Channel exists, 2) Bot is added to channel, 3) Bot has 'channels:read' permission"
181+
)
182+
146183
def __get_resource_minimal(self, resource: ResourceType) -> float:
147184
if resource == ResourceType.CPU:
148185
return 1 / 1000 * settings.cpu_min_value

robusta_krr/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def run_strategy(
263263
slack_output: Optional[str] = typer.Option(
264264
None,
265265
"--slackoutput",
266-
help="Send to output to a slack channel, must have SLACK_BOT_TOKEN",
266+
help="Send to output to a slack channel, must have SLACK_BOT_TOKEN with permissions: channels:read, groups:read, chat:write, chat:write.public, files:write. Bot must be added to the channel.",
267267
rich_help_panel="Output Settings",
268268
),
269269
publish_scan_url: Optional[str] = typer.Option(

0 commit comments

Comments
 (0)