-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_hosts
executable file
·120 lines (88 loc) · 3.19 KB
/
make_hosts
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
#!/bin/bash -f
#
# Queries the CRI using crictl to find the pod names and IP addresses
# Creates a CoreDNS file plugin-compatible hosts file
#
#
mkdir -p /tmp/cridns
TMPFILE="/tmp/cridns/smarterdns.$$"
DNSFILE="/tmp/cridns/smarterdns"
env
# cluster
CLUSTER=${CLUSTER_DOMAIN:-"cluster.local"}
# cluster dns
DNS=${CLUSTER_DNS:-"169.254.0.2"}
# Setup to use default containerd socket
CRI_SOCKET_DIR=${CRI_DIR:-"unix:///run/containerd"}
CRI_SOCKET_FILE=${CRI_FILE:-"containerd.sock"}
CRICTL_SOCKET=${CRI_SOCKET_DIR}/${CRI_SOCKET_FILE}
export CRI_CONFIG_FILE=${CRICTL_CONFIG}:-"/crictl.yaml"
export CONTAINER_RUNTIME_ENDPOINT=${CRICTL_SOCKET}
# Try to find the k3s binary
if [ -f /host/usr/bin/k3s ]; then
K3S_BIN="/host/usr/bin/k3s"
elif [ -f /host/usr/local/bin/k3s ]; then
K3S_BIN="/host/usr/local/bin/k3s"
fi
if [ ! -z "${CRICTL_BIN}" ]; then
CRICTL=${CRICTL_BIN}
else
CRICTL="/crictl"
fi
#
echo "Using ${CRICTL}"
echo "crictl using socket: ${CRICTL_SOCKET}"
echo "crictl using config: ${CRI_CONFIG_FILE}"
echo "Container runtime info:"
${CRICTL} info
LOGGING=${DNS_LOGGING:-"False"}
INTERVAL=${WAIT_INTERVAL:-"30"}
host=`hostname`
#NET_IFACE=${IFACE:-"eth0"}
#HOSTIP=$(ip addr show dev ${NET_IFACE} | grep "inet" | sed -ne "s/inet \([0-9][0-9.]*\).*/\1/p")
HOSTIP=127.0.0.1
comment=smarter-dns
# Remove any existing iptable rules
echo "Removing any existing iptables rules"
iptables-legacy -t nat -S | grep "${comment}" | sed 's/^-A //' | while read rule; do iptables-legacy -t nat -D $rule; done
# Add iptable rules for smarter-dns
echo "Add iptable rules for smarter-dns"
iptables-legacy -t nat -A PREROUTING -d ${DNS}/32 -p udp -m udp --dport 53 -m comment --comment "${comment}" -j DNAT --to-destination ${HOSTIP}:353
iptables-legacy -t nat -A OUTPUT -d ${DNS}/32 -p udp -m udp --dport 53 -m comment --comment "${comment}" -j DNAT --to-destination ${HOSTIP}:353
# cleanup
rm -rf pod_dns.*
while true; do
done=""
# Get the list of pods from the container runtime
pods=`${CRICTL} pods -q`
# if there are pods
if [[ ! -z $pods ]]; then
rm -f $TMPFILE
echo -n "# " > $TMPFILE
date >> $TMPFILE
for pod in $pods
do
IFS=', ' read -r -a res < <(${CRICTL} inspectp -o json $pod | tr '[:upper:]' '[:lower:]' | jq -r '[.status.network.ip, .info.config.hostname, .status.state, .status.labels.name, .status.metadata.namespace] | join(",")')
if [ ${res[2]} == "sandbox_ready" ]; then
if [[ -z "${res[0]}" ]]; then
ip=$HOSTIP
name=${res[3]}
else
ip=${res[0]}
name=${res[1]}
fi
namespace=${res[4]}
if [[ ! -z $name ]]; then
if [ ${LOGGING} == "True" ]; then
echo $ip $name $name.$namespace.svc.${CLUSTER} $name.svc.${CLUSTER} $name."cluster.local"
fi
echo $ip $name $name.$namespace.svc.${CLUSTER} $name.svc.${CLUSTER} $name."cluster.local" >> $TMPFILE
fi
fi
done
# swap TMPFILE and File read by COREDNS
rm -f $DNSFILE
mv $TMPFILE $DNSFILE
fi
sleep $INTERVAL
done