From cc81c5f558a0c3dbbed0309cab3757c99c2c804e Mon Sep 17 00:00:00 2001
From: portdirect <pete@port.direct>
Date: Tue, 28 Nov 2017 21:10:02 -0500
Subject: [PATCH] OvS: Implement cleaner shutdown and start logic

This PS adds cleaner shutdown and start logic to the OvS components.

Change-Id: I5c7fb212163a19d7584b37c20e7c6c757b498856
Co-Authored-By: SamYaple <sam@yaple.net>
---
 .../bin/_openvswitch-db-server.sh.tpl         | 37 +++++---
 .../bin/_openvswitch-vswitchd.sh.tpl          | 88 +++++++++++--------
 openvswitch/templates/daemonset-ovs-db.yaml   |  7 ++
 .../templates/daemonset-ovs-vswitchd.yaml     |  7 ++
 4 files changed, 90 insertions(+), 49 deletions(-)

diff --git a/openvswitch/templates/bin/_openvswitch-db-server.sh.tpl b/openvswitch/templates/bin/_openvswitch-db-server.sh.tpl
index a53b667c71..fc3365e6b4 100644
--- a/openvswitch/templates/bin/_openvswitch-db-server.sh.tpl
+++ b/openvswitch/templates/bin/_openvswitch-db-server.sh.tpl
@@ -17,15 +17,32 @@ limitations under the License.
 */}}
 
 set -ex
+COMMAND="${@:-start}"
 
-mkdir -p "/run/openvswitch"
-if [[ ! -e "/run/openvswitch/conf.db" ]]; then
-  ovsdb-tool create "/run/openvswitch/conf.db"
-fi
+OVS_DB=/run/openvswitch/conf.db
+OVS_SOCKET=/run/openvswitch/db.sock
+OVS_SCHEMA=/usr/share/openvswitch/vswitch.ovsschema
 
-umask 000
-exec /usr/sbin/ovsdb-server /run/openvswitch/conf.db \
-        -vconsole:emer \
-        -vconsole:err \
-        -vconsole:info \
-        --remote=punix:/run/openvswitch/db.sock
+function start () {
+  mkdir -p "$(dirname ${OVS_DB})"
+  if [[ ! -e "${OVS_DB}" ]]; then
+    ovsdb-tool create "${OVS_DB}"
+  fi
+
+  if [[ "$(ovsdb-tool needs-conversion ${OVS_DB} ${OVS_SCHEMA})" == 'yes' ]]; then
+      ovsdb-tool convert ${OVS_DB} ${OVS_SCHEMA}
+  fi
+
+  umask 000
+  exec /usr/sbin/ovsdb-server ${OVS_DB} \
+          -vconsole:emer \
+          -vconsole:err \
+          -vconsole:info \
+          --remote=punix:${OVS_SOCKET}
+}
+
+function stop () {
+  ovs-appctl -T1 -t /run/openvswitch/ovsdb-server.1.ctl exit
+}
+
+$COMMAND
diff --git a/openvswitch/templates/bin/_openvswitch-vswitchd.sh.tpl b/openvswitch/templates/bin/_openvswitch-vswitchd.sh.tpl
index 0c0477a5da..30a7db79b7 100644
--- a/openvswitch/templates/bin/_openvswitch-vswitchd.sh.tpl
+++ b/openvswitch/templates/bin/_openvswitch-vswitchd.sh.tpl
@@ -17,47 +17,57 @@ limitations under the License.
 */}}
 
 set -ex
+COMMAND="${@:-start}"
 
-sock="/var/run/openvswitch/db.sock"
-t=0
-while [ ! -e "${sock}" ] ; do
-    echo "waiting for ovs socket $sock"
-    sleep 1
-    t=$(($t+1))
-    if [ $t -ge 10 ] ; then
-        echo "no ovs socket, giving up"
-        exit 1
-    fi
-done
+OVS_SOCKET=/run/openvswitch/db.sock
 
-ovs-vsctl --no-wait show
+function start () {
+  t=0
+  while [ ! -e "${OVS_SOCKET}" ] ; do
+      echo "waiting for ovs socket $sock"
+      sleep 1
+      t=$(($t+1))
+      if [ $t -ge 10 ] ; then
+          echo "no ovs socket, giving up"
+          exit 1
+      fi
+  done
 
-external_bridge="{{- .Values.network.external_bridge -}}"
-external_interface="{{- .Values.network.interface.external -}}"
-if [ -n "${external_bridge}" ] ; then
-    # create bridge device
-    ovs-vsctl --no-wait --may-exist add-br $external_bridge
-    if [ -n "$external_interface" ] ; then
-        # add external interface to the bridge
-        ovs-vsctl --no-wait --may-exist add-port $external_bridge $external_interface
-        ip link set dev $external_interface up
-    fi
-fi
+  ovs-vsctl --no-wait show
 
-# handle any bridge mappings
-{{- range $br, $phys := .Values.network.auto_bridge_add }}
-if [ -n "{{- $br -}}" ] ; then
-    # create {{ $br }}{{ if $phys }} and add port {{ $phys }}{{ end }}
-    ovs-vsctl --no-wait --may-exist add-br "{{ $br }}"
-    if [ -n "{{- $phys -}}" ] ; then
-        ovs-vsctl --no-wait --may-exist add-port "{{ $br }}" "{{ $phys }}"
-        ip link set dev "{{ $phys }}" up
-    fi
-fi
-{{- end }}
+  external_bridge="{{- .Values.network.external_bridge -}}"
+  external_interface="{{- .Values.network.interface.external -}}"
+  if [ -n "${external_bridge}" ] ; then
+      # create bridge device
+      ovs-vsctl --no-wait --may-exist add-br $external_bridge
+      if [ -n "$external_interface" ] ; then
+          # add external interface to the bridge
+          ovs-vsctl --no-wait --may-exist add-port $external_bridge $external_interface
+          ip link set dev $external_interface up
+      fi
+  fi
 
-exec /usr/sbin/ovs-vswitchd unix:/run/openvswitch/db.sock \
-        -vconsole:emer \
-        -vconsole:err \
-        -vconsole:info \
-        --mlockall
+  # handle any bridge mappings
+  {{- range $br, $phys := .Values.network.auto_bridge_add }}
+  if [ -n "{{- $br -}}" ] ; then
+      # create {{ $br }}{{ if $phys }} and add port {{ $phys }}{{ end }}
+      ovs-vsctl --no-wait --may-exist add-br "{{ $br }}"
+      if [ -n "{{- $phys -}}" ] ; then
+          ovs-vsctl --no-wait --may-exist add-port "{{ $br }}" "{{ $phys }}"
+          ip link set dev "{{ $phys }}" up
+      fi
+  fi
+  {{- end }}
+
+  exec /usr/sbin/ovs-vswitchd unix:${OVS_SOCKET} \
+          -vconsole:emer \
+          -vconsole:err \
+          -vconsole:info \
+          --mlockall
+}
+
+function stop () {
+  ovs-appctl -T1 -t /run/openvswitch/ovs-vswitchd.1.ctl exit
+}
+
+$COMMAND
diff --git a/openvswitch/templates/daemonset-ovs-db.yaml b/openvswitch/templates/daemonset-ovs-db.yaml
index 7a53393282..3d2cbde0a7 100644
--- a/openvswitch/templates/daemonset-ovs-db.yaml
+++ b/openvswitch/templates/daemonset-ovs-db.yaml
@@ -44,6 +44,13 @@ spec:
             privileged: true
           command:
             - /tmp/openvswitch-db-server.sh
+            - start
+          lifecycle:
+            preStop:
+              exec:
+                command:
+                  - /tmp/openvswitch-db-server.sh
+                  - stop
           volumeMounts:
             - name: openvswitch-bin
               mountPath: /tmp/openvswitch-db-server.sh
diff --git a/openvswitch/templates/daemonset-ovs-vswitchd.yaml b/openvswitch/templates/daemonset-ovs-vswitchd.yaml
index 24127cc111..d3bf8198ec 100644
--- a/openvswitch/templates/daemonset-ovs-vswitchd.yaml
+++ b/openvswitch/templates/daemonset-ovs-vswitchd.yaml
@@ -70,6 +70,13 @@ spec:
                 - show
           command:
             - /tmp/openvswitch-vswitchd.sh
+            - start
+          lifecycle:
+            preStop:
+              exec:
+                command:
+                  - /tmp/openvswitch-db-server.sh
+                  - stop
           volumeMounts:
             - name: openvswitch-bin
               mountPath: /tmp/openvswitch-vswitchd.sh