Skip to content

Instantly share code, notes, and snippets.

@Tedezed Tedezed/.bashrc_profile
Last active Oct 15, 2019

Embed
What would you like to do?
My .bashrc_profile
## By: Tedezed
#PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]anon@$RANDOM\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# Variables
export KUBE_EDITOR="nano"
# Completion
source <(kubectl completion bash)
source <(helm completion bash)
# Alias
alias ll="ls -lhaG"
alias nano="nano -cl"
alias dps="watch docker ps"
alias kk="kubectl get pod --all-namespaces | grep -v kube-system"
alias kka="kubectl get pod --all-namespaces"
alias kn="kubectl get namespaces"
alias kt="kubectl top node"
# Commands
alias kresourses="kubectl describe nodes | grep -A 3 '(Total limits may be over 100 percent, i.e., overcommitted.)\|Hostname:' | grep -v '(Total limits may be over 100 percent, i.e.,
overcommitted.)'"
alias kd="kubectl describe nodes"
alias ktop="watch -n 2 'kubectl top node && echo '' && kubectl describe nodes | grep -A 4 '\''(Total limits may be over 100 percent, i.e., overcommitted.)\|Hostname:'\'' | grep -v '\''(Total
limits may be over 100 percent, i.e., overcommitted.)'\'''"
alias ktop_stop="bash -c 'kubectl top node && echo '' && kubectl describe nodes | grep -A 4 '\''(Total limits may be over 100 percent, i.e., overcommitted.)\|Hostname:'\'' | grep -v
'\''(Total limits may be over 100 percent, i.e., overcommitted.)'\'''"
# Functions
function digall() { dig +nocmd $@ any +multiline +noall +answer ; }
## Define Kubernetes default namespace
function kndefault() { kubectl config set-context $(kubectl config current-context) --namespace=$@ ; }
## Summary of Kubernetes node
function knode() { kubectl describe nodes | grep -A 200 "Name:.*$1" | grep -B 200 -A 2 -m 1 "Events" | grep -v xx00; }
## Kill "-9" pod
function kkill() { kubectl delete pod $1 -n $2 --grace-period=0 --force; }
## See tags from Docker Hub
function dtags() { wget -q https://registry.hub.docker.com/v1/repositories/$1/tags -O - | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | tr '}' '\n' | awk -F: '{print $3}'; }
# Switched to context X
function get_kube_clusters () {
cont=0
cluster_found=0
for cluster in $(kubectl config get-contexts | grep -v NAME | awk '{ print $2 }'); do
if [ "$1" == "get_clusters" ]; then
echo "[$cont] $cluster";
fi
if [ "$1" == "connect_to_clusters" ] && [ "$2" == "$cont" ]; then
kubectl config use-context $cluster
cluster_found=1
fi
cont=$(( cont + 1 ))
done
if [ "$1" == "connect_to_clusters" ] && [ "$cluster_found" == "0" ]; then
echo "[ERROR] Kubernetes cluster not found"
fi
}
function kcontext () {
get_kube_clusters "get_clusters"
read -r -p 'Cluster number: ' num_cluster
get_kube_clusters "connect_to_clusters" $num_cluster
}
## Force root in pod of Kubernetes without su or sudo!!
function kube_exec_root() {
NAME_POD=$1
NAMESPACE=$2
NODE_POD=$(kubectl get pod $NAME_POD -n $NAMESPACE -o json | jq '.spec.nodeName' | tr -d '"')
ID_POD_DOCKER=$(gcloud compute ssh $NODE_POD -- docker ps | grep "$NAME_POD" | grep "$NAMESPACE" | grep -v "POD" | awk '{print $1}')
gcloud compute ssh $NODE_POD -- docker exec -it -u 0 $ID_POD_DOCKER /bin/sh
}
## Decode base64 secret
function ksecret() {
# See secret data
SECRET_NAME=$1
NAMESPACE=$2
for k in $(kubectl get secret $SECRET_NAME -n $NAMESPACE -o json | jq ".data | keys[]" | tr -d '"')
do
echo "Secret $k: $(kubectl get secret $SECRET_NAME -n $NAMESPACE -o json | jq ".data.$k" | tr -d '"' | base64 --decode)"
done
}
alias kmae="kmae_main editor"
alias kselect="kmae_main select"
function kmae_main() {
MODE=$1
TYPE=$2
WHERE=$3
WHERE_INTERNAL=$4
JSON=$5
NAMESPACE=$6
if [ ! -z "$TYPE" ] && [ ! -z "$NAMESPACE" ] && [ ! -z "$JSON" ] && [ ! -z "$WHERE" ];
then
if [ "$NAMESPACE" == "--all-namespaces" ]
then
LIST=$(kubectl get $TYPE $NAMESPACE | grep -v NAME | grep -E "$WHERE" | awk '{ printf "%s:%s\n", $1, $2 }')
else
LIST=$(kubectl get $TYPE -n $NAMESPACE | grep -v NAME | grep -E "$WHERE" | awk '{ printf "'$namespace':%s\n", $1 }')
fi
for x in $LIST
do
OBJ=$(echo $x | cut -d ":" -f2)
NSPACE=$(echo $x | cut -d ":" -f1)
if [[ $(kubectl get $TYPE $OBJ -n $NSPACE -o json | tr -d "\n" | grep -E "$WHERE_INTERNAL") ]]
then
echo "$TYPE: $OBJ $NSPACE"
if [ "$MODE" == "editor" ]
then
mkdir -p $HOME/.kmae
find $HOME/.kmae -type f -name "*.json" -mtime +3 -exec rm {} \;
kubectl get $TYPE $OBJ -n $NSPACE -o json > $HOME/.kmae/$TYPE-$OBJ-$NSPACE-$RANDOM-$RANDOM.json
eval "kubectl patch $TYPE $OBJ -n $NSPACE -p '$JSON'"
else
kubectl get $TYPE $OBJ -n $NSPACE -o json | jq $JSON
fi
fi
done
else
if [ "$MODE" == "editor" ]
then
echo "Format: kmae (pod,rc,deploy...) (Grep string for execute where) (Grep string for execute where internal in json) (JSON_TO_UPDATE) (--all-namespaces || -n namespace-test1)"
echo "Example: kmae secret "\""(NAMESPACE1|NAMESPACE2).*(NAME_SECRET)"\"" "\""test"\"" '{"\""metadata"\"":{"\""annotations"\"":{"\""test1"\"":"\""false"\"", "\""test2"\"": "\""false"\""}}}' --all-namespaces"
else
echo "Format: (kmae_select pod,rc,deploy...) (Grep string for execute where) (Grep string for execute where internal in json) (JSON_PATH_TO_SEE) (NAMESPACE: --all-namespaces || -n namespace-test1)"
echo "Example: kmae_select secret "\""(NAMESPACE1|NAMESPACE2).*(NAME_SECRET)"\"" "\""test"\"" .metadata.annotations --all-namespaces"
fi
fi
}
## Compile and execute
function csharp+ () {
FILE_CSHARP=$1
BIN_CSHARP=$(echo "$1" | cut -d "." -f1)
mcs -out:$BIN_CSHARP $FILE_CSHARP
./$BIN_CSHARP
rm -f $BIN_CSHARP
}
function c+++ () {
FILE_CPP=$1
BIN_CPP=$(echo "$1" | cut -d "." -f1)
g++ $FILE_CPP -o $BIN_CPP
./$BIN_CPP
rm -f $BIN_CPP
}
function gcloud_cold_disk () {
# gcloud compute disks snapshot disk-test --zone europe-west1-b --snapshot-names snapshot-disk-test --storage-location=europe-west1
gcloud compute disks snapshot $1 --zone $2 --snapshot-names cold-$1 --storage-location=$3
gcloud compute disks delete $1 --zone $2
}
function gcloud_kill_disk () {
# gcloud compute disks snapshot disk-test --zone europe-west1-b --snapshot-names snapshot-disk-test --storage-location=europe-west1
gcloud compute disks snapshot $1 --zone $2 --snapshot-names dead-$1 --storage-location=$3 --labels funeral-home=true
gcloud compute disks delete $1 --zone $2
}
function gcloud_disk_capacity () {
STORAGE_CAPACITY=0
for s in $(gcloud compute disks list | awk '{ print $4 }' | grep -v SIZE_GB); do
STORAGE_CAPACITY=$(( STORAGE_CAPACITY + s ))
done
echo "Disks: $STORAGE_CAPACITY GB"
STORAGE_CAPACITY_SNAPSHOTS=0
for s in $(gcloud compute snapshots list | awk '{ print $2 }' | grep -v DISK_SIZE_GB); do
STORAGE_CAPACITY_SNAPSHOTS=$(( STORAGE_CAPACITY_SNAPSHOTS + s ))
done
echo "Snapshots: $STORAGE_CAPACITY_SNAPSHOTS GB"
echo "Total: $((STORAGE_CAPACITY_SNAPSHOTS + STORAGE_CAPACITY)) GB"
echo "Total: $((STORAGE_CAPACITY_SNAPSHOTS/1024 + STORAGE_CAPACITY/1024)) TB"
}
function git_pull_force() {
git fetch --all
git reset --hard origin/master
git reset --hard origin/$(cat .git/config | grep branch | grep -o '".*"' | grep -o "[A-Za-z0-1_-]*")
}
function kgpod() {
kubectl get pod --all-namespaces | awk '{ print $2 }' | grep $1
}
function rnano () {
echo '' > $1; nano $1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.