Skip to content

Commit

Permalink
[k8s] Add netcat to dependency checks (#2712)
Browse files Browse the repository at this point in the history
* add netcat dep

* nc -h
  • Loading branch information
romilbhardwaj authored Oct 16, 2023
1 parent e6e682f commit 09849f8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
6 changes: 3 additions & 3 deletions docs/source/reference/kubernetes/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ Submitting SkyPilot tasks to Kubernetes Clusters

Once your cluster administrator has :ref:`setup a Kubernetes cluster <kubernetes-setup>` and provided you with a kubeconfig file:

0. Make sure `kubectl <https://kubernetes.io/docs/tasks/tools/>`_ and ``socat`` are installed on your local machine.
0. Make sure `kubectl <https://kubernetes.io/docs/tasks/tools/>`_, ``socat`` and ``nc`` (netcat) are installed on your local machine.

.. code-block:: console
$ # MacOS
$ brew install kubectl socat
$ brew install kubectl socat netcat
$ # Linux (may have socat already installed)
$ sudo apt-get install kubectl socat
$ sudo apt-get install kubectl socat netcat
1. Place your kubeconfig file at ``~/.kube/config``.
Expand Down
6 changes: 6 additions & 0 deletions sky/templates/kubernetes-port-forward-proxy-command.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ if ! command -v socat > /dev/null; then
exit
fi

# Checks if netcat is installed (may not be present in many docker images)
if ! command -v nc > /dev/null; then
echo "Using 'port-forward' mode to run ssh session on Kubernetes instances requires 'nc' to be installed. Please install 'nc' (netcat)." >&2
exit
fi

# Establishes connection between local port and the ssh jump pod using kube port-forward
# Instead of specifying a port, we let kubectl select a random port.
# This is preferred because of socket re-use issues in kubectl port-forward,
Expand Down
15 changes: 10 additions & 5 deletions sky/utils/kubernetes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,19 +991,24 @@ def fill_ssh_jump_template(ssh_key_secret: str, ssh_jump_image: str,

def check_port_forward_mode_dependencies() -> None:
"""Checks if 'socat' is installed"""
for name, option in [('socat', '-V')]:
# We store the dependency list as a list of lists. Each inner list
# contains the name of the dependency, the command to check if it is
# installed, and the package name to install it.
dependency_list = [['socat', ['socat', '-V'], 'socat'],
['nc', ['nc', '-h'], 'netcat']]
for name, check_cmd, install_cmd in dependency_list:
try:
subprocess.run([name, option],
subprocess.run(check_cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True)
except FileNotFoundError:
except (FileNotFoundError, subprocess.CalledProcessError):
with ux_utils.print_exception_no_traceback():
raise RuntimeError(
f'`{name}` is required to setup Kubernetes cloud with '
f'`{KubernetesNetworkingMode.PORTFORWARD.value}` default '
'networking mode and it is not installed. '
'On Debian/Ubuntu, install it with:\n'
f' $ sudo apt install {name}\n'
f' $ sudo apt install {install_cmd}\n'
f'On MacOS, install it with: \n'
f' $ brew install {name}') from None
f' $ brew install {install_cmd}') from None

0 comments on commit 09849f8

Please sign in to comment.