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

Add function to retrieve docker0 ipv4 and ipv6 addresses #15606

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/sonic-py-common/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
dependencies = [
'natsort==6.2.1', # 6.2.1 is the last version which supports Python 2
'pyyaml',
'netifaces>=0.10.7',
]

dependencies += sonic_dependencies
Expand Down
24 changes: 24 additions & 0 deletions src/sonic-py-common/sonic_py_common/multi_asic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import glob
import os
import subprocess
import netifaces

from natsort import natsorted
from swsscommon import swsscommon
Expand Down Expand Up @@ -484,3 +485,26 @@ def get_asic_presence_list():
# asic is asid id: asic0, asic1.... asicN. Get the numeric value.
asics_list.append(int(get_asic_id_from_name(asic)))
return asics_list

def get_docker0_ips():
"""
@summary: This function will return docker0 ipv4 and ipv6 addresses
for multi_asic platform.
This function will return None for single asic platform.
@return: tuple containing docker0 ipv4 and ipv6 address
"""
docker0_v4 = None
docker0_v6 = None

# return (None, None) for single asic platform
if not is_multi_asic():
Copy link
Collaborator

Choose a reason for hiding this comment

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

is_multi_asic

Is it harmful to let single-asic return the proper ips?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This helper function is added so that we could use this to get docker0 ip for multi-asic platform.
Most likely we will need docker0 information only for multi-asic platform.
For example, we want to update SNMP_AGENT_ADDRESS_CONFIG table in config_db with docker0 and mgmt IP in miniraph parser and we could avoid adding the single/multi-asic in minigraph parser if we ensure that required IP is passed from this helped function.

return (docker0_v4, docker0_v6)

interfaces = netifaces.interfaces()
if "docker0" in interfaces:
addresses = netifaces.ifaddresses("docker0")
if netifaces.AF_INET in addresses:
docker0_v4 = addresses[netifaces.AF_INET][0]['addr']
if netifaces.AF_INET6 in addresses:
docker0_v6 = addresses[netifaces.AF_INET6][0]['addr']
return (docker0_v4, docker0_v6)