-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathk3d_local_git.sh
executable file
·295 lines (255 loc) · 9.52 KB
/
k3d_local_git.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env bash
set -m # enable job control
## run with "-d" command line switch to debug each step, script will wait for keypress to go on
## integrated mods by KingdonB in his fork: https://gist.github.com/kingdonb/dec74f3b74ffbb83b54d53d5c033e508
## added proper coredns patching via "coredns-custom" configmap
## added automatic /etc/hosts file modification if needed, sudo password will be asked in case
## added (commented out) lines to add Flux extra controllers to the setup
## deployed podinfo as in Flux Get Started guide
## added kustomize patch to change color in podinfo deployment
## added ingress on http port 8081
## added automatic local ip retrieval, thanks to Log2: https://gist.github.com/log2/f2fd0fa040509d3b6829ce813e5470dc
## ask for confirmation on project folder deletion, and added a bit of "lipstick on a pig", ehm, color :)
## issue: https://github.com/fluxcd/flux/issues/3594
BLACK="\033[0;30m"
BLUE="\033[0;34m"
GREEN="\033[0;32m"
GREY="\033[0;90m"
YELLOW="\033[0;93m"
CYAN="\033[0;36m"
RED="\033[0;31m"
PURPLE="\033[0;35m"
BROWN="\033[0;33m"
WHITE="\033[1;37m"
COLOR_RESET="\033[0m"
echog(){
echo;echo;echo -e "### ${GREEN}${1}${COLOR_RESET} ###"
}
readr(){
echo;echo;echo -en ">>> ${RED}${1}${COLOR_RESET} <<<" && read -p ""
}
CONFIG_HOME=~/testgit
CONFIG_REPO=testrepo
CLONE_PATH=${CONFIG_HOME}/${CONFIG_REPO}
BARE_REPO_PATH=${CONFIG_HOME}/gitsrv/${CONFIG_REPO}.git
CLUSTER_NAME=testclu
PUBLICDNS=e4t.example.com
GITSRV_IMG=jkarlos/git-server-docker
_is_good_local_ip() {
local ip="$1"
[ -n "$ip" ] && ping "$ip" -c 1 -W 50 &>/dev/null
}
if ! _is_good_local_ip "$PUBLICIP"; then
echo "PUBLICIP undefined or not reachable, searching for it among defined interfaces (en0 through en9)"
PUBLICIP=
for intf in $(seq 0 9)
do
tempIP="$(ipconfig getifaddr en$intf)"
echo -n "Probing local IP $tempIP from interface en$intf ..."
if _is_good_local_ip "$tempIP" ; then
echo " IP is reachable, will use it for the rest of this script"
PUBLICIP="$tempIP"
break
else
echo " IP is not reachable"
fi
done
[ -z "$PUBLICIP" ] && echo "Can't find local IP, please set it accordingly via PUBLICIP variable before starting this script" && exit 1
fi
echog "creating folder structure and ssh keys"
mkdir -p ${CONFIG_HOME}/{${CONFIG_REPO},sshkeys,gitsrv}
ssh-keygen -b 521 -o -t ecdsa -N "" -f ${CONFIG_HOME}/sshkeys/identity
echog "creating a test repo for the gitsrv"
cd ${CLONE_PATH}
git init --shared=true
echo Nothing > README.md
git add .
git commit -m "1st commit"
git clone --bare ${CLONE_PATH} ${BARE_REPO_PATH}
[ "$1" == "-d" ] && readr "Press [Enter] key to continue and create k3d cluster..."
echog "creating a k3d local test cluster with loadbalancer on port 8081"
k3d cluster create ${CLUSTER_NAME} -p "8081:80@loadbalancer"
echog "Waiting for CoreDNS deploying"
ATTEMPTS=0
ROLLOUT_STATUS_CMD="kubectl rollout status deployment/coredns -n kube-system"
until $ROLLOUT_STATUS_CMD || [ $ATTEMPTS -eq 120 ]; do
$ROLLOUT_STATUS_CMD
ATTEMPTS=$((ATTEMPTS + 1))
sleep 1
done
echog "CoreDNS deployed, waiting for its ConfigMap"
while true ; do
result=$(kubectl -n kube-system get configmap coredns 2>&1)
if [[ $result == *NotFound* ]] || [[ $result == *refused* ]]; then
sleep 1
else
break
fi
done
[ "$1" == "-d" ] && readr "Press [Enter] key to continue and patch coredns..."
echog "checking /etc/hosts line presence, or adding it if missing"
if grep -q "$PUBLICDNS" /etc/hosts ; then
echo "/etc/hosts file already modded"
else
echo "127.0.0.1 $PUBLICDNS" | sudo tee -a /etc/hosts > /dev/null
echo "added missing '127.0.0.1 $PUBLICDNS' line to your /etc/hosts file"
fi
echog "CoreDNS ConfigMap ready, patching it"
read -rd '' coredns_custom << EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns-custom
namespace: kube-system
data:
example.server: |
example.com {
hosts {
${PUBLICIP} ${PUBLICDNS}
fallthrough
}
whoami
}
EOF
echo "$coredns_custom" > ${CONFIG_HOME}/coredns-custom.yaml
kubectl apply -f ${CONFIG_HOME}/coredns-custom.yaml
kubectl get pods -n kube-system|grep coredns|cut -d\ -f1|xargs kubectl delete pod -n kube-system
# to test new dns record working, use: kubectl apply -f https://k8s.io/examples/admin/dns/dnsutils.yaml
[ "$1" == "-d" ] && readr "Press [Enter] key to continue and create local git server..."
echog "creating the local gitserver pointing to the folders created above"
docker run -d -p 2222:22 \
-v ${CONFIG_HOME}/sshkeys:/git-server/keys \
-v ${CONFIG_HOME}/gitsrv:/git-server/repos \
--name gitsrv $GITSRV_IMG
echog "wait for port 2222 to be opened"
while ! nc -z localhost 2222; do
sleep 0.1
done
echog "adding ssh key and testing ssh"
ssh-add -D ${CONFIG_HOME}/sshkeys/identity
ssh-add ${CONFIG_HOME}/sshkeys/identity
sleep 1
ssh git@${PUBLICDNS} -p 2222
[ "$1" == "-d" ] && readr "Press [Enter] key to continue and bootstrap Flux on local git server and cluster..."
echog "bootstrapping flux on test cluster, using SAME private key already used for the git server"
flux bootstrap git --branch=develop --path=. \
--url=ssh://git@${PUBLICDNS}:2222/git-server/repos/${CONFIG_REPO}.git \
--private-key-file=../sshkeys/identity --silent
[ "$1" == "-d" ] && readr "Press [Enter] key to continue cloning the git repo and adding coredns ..."
echog "cloning created git repo and adding coredns patch"
cd ${CONFIG_HOME}
rm -rf ${CLONE_PATH}
git clone ssh://git@${PUBLICDNS}:2222/git-server/repos/${CONFIG_REPO}.git -b develop
cd ${CLONE_PATH}
mv ${CONFIG_HOME}/coredns-custom.yaml ${CLONE_PATH}
git add -A && git commit -m "Add coredns patch"
git push
[ "$1" == "-d" ] && readr "Press [Enter] key to continue and add podinfo git repo..."
echog "Add podinfo repository to Flux"
flux create source git podinfo \
--url=https://github.com/stefanprodan/podinfo \
--branch=master \
--interval=30s \
--export > podinfo-source.yaml
git add -A && git commit -m "Add podinfo GitRepository"
git push
[ "$1" == "-d" ] && readr "Press [Enter] key to continue and add podinfo application..."
echog "Deploy podinfo application"
flux create kustomization podinfo \
--target-namespace=default \
--source=podinfo \
--path="./kustomize" \
--prune=true \
--wait=true \
--interval=5m \
--export > podinfo-kustomization.yaml
git add -A && git commit -m "Add podinfo Kustomization"
git push
echog "Waiting for PodInfo deploying"
ATTEMPTS=0
ROLLOUT_STATUS_CMD="kubectl rollout status deployment/podinfo -n default"
FLUX_RECONCILE_CMD="flux reconcile kustomization flux-system --with-source"
$FLUX_RECONCILE_CMD &
until $ROLLOUT_STATUS_CMD || [ $ATTEMPTS -eq 120 ]; do
$ROLLOUT_STATUS_CMD
ATTEMPTS=$((ATTEMPTS + 1))
sleep 1
done
if [ "$1" == "-d" ]; then
echog "opening http://localhost:9898 in your browser (only on Mac - do it manually on other systems)"
kubectl port-forward deployment/podinfo 9898:9898 &
PID=$!
open http://${PUBLICDNS}:9898
fi
[ "$1" == "-d" ] && readr "Press [Enter] key to continue and change podinfo background via kustomize patch..." && kill -9 $PID
echog "adding kustomize patch to alter podinfo background"
cd ${CLONE_PATH}
IFS= read -rd '' podinfopatch << EOF
patches:
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: podinfo
spec:
template:
spec:
containers:
- name: podinfod
env:
- name: PODINFO_UI_COLOR
value: '#ffff00'
target:
name: podinfo
kind: Deployment
EOF
echo -e "$podinfopatch" >> podinfo-kustomization.yaml
git add -A
git commit -m "changed color in podinfo deployment via kustomize patch"
git push
echog "adding ingress for podinfo on http://${PUBLICDNS}:8081"
cd ${CLONE_PATH}
IFS= read -rd '' podinfoingress << EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: podinfo
namespace: default
annotations:
ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- host: ${PUBLICDNS}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: podinfo
port:
number: 9898
EOF
echo -e "$podinfoingress" > podinfo-ingress.yaml
git add -A
git commit -m "adding ingress for podinfo on http port 8081"
git push
$FLUX_RECONCILE_CMD # flux will wait for the new deploy to become ready, but we cannot create a new
flux reconcile kustomization podinfo --with-source # NEEDED otherwise next port-forward will fail...
$ROLLOUT_STATUS_CMD # port-forward on the same port before it finished terminating! wait for that,
kubectl port-forward deployment/podinfo 9898:9898 &
PID=$!
[ "$1" == "" ] && open http://${PUBLICDNS}:9898 # browse port-forward, open only if in unattended mode
open http://${PUBLICDNS}:8081 # browse ingress
echo; echo
readr "Press [Enter] key to continue and close portforward, ending script (or ^C to leave it running) "
kill -9 $PID
echo;echo;echo -e ">>> ${GREEN}to remove everything, run the following lines${COLOR_RESET} <<<\n${YELLOW}docker stop gitsrv\ndocker rm gitsrv\nk3d cluster delete ${CLUSTER_NAME}\nrm -rf ${CONFIG_HOME}${COLOR_RESET}"
# to add flux extra controllers, uncomment and run next lines:
# echo "adding image-reflector-controller,image-automation-controller Flux extra controllers"
# cd ${CLONE_PATH}/flux-system
# flux install --components-extra=image-reflector-controller,image-automation-controller --export > gotk-components.yaml
# git add .
# git commit -m "adding Flux image-reflector-controller and image-automation-controller"
# git push
# $FLUX_RECONCILE_CMD &