Mohammed Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 1 | #!/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 Naser | 19d6312 | 2024-01-08 17:10:05 -0500 | [diff] [blame] | 17 | ANNOTATION_KEY="atmosphere.cloud/ovn-system-id" |
| 18 | |
Mohammed Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 19 | function get_ip_address_from_interface { |
| 20 | local interface=$1 |
Oleksandr K. | e4e8316 | 2024-10-23 22:09:30 +0200 | [diff] [blame] | 21 | local ip=$(ip -4 -o addr s "${interface}" | awk '{ print $4; exit }' | awk -F '/' 'NR==1 {print $1}') |
Mohammed Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 22 | if [ -z "${ip}" ] ; then |
| 23 | exit 1 |
| 24 | fi |
| 25 | echo ${ip} |
| 26 | } |
| 27 | |
Mohammed Naser | 19d6312 | 2024-01-08 17:10:05 -0500 | [diff] [blame] | 28 | function get_current_system_id { |
| 29 | ovs-vsctl --if-exists get Open_vSwitch . external_ids:system-id | tr -d '"' |
| 30 | } |
| 31 | |
| 32 | function get_stored_system_id { |
| 33 | kubectl get node "$NODE_NAME" -o "jsonpath={.metadata.annotations.atmosphere\.cloud/ovn-system-id}" |
| 34 | } |
| 35 | |
| 36 | function store_system_id() { |
| 37 | local system_id=$1 |
| 38 | kubectl annotate node "$NODE_NAME" "$ANNOTATION_KEY=$system_id" |
| 39 | } |
| 40 | |
Mohammed Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 41 | # Detect tunnel interface |
| 42 | tunnel_interface="{{- .Values.network.interface.tunnel -}}" |
| 43 | if [ -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 |
| 52 | fi |
| 53 | ovs-vsctl set open . external_ids:ovn-encap-ip="$(get_ip_address_from_interface ${tunnel_interface})" |
| 54 | |
Mohammed Naser | 19d6312 | 2024-01-08 17:10:05 -0500 | [diff] [blame] | 55 | # Get the stored system-id from the Kubernetes node annotation |
| 56 | stored_system_id=$(get_stored_system_id) |
| 57 | |
| 58 | # Get the current system-id set in OVS |
| 59 | current_system_id=$(get_current_system_id) |
| 60 | |
| 61 | if [ -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" |
| 64 | elif [ -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" |
| 70 | elif [ -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 Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 73 | fi |
Mohammed Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 74 | |
| 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 Naser | d6db245 | 2023-07-23 14:34:59 +0000 | [diff] [blame] | 79 | {{- $sb_port := (tuple "ovn-ovsdb-sb" "internal" "ovsdb" . | include "helm-toolkit.endpoints.endpoint_port_lookup") -}} |
Mohammed Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 80 | {{- $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 | |
| 85 | ovs-vsctl set open . external-ids:ovn-remote="{{ include "helm-toolkit.utils.joinListWithComma" $sb_service_list }}" |
| 86 | {{- else -}} |
| 87 | ovs-vsctl set open . external-ids:ovn-remote="{{ .Values.conf.ovn_remote }}" |
| 88 | {{- end }} |
| 89 | |
| 90 | # Configure OVN values |
| 91 | ovs-vsctl set open . external-ids:rundir="/var/run/openvswitch" |
| 92 | ovs-vsctl set open . external-ids:ovn-encap-type="{{ .Values.conf.ovn_encap_type }}" |
| 93 | ovs-vsctl set open . external-ids:ovn-bridge="{{ .Values.conf.ovn_bridge }}" |
| 94 | ovs-vsctl set open . external-ids:ovn-bridge-mappings="{{ .Values.conf.ovn_bridge_mappings }}" |
Oleksandr K. | 7963525 | 2024-10-25 16:42:49 +0200 | [diff] [blame] | 95 | |
| 96 | GW_ENABLED=$(cat /tmp/gw-enabled/gw-enabled) |
| 97 | if [[ ${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 }} |
| 99 | else |
| 100 | ovs-vsctl set open . external-ids:ovn-cms-options={{ .Values.conf.ovn_cms_options }} |
| 101 | fi |
Oleksandr K. | e4e8316 | 2024-10-23 22:09:30 +0200 | [diff] [blame] | 102 | |
thywyn | e85e0ed | 2023-12-22 16:52:24 +0000 | [diff] [blame] | 103 | {{ if .Values.conf.ovn_bridge_datapath_type -}} |
| 104 | ovs-vsctl set open . external-ids:ovn-bridge-datapath-type="{{ .Values.conf.ovn_bridge_datapath_type }}" |
| 105 | {{- end }} |
Mohammed Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 106 | |
| 107 | # Configure hostname |
Oleksandr K. | e4e8316 | 2024-10-23 22:09:30 +0200 | [diff] [blame] | 108 | {{- if .Values.pod.use_fqdn.compute }} |
Mohammed Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 109 | 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"} |
| 117 | for bmap in `sed 's/[{}"]//g' /tmp/auto_bridge_add | tr "," "\n"` |
| 118 | do |
| 119 | bridge=${bmap%:*} |
| 120 | iface=${bmap#*:} |
| 121 | ovs-vsctl --may-exist add-br $bridge -- set bridge $bridge protocols=OpenFlow13 |
Oleksandr K. | e4e8316 | 2024-10-23 22:09:30 +0200 | [diff] [blame] | 122 | if [ -n "$iface" ] && [ "$iface" != "null" ] && ( ip link show $iface 1>/dev/null 2>&1 ); |
Mohammed Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 123 | then |
| 124 | ovs-vsctl --may-exist add-port $bridge $iface |
Mohammed Naser | 54ee992 | 2023-07-22 18:40:25 +0000 | [diff] [blame] | 125 | fi |
| 126 | done |
Mohammed Naser | 62c4dd9 | 2025-02-16 13:18:14 -0500 | [diff] [blame] | 127 | |
| 128 | /usr/local/bin/ovsinit /tmp/auto_bridge_add |