kubeconnect 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/bash
  2. # version 1.0
  3. # get a shell in a pod
  4. # kubeconect podname context
  5. #
  6. # podname = the string to find
  7. # context = the context to use for kubectl
  8. #
  9. # uses the default container and /bin/bash as a shell
  10. # needs kubectl to be installed
  11. # This program is free software: you can redistribute it and/or modify
  12. # it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  13. #
  14. # You should have received a copy of the
  15. # COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  16. # along with this program. If not, see http://www.sun.com/cddl/cddl.html
  17. # 2019 http://www.bananas-playground.net
  18. if [ $# -lt 2 ]; then
  19. echo "You need to provide pod name and context";
  20. echo "kubeconnect pod-name context";
  21. exit 2;
  22. fi;
  23. PODNAME="$1"
  24. CONTEXT="$2"
  25. if kubectl config use-context "$CONTEXT" &> /dev/null
  26. then
  27. echo "Using context: "`kubectl config current-context`;
  28. AMOUNT=$(kubectl get pods | grep $PODNAME | awk '{print $1}' | wc -l);
  29. if [[ $AMOUNT -eq 1 ]];
  30. then
  31. REALPODNAME=$(kubectl get pods | grep $PODNAME | awk '{print $1}');
  32. echo "Pod found: "$REALPODNAME;
  33. kubectl exec -it $REALPODNAME -- /bin/bash
  34. else
  35. echo "ERROR: No such pod found. '$PODNAME'";
  36. exit 1;
  37. fi
  38. else
  39. echo "Could not set context: '$CONTEXT'"
  40. exit 1;
  41. fi
  42. exit 0