I wrote the following bash to get my current k8s namespace that I eventually add to my PS1 var:
cluster=$(kubectl cluster-info | grep 'Kubernetes control plane' | grep 'running' 2> /dev/null)
if [[ -n "$cluster" ]]; then
K="($(kubectl config get-contexts | grep 'minikube' | awk '{print $5}'))"
fi
it does the job, but not very well
for one, I keep getting the following nasty error:
E0520 11:49:06.373475 5210 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp 127.0.0.1:8080: connect: connection refused
The connection to the server localhost:8080 was refused - did you specify the right host or port?
when I don’t have a cluster running
secondly, the namespace isn’t updated as soon as a cluster is started (I have to open a new terminal to see the namespace)
I know implementations exist but I’m learning bash as I go and would like to know how to do it better
Note – I’m aware this implementation is Minikube specific, I’m learning k8s locally so Minikube is what I’m using but I’ll appreciate any other k8s implementation that’ll work for all clusters