Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. trap 'kubectl delete pod $container >/dev/null 2>&1 &' 0 1 2 3 15
  4.  
  5. usage() { echo -e "Usage: kubectl ssh <options> <pod name>" && grep " .)\ #" $0; exit 0; }
  6. [ $# -eq 0 ] && usage
  7.  
  8. while getopts ":u:c:p:n:h" arg; do
  9. case $arg in
  10. p) # Specify pod name.
  11. POD=${OPTARG}
  12. ;;
  13. u) # Specify user
  14. USERNAME=${OPTARG}
  15. ;;
  16. c) # Specify container
  17. CONTAINER=${OPTARG}
  18. ;;
  19. n) # Specify namespaxce
  20. NAMESPACE=${OPTARG}
  21. ;;
  22. h) # Display help.
  23. usage
  24. exit 0
  25. ;;
  26. -- ) # Optional command to execute. Defaults to /bin/sh
  27. ;;
  28. *)
  29. ;;
  30. esac
  31. done
  32.  
  33. current_namespace() {
  34. local cur_ctx
  35.  
  36. cur_ctx="$(current_context)" || exit_err "error getting current context"
  37. ns="$($KUBECTL config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" \
  38. || exit_err "error getting current namespace"
  39.  
  40. if [[ -z "${ns}" ]]; then
  41. echo "default"
  42. else
  43. echo "${ns}"
  44. fi
  45. }
  46.  
  47. current_context() {
  48. $KUBECTL config current-context
  49. }
  50.  
  51. COMMAND=$(echo $@ | grep '\-\-' | sed 's|\(.*\) -- \(.*\)|\2|g')
  52. COMMAND="${COMMAND:-/bin/sh}"
  53.  
  54. if [ -z "$POD" ] && [ -z "$CONTAINER" ] && [ -z "$USERNAME" ] && [ -z "$NAMESPACE" ]; then
  55. POD="$1"
  56. fi
  57.  
  58. USERNAME="${USERNAME:-root}"
  59.  
  60. [ -z $POD ] && echo -e "\nMissing Pod Name" && exit 1
  61.  
  62. KUBECTL=$(which kubectl)
  63.  
  64. NAMESPACE="${NAMESPACE:-$(current_namespace)}"
  65.  
  66. echo -e "\nConnecting...\nPod: ${POD}\nNamespace: ${NAMESPACE}\nUser: ${USERNAME}\nContainer: $CONTAINER\nCommand: $COMMAND\n"
  67.  
  68. # Limits concurrent ssh sessions (each session deploys a pod) to 2. It's not necessary, just a preference.
  69. test "$(exec $KUBECTL -n "${NAMESPACE}" get po "$(whoami)-1" 2>/dev/null)" && container="$(whoami)-2" || container="$(whoami)-1"
  70.  
  71. # We want to mount the docker socket on the node of the pod we're exec'ing into.
  72. NODENAME=$( ${KUBECTL} get pod ${POD} -o go-template='{{.spec.nodeName}}' )
  73. NODESELECTOR='"nodeSelector": {"kubernetes.io/hostname": "'$NODENAME'"},'
  74.  
  75. # Adds toleration if the target container runs on a tainted node. Assumes no more than one taint. Change if yours have more than one or are configured differently.
  76. TOLERATION_VALUE=$($KUBECTL -n "${NAMESPACE}" get pod ${POD} -ojsonpath='{.spec.tolerations[].value}') >/dev/null 2>&1
  77. if [[ "$TOLERATION_VALUE" ]]; then
  78. TOLERATION_KEY=$($KUBECTL -n "${NAMESPACE}" get pod ${POD} -ojsonpath='{.spec.tolerations[].key}')
  79. TOLERATION_OPERATOR=$($KUBECTL -n "${NAMESPACE}" get pod ${POD} -ojsonpath='{.spec.tolerations[].operator}')
  80. TOLERATION_EFFECT=$($KUBECTL -n "${NAMESPACE}" get pod ${POD} -ojsonpath='{.spec.tolerations[].effect}')
  81. TOLERATIONS='"tolerations": [{"effect": "'$TOLERATION_EFFECT'","key": "'$TOLERATION_KEY'","operator": "'$TOLERATION_OPERATOR'","value": "'$TOLERATION_VALUE'"}],'
  82. else
  83. TOLERATIONS=''
  84. fi
  85.  
  86. if [[ -n ${CONTAINER} ]]; then
  87. DOCKER_CONTAINERID=$( eval $KUBECTL -n "${NAMESPACE}" get pod ${POD} -o go-template="'{{ range .status.containerStatuses }}{{ if eq .name \"${CONTAINER}\" }}{{ .containerID }}{{ end }}{{ end }}'" )
  88. else
  89. DOCKER_CONTAINERID=$( $KUBECTL -n "${NAMESPACE}" get pod ${POD} -o go-template='{{ (index .status.containerStatuses 0).containerID }}' )
  90. fi
  91. CONTAINERID=${DOCKER_CONTAINERID#*//}
  92.  
  93. read -r -d '' OVERRIDES <<EOF
  94. {
  95. "apiVersion": "v1",
  96. "spec": {
  97. "containers": [
  98. {
  99. "image": "docker",
  100. "name": "'$container'",
  101. "stdin": true,
  102. "stdinOnce": true,
  103. "tty": true,
  104. "restartPolicy": "Never",
  105. "args": [
  106. "exec",
  107. "-it",
  108. "-u",
  109. "${USERNAME}",
  110. "${CONTAINERID}",
  111. "${COMMAND}"
  112. ],
  113. "volumeMounts": [
  114. {
  115. "mountPath": "/var/run/docker.sock",
  116. "name": "docker"
  117. }
  118. ]
  119. }
  120. ],
  121. $NODESELECTOR
  122. $TOLERATIONS
  123. "volumes": [
  124. {
  125. "name": "docker",
  126. "hostPath": {
  127. "path": "/var/run/docker.sock",
  128. "type": "File"
  129. }
  130. }
  131. ]
  132. }
  133. }
  134. EOF
  135.  
  136. eval $KUBECTL -n "${NAMESPACE}" run -it --restart=Never --image=docker --overrides="'${OVERRIDES}'" $container
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement