blob: 049f731bb2ff7cc51414161f049482d9618d5df3 [file] [log] [blame]
Mohammed Naser54ee9922023-07-22 18:40:25 +00001#!/bin/bash -xe
2
3# Copyright 2023 VEXXHOST, Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Mohammed Naser19d63122024-01-08 17:10:05 -050017ANNOTATION_KEY="atmosphere.cloud/ovn-system-id"
18
Mohammed Naser54ee9922023-07-22 18:40:25 +000019function get_ip_address_from_interface {
20 local interface=$1
Oleksandr K.e4e83162024-10-23 22:09:30 +020021 local ip=$(ip -4 -o addr s "${interface}" | awk '{ print $4; exit }' | awk -F '/' 'NR==1 {print $1}')
Mohammed Naser54ee9922023-07-22 18:40:25 +000022 if [ -z "${ip}" ] ; then
23 exit 1
24 fi
25 echo ${ip}
26}
27
Mohammed Naser19d63122024-01-08 17:10:05 -050028function get_current_system_id {
29 ovs-vsctl --if-exists get Open_vSwitch . external_ids:system-id | tr -d '"'
30}
31
32function get_stored_system_id {
33 kubectl get node "$NODE_NAME" -o "jsonpath={.metadata.annotations.atmosphere\.cloud/ovn-system-id}"
34}
35
36function store_system_id() {
37 local system_id=$1
38 kubectl annotate node "$NODE_NAME" "$ANNOTATION_KEY=$system_id"
39}
40
Mohammed Naser54ee9922023-07-22 18:40:25 +000041# Detect tunnel interface
42tunnel_interface="{{- .Values.network.interface.tunnel -}}"
43if [ -z "${tunnel_interface}" ] ; then
44 # search for interface with tunnel network routing
45 tunnel_network_cidr="{{- .Values.network.interface.tunnel_network_cidr -}}"
46 if [ -z "${tunnel_network_cidr}" ] ; then
47 tunnel_network_cidr="0/0"
48 fi
49 # If there is not tunnel network gateway, exit
50 tunnel_interface=$(ip -4 route list ${tunnel_network_cidr} | awk -F 'dev' '{ print $2; exit }' \
51 | awk '{ print $1 }') || exit 1
52fi
53ovs-vsctl set open . external_ids:ovn-encap-ip="$(get_ip_address_from_interface ${tunnel_interface})"
54
Mohammed Naser19d63122024-01-08 17:10:05 -050055# Get the stored system-id from the Kubernetes node annotation
56stored_system_id=$(get_stored_system_id)
57
58# Get the current system-id set in OVS
59current_system_id=$(get_current_system_id)
60
61if [ -n "$stored_system_id" ] && [ "$stored_system_id" != "$current_system_id" ]; then
62 # If the annotation exists and does not match the current system-id, set the system-id to the stored one
63 ovs-vsctl set Open_vSwitch . external_ids:system-id="$stored_system_id"
64elif [ -z "$current_system_id" ]; then
65 # If no current system-id is set, generate a new one
66 current_system_id=$(uuidgen)
67 ovs-vsctl set Open_vSwitch . external_ids:system-id="$current_system_id"
68 # Store the new system-id in the Kubernetes node annotation
69 store_system_id "$current_system_id"
70elif [ -z "$stored_system_id" ]; then
71 # If there is no stored system-id, store the current one
72 store_system_id "$current_system_id"
Mohammed Naser54ee9922023-07-22 18:40:25 +000073fi
Mohammed Naser54ee9922023-07-22 18:40:25 +000074
75# Configure OVN remote
76{{- if empty .Values.conf.ovn_remote -}}
77{{- $sb_svc_name := "ovn-ovsdb-sb" -}}
78{{- $sb_svc := (tuple $sb_svc_name "internal" . | include "helm-toolkit.endpoints.hostname_fqdn_endpoint_lookup") -}}
Mohammed Naserd6db2452023-07-23 14:34:59 +000079{{- $sb_port := (tuple "ovn-ovsdb-sb" "internal" "ovsdb" . | include "helm-toolkit.endpoints.endpoint_port_lookup") -}}
Mohammed Naser54ee9922023-07-22 18:40:25 +000080{{- $sb_service_list := list -}}
81{{- range $i := until (.Values.pod.replicas.ovn_ovsdb_sb | int) -}}
82 {{- $sb_service_list = printf "tcp:%s-%d.%s:%s" $sb_svc_name $i $sb_svc $sb_port | append $sb_service_list -}}
83{{- end }}
84
85ovs-vsctl set open . external-ids:ovn-remote="{{ include "helm-toolkit.utils.joinListWithComma" $sb_service_list }}"
86{{- else -}}
87ovs-vsctl set open . external-ids:ovn-remote="{{ .Values.conf.ovn_remote }}"
88{{- end }}
89
90# Configure OVN values
91ovs-vsctl set open . external-ids:rundir="/var/run/openvswitch"
92ovs-vsctl set open . external-ids:ovn-encap-type="{{ .Values.conf.ovn_encap_type }}"
93ovs-vsctl set open . external-ids:ovn-bridge="{{ .Values.conf.ovn_bridge }}"
94ovs-vsctl set open . external-ids:ovn-bridge-mappings="{{ .Values.conf.ovn_bridge_mappings }}"
Oleksandr K.79635252024-10-25 16:42:49 +020095
96GW_ENABLED=$(cat /tmp/gw-enabled/gw-enabled)
97if [[ ${GW_ENABLED} == {{ .Values.labels.ovn_controller_gw.node_selector_value }} ]]; then
98 ovs-vsctl set open . external-ids:ovn-cms-options={{ .Values.conf.ovn_cms_options_gw_enabled }}
99else
100 ovs-vsctl set open . external-ids:ovn-cms-options={{ .Values.conf.ovn_cms_options }}
101fi
Oleksandr K.e4e83162024-10-23 22:09:30 +0200102
thywyne85e0ed2023-12-22 16:52:24 +0000103{{ if .Values.conf.ovn_bridge_datapath_type -}}
104ovs-vsctl set open . external-ids:ovn-bridge-datapath-type="{{ .Values.conf.ovn_bridge_datapath_type }}"
105{{- end }}
Mohammed Naser54ee9922023-07-22 18:40:25 +0000106
107# Configure hostname
Oleksandr K.e4e83162024-10-23 22:09:30 +0200108{{- if .Values.pod.use_fqdn.compute }}
Mohammed Naser54ee9922023-07-22 18:40:25 +0000109 ovs-vsctl set open . external-ids:hostname="$(hostname -f)"
110{{- else }}
111 ovs-vsctl set open . external-ids:hostname="$(hostname)"
112{{- end }}
113
114# Create bridges and create ports
115# handle any bridge mappings
116# /tmp/auto_bridge_add is one line json file: {"br-ex1":"eth1","br-ex2":"eth2"}
117for bmap in `sed 's/[{}"]//g' /tmp/auto_bridge_add | tr "," "\n"`
118do
119 bridge=${bmap%:*}
120 iface=${bmap#*:}
121 ovs-vsctl --may-exist add-br $bridge -- set bridge $bridge protocols=OpenFlow13
Oleksandr K.e4e83162024-10-23 22:09:30 +0200122 if [ -n "$iface" ] && [ "$iface" != "null" ] && ( ip link show $iface 1>/dev/null 2>&1 );
Mohammed Naser54ee9922023-07-22 18:40:25 +0000123 then
124 ovs-vsctl --may-exist add-port $bridge $iface
Mohammed Naser54ee9922023-07-22 18:40:25 +0000125 fi
126done
Mohammed Naser62c4dd92025-02-16 13:18:14 -0500127
128/usr/local/bin/ovsinit /tmp/auto_bridge_add