Skip to content

Instantly share code, notes, and snippets.

@richardsonlima
Last active March 4, 2021 20:55
Show Gist options
  • Save richardsonlima/1472af7257a94f32f91165e30a5681cf to your computer and use it in GitHub Desktop.
Save richardsonlima/1472af7257a94f32f91165e30a5681cf to your computer and use it in GitHub Desktop.
my zsh ==> Save it into ~/.zshrc
export PATH=$HOME/bin:/usr/bin:/usr/local/bin:$PATH
export PATH="/usr/local/sbin:$PATH"
export ZSH="$HOME/.oh-my-zsh"
plugins=(zsh-autosuggestions git git-flow brew history node npm kubectl docker docker-compose zsh-syntax-highlighting)
source $ZSH/oh-my-zsh.sh
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
#### Terminal Theme ####
ZSH_THEME="agnoster"
#source ~/powerlevel10k/powerlevel10k.zsh-theme
#[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
#if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
# source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
#fi
#### Corp Proxy ####
export no_proxy=.company
function proxy_on(){
export {http,https,ftp,all}_proxy="$(scutil --proxy | grep ProxyAutoConfigURLString | grep -Eo '(http|https)://[^/"]+'):8080"
}
function proxy_off(){
unset {http,https,ftp,all}_proxy
}
# ASDF
source /usr/local/Cellar/asdf/0.8.0_1/asdf.sh
source /usr/local/Cellar/asdf/0.8.0_1/etc/bash_completion.d/asdf.bash
source /usr/local/opt/asdf/asdf.sh
source /usr/local/opt/asdf/etc/bash_completion.d/asdf.bash
#### Observability local stack setup
declare -a StringArray=("prometheus" "influxdb" "telegraf" "elasticsearch" "kibana" "logstash" "node_exporter")
function localObservabilityStack_install(){
echo -n '\033[1;33m Services local access information \n\033[0m'
echo -n '\033[1;33m [+] ElasticSearch: localhost:9300 \n\033[0m'
echo -n '\033[1;33m [+] InfluxDB: localhost:8086 \n\033[0m'
echo -n '\033[1;33m [+] Telegraf: TCP localhost:62990 / UDP localhost:8125 \n\033[0m'
echo -n '\033[1;33m [+] Grafana: localhost:3000 \n\033[0m'
echo -n '\033[1;33m [+] Prometheus: localhost:9090 \n\033[0m'
echo -n '\033[1;33m [+] NodeExporter: localhost:9100 \n\033[0m'
echo ''
for service in ${StringArray[@]}; do echo -n '\033[1;34m ==> Install local Observability service \033[0m' $service && brew install $service; done
}
function localObservabilityStack_uninstall(){
for service in ${StringArray[@]}; do echo -n '\033[1;34m ==> Uninstall local Observability service \033[0m' $service && brew uninstall $service; done
}
function localObservabilityStack_start(){
echo -n '\033[1;33m Services local access information \n\033[0m'
echo -n '\033[1;33m [+] ElasticSearch: localhost:9300 \n\033[0m'
echo -n '\033[1;33m [+] InfluxDB: localhost:8086 \n\033[0m'
echo -n '\033[1;33m [+] Telegraf: TCP localhost:62990 / UDP localhost:8125 \n\033[0m'
echo -n '\033[1;33m [+] Grafana: localhost:3000 \n\033[0m'
echo -n '\033[1;33m [+] Prometheus: localhost:9090 \n\033[0m'
echo -n '\033[1;33m [+] NodeExporter: localhost:9100 \n\033[0m'
echo ''
for service in ${StringArray[@]}; do echo -n '\033[1;34m ==> Starting local Observability service \033[0m' $service && brew services start $service; done
}
function localObservabilityStack_stop(){
for service in ${StringArray[@]}; do echo -n '\033[1;34m ==> Stopping local Observability service \033[0m' $service && brew services stop $service; done
}
#### GO LANG ####
# don't forget to set path correctly!
# INFO
# $GOPATH/src : Where your Go projects / programs are located
# $GOPATH/pkg : contains every package objects
# $GOPATH/bin : The compiled binaries home
#export GOPATH=$HOME/golang
#export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$HOME/.asdf/installs/golang/1.16/packages/bin
export GOROOT="$HOME/.asdf/installs/golang/1.16/go"
export GOPATH="$HOME/.asdf/installs/golang/1.16/packages"
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
# kubectl tricks
alias k="kubectl"
alias kg="kubectl get"
alias kctx="kubectx"
alias kns="kubens"
complete -F __start_kubectl k
[[ $HOME/.asdf/shims/kubectl ]] && source <(kubectl completion zsh)
complete -F __start_kubectl k
function k8s_deleteAllEvictedPods(){
echo -n '\033[1;33m [+] This Bash snippet can delete all evicted pods in all namespaces.\n\033[0m'
IFS=$'\n'
for line in $(kubectl get pods -A | awk {'printf "%s,%s,%s\n", $1,$2,$4'} | grep -E "Evicted"); do
ns=$(echo $line | cut -d',' -f1)
pod=$(echo $line | cut -d',' -f2)
kubectl delete pod -n $ns $pod
echo -n '\033[1;34m ==> Removing evicted POD: \033[0m' $pod
done
}
###
autoload -U colors; colors
function right_prompt() {
local color="blue"
if [[ "$ZSH_KUBECTL_USER" =~ "prod" ]]; then
color=red
fi
if [[ "$ZSH_KUBECTL_USER" =~ "hom" ]]; then
color=orange
fi
if [[ "$ZSH_KUBECTL_USER" =~ "dev" ]]; then
color=green
fi
echo "%{$fg[$color]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}"
}
RPROMPT='$(right_prompt)'
###
setopt prompt_subst
autoload -U add-zsh-hook
function() {
local namespace separator binary
# Specify the separator between context and namespace
zstyle -s ':zsh-kubectl-prompt:' separator separator
if [[ -z "$separator" ]]; then
zstyle ':zsh-kubectl-prompt:' separator '/'
fi
# Display the current namespace if `namespace` is true
zstyle -s ':zsh-kubectl-prompt:' namespace namespace
if [[ -z "$namespace" ]]; then
zstyle ':zsh-kubectl-prompt:' namespace true
fi
# Specify the binary to get the information from kubeconfig (e.g. `oc`)
zstyle -s ':zsh-kubectl-binary:' binary binary
if [[ -z "$binary" ]]; then
zstyle ':zsh-kubectl-prompt:' binary "kubectl"
fi
}
add-zsh-hook precmd _zsh_kubectl_prompt_precmd
function _zsh_kubectl_prompt_precmd() {
local kubeconfig config updated_at now context namespace ns separator modified_time_fmt binary
zstyle -s ':zsh-kubectl-prompt:' binary binary
if ! command -v "$binary" >/dev/null; then
ZSH_KUBECTL_PROMPT="${binary} command not found"
return 1
fi
kubeconfig="$HOME/.kube/config"
if [[ -n "$KUBECONFIG" ]]; then
kubeconfig="$KUBECONFIG"
fi
zstyle -s ':zsh-kubectl-prompt:' modified_time_fmt modified_time_fmt
if [[ -z "$modified_time_fmt" ]]; then
# Check the stat command because it has a different syntax between GNU coreutils and FreeBSD.
if stat --help >/dev/null 2>&1; then
modified_time_fmt='-c%y' # GNU coreutils
else
modified_time_fmt='-f%m' # FreeBSD
fi
zstyle ':zsh-kubectl-prompt:' modified_time_fmt $modified_time_fmt
fi
# KUBECONFIG environment variable can hold a list of kubeconfig files that is colon-delimited.
# Therefore, if KUBECONFIG has been held multiple files, each files need to be checked.
while read -d ":" config; do
if ! now="${now}$(stat -L $modified_time_fmt "$config" 2>/dev/null)"; then
ZSH_KUBECTL_PROMPT="$config doesn't exist"
return 1
fi
done <<< "${kubeconfig}:"
zstyle -s ':zsh-kubectl-prompt:' updated_at updated_at
if [[ "$updated_at" == "$now" ]]; then
return 0
fi
zstyle ':zsh-kubectl-prompt:' updated_at "$now"
# Set environment variable if context is not set
if ! context="$("$binary" config current-context 2>/dev/null)"; then
ZSH_KUBECTL_PROMPT="current-context is not set"
return 1
fi
ZSH_KUBECTL_USER="$("$binary" config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.user}")"
ZSH_KUBECTL_CONTEXT="${context}"
ns="$("$binary" config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.namespace}")"
[[ -z "$ns" ]] && ns="default"
ZSH_KUBECTL_NAMESPACE="${ns}"
# Specify the entry before prompt (default empty)
zstyle -s ':zsh-kubectl-prompt:' preprompt preprompt
# Specify the entry after prompt (default empty)
zstyle -s ':zsh-kubectl-prompt:' postprompt postprompt
# Set environment variable without namespace
zstyle -s ':zsh-kubectl-prompt:' namespace namespace
if [[ "$namespace" != true ]]; then
ZSH_KUBECTL_PROMPT="${preprompt}${context}${postprompt}"
return 0
fi
zstyle -s ':zsh-kubectl-prompt:' separator separator
ZSH_KUBECTL_PROMPT="${preprompt}${context}${separator}${ns}${postprompt}"
return 0
}
### Kind - a tool for running local Kubernetes clusters using Docker container “nodes”
function kind_Install(){
echo -n '\033[1;34m ==> kind - Install: \n\033[0m'
GO111MODULE="on" go get sigs.k8s.io/[email protected]
#brew install kind
}
function kind_CreateCluster(){
echo -n '\033[1;34m ==> kind - Creating a Cluster: \n\033[0m'
kind create cluster
}
function kind_DeleteCluster(){
echo -n '\033[1;34m ==> kind - Deleting a Cluster : \n\033[0m'
kind delete cluster
}
function kind_CreateKubernetesDashboard(){
echo -n '\033[1;34m ==> kind - Creating Kubernetes Dashboard: \n\033[0m'
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml
}
function kind_DeleteServiceAccount(){
echo -n '\033[1;34m ==> kind - Deleting Kubernetes Service Account: \n\033[0m'
kubectl -n kubernetes-dashboard delete serviceaccount admin-user
}
function kind_DeleteClusterRoleBinding(){
echo -n '\033[1;34m ==> kind - Deleting Kubernetes Cluster Role Binding: \n\033[0m'
kubectl -n kubernetes-dashboard delete clusterrolebinding admin-u
}
function kind_GetDashboardAccessToken(){
echo -n '\033[1;34m ==> kind - Getting Kubernetes Dashboard Access Token: \n\033[0m'
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
}
function kind_Accessing Kubernetes Dashboard ... (){
echo -n '\033[1;34m ==> kind - Accessing Kubernetes Dashboard: \n\033[0m'
open -a "Google Chrome" http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
}
function kind_CreateServiceAccount(){
echo -n '\033[1;34m ==> kind - Creating Kubernetes Service Account: \n\033[0m'
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
EOF
}
function kind_CreateClusterRoleBinding(){
echo -n '\033[1;34m ==> kind - Creating Kubernetes CLuster Role Binding: \n\033[0m'
cat <<EOF | kubectl apply -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
EOF
}
# THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="$HOME/.sdkman"
[[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh"
export PATH="/usr/local/opt/openjdk/bin:$PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment