From 8f25868d06e1760323e435c3a4d3434a3b68a752 Mon Sep 17 00:00:00 2001 From: Konstantinos Koukopoulos Date: Tue, 25 Aug 2020 11:31:44 +0300 Subject: [PATCH 1/2] Add autokuttle. Fixes: #2 --- autokuttle | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 autokuttle diff --git a/autokuttle b/autokuttle new file mode 100755 index 0000000..885e06a --- /dev/null +++ b/autokuttle @@ -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 </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 From 2f5b5c7a371d4dc5cdd8a725103fd075fc5ecf8d Mon Sep 17 00:00:00 2001 From: Konstantinos Koukopoulos Date: Tue, 25 Aug 2020 11:37:06 +0300 Subject: [PATCH 2/2] Update README, Refs: #2 --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 37dd0c2..277225e 100644 --- a/README.md +++ b/README.md @@ -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: