-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[10996] - use 'ip' structured data to safely retrieve default route interface #11800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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
|
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)
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
it worked on ubuntu 24 however, on RHEL and variants, the path of |
|
well, until now plain |
|
I changed the script to use short-paths for |
|
@weizhouapache anything missing still? |
|
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: The following one also accomplish the same goal but I would prefer to use the one above because does not require 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'"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Using ip route structured data allows to query safely the default route interface, no matter the output of the standard 'ip route' output.