Last active
July 10, 2020 15:53
-
-
Save weibeld/40433e084b84515fc9bae64c94f7f111 to your computer and use it in GitHub Desktop.
Code of k1s version 0.1.2 (referenced from https://itnext.io/the-worlds-simplest-kubernetes-dashboard-k1s-4246e03191df)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
[[ "$1" = -v || "$1" = --version ]] && { echo "0.1.2"; exit; } | |
for d in jq watch curl kubectl; do which "$d" >/dev/null || { echo "Missing dependency: $d"; exit 1; }; done | |
ns=${1:-default}; res=${2:-pods} | |
c() { echo -e "\033[$1m"; } | |
cc() { echo -e "\033[$1;1m"; } | |
printf Loading && while true; do printf . && sleep 0.1; done & | |
set -o pipefail | |
path=$(kubectl get "$res" "$([[ "$ns" = - ]] && echo --all-namespaces || echo -n=$ns)" -v 6 2>&1 >/dev/null | grep GET | tail -n 1 | sed -n 's#.*https://[^/]*\([a-z0-9/.-]*\).*#\1#p') | |
pid=$? | |
kill -9 "$!" && wait "$!" 2>/dev/null | |
[[ "$pid" -ne 0 ]] && echo -e "\nInvalid resource type: $res" && exit 1 | |
[[ $(echo -n "${path//[^\/]}" | wc -c) -lt 5 ]] && ns=- | |
res=${path##*/} | |
exec 3< <(kubectl proxy -p 0) | |
port=$(head -n 1 <&3 | sed 's/.*:\([0-9]\{4,5\}\).*/\1/') | |
file=$(mktemp) | |
cat <<EOF >"$file" | |
$(cc 36) ____ ____ ____ | |
||$(cc 33)k$(cc 36) |||$(cc 33)1$(cc 36) |||$(cc 33)s$(cc 36) || $(cc 0)Kubernetes Dashboard$(cc 36) | |
||__|||__|||__|| $(cc 0)Namespace: $ns$(cc 36) | |
|/__\|/__\|/__\| $(cc 0)Resources: $res$(c 0) | |
EOF | |
curl -N -s "http://localhost:$port$path?watch=true" | | |
while read -r event; do | |
name=$(jq -r '.object.metadata.name' <<<"$event") | |
case "$res" in | |
pods) | |
phase=$(jq -r '.object.status.phase' <<<"$event") | |
is_ready=$(jq -r 'if .object.status | has("conditions") then .object.status.conditions[] | if select(.type=="Ready").status=="True" then "1" else "" end else "" end' <<<"$event") | |
is_scheduled=$(jq -r 'if .object.status | has("conditions") then .object.status.conditions[] | if select(.type=="PodScheduled").status=="True" then "1" else "" end else "" end' <<<"$event") | |
[[ "$is_scheduled" && ! "$is_ready" ]] && info=NonReady || info=$phase | |
[[ "$info" = Running ]] && info=$(c 32)$info$(c 0) || info=$(c 33)$info$(c 0) ;; | |
deployments|replicasets|statefulsets) | |
spec=$(jq -r '.object.spec.replicas' <<<"$event") | |
stat=$(jq -r '.object.status.readyReplicas // 0' <<<"$event") | |
[[ "$stat" = "$spec" ]] && info="$(c 32)($stat/$spec)$(c 0)" || info="$(c 33)($stat/$spec)$(c 0)" ;; | |
esac | |
case $(jq -r .type <<<"$event") in | |
ADDED) echo "$name $info" >>"$file" ;; | |
MODIFIED) sed -i.bkp "s/^$name .*$/$name ${info//\//\\/}/" "$file" ;; | |
DELETED) sed -i.bkp "/^$name .*$/d" "$file";; | |
esac | |
done & | |
watch -ctn 0.1 cat "$file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment