-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_kubecfg.sh
141 lines (128 loc) · 3.13 KB
/
dot_kubecfg.sh
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
# kubecfg
kubecfg() {
local ctx_home="$HOME/.kube/contexts"
[[ -d $ctx_home ]] || mkdir -p $ctx_home
if [[ $# -eq 0 ]]; then
printf "help wip!"
return 0
fi
print_usage() {
printf "help wip!\n"
}
refresh_env_kubeconfig() {
# Add default kube config first
export KUBECONFIG="$HOME/.kube/config"
for c in $(IFS=$'\n' find ~/.kube/contexts -type f); do
export KUBECONFIG="$c:$KUBECONFIG"
done
}
local cmd=$1
shift
case $cmd in
cd)
cd "$ctx_home"
;;
clear)
export KUBECONFIG=
;;
path)
printf "$ctx_home"
;;
refresh|ref)
refresh_env_kubeconfig
;;
*)
printf "Unknown command:%s\n" $cmd
print_usage
esac
}
kubecfg ref
alias k=kubectl
alias kcr="kubecfg ref"
alias kc="kubectl config"
alias kccc="kubectl config current-context"
alias kcgc="kubectl config get-contexts"
alias kcuc="kubectl config use-context"
dockerconfigjson() {
local OPTIND opt ns name user reg_url passwd fmt
while getopts ":n:N:u:r:p:f:h" opt ; do
case "$opt" in
n) # namespace for this secret
ns="$OPTARG"
;;
N) # name for this secret
name="$OPTARG"
;;
u) # docker user name
user="$OPTARG"
;;
r) # docker registry address
reg_url="$OPTARG"
;;
p) # docker password/token
passwd="$OPTARG"
;;
f) # output format: [json | k8s]
fmt="$OPTARG"
;;
h) # print help message
echo "WIP!"
return 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
:)
echo "Option -$OPTARG requires an argument." >&2
return 1
;;
esac
done
shift $((OPTIND-1))
[ -z "$fmt" ] && fmt="json"
if [[ "$fmt" != "json" ]] && [[ "$fmt" != "k8s" ]]; then
echo "Invalid value of -f: $fmt"
echo "Valid values are: json, k8s"
fi
if [[ -z "$user" ]] || [[ -z "$reg_url" ]]; then
echo "-u and -r are required."
return 1
fi
if [[ -z "$passwd" ]]; then
echo "p:$passwd"
echo "Enter password of docker registry:"
read -s passwd
if [[ -z "$passwd" ]]; then
echo "Password cannot be empty."
return 1
fi
fi
local json="{
\"auths\": {
\"$reg_url\": {
\"auth\": \"$(echo -n "$user:$passwd" | base64)\"
}
}
}"
if [[ "$fmt" == "json" ]]; then
echo $json
return 0
fi
if [[ -z "$ns" ]]; then
ns='default'
fi
if [[ -z "$name" ]]; then
echo "When apply to clusters, -m is required."
return 1
fi
cat <<EOF
apiVersion: v1
kind: Secret
metadata:
namespace: $ns
name: $name
data:
config.json: $(echo -n $json | base64)
EOF
return 0
}