Skip to content

Commit e3c4fef

Browse files
committed
Add ipsec connect wait service
When node goes for a reboot on an IPsec enabled cluster, once it comes up, libreswan parses /etc/ipsec.d/openshift.conf file and establishes SAs with peers and it may be still in progress even after kubelet is started, pod scheduled on this node would fail communicating with other pods until IPsec tunnels are established. So this commit adds wait-for-ipsec-connect.service systemd service which depends on ipsecenabler.service created by IPsec machine config. This new service loads existing connections into libreswan with auto=start option for every connection and waits upto 3 minutes until IPsec tunnels are established. This service is added into the base template to avoid two reboots during upgrade if it goes into IPsec machine configs rendered by CNO. TODO: observe ipsec-upgrade behavior with this in CI and need to revisit the logic as it needs to be enabled only on IPsec enabled clusters. Signed-off-by: Periyasamy Palanisamy <[email protected]>
1 parent d1aebf3 commit e3c4fef

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
mode: 0755
2+
path: "/usr/local/bin/ipsec-connect-wait.sh"
3+
contents:
4+
inline: |
5+
#!/bin/bash
6+
set -x
7+
8+
if [ ! -e "/etc/ipsec.d/openshift.conf" ]; then
9+
exit 0
10+
fi
11+
12+
#
13+
if ! grep -q "auto=start" /etc/ipsec.d/openshift.conf; then
14+
sed -i '/^.*conn ovn.*$/a\ auto=start' /etc/ipsec.d/openshift.conf
15+
fi
16+
17+
cat /etc/ipsec.d/openshift.conf
18+
19+
chroot /proc/1/root ipsec restart
20+
21+
timeout=180
22+
elapsed=0
23+
desiredconn=""
24+
establishedsa=""
25+
26+
while [[ $elapsed -lt $timeout ]]; do
27+
desiredconn=$(grep -E '^\s*conn\s+' /etc/ipsec.d/openshift.conf | grep -v '%default' | awk '{print $2}' | tr ' ' '\n' | sort | tr '\n' ' ')
28+
establishedsa=$(ipsec showstates | grep STATE_V2_ESTABLISHED_CHILD_SA | grep -o '"[^"]*"' | sed 's/"//g' | tr ' ' '\n' | sort | uniq | tr '\n' ' ')
29+
if [ "$desiredconn" == "$establishedsa" ]; then
30+
echo "IPsec SAs are established for desired connections"
31+
break
32+
else
33+
echo "IPsec SAs are not established yet, waiting"
34+
sleep 2s
35+
fi
36+
elapsed=$((elapsed + 2))
37+
done
38+
39+
if [[ $elapsed -ge $timeout ]]; then
40+
echo "Timed out waiting, some connections are not established, desired conns $desiredconn, established conns $establishedsa"
41+
fi
42+
43+
ipsec status
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: wait-for-ipsec-connect.service
2+
enabled: true
3+
contents: |
4+
[Unit]
5+
Description=Ensure IKE SA established for existing IPsec connections.
6+
After=ipsec.service
7+
Before=kubelet-dependencies.target node-valid-hostname.service
8+
9+
[Service]
10+
Type=oneshot
11+
ExecStart=/usr/local/bin/ipsec-connect-wait.sh
12+
StandardOutput=journal+console
13+
StandardError=journal+console
14+
15+
[Install]
16+
WantedBy=ipsec.service

0 commit comments

Comments
 (0)