67 lines
2.3 KiB
Bash
Executable File
67 lines
2.3 KiB
Bash
Executable File
function adminer() {
|
|
command -v kubectl >/dev/null 2>&1 || { echo >&2 "kubectl is required for portforwarding"; return 1; }
|
|
|
|
local ctx="${1}"
|
|
local ns="${2}"
|
|
local label="${3}"
|
|
local remotePort=${4}
|
|
local localPort=
|
|
[[ "${ctx}" == "" ]] && {echo >&2 "please provide name of the kubeconfig context\n\navailable contexts:"; kubectl config get-contexts --no-headers -o name >&2; return 1; }
|
|
[[ "${ns}" == "" ]] && ns="devops"
|
|
[[ "${label}" == "" ]] && label="app.kubernetes.io/name=adminer"
|
|
[[ "${remotePort}" == "" ]] && remotePort="8080"
|
|
|
|
kubectl config get-contexts --no-headers -o name | grep -q -- "^${ctx}$"
|
|
if [[ $? -ne 0 ]]; then
|
|
echo >&2 "context ${ctx} not found in kubectl\n\navailable contexts:\n"
|
|
kubectl config get-contexts --no-headers -o name >&2
|
|
return 1
|
|
fi
|
|
|
|
echo -n "specify a local port [8080]: "
|
|
read localPort
|
|
[[ "${localPort}" == "" ]] && localPort="8080"
|
|
|
|
local pod=$(kubectl --context "${ctx}" -n "${ns}" get pods --no-headers -l "${label}" -o name)
|
|
|
|
[[ "${pod}" == "" ]] && { echo >&2 "could not find the adminer pod using context '${ctx}' and namespace '${ns}' and label '${label}'"; return 1; }
|
|
|
|
|
|
|
|
kubectl --context "${ctx}" -n "${ns}" port-forward "${pod}" "${localPort}":"${remotePort}" &
|
|
local pfPid=$!
|
|
|
|
local attempt=0
|
|
echo "waiting for connection to open..."
|
|
while kill -0 $pfPid > /dev/null 2>&1; do
|
|
timeout 1 bash -c "</dev/tcp/127.0.0.1/${localPort}" > /dev/null 2>&1
|
|
if [[ $? -eq 0 ]]; then
|
|
xdg-open "http://adminer${localPort}:${localPort}"
|
|
break
|
|
fi
|
|
|
|
attempt=$((attempt+1))
|
|
[[ ${attempt} -eq 10 ]] && { echo &>2 "Connection took too long to establish"; break; }
|
|
|
|
sleep 1
|
|
done
|
|
|
|
fg %kubectl > /dev/null 2>&1
|
|
}
|
|
|
|
function adminer-local() {
|
|
local localPort=
|
|
echo -n "specify a local port [8080]: "
|
|
read localPort
|
|
[[ "${localPort}" == "" ]] && localPort="8080"
|
|
local contName="local-adminer-${localPort}"
|
|
|
|
sudo docker run --name $contName --rm -d --net host adminer
|
|
#sudo docker run --name $contName --rm -d -p $localPort:8080 adminer
|
|
[[ $? -ne 0 ]] && echo "Could not start adminer" && exit 1
|
|
|
|
xdg-open "http://adminer:${localPort}"
|
|
|
|
sudo docker attach $contName
|
|
}
|