Skip to content

Commit 555d31c

Browse files
committed
Consume openvswitch-ipsec systemd service for OVN IPsec deployment
The ovn-ipsec-host daemonset pod currently spins up ovs-monitor-ipsec process to configure IPsec connections with the peer nodes. This would make ipsec connections to be established for the existing nodes a bit later after kubelet is started at the time node/service restart scenario, but by the time workloads are scheduled on the node started hitting traffic drops because of unavailability of IPsec connections between nodes. This makes IPsec jobs in CI so unstable and monitor jobs always failing during IPsec upgrade. The FDP story (https://issues.redhat.com/browse/FDP-1051) gets openvswitch-ipsec systemd service (runs ovs-monitor-ipsec) with required configurable parameters, It's available with OVS 3.5 version. So this commit does the following. 1. Stop spawning ovs-monitor-ipsec as foreground process in the ovn-ipsec container, Instead setup required IPsec configuration parameters in the /etc/sysconfig/openvswitch file, enable and start the openvswitch-ipsec service on the host. This is done at the of when ovn-ipsec-host pod is coming up for the first time, for the pod restart scenarios, it just checks openvswitch-ipsec service is running on the host, otherwise exit from the container with error. 2. Keep running an ovn-ipsec container and redirects /var/log/openvswitch/ovs-monitor-ipsec.log to the ovn-ipsec container's stdout console. 3. There is no necessity of having ovn-ipsec-clean container anymore with openvswitch-ipsec service as it's going to handle OVN IPsec states appropriately. 4. During the OCP upgrade, the new ipsec os extension takes while to deploy with openvswitch3.5-ipsec package, so by the time ovn-ipsec-host daemonset is rendered, We need to handle that scenario by running ovs-monitor-ipsec in the container. so this commit is also considering the transition phase of the process that is moving from container to host. 5. The ovn-keys init container configures ovs with IPsec certificate paths, so the container uses same host directory path to store and configure ovs with certificates because the ovs-monitor-ipsec process is running on the host now. Signed-off-by: Periyasamy Palanisamy <[email protected]>
1 parent a1283bf commit 555d31c

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

bindata/network/ovn-kubernetes/common/ipsec-host.yaml

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ spec:
109109
# authentic.
110110
echo "Configuring IPsec keys"
111111

112-
cert_pem=/etc/openvswitch/keys/ipsec-cert.pem
112+
cert_pem=/var/lib/openvswitch/etc/keys/ipsec-cert.pem
113113

114114
# If the certificate does not exist or it will expire in the next 6 months
115115
# (15770000 seconds), we will generate a new one.
@@ -118,18 +118,18 @@ spec:
118118
# is a requirement by OVN.
119119
cn=$(ovs-vsctl --retry -t 60 get Open_vSwitch . external-ids:system-id | tr -d "\"")
120120

121-
mkdir -p /etc/openvswitch/keys
121+
mkdir -p /var/lib/openvswitch/etc/keys
122122

123123
# Generate an SSL private key and use the key to create a certitificate signing request
124-
umask 077 && openssl genrsa -out /etc/openvswitch/keys/ipsec-privkey.pem 2048
124+
umask 077 && openssl genrsa -out /var/lib/openvswitch/etc/keys/ipsec-privkey.pem 2048
125125
openssl req -new -text \
126126
-extensions v3_req \
127127
-addext "subjectAltName = DNS:${cn}" \
128128
-subj "/C=US/O=ovnkubernetes/OU=kind/CN=${cn}" \
129-
-key /etc/openvswitch/keys/ipsec-privkey.pem \
130-
-out /etc/openvswitch/keys/ipsec-req.pem
129+
-key /var/lib/openvswitch/etc/keys/ipsec-privkey.pem \
130+
-out /var/lib/openvswitch/etc/keys/ipsec-req.pem
131131

132-
csr_64=$(base64 -w0 /etc/openvswitch/keys/ipsec-req.pem) # -w0 to avoid line-wrap
132+
csr_64=$(base64 -w0 /var/lib/openvswitch/etc/keys/ipsec-req.pem) # -w0 to avoid line-wrap
133133

134134
# Request that our generated certificate signing request is
135135
# signed by the "network.openshift.io/signer" signer that is
@@ -169,7 +169,7 @@ spec:
169169
# kubectl delete csr/$(hostname)
170170

171171
# Get the CA certificate so we can authenticate peer nodes.
172-
openssl x509 -in /signer-ca/ca-bundle.crt -outform pem -text -out /etc/openvswitch/keys/ipsec-cacert.pem
172+
openssl x509 -in /signer-ca/ca-bundle.crt -outform pem -text -out /var/lib/openvswitch/etc/keys/ipsec-cacert.pem
173173
fi
174174

175175
# Configure OVS with the relevant keys for this node. This is required by ovs-monitor-ipsec.
@@ -178,8 +178,8 @@ spec:
178178
# the will get read and loaded into NSS by the ovs-monitor-ipsec process
179179
# which has not started yet.
180180
ovs-vsctl --retry -t 60 set Open_vSwitch . other_config:certificate=$cert_pem \
181-
other_config:private_key=/etc/openvswitch/keys/ipsec-privkey.pem \
182-
other_config:ca_cert=/etc/openvswitch/keys/ipsec-cacert.pem
181+
other_config:private_key=/var/lib/openvswitch/etc/keys/ipsec-privkey.pem \
182+
other_config:ca_cert=/var/lib/openvswitch/etc/keys/ipsec-cacert.pem
183183
env:
184184
- name: K8S_NODE
185185
valueFrom:
@@ -196,8 +196,8 @@ spec:
196196
name: host-var-run
197197
- mountPath: /signer-ca
198198
name: signer-ca
199-
- mountPath: /etc/openvswitch
200-
name: etc-openvswitch
199+
- mountPath: /var/lib
200+
name: host-var-lib
201201
- mountPath: /etc
202202
name: host-etc
203203
resources:
@@ -242,6 +242,36 @@ spec:
242242
exit 2
243243
fi
244244

245+
# When openvswitch-ipsec service exists on the node, then reuse it to
246+
# configure IPsec for the pod traffic. Otherwise fall back to configuring
247+
# IPsec by spinning up ovs-monitor-ipsec process within the container.
248+
ovsipsecservice="openvswitch-ipsec"
249+
if chroot /proc/1/root systemctl list-unit-files --type=service | grep "$ovsipsecservice"; then
250+
if ! grep -q "openshift.conf" /etc/sysconfig/openvswitch; then
251+
sed -i 's|OPTIONS=\"\"|OPTIONS=\"--no-restart-ike-daemon --ovs-monitor-ipsec-options='\''--ipsec-conf=\/etc\/ipsec.d\/openshift.conf --root-ipsec-conf=\/etc\/ipsec.conf --ipsec-d=\/var\/lib\/ipsec\/nss --use-default-crypto'\''\"|' /etc/sysconfig/openvswitch
252+
chroot /proc/1/root systemctl enable --now $ovsipsecservice
253+
counter=0
254+
until [ -r /var/run/openvswitch/ovs-monitor-ipsec.pid ]; do
255+
counter=$((counter+1))
256+
sleep 1
257+
if [ $counter -gt 300 ];
258+
then
259+
echo "$ovsipsecservice service has not started after $counter seconds"
260+
exit 1
261+
fi
262+
done
263+
fi
264+
if ! chroot /proc/1/root systemctl is-active --quiet $ovsipsecservice; then
265+
echo "$ovsipsecservice service is not running, check system logs"
266+
exit 1
267+
fi
268+
while true; do
269+
tail -F /var/log/openvswitch/ovs-monitor-ipsec.log
270+
echo "tail on ovs-monitor-ipsec.log failed. attempting again"
271+
sleep 2s
272+
done
273+
fi
274+
245275
# The ovs-monitor-ipsec doesn't set authby, so when it calls ipsec auto --start
246276
# the default ones defined at Libreswan's compile time will be used. On restart,
247277
# Libreswan will use authby from libreswan.config. If libreswan.config is
@@ -295,6 +325,10 @@ spec:
295325
- |
296326
#!/bin/bash
297327
set -exuo pipefail
328+
if chroot /proc/1/root systemctl is-active --quiet openvswitch-ipsec; then
329+
echo "openvswitch-ipsec service is running on the host, no action needed in pre stop"
330+
exit 0
331+
fi
298332
# In order to maintain traffic flows during container restart, we
299333
# need to ensure that xfrm state and policies are not flushed.
300334
@@ -322,8 +356,6 @@ spec:
322356
name: host-var-run
323357
- mountPath: /var/log/openvswitch/
324358
name: host-var-log-ovs
325-
- mountPath: /etc/openvswitch
326-
name: etc-openvswitch
327359
- mountPath: /var/lib
328360
name: host-var-lib
329361
- mountPath: /etc
@@ -332,6 +364,8 @@ spec:
332364
name: usr-sbin
333365
- mountPath: /usr/libexec
334366
name: usr-libexec
367+
- mountPath: /usr/lib/systemd
368+
name: usr-libsystemd
335369
resources:
336370
requests:
337371
cpu: 10m
@@ -430,7 +464,15 @@ spec:
430464

431465
# Function to handle SIGTERM
432466
cleanup() {
433-
echo "received SIGTERM, flushing ipsec config"
467+
echo "received SIGTERM, flushing ipsec config if required"
468+
# When IPsec connections are managed by openvswitch-ipsec systemd service,
469+
# It means ovs-monitor-ipsec process is running on the host always, so ip
470+
# xfrm state and policy entries are managed properly according to ipsec
471+
# configuration on the OVN. so skip flushing ipsec for this case.
472+
if chroot /proc/1/root systemctl is-active --quiet openvswitch-ipsec; then
473+
echo "openvswitch-ipsec service is running on the host, no ipsec flush needed"
474+
exit 0
475+
fi
434476
# Wait upto 15 seconds for ovs-monitor-ipsec process to terminate before
435477
# cleaning up ipsec entries.
436478
counter=0
@@ -462,14 +504,19 @@ spec:
462504
done
463505
echo "ovs-monitor-ipsec is started"
464506

465-
# Monitor the ovs-monitor-ipsec process.
466-
while kill -0 "$(cat /var/run/openvswitch/ovs-monitor-ipsec.pid 2>/dev/null)"; do
467-
sleep 1
468-
done
507+
# When ovs-monitor-ipsec process is not running via openvswitch-ipsec systemd
508+
# service, then keep monitoring ovn-ipsec container process until it terminates
509+
# in the ovn-ipsec container's pre stop hook.
510+
if ! chroot /proc/1/root systemctl is-active --quiet openvswitch-ipsec; then
511+
# Monitor the ovs-monitor-ipsec process.
512+
while kill -0 "$(cat /var/run/openvswitch/ovs-monitor-ipsec.pid 2>/dev/null)"; do
513+
sleep 1
514+
done
469515

470-
# Once the ovs-monitor-ipsec process terminates, execute the cleanup command.
471-
echo "ovs-monitor-ipsec is terminated, flushing ipsec config"
472-
ipsecflush
516+
# Once the ovs-monitor-ipsec process terminates, execute the cleanup command.
517+
echo "ovs-monitor-ipsec is terminated, flushing ipsec config"
518+
ipsecflush
519+
fi
473520

474521
# Continue running until SIGTERM is received (or exit naturally)
475522
while true; do
@@ -508,10 +555,6 @@ spec:
508555
defaultMode: 420
509556
name: signer-ca
510557
name: signer-ca
511-
- hostPath:
512-
path: /var/lib/openvswitch/etc
513-
type: DirectoryOrCreate
514-
name: etc-openvswitch
515558
- hostPath:
516559
path: "{{.CNIConfDir}}"
517560
name: host-cni-netd
@@ -535,6 +578,10 @@ spec:
535578
path: /usr/libexec
536579
type: Directory
537580
name: usr-libexec
581+
- hostPath:
582+
path: /usr/lib/systemd
583+
type: DirectoryOrCreate
584+
name: usr-libsystemd
538585
tolerations:
539586
- operator: "Exists"
540587
{{end}}

0 commit comments

Comments
 (0)