-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.genericrc
167 lines (149 loc) · 4.63 KB
/
.genericrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
alias python=python3
alias pip=pip3
alias gbr="git branch | egrep -v "master" | xargs git branch -D"
alias ee="open ./"
alias k=kubectl
export K8S_NAMESPACE=${USER}
export KUBECONFIG="/Users/${USER}/.kube/config"
# for kubectl neat
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
# auto activate/deactivate venv called `.env` when moving between dirs
# https://stackoverflow.com/questions/45216663/how-to-automatically-activate-virtualenvs-when-cding-into-a-directory
function cd() {
builtin cd "$@"
if [[ -z "$VIRTUAL_ENV" ]] ; then
## If env folder is found then activate the vitualenv
if [[ -d ./.env ]] ; then
source ./.env/bin/activate
fi
else
## check the current folder belong to earlier VIRTUAL_ENV folder
# if yes then do nothing
# else deactivate
parentdir="$(dirname "$VIRTUAL_ENV")"
if [[ "$PWD"/ != "$parentdir"/* ]] ; then
deactivate
fi
fi
}
# get logs for pod where only 1 pod is expected
logs() {
local container_data=""
if [ -n "$2" ]; then
container_data="-c $2"
fi
kubectl logs -f $(kubectl get pods | egrep "$1" | awk '{print $1}') ${container_data}
}
# delete resource based on the pattern provided
# default resource is pod
delete() {
if [[ -z "$2" && -z "$1" ]]; then
echo "usage: delete <POD_PATTERN>"
echo "usage: delete <KUBERNETES_RESOURCE> <RESOURCE_PATTERN>"
echo "usage: delete <KUBERNETES_RESOURCE> <RESOURCE_PATTERN> <OPTIONS>"
echo "Provide pattern to delete pod, or resource and pattern"
return
fi
resource="pod"
pattern="$1"
if [ -n "$2" ]; then
resource="$1"
pattern="$2"
fi
options="$3"
kubectl delete ${resource} $(kubectl get ${resource} | egrep "${pattern}" | awk '{print $1}') ${options}
}
# kubectl exec into pod matching the pattern with given command, default command is bash
execit() {
command="bash"
pattern="$1"
if [ -n "$2" ]; then
container_data="-c $2"
fi
if [ -n "$3" ]; then
command="$3"
fi
kubectl exec -it $(kubectl get pod | egrep "${pattern}" | awk '{print $1}') ${container_data} -- ${command}
}
# remove docker images with string in name
rmi () {
docker rmi $(docker images | egrep "$1")
}
# patch service to nodeport
# usage: patch <service>
# usage: patch <service> <nodeport to use>
patch () {
if [ -z "$2" ]; then
# plain patch to nodeport
kubectl patch service $1 --type='json' -p "[{'op':'replace','path':'/spec/type','value':'NodePort'}]"
else
# patch to nodeport with specific port
kubectl patch service $1 --type='json' -p "[{'op':'replace','path':'/spec/type','value':'NodePort'},{'op':'replace','path':'/spec/ports/0/nodePort','value':$2}]"
fi
}
# switch namespace
kn() {
kubens $1
if [ $? -ne 0 ]; then
if [ $# -eq 0 ]; then
echo -e "Missing namespace...\nusage: kn NAMESPACE"
return 1
fi
kubectl config set-context --current --namespace=$1
fi
}
# get every single resource available on the cluster
# or provide a list of resources you are interested in
# eg: "kall pods service"
kall() {
lst=$@
if [ -z $1 ]; then
kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found -n
lst=`kubectl api-resources --no-headers | awk '{print $1}'`
fi
for resource in $lst; do
echo -e "***${resource}***";
kubectl get $resource;
done
}
# get current namespace
k_cn() {
kubectl config view --minify --output 'jsonpath={..namespace}'; echo
}
# switch context
alias kk=kubectx
# current context
k_cc() {
kubectl config view --minify --output 'jsonpath={..current-context}'; echo
}
# monitor given namespace (or K8S_NAMESPACE) services and pods
monitor() {
NAMESPACE=$1
if [ -z "$1" ]; then
NAMESPACE=$(k_cn)
fi
watch "kubectl get pods,svc -n $NAMESPACE"
}
# uninstall everything and reset namespace
# usage: reset
# Defaults to resetting $K8S_NAMESPACE
# usage: reset <namespace>
function reset {
if [ -z "$1" ]; then
NAMESPACE=$K8S_NAMESPACE
else
NAMESPACE=$1
fi
k_sn $NAMESPACE > /dev/null && echo "On $(k_cc):$(k_cn)"
helm delete $(helm ls --all --short --namespace $NAMESPACE) --namespace $NAMESPACE
kubectl delete namespace $NAMESPACE && kubectl create namespace $NAMESPACE || kubectl create namespace $NAMESPACE
}
go_work () {
local curr=$PWD
cd $BASE
go work init
# go work use $(ls -d $BASE/**/*/ $BASE/*/ -1 | grep -v out | grep -v moneta-development-environment)
go work use $(ls -d $BASE/**/*/ $BASE/*/ -1 | egrep -e "dsm|moneta|provisioner")
# go work edit -replace k8s.io/kube-openapi=k8s.io/[email protected]
cd $curr
}