diff --git a/gotty/server/handlers.go b/gotty/server/handlers.go index d96b748..d0b9d5b 100644 --- a/gotty/server/handlers.go +++ b/gotty/server/handlers.go @@ -130,6 +130,7 @@ func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn) e arg := "" podname := params.Get("podname") + deployname := params.Get("deployname") namespace := params.Get("namespace") if len(params.Get("token")) > 0 { @@ -137,7 +138,14 @@ func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn) e cachedKey := params.Get("token") if ttyParameter != nil { windowTitle = ttyParameter.Title - arg = ttyParameter.Arg + " " + podname + " " + namespace + // If podname is not empty, use it in args + if len(podname) > 0 { + arg = ttyParameter.Arg + " " + podname + " " + namespace + } else if len(deployname) > 0 { + arg = ttyParameter.Arg + " " + deployname + " " + namespace + " " + "true" + } else { + panic("podname or deployname must be provided") + } server.cache.Delete(cachedKey) } else { arg = "ERROR:Invalid Token" diff --git a/init-kubectl.sh b/init-kubectl.sh index 4990749..f355dac 100644 --- a/init-kubectl.sh +++ b/init-kubectl.sh @@ -5,9 +5,10 @@ if [ "${WELCOME_BANNER}" ]; then echo ${WELCOME_BANNER} fi -arg1=$1 -arg2=$2 -arg3=$3 +KUBECONFIG_INPUT=$1 +OBJECT_NAME=$2 # eg: pod name or deployment name +OBJECT_NAMESPACE=$3 # eg: mentored +USE_DEPLOY_NAME=$4 # eg: true or false mkdir -p /nonexistent mount -t tmpfs -o size=${SESSION_STORAGE_SIZE} tmpfs /nonexistent @@ -20,7 +21,7 @@ mkdir -p .kube export HOME=/nonexistent -echo $arg1| base64 -d > .kube/config +echo $KUBECONFIG_INPUT| base64 -d > .kube/config if [ ${KUBECTL_INSECURE_SKIP_TLS_VERIFY} == "true" ];then { @@ -53,15 +54,18 @@ done unset WELCOME_BANNER PPROF_ENABLED KUBECTL_INSECURE_SKIP_TLS_VERIFY SESSION_STORAGE_SIZE KUBECTL_VERSION -# Check arg3 and define it with a default value if is empty -if [ -z "${arg3}" ]; then - arg3="mentored" +# Check OBJECT_NAMESPACE and define it with a default value if is empty +if [ -z "${OBJECT_NAMESPACE}" ]; then + OBJECT_NAMESPACE="mentored" fi # Run -# if arg3 is not empty -if [ -z "${arg2}" ]; then +# if OBJECT_NAMESPACE is not empty +if [ -z "${OBJECT_NAME}" ]; then exec su -s /bin/bash nobody +elif [ "${USE_DEPLOY_NAME}" == "true" ]; then + # If USE_DEPLOY_NAME is true, then use OBJECT_NAME as the deployment name + kubectl exec -n $OBJECT_NAMESPACE -it --tty deploy/$OBJECT_NAME -- /bin/bash else - kubectl exec -n $arg3 -it --tty $arg2 -- /bin/bash + kubectl exec -n $OBJECT_NAMESPACE -it --tty $OBJECT_NAME -- /bin/bash fi \ No newline at end of file