canonical / cloud-init

Official upstream for the cloud-init: cloud instance initialization
https://cloud-init.io/
Other
3.01k stars 887 forks source link

Configuring metric for default gateway is not working with sysconfig renderer #5776

Closed ani-sinha closed 1 month ago

ani-sinha commented 1 month ago

The following config

# cat /etc/cloud/cloud.cfg.d/custom.cfg 
            network:
              config:
                - name: ens160
                  subnets:
                    - address: 172.16.127.125/24
                      gateway: 172.16.127.2
                      type: static
                      routes:
                        - destination: 0.0.0.0/0
                          gateway: 172.16.127.2
                          metric: 99
                  type: physical
              version: 1

produced a interface config file something like this:

# cat /etc/sysconfig/network-scripts/ifcfg-ens160 
# Created by cloud-init automatically, do not edit.
#
AUTOCONNECT_PRIORITY=120
BOOTPROTO=none
DEFROUTE=yes
DEVICE=ens160
GATEWAY=172.16.127.2
IPADDR=172.16.127.125
METRIC=99
NETMASK=255.255.255.0
ONBOOT=yes
TYPE=Ethernet
USERCTL=no

However, the route is not configured with this metric:

# # ip r show
default via 172.16.127.2 dev ens160 proto static metric 100
172.16.127.0/24 dev ens160 proto kernel scope link src 172.16.127.125 metric 100

NetworkManager does not recognize METRIC for global generic metric value

nmcli con mod 'System ens160' ipv4.route-metric 90
# grep -i metric /etc/sysconfig/network-scripts/ifcfg-ens160
IPV4_ROUTE_METRIC=90

nmcli con up 'System ens160'
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
# ip r show
default via 172.16.127.2 dev ens160 proto static metric 90
172.16.127.0/24 dev ens160 proto kernel scope link src 172.16.127.125 metric 90

NM uses IPV4_ROUTE_METRIC and IPV6_ROUTE_METRIC.

See also https://people.freedesktop.org/~lkundrak/nm-docs/nm-settings-ifcfg-rh.html and https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html-single/configuring_and_managing_networking/index#con_how-networkmanager-manages-multiple-default-gateways_managing-the-default-gateway-setting

We need to fix this.