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 NM renderer #5877

Closed xiachen-rh closed 5 days ago

xiachen-rh commented 1 week ago

Similar issue as https://github.com/canonical/cloud-init/issues/5776

Bug report

The following config

            network:
              config:
                - name: enp1s0
                  subnets:
                    - address: 10.0.2.2/24
                      gateway: 10.0.2.1
                      type: static
                      routes:
                        - destination: 0.0.0.0/0
                          gateway: 10.0.2.1
                          metric: 99
                  type: physical
              version: 1

when using NM renderer, produced a interface config file something like this:

# cat /etc/NetworkManager/system-connections/cloud-init-enp1s0.nmconnection
# Generated by cloud-init. Changes will be lost.

[connection]
id=cloud-init enp1s0
uuid=a41601f3-3acc-5f60-ac5f-9d9011ab7c25
autoconnect-priority=120
type=ethernet
interface-name=enp1s0

[user]
org.freedesktop.NetworkManager.origin=cloud-init

[ethernet]

[ipv4]
method=manual
may-fail=false
address1=10.0.2.2/24
gateway=10.0.2.1
route1=0.0.0.0/0,10.0.2.1

However, the route is not configured with this metric:

# ip route show
default via 10.0.2.1 dev enp1s0 proto static metric 100
10.0.2.0/24 dev enp1s0 proto kernel scope link src 10.0.2.2 metric 100

NM uses route-metric, see below documents 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 https://networkmanager.dev/docs/api/latest/nm-settings-nmcli.html

we need to fix it.