tktl (2718B)
1 #!/usr/bin/env zsh 2 set -eu 3 local preview pods p follow choose_containers containeri container use_tkn 4 local use_kss last 5 6 function help { 7 cat <<EOF 8 tktl [TASK Query] [Container Query]" 9 10 Queries are initial queries for selection on fzf, i.e: 11 12 tktl buildah push 13 14 will autoselect the buildah taskrun (or ask to choose if there is multiple) and 15 the push container 16 17 Flags: 18 19 -l select the last taskruns 20 -f follow logs 21 -K use kss for previewing the taskruns (need to be installed) 22 -t use tkn to display the logs 23 -c choose a specific container (ie step) not supported with -t 24 25 EOF 26 } 27 while getopts 'lhfKtc' arg 28 do 29 case $arg in 30 (l) last="true";; 31 (f) follow="-f" ;; 32 (K) use_kss=yes ;; 33 (t) use_tkn="yes";; 34 (c) use_tkn="";choose_containers="yes";; # use kubectl until tkn support limiting to container we should be implicit 35 (h) help; exit 0;; 36 (\?) help;exit 1;; 37 esac 38 done 39 (( OPTIND > 1 )) && shift $(( OPTIND - 1 )) 40 41 chooseTaskArg=${1:-""};[[ -n ${chooseTaskArg} ]] && chooseTaskArg="-q ${chooseTaskArg}" 42 chooseContainerArg=${2:-""} 43 44 if [[ -n ${use_kss} ]];then 45 preview='kss `kubectl get -o json tr {}| jq -r .status.podName`' 46 else 47 preview='tkn taskrun describe {}' 48 fi 49 50 if [[ -n ${last} ]];then 51 task=$(kubectl get tr -o name --sort-by=.metadata.creationTimestamp|sed 's,.*/,,'|tail -1) 52 else 53 task=$(kubectl get tr -o json --sort-by=.metadata.creationTimestamp| python <(cat <<EOF 54 import json,sys 55 jeez=json.loads(sys.stdin.read()) 56 def colit(condition, name): 57 if condition=='Succeeded': 58 color=32 59 elif condition=='Failed': 60 color=31 61 elif condition=='Running': 62 color=34 63 else: 64 color=30 65 return("\033[1;%dm%s\033[0;0m" % (color, name)) 66 for i in jeez['items']: 67 print(colit(i['status']['conditions'][0]['reason'], i['metadata']['name'])) 68 69 EOF 70 ) | fzf --ansi -1 ${chooseTaskArg} --tac \ 71 --header "Choose a taskrun" \ 72 --preview ${preview}) 73 fi 74 75 [[ -z ${task} ]] && return 76 77 podName=$(kubectl get tr -o json ${task} | jq -r '.status.podName') 78 if [[ -n ${choose_containers} ]];then 79 [[ -n ${chooseContainerArg} ]] && containeri="-q ${chooseContainerArg}" 80 container=$(kubectl get pod ${podName} -o json|sed 's/step-//'|jq -r '.spec.containers[].name'| \ 81 fzf --header "Choose a container." \ 82 --tac -1 ${containeri} --preview "kubectl logs ${podName} -c step-{}") 83 [[ -n ${container} ]] && container=(-c step-${container}) 84 else 85 container=(--all-containers --max-log-requests=10) 86 fi 87 88 t=$(basename ${task}); 89 if [[ -n ${use_tkn} ]];then 90 tkn taskrun logs ${follow} ${t} 91 else 92 kubectl logs ${follow} ${podName} ${container[@]} 93 fi