Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label selector argument #103

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ If you want only specific namespaces (default and ingress-nginx):
krr simple -n default -n ingress-nginx
```

Filtering by labels (more info [here](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#api)):

```sh
python krr.py simple --selector 'app.kubernetes.io/instance in (robusta, ingress-nginx)'
```

By default krr will run in the current context. If you want to run it in a different context:

```sh
Expand Down
40 changes: 34 additions & 6 deletions robusta_krr/core/integrations/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ async def _list_deployments(self) -> list[K8sObjectData]:
self.debug(f"Listing deployments in {self.cluster}")
loop = asyncio.get_running_loop()
ret: V1DeploymentList = await loop.run_in_executor(
self.executor, lambda: self.apps.list_deployment_for_all_namespaces(watch=False)
self.executor,
lambda: self.apps.list_deployment_for_all_namespaces(
watch=False,
label_selector=self.config.selector,
),
)
self.debug(f"Found {len(ret.items)} deployments in {self.cluster}")

Expand All @@ -156,8 +160,16 @@ async def _list_deployments(self) -> list[K8sObjectData]:
)

async def _list_rollouts(self) -> list[K8sObjectData]:
self.debug(f"Listing ArgoCD rollouts in {self.cluster}")
loop = asyncio.get_running_loop()
try:
ret: V1DeploymentList = await asyncio.to_thread(self.rollout.list_rollout_for_all_namespaces, watch=False)
ret: V1DeploymentList = await loop.run_in_executor(
self.executor,
lambda: self.rollout.list_rollout_for_all_namespaces(
watch=False,
label_selector=self.config.selector,
),
)
except ApiException as e:
if e.status == 404:
self.debug(f"Rollout API not available in {self.cluster}")
Expand All @@ -178,7 +190,11 @@ async def _list_all_statefulsets(self) -> list[K8sObjectData]:
self.debug(f"Listing statefulsets in {self.cluster}")
loop = asyncio.get_running_loop()
ret: V1StatefulSetList = await loop.run_in_executor(
self.executor, lambda: self.apps.list_stateful_set_for_all_namespaces(watch=False)
self.executor,
lambda: self.apps.list_stateful_set_for_all_namespaces(
watch=False,
label_selector=self.config.selector,
),
)
self.debug(f"Found {len(ret.items)} statefulsets in {self.cluster}")

Expand All @@ -194,7 +210,11 @@ async def _list_all_daemon_set(self) -> list[K8sObjectData]:
self.debug(f"Listing daemonsets in {self.cluster}")
loop = asyncio.get_running_loop()
ret: V1DaemonSetList = await loop.run_in_executor(
self.executor, lambda: self.apps.list_daemon_set_for_all_namespaces(watch=False)
self.executor,
lambda: self.apps.list_daemon_set_for_all_namespaces(
watch=False,
label_selector=self.config.selector,
),
)
self.debug(f"Found {len(ret.items)} daemonsets in {self.cluster}")

Expand All @@ -210,7 +230,11 @@ async def _list_all_jobs(self) -> list[K8sObjectData]:
self.debug(f"Listing jobs in {self.cluster}")
loop = asyncio.get_running_loop()
ret: V1JobList = await loop.run_in_executor(
self.executor, lambda: self.batch.list_job_for_all_namespaces(watch=False)
self.executor,
lambda: self.batch.list_job_for_all_namespaces(
watch=False,
label_selector=self.config.selector,
),
)
self.debug(f"Found {len(ret.items)} jobs in {self.cluster}")

Expand All @@ -228,7 +252,11 @@ async def _list_pods(self) -> list[K8sObjectData]:
self.debug(f"Listing pods in {self.cluster}")
loop = asyncio.get_running_loop()
ret: V1PodList = await loop.run_in_executor(
self.executor, lambda: self.apps.list_pod_for_all_namespaces(watch=False)
self.executor,
lambda: self.apps.list_pod_for_all_namespaces(
watch=False,
label_selector=self.config.selector,
),
)
self.debug(f"Found {len(ret.items)} pods in {self.cluster}")

Expand Down
1 change: 1 addition & 0 deletions robusta_krr/core/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Config(pd.BaseSettings):
clusters: Union[list[str], Literal["*"], None] = None
kubeconfig: Optional[str] = None
namespaces: Union[list[str], Literal["*"]] = pd.Field("*")
selector: Optional[str] = None

# Value settings
cpu_min_value: int = pd.Field(5, ge=0) # in millicores
Expand Down
8 changes: 8 additions & 0 deletions robusta_krr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def {func_name}(
help="List of namespaces to run on. By default, will run on all namespaces.",
rich_help_panel="Kubernetes Settings"
),
selector: Optional[str] = typer.Option(
None,
"--selector",
"-s",
help="Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -s key1=value1,key2=value2). Matching objects must satisfy all of the specified label constraints.",
rich_help_panel="Kubernetes Settings"
),
prometheus_url: Optional[str] = typer.Option(
None,
"--prometheus-url",
Expand Down Expand Up @@ -123,6 +130,7 @@ def {func_name}(
kubeconfig=kubeconfig,
clusters="*" if all_clusters else clusters,
namespaces="*" if "*" in namespaces else namespaces,
selector=selector,
prometheus_url=prometheus_url,
prometheus_auth_header=prometheus_auth_header,
prometheus_ssl_enabled=prometheus_ssl_enabled,
Expand Down