Skip to content

Commit

Permalink
Add port-forward CLI utility (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson committed May 16, 2023
1 parent dcfa11b commit c8d408e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
27 changes: 26 additions & 1 deletion dask_kubernetes/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import click
import yaml
import json
import time
from rich.console import Console

from dask_kubernetes.operator import make_cluster_spec
from dask_kubernetes.operator import make_cluster_spec, KubeCluster

console = Console()


class NoAliasDumper(yaml.SafeDumper):
Expand Down Expand Up @@ -70,3 +74,24 @@ def cluster(**kwargs):
Dumper=NoAliasDumper,
)
)


@main.command(help="Port-forward the scheduler of a DaskCluster resource")
@click.argument("cluster")
def port_forward(cluster):
with console.status(f"Connecting to cluster {cluster}") as status:
try:
kcluster = KubeCluster.from_name(
cluster, shutdown_on_close=False, quiet=True
)
except ValueError:
raise click.ClickException(f"No such cluster {cluster}")
try:
console.print(f"Scheduler at: [magenta][not bold]{kcluster.scheduler_address}")
console.print(f"Dashboard at: [cyan][not bold]{kcluster.dashboard_link}")
console.print("Press ctrl+c to exit", style="bright_black")
while True:
time.sleep(0.1)
except KeyboardInterrupt:
console.print("Shutting down port-forward")
kcluster.close()
6 changes: 4 additions & 2 deletions dask_kubernetes/operator/kubecluster/kubecluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ async def _start(self):
watch_component_status_task = asyncio.create_task(
self._watch_component_status()
)
show_rich_output_task = asyncio.create_task(self._show_rich_output())
if not self.quiet:
show_rich_output_task = asyncio.create_task(self._show_rich_output())
await ClusterAuth.load_first(self.auth)
cluster_exists = (await self._get_cluster()) is not None

Expand All @@ -299,7 +300,8 @@ async def _start(self):
self._log(f"Ready, dashboard available at {self.dashboard_link}")
finally:
watch_component_status_task.cancel()
show_rich_output_task.cancel()
if not self.quiet:
show_rich_output_task.cancel()

def __await__(self):
async def _():
Expand Down

0 comments on commit c8d408e

Please sign in to comment.