cm-router.yaml 1.4 KB

123456789101112131415161718192021222324252627
  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: {{ include "openvpn.fullname" . }}-routecheck
  5. data:
  6. route-check: |
  7. set -x
  8. while true
  9. do
  10. date
  11. /sbin/ip link list {{ .Values.dev_name }} > /dev/null 2>&1
  12. if [ $? -ne 0 ]; then
  13. __server_pod_ip=$(kubectl get po -o json | jq -r '.items[] | select(.metadata.labels.component=="application" and .metadata.labels["app.kubernetes.io/instance"]=="{{ .Release.Name }}" and .metadata.labels["app.kubernetes.io/name"]=="{{ include "openvpn.name" . }}" and .metadata.deletionTimestamp==null and .status.phase=="Running").status.podIP')
  14. __route_ip=$(ip route get {{ .Values.net }} | awk '{print $3}')
  15. if [[ $__server_pod_ip != $__route_ip ]]; then
  16. ip route delete {{ .Values.net }}/$(mask2cdr {{ .Values.mask }}) > /dev/null 2>&1
  17. ip route add {{ .Values.net }}/$(mask2cdr {{ .Values.mask }}) via $__server_pod_ip
  18. fi
  19. elif [ $(ip route show to match {{ .Values.net }} | wc -l) -gt 2 ]; then
  20. ip route delete {{ .Values.net }}/$(mask2cdr {{ .Values.mask }}) > /dev/null 2>&1
  21. elif [ $(ip route show to match {{ .Values.net }} | wc -l) -le 2 ]; then
  22. ip route add {{ .Values.net }}/$(mask2cdr {{ .Values.mask }}) dev {{ .Values.dev_name }}
  23. fi
  24. sleep 60
  25. done
  26. route-delete: |
  27. ip route delete {{ .Values.net }}/$(mask2cdr {{ .Values.mask }}) > /dev/null 2>&1