forked from nemonik/hands-on-DevOps-gen2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch.sh
executable file
·128 lines (84 loc) · 3.19 KB
/
patch.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
#!/usr/bin/env bash
# Copyright (C) 2021 Michael Joseph Walsh - All Rights Reserved
# You may use, distribute and modify this code under the
# terms of the the license.
#
# You should have received a copy of the license with
# this file. If not, please email <mjwalsh@nemonik.com>
set -a
skip_encrypted_variables=true
. ../.env
images_into_registry traefik_images
notify "Get host ip..."
kubectl run get-host-ip --image=${registry_name}:${registry_port}/${traefik_image_name}:${traefik_image_tag} --restart=Never --command -- sh -c "while true; do sleep 15; done"
kubectl wait --for=condition=Ready pod/get-host-ip --timeout 360s
notify "Block waiting for CoreDNS to start responding... "
kubectl exec pod/get-host-ip -- sh -c "loop=true && while \$loop; do if ping -c 1 host.k3d.internal; then loop=false; echo \"found\"; else sleep 5; fi; done"
output=`kubectl exec pod/get-host-ip -- sh -c "nslookup host.k3d.internal"`
read -ra parts <<< "${output##*$'\n'}"
host_ip="${parts[1]}"
kubectl delete pod/get-host-ip --wait=false
notify "Patching DNS in the cluster to resolve application FDQNs using ${host_ip} ip..."
node_hosts=$(kubectl get cm coredns -n kube-system --template='{{.data.NodeHosts}}')
IFS='
'
new_node_hosts=""
for node_host in $node_hosts
do
match=false
for entry in "${coredns_entries[@]}"
do
if [[ "${node_host}" == *"${entry}"* ]]; then
match=true
fi
done
if [ "$match" = false ]; then
if [[ ! -z $new_node_hosts ]]; then
new_node_hosts="${new_node_hosts}\n${node_host}"
else
new_node_hosts="${node_host}"
fi
fi
done
for entry in "${coredns_entries[@]}"
do
new_node_hosts="$new_node_hosts\n${host_ip} ${entry}"
done
patch=`echo $new_node_hosts | xargs -0 printf '{"data": {"NodeHosts":"%s"}}'`
patch_command="kubectl patch cm coredns -n kube-system -p='${patch}'"
echo $patch_command
eval $patch_command
notify "Forcing retart of coredns so that the tests can run immediately..."
current_kube_dns_pod_name=`kubectl get pod -n kube-system -l "k8s-app=kube-dns" -o json | jq -r '.items | .[] | .metadata.name'`
kubectl rollout restart deployment coredns -n kube-system
kubectl rollout status deployment coredns -n kube-system
while : ; do
kube_dns_pod_name=`kubectl get pod -n kube-system -l "k8s-app=kube-dns" -o json | jq -r '.items | .[] | .metadata.name'`
if [[ "${kube_dns_pod_name}" == *"${current_kube_dns_pod_name}"* ]]; then
sleep 0.5
else
break
fi
done
kubectl wait --for=condition=Ready pod ${kube_dns_pod_name} -n kube-system --timeout 360s
## The test has a built in ping loop needed to block until coredns is responding.
##
cd tests
eval ./test_patch.sh
cd ..
echo
warn "======================================================="
warn "Your host IP is ${host_ip}"
warn "Writing ${host_ip} into /tmp/host_ip"
warn "======================================================="
echo "${host_ip}" > /tmp/host_ip
echo
warn "======================================================="
warn "Ensure the following lines are in your /etc/hosts file:"
warn "======================================================="
echo
echo "127.0.0.1 host.k3d.internal"
for entry in "${coredns_entries[@]}"
do
echo "127.0.0.1 $entry"
done