Skip to content

Conversation

@mosys0815
Copy link

Using ip route structured data allows to query safely the default route interface, no matter the output of the standard 'ip route' output.

commit dbe1f87
Merge: 35b7a74 4d46bec
Author: Bjoern Anters <[email protected]>
Date:   Mon Jul 14 09:41:30 2025 +0200

    Merge branch 'main' into 10996_get_default_nic

commit 35b7a74
Author: Bjoern Anters <[email protected]>
Date:   Thu Jun 26 16:25:57 2025 +0200

    use ip route structured data to retrieve default interface

commit 234dfd3
Merge: a310b80 16c60c7
Author: Björn Anters <[email protected]>
Date:   Tue Jun 24 17:51:37 2025 +0200

    Merge branch 'apache:main' into 10996_get_default_nic

commit a310b80
Author: Bjoern Anters <[email protected]>
Date:   Mon Jun 23 16:40:10 2025 +0200

    use ip route structured data to retrieve default interface

commit 8e8549c
Author: Bjoern Anters <[email protected]>
Date:   Mon Jun 23 15:47:13 2025 +0200

    Revert "make debian build vars work"

    This reverts commit c1ccb16.

commit 0446631
Author: Bjoern Anters <[email protected]>
Date:   Mon Jun 23 15:45:37 2025 +0200

    Revert "use ip route structured data to retrieve default interface"

    This reverts commit 91578d7.

commit c1ccb16
Author: Bjoern Anters <[email protected]>
Date:   Tue Jun 17 12:06:44 2025 +0200

    make debian build vars work

commit 91578d7
Author: Bjoern Anters <[email protected]>
Date:   Mon Jun 16 16:18:36 2025 +0200

    use ip route structured data to retrieve default interface

commit dbaeeda
Author: Bjoern Anters <[email protected]>
Date:   Mon Jun 16 16:17:49 2025 +0200

    use ip route structured data to retrieve default interface

commit 44207cd
Author: Bjoern Anters <[email protected]>
Date:   Mon Jun 16 16:15:33 2025 +0200

    use ip route structured data to retrieve default interface

commit c88a905
Author: Bjoern Anters <[email protected]>
Date:   Mon Jun 16 16:12:13 2025 +0200

    use ip route structured data to retrieve default interface

commit 6200d66
Author: Bjoern Anters <[email protected]>
Date:   Mon Jun 16 16:07:40 2025 +0200

    use ip route structured data to retrieve default interface

commit e547724
Author: Bjoern Anters <[email protected]>
Date:   Mon Jun 16 15:53:43 2025 +0200

    use ip route structured data to retrieve default interface

commit 9fb0150
Author: Bjoern Anters <[email protected]>
Date:   Tue Jun 10 17:23:45 2025 +0200

    use ip route structured data to retrieve default interface
@boring-cyborg
Copy link

boring-cyborg bot commented Oct 7, 2025

Congratulations on your first Pull Request and welcome to the Apache CloudStack community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/cloudstack/blob/main/CONTRIBUTING.md)
Here are some useful points:

@codecov
Copy link

codecov bot commented Oct 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 17.54%. Comparing base (4d46bec) to head (6b9c422).
⚠️ Report is 331 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main   #11800      +/-   ##
============================================
+ Coverage     16.57%   17.54%   +0.96%     
- Complexity    13987    15467    +1480     
============================================
  Files          5746     5897     +151     
  Lines        510860   527397   +16537     
  Branches      62140    64407    +2267     
============================================
+ Hits          84696    92516    +7820     
- Misses       416690   424486    +7796     
- Partials       9474    10395     +921     
Flag Coverage Δ
uitests 3.60% <ø> (-0.31%) ⬇️
unittests 18.60% <100.00%> (+1.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@weizhouapache
Copy link
Member

it worked on ubuntu 24

# /bin/ip -j a | /bin/jq -r '.[] | .addr_info | map(select(.local == "'`/bin/ip -j r s default | /bin/jq -r '.[0] | .prefsrc'`'")) | .[].label'
eth0

however, on RHEL and variants, the path of ip is not /bin/ip

# which ip
/usr/sbin/ip

@mosys0815
Copy link
Author

mosys0815 commented Oct 7, 2025

well, until now plain ip is used, i could replace is with just ip, or the script can get the path beforehand, what would you prefer?

@mosys0815
Copy link
Author

I changed the script to use short-paths for ipand jq, as like as it was before

@mosys0815 mosys0815 changed the title [10996] - use 'ip route' structured data to safely retrieve default route interface [10996] - use 'ip' structured data to safely retrieve default route interface Oct 8, 2025
@mosys0815
Copy link
Author

@weizhouapache anything missing still?

@daviftorres
Copy link
Contributor

daviftorres commented Nov 17, 2025

Dear @mosys0815,

I agree with the goal of finding a more reliable way to retrieve the interface, but I want to apologize in advance for my straightforward feedback on the proposed solutions:

The approach feels overly complex, which makes it hard to understand and, in my opinion, not an effective way to achieve the goal.

When I tried to run it, it didn’t work due to unescaped characters (as @weizhouapache also mentioned).

Were you able to run the command successfully? If so, could you share your environment or context?

I would prefer updating the PR to use the following instead:

ip route show default | awk '{print $5}'

The following one also accomplish the same goal but I would prefer to use the one above because does not require jq to be installed.

ip -j r s default | jq -r '.[0] | .dev'

Regards,

return defDev;
}
return Script.runSimpleBashScript("ip route show default 0.0.0.0/0 | head -1 | awk '{print $5}'");
return Script.runSimpleBashScript("ip -j a | jq -r '.[] | .addr_info | map(select(.local == \"'`ip -j r s default | jq -r '.[0] | .prefsrc'`'\")) | .[].label'");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Script.runSimpleBashScript("ip -j a | jq -r '.[] | .addr_info | map(select(.local == \"'`ip -j r s default | jq -r '.[0] | .prefsrc'`'\")) | .[].label'");
return Script.runSimpleBashScript("ip route show default | awk '{print $5}'");

My suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Script.runSimpleBashScript("ip -j a | jq -r '.[] | .addr_info | map(select(.local == \"'`ip -j r s default | jq -r '.[0] | .prefsrc'`'\")) | .[].label'");
return Script.runSimpleBashScript("ip route show default | awk '{print $5}'");

My suggestion.

agree with it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Script.runSimpleBashScript("ip -j a | jq -r '.[] | .addr_info | map(select(.local == \"'`ip -j r s default | jq -r '.[0] | .prefsrc'`'\")) | .[].label'");
return Script.runSimpleBashScript("ip route show default | awk '{print $5}'");

My suggestion.

agree with it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback @daviftorres :)

Using json to retrieve structured data makes plain text parsing unnecessary, it does not matter how the text output is formatted and how the content differs in the future.

Tbh i am not quite sure whether jq is available in a minimal installation, but i wouldn't see it as a big deal or security relevant dependency.

For testing purposes you can not just copy the script into your console. I had to escape and quote parts additionally because of the general quoting in Script.runSimpleBashScript. The End result, which you also can see in debug output of management server log looks like:

ip -j a | jq -r '.[] | .addr_info | map(select(.local == "'`ip -j r s default | jq -r '.[0] | .prefsrc'`'")) | .[].label'

To show you the output of your suggestion on our l3 network:

~$ ip route show default
default proto bird src 10.64.16.3 metric 32
	nexthop via inet6 fe80::9e5a:80ff:feaf:b108 dev eth1a weight 1
	nexthop via inet6 fe80::e65e:ccff:fe44:1a08 dev eth1b weight 1
default via 10.64.17.229 dev eth1b proto static src 10.64.17.230 metric 1024 onlink
default via 10.64.16.229 dev eth1a proto static src 10.64.16.230 metric 1024 onlink
~$ ip route show default | awk '{print $5}'
10.64.16.3
dev
dev
eth1b
eth1a

That is nothing different from where I come from, hence proposing my change.
Using json structured data and filtering for "label" in "prefsrc" always gives the correct default outgoing interface.

Your json variant

~$ ip -j r s default | jq -r '.[0] | .dev'
null

unfortunately does not return any interface at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants