Skip to content
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

Handle IP to int conversion for inet + inet6 #1477

Merged
merged 5 commits into from
Jul 22, 2020
Merged

Handle IP to int conversion for inet + inet6 #1477

merged 5 commits into from
Jul 22, 2020

Conversation

cooperlees
Copy link
Contributor

  • IPv4 can have IPv6 next hops, so we could use the wrong .to_u* method
  • Lets try both before throwing on int conversions for sorting purposes

Addresses more for #1474

Signed-off-by: Cooper Lees me@cooperlees.com

- IPv4 can have IPv6 next hops, so we could use the wrong `.to_u*` method
- Lets try both before throwing on int conversions for sorting purposes

Addresses more for #1474

Signed-off-by: Cooper Lees <me@cooperlees.com>
@cooperlees cooperlees requested review from a team as code owners July 2, 2020 00:03
Signed-off-by: Cooper Lees <me@cooperlees.com>
@tas50
Copy link
Contributor

tas50 commented Jul 2, 2020

Can you add a unit test for this @cooperlees

Comment on lines 30 to 33
ipaddress.to_u32
rescue NoMethodError
ipaddress.to_u128
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Cathing NoMethodError leads to all sorts of fun bugs. Instead try:

ipaddress.respond_to?(:to_u32) ? ipaddress.to_u32 : ipaddress.to_u128

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, why are we doing this at all? .to_i method does the right thing in all cases:

irb(main):003:0> x = IPAddress('10.1.1.1')
=> #<IPAddress::IPv4:0x0000000002aa1930 @address="10.1.1.1", @prefix=32, @octets=[10, 1, 1, 1], @u32=167837953>
irb(main):004:0> x.ipv4?
=> true
irb(main):005:0> x.to_u32
=> 167837953
irb(main):006:0> x.to_i
=> 167837953
irb(main):007:0> x = IPAddress('fe80::96c6:91ff:fea0:7a03')
=> #<IPAddress::IPv6:0x00000000024ad678 @groups=[65152, 0, 0, 0, 38598, 37375, 65184, 31235], @address="fe80:0000:0000:0000:96c6:91ff:fea0:7a03", @compressed="fe80::96c6:91ff:fea0:7a03", @prefix=128>
irb(main):008:0> x.to_i
=> 338288524927261089664883428521100212739
irb(main):009:0> x.to_u128
=> 338288524927261089664883428521100212739

@@ -25,6 +25,13 @@

depends "network/interfaces"

# Try to u32 an IPv4 and fallback to u128 if it fails before throwing
def int_an_ip(ipaddress)
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe ip_to_int?

Comment on lines 30 to 33
ipaddress.to_u32
rescue NoMethodError
ipaddress.to_u128
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, why are we doing this at all? .to_i method does the right thing in all cases:

irb(main):003:0> x = IPAddress('10.1.1.1')
=> #<IPAddress::IPv4:0x0000000002aa1930 @address="10.1.1.1", @prefix=32, @octets=[10, 1, 1, 1], @u32=167837953>
irb(main):004:0> x.ipv4?
=> true
irb(main):005:0> x.to_u32
=> 167837953
irb(main):006:0> x.to_i
=> 167837953
irb(main):007:0> x = IPAddress('fe80::96c6:91ff:fea0:7a03')
=> #<IPAddress::IPv6:0x00000000024ad678 @groups=[65152, 0, 0, 0, 38598, 37375, 65184, 31235], @address="fe80:0000:0000:0000:96c6:91ff:fea0:7a03", @compressed="fe80::96c6:91ff:fea0:7a03", @prefix=128>
irb(main):008:0> x.to_i
=> 338288524927261089664883428521100212739
irb(main):009:0> x.to_u128
=> 338288524927261089664883428521100212739

@cooperlees
Copy link
Contributor Author

I wondered the same thing, but am more focused on removing IPv4 Link Local from FB's infra and this is holding me up :)

Can I get support to do the renames, smarter exception handling + add a unit test and we file an issue to follow up and look into this and evaluate its importance?

Signed-off-by: Cooper Lees <me@cooperlees.com>
@cooperlees
Copy link
Contributor Author

cooperlees commented Jul 2, 2020

So I was able to clean up the code back to a one liner, but I don't see a direct test today for sorted_ips. Due to this can I please ask for some tips on where and how you'd like this tested, since it was not directly tested BC (Before Cooper), as it already is indirectly.

cooper-mbp1:ohai cooper$ grep -R sorted_ips lib/ohai/plugins/*
lib/ohai/plugins/network.rb:  def sorted_ips(family = "inet")
lib/ohai/plugins/network.rb:    ips = sorted_ips(family)

Signed-off-by: Cooper Lees <me@cooperlees.com>
@@ -54,7 +54,7 @@ def sorted_ips(family = "inet")
ipaddresses.sort_by do |v|
[ ( scope_prio.index(v[:scope]) || 999999 ),
128 - v[:ipaddress].prefix.to_i,
( family == "inet" ? v[:ipaddress].to_u32 : v[:ipaddress].to_u128 ),
v[:ipaddress].respond_to?(:to_u32) ? v[:ipaddress].to_u32 : v[:ipaddress].to_u128,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I can't see any reason to just just make this:

Suggested change
v[:ipaddress].respond_to?(:to_u32) ? v[:ipaddress].to_u32 : v[:ipaddress].to_u128,
v[:ipaddress].to_i

@lamont / @tas50 am I missing something?

Signed-off-by: Cooper Lees <me@cooperlees.com>
Copy link
Collaborator

@jaymzh jaymzh left a comment

Choose a reason for hiding this comment

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

LGTM - but @lamont-granquist or @tas50 should ack this.

@cooperlees
Copy link
Contributor Author

cooperlees commented Jul 22, 2020

FWIW - this has been backported and is in production @ Facebook. (note, there are not many boxes with IPv4 inet6 routes, but there will be soon).

@tas50 tas50 merged commit 3b7a989 into chef:master Jul 22, 2020
@tas50
Copy link
Contributor

tas50 commented Jul 22, 2020

I forgot about this one. Sorry for the delay

@cooperlees cooperlees deleted the handle_both_uints branch July 22, 2020 20:07
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.

None yet

3 participants