本文整理汇总了Python中pycalico.datastore_datatypes.Endpoint.if_name方法的典型用法代码示例。如果您正苦于以下问题:Python Endpoint.if_name方法的具体用法?Python Endpoint.if_name怎么用?Python Endpoint.if_name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycalico.datastore_datatypes.Endpoint
的用法示例。
在下文中一共展示了Endpoint.if_name方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_up_endpoint
# 需要导入模块: from pycalico.datastore_datatypes import Endpoint [as 别名]
# 或者: from pycalico.datastore_datatypes.Endpoint import if_name [as 别名]
def set_up_endpoint(
ip, hostname, orchestrator_id, workload_id, cpid, next_hop_ips, veth_name=VETH_NAME, proc_alias=PROC_ALIAS, mac=None
):
"""
Set up an endpoint (veth) in the network namespace identified by the PID.
:param ip: The IP address to assign to the endpoint (veth) as Netaddr
IPAddress.
:param hostname: The host that this endpoint's workload resides on.
:param orchestrator_id: The orchestrator_id that this endpoint was created on.
:param workload_id: The workload_id that this endpoint resides on.
:param cpid: The PID of a process currently running in the namespace.
:param next_hop_ips: Dict of {version: IPAddress} for the next hops of the
default routes namespace, as opposed to the root namespace. If so, this
method also moves the other end of the veth into the root namespace.
:param veth_name: The name of the interface inside the container namespace,
e.g. eth1
:param proc_alias: The location of the /proc filesystem on the host.
:param mac: The interface MAC to use. Set to None to auto assign a MAC.
:return: An Endpoint describing the veth just created.
"""
assert isinstance(ip, IPAddress)
# Generate a new endpoint ID.
ep_id = uuid.uuid1().hex
iface = IF_PREFIX + ep_id[:11]
iface_tmp = "tmp" + ep_id[:11]
# Provision the networking. We create a temporary link from the proc
# alias to the /var/run/netns to provide a named namespace. If we don't
# do this, when run from the calico-node container the PID of the
# container process is not recognised by `ip link set <if> netns <pid>`
# command because that uses /proc rather than the proc alias to
# dereference the PID.
with NamedNamespace(cpid, proc=proc_alias) as ns:
# Create the veth pair and move one end into container:
check_call("ip link add %s type veth peer name %s" % (iface, iface_tmp), shell=True)
check_call("ip link set %s up" % iface, shell=True)
check_call("ip link set %s netns %s" % (iface_tmp, ns.name), shell=True)
_log.debug(check_output("ip link", shell=True))
if mac:
ns.check_call("ip link set dev %s name %s address %s" % (iface_tmp, veth_name, str(mac)), shell=True)
else:
ns.check_call("ip link set dev %s name %s" % (iface_tmp, veth_name), shell=True)
ns.check_call("ip link set %s up" % veth_name, shell=True)
# Add an IP address.
add_ip_to_interface(cpid, ip, veth_name, proc_alias=proc_alias)
with NamedNamespace(cpid, proc=proc_alias) as ns:
# Connected route to next hop & default route.
next_hop = next_hop_ips[ip.version]
ns.check_call(
"ip -%(version)s route replace"
" %(next_hop)s dev %(device)s" % {"version": ip.version, "device": veth_name, "next_hop": next_hop},
shell=True,
)
ns.check_call(
"ip -%(version)s route replace"
" default via %(next_hop)s dev %(device)s"
% {"version": ip.version, "device": veth_name, "next_hop": next_hop},
shell=True,
)
# Get the MAC address.
mac = ns.check_output("ip link show %s | grep ether | awk '{print $2}'" % (veth_name), shell=True).strip()
# Return an Endpoint.
network = IPNetwork(IPAddress(ip))
ep = Endpoint(
hostname=hostname,
orchestrator_id=orchestrator_id,
workload_id=workload_id,
endpoint_id=ep_id,
state="active",
mac=mac,
)
ep.if_name = veth_name
if network.version == 4:
ep.ipv4_nets.add(network)
ep.ipv4_gateway = next_hop
else:
ep.ipv6_nets.add(network)
ep.ipv6_gateway = next_hop
return ep