Skip to content

Instantly share code, notes, and snippets.

@kouk
Created August 20, 2020 10:30
Show Gist options
  • Select an option

  • Save kouk/5ee3c5892177b5db2fa22f2f0478646f to your computer and use it in GitHub Desktop.

Select an option

Save kouk/5ee3c5892177b5db2fa22f2f0478646f to your computer and use it in GitHub Desktop.

Revisions

  1. kouk created this gist Aug 20, 2020.
    63 changes: 63 additions & 0 deletions autokuttle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/usr/bin/env bash
    # Inspired by: https://github.com/kayrus/kuttle
    # Usage:
    #
    # sshuttle -v -r 'none' -e /path/to/this_script 172.20.0.0/16
    #

    set -e

    VERSION=0.1
    if [ "$1" == "--version" ] ; then
    echo "Version: $VERSION"
    exit
    fi

    NAME=$(basename $0)
    DEPLOYMENT="deployment/$NAME"

    # skip until python command
    # https://github.com/sshuttle/sshuttle/blob/45f8cce2f892749a0971f0d1e299dcdc32f88afe/sshuttle/ssh.py#L141
    while [ $# -gt 0 ] ; 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 default $DEPLOYMENT -o name --ignore-not-found)" ] ; then
    kubectl create -n default --save-config -f - >/dev/null <<EOF
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    labels:
    app: $NAME
    creator: $(whoami)
    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
    fi

    kubectl wait po -l app=$NAME -n default --for condition=Ready --timeout 30s >/dev/null

    eval exec kubectl exec --namespace=default -i $DEPLOYMENT -- "$@"