forked from rh-ecosystem-edge/ib-orchestrate-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-dnsmasq-machineconfig.sh
executable file
·158 lines (138 loc) · 4.3 KB
/
generate-dnsmasq-machineconfig.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
#!/bin/bash
#
# Mostly copied and and repurposed for creating a MachineConfig from
# https://github.com/donpenney/lifecycle-agent/blob/site-policy-helper/hack/generate-dnsmasq-site-policy-section.sh
PROG=$(basename "$0")
declare CLUSTER_NAME_DOMAIN=
declare CLUSTER_IP=
function usage {
cat <<EOF
Usage: ${PROG} --name --ip
Options:
--name <cluster name + baseDomain>
--ip <node ip>
Summary:
Generates a Machineconfig to include dnsmasq config for an SNO.
Example:
${PROG} --name cnfde8.sno.ptp.lab.eng.bos.redhat.com --ip 10.16.231.8
EOF
exit 1
}
function generate_single_node_conf_template {
cat <<EOF
address=/apps.${CLUSTER_NAME_DOMAIN}/HOST_IP
address=/api-int.${CLUSTER_NAME_DOMAIN}/HOST_IP
address=/api.${CLUSTER_NAME_DOMAIN}/HOST_IP
EOF
}
function generate_forcedns {
cat <<EOF
#!/bin/bash
export IP="${CLUSTER_IP}"
if [ -f /etc/default/node-ip ]; then
export IP=\$(cat /etc/default/node-ip)
fi
export BASE_RESOLV_CONF=/run/NetworkManager/resolv.conf
if [ "\$2" = "dhcp4-change" ] || [ "\$2" = "dhcp6-change" ] || [ "\$2" = "up" ] || [ "\$2" = "connectivity-change" ]; then
export TMP_FILE=\$(mktemp /etc/forcedns_resolv.conf.XXXXXX)
cp \$BASE_RESOLV_CONF \$TMP_FILE
chmod --reference=\$BASE_RESOLV_CONF \$TMP_FILE
sed -i -e "s/${CLUSTER_NAME_DOMAIN}//" \\
-e "s/search /& ${CLUSTER_NAME_DOMAIN} /" \\
-e "0,/nameserver/s/nameserver/& \$IP\n&/" \$TMP_FILE
mv \$TMP_FILE /etc/resolv.conf
fi
EOF
}
function generate_single_node_conf {
cat <<EOF
[main]
rc-manager=unmanaged
EOF
}
function generate_machineconfig {
cat <<EOF | cut -c 8-
apiVersion: machineconfiguration.openshift.io/v1
kind: MachineConfig
metadata:
labels:
machineconfiguration.openshift.io/role: master
name: 50-master-dnsmasq-configuration
spec:
config:
storage:
files:
- contents:
source: data:text/plain;charset=utf-8;base64,$(generate_single_node_conf_template | base64 -w 0)
mode: 420
path: /etc/default/single-node.conf_template
overwrite: true
- contents:
source: data:text/plain;charset=utf-8;base64,$(generate_forcedns | base64 -w 0)
mode: 365
path: /etc/NetworkManager/dispatcher.d/forcedns
overwrite: true
- contents:
source: data:text/plain;charset=utf-8;base64,$(generate_single_node_conf | base64 -w 0)
mode: 420
path: /etc/NetworkManager/conf.d/single-node.conf
overwrite: true
systemd:
units:
- name: dnsmasq.service
enabled: true
contents: |
[Unit]
Description=Run dnsmasq to provide local dns for Single Node OpenShift
Before=kubelet.service crio.service
After=network.target nodeip-configuration.service
[Service]
TimeoutStartSec=30
ExecStartPre=/bin/bash -c 'until [ -e /var/run/nodeip-configuration/primary-ip ]; do sleep 1; done'
ExecStartPre=/bin/bash -c 'sed "s/HOST_IP/\$(cat /var/run/nodeip-configuration/primary-ip)/g" /etc/default/single-node.conf_template > /etc/dnsmasq.d/single-node.conf'
ExecStart=/usr/sbin/dnsmasq -k
Restart=always
[Install]
WantedBy=multi-user.target
EOF
}
#
# Process cmdline arguments
#
longopts=(
"help"
"name:"
"ip:"
)
longopts_str=$(IFS=,; echo "${longopts[*]}")
if ! OPTS=$(getopt -o "hn:i:" --long "${longopts_str}" --name "$0" -- "$@"); then
usage
exit 1
fi
eval set -- "${OPTS}"
while :; do
case "$1" in
-n|--name)
CLUSTER_NAME_DOMAIN="${2}"
shift 2
;;
-i|--ip)
CLUSTER_IP="${2}"
shift 2
;;
--)
shift
break
;;
-h|--help)
usage
;;
*)
usage
;;
esac
done
if [ -z "${CLUSTER_NAME_DOMAIN}" ] || [ -z "${CLUSTER_IP}" ]; then
usage
fi
generate_machineconfig