Skip to content

Add autokuttle. Fixes: #2 #3

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ kubectl run kuttle --image=alpine:latest --restart=Never -- sh -c 'apk add pytho
sshuttle -r kuttle -e kuttle 0.0.0.0/0
```

## Automatic target pod creation

If you don't care about having a long-running pod in your cluster, you can use the `autokuttle` script instead:

```sh
wget https://github.com/kayrus/kuttle/raw/master/autokuttle
chmod +x autokuttle
sshuttle -r kuttle -e autokuttle 0.0.0.0/0
```

This will create a deployment named `autokuttle` with one replica pod and use
that as the target. If the deployment already exists it will re-use it the next
time it runs.

# Examples

Route local requests to the `10.254.0.0/16` subnet via `pod-with-python` pod in your Kubernetes cluster:
Expand Down
67 changes: 67 additions & 0 deletions autokuttle
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/sh
#
# Alternative version of kuttle (https://github.com/kayrus/kuttle) that
# automatically creates a long-running pod with python to run the sshuttle
# python command in.
#
# Usage:
#
# sshuttle -v -r 'whatever' -e /path/to/autokuttle 172.20.0.0/16
#

set -e

NAME=$(basename $0)
NAMESPACE=default

# skip until "--" or the last argument (the python command)
# https://github.com/sshuttle/sshuttle/blob/45f8cce2f892749a0971f0d1e299dcdc32f88afe/sshuttle/ssh.py#L141
while [ $# -gt 1 ] ; do
arg=$1
shift
if [ "$arg" = "--" ] ; then
break
fi
done

if [ $# -lt 1 ] ; then
printf "Missing command to exec in pod!\n"
exit 1
fi

if [ -z "$(kubectl get -n $NAMESPACE deployment/$NAME -o name --ignore-not-found)" ] ; then
kubectl create -n $NAMESPACE --save-config -f - >/dev/null <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: $NAME
name: $NAME
spec:
replicas: 1
selector:
matchLabels:
app: $NAME
template:
metadata:
labels:
app: $NAME
spec:
containers:
- image: python:3-alpine
name: python
command: ["sh"]
args: ["-c", "exec tail -f /dev/null"]
EOF
kubectl -n $NAMESPACE wait pod -l app=$NAME --for condition=Ready --timeout 30s >/dev/null
fi

FILTER_READY='{{range $index, $element := .items}}{{range .status.containerStatuses}}{{if .ready}}{{$element.metadata.name}}{{"\n"}}{{end}}{{end}}{{end}}'
POD=$(kubectl -n $NAMESPACE get pods -l app=$NAME -o go-template="$FILTER_READY")

if [ -n "$POD" ] ; then
eval exec kubectl -n $NAMESPACE exec -i $POD -- "$@"
else
printf "No pods matching 'app=$NAME' are Ready.\n"
exit 1
fi