forked from vyos/vyos-1x
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.py
More file actions
57 lines (51 loc) · 2.41 KB
/
Copy pathcontainer.py
File metadata and controls
57 lines (51 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from hashlib import sha256
from vyos.config import Config
from vyos.ifconfig import Interface
from vyos.utils.dict import dict_search
from vyos.utils.network import interface_exists
def get_container_host_ifname(name: str) -> str:
"""
Deterministic host-side veth interface name for a container's network
attachment (verify() only allows one network per container). Kept within
IFNAMSIZ and - thanks to the leading "veth-" (a hyphen can never appear in
a VyOS "vethN" interface name) - guaranteed to never collide with the
"virtual-ethernet" naming scheme..
"""
prefix = f'veth-{name}'
if len(prefix) <= 15:
return prefix
digest = sha256(name.encode()).hexdigest()[:4]
return f'veth-{name[:5]}-{digest}'
def restart_network(config: Config) -> None:
"""
Start network and assign it to given VRF if requested.
This can only be done after the containers got started as the podman network
interface will only be enabled by the first container and yet I do not know
how to enable the network interface in advance.
"""
if 'network' in config:
for network, network_config in config['network'].items():
type_config = dict_search('type', network_config)
if not dict_search('macvlan', type_config):
network_name = f'pod-{network}'
# T5147: Networks are started only as soon as there is a consumer.
# If only a network is created in the first place, no need to assign
# it to a VRF as there's no consumer, yet.
if interface_exists(network_name):
tmp = Interface(network_name)
tmp.set_vrf(network_config.get('vrf', ''))
tmp.add_ipv6_eui64_address('fe80::/64')
return None