kubeconnect 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/bash
  2. # Klimbim Software collection, A bag full of things
  3. # Copyright (C) 2011-2023 Johannes 'Banana' Keßler
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. # 2019 http://www.bananas-playground.net
  18. # version 1.0
  19. # get a shell in a pod
  20. # kubeconect podname context
  21. #
  22. # podname = the string to find
  23. # context = the context to use for kubectl
  24. #
  25. # uses the default container and /bin/bash as a shell
  26. # needs kubectl to be installed
  27. if [ $# -lt 2 ]; then
  28. echo "You need to provide pod name and context";
  29. echo "kubeconnect pod-name context";
  30. exit 2;
  31. fi;
  32. PODNAME="$1"
  33. CONTEXT="$2"
  34. if kubectl config use-context "$CONTEXT" &> /dev/null
  35. then
  36. echo "Using context: "`kubectl config current-context`;
  37. AMOUNT=$(kubectl get pods | grep $PODNAME | awk '{print $1}' | wc -l);
  38. if [[ $AMOUNT -eq 1 ]];
  39. then
  40. REALPODNAME=$(kubectl get pods | grep $PODNAME | awk '{print $1}');
  41. echo "Pod found: "$REALPODNAME;
  42. kubectl exec -it $REALPODNAME -- /bin/bash
  43. else
  44. echo "ERROR: No such pod found. '$PODNAME'";
  45. exit 1;
  46. fi
  47. else
  48. echo "Could not set context: '$CONTEXT'"
  49. exit 1;
  50. fi
  51. exit 0