Skip to content

Commit 224cc39

Browse files
authored
[CLI][Multi ASIC] update get_all_namespace to return current namespace (#5446)
1 parent 39edac5 commit 224cc39

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/sonic-py-common/sonic_py_common/multi_asic.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,49 @@ def get_asic_id_from_name(asic_name):
136136
raise ValueError('Unknown asic namespace name {}'.format(asic_name))
137137

138138

139+
def get_current_namespace():
140+
"""
141+
This API returns the network namespace in which it is
142+
invoked. In case of global namepace the API returns None
143+
"""
144+
145+
net_namespace = None
146+
command = ["/bin/ip netns identify", str(os.getpid())]
147+
proc = subprocess.Popen(command,
148+
stdout=subprocess.PIPE,
149+
shell=True,
150+
stderr=subprocess.STDOUT)
151+
try:
152+
stdout, stderr = proc.communicate()
153+
if proc.returncode != 0:
154+
raise RuntimeError(
155+
"Command {} failed with stderr {}".format(command, stderr)
156+
)
157+
if stdout.rstrip('\n') != "":
158+
net_namespace = stdout.rstrip('\n')
159+
except OSError as e:
160+
raise OSError("Error running command {}".format(command))
161+
162+
return net_namespace
163+
164+
139165
def get_namespaces_from_linux():
140166
"""
141167
In a multi asic platform, each ASIC is in a Linux Namespace.
142-
This method returns list of all the Namespace present on the device
168+
This method returns the asic namespace under which this is invoked,
169+
if namespace is None (global namespace) it returns list of all
170+
the Namespace present on the device
143171
144172
Note: It is preferable to use this function only when config_db is not
145173
available. When configdb is available use get_all_namespaces()
146174
147175
Returns:
148176
List of the namespaces present in the system
149177
"""
178+
current_ns = get_current_namespace()
179+
if current_ns:
180+
return [current_ns]
181+
150182
ns_list = []
151183
for path in glob.glob(NAMESPACE_PATH_GLOB):
152184
ns = os.path.basename(path)

0 commit comments

Comments
 (0)