예제 #1
0
def setup(pod_namespace, pod_name, docker_id, retry=True):
    """
    project: pod_namespace
    network: pod_name
    netns: docker_id{12}
    """

    # Kubelet::createPodInfraContainer ensures that State.Pid is set
    pid = docker_get_pid(docker_id)
    if pid == 0:
        raise Exception('Unable to read State.Pid')

    short_id = docker_id[0:12]

    if not os.path.exists('/var/run/netns'):
        os.mkdir('/var/run/netns')

    Shell.run('ln -sf /proc/%d/ns/net /var/run/netns/%s' % (pid, short_id))

    manager = LxcManager()

    if opt_net_mode == 'none':
        instance_ifname = 'veth0'
    else:
        instance_ifname = 'eth0'

    uid, podName = getDockerPod(docker_id)
    podInfo = None
    podState = None
    for i in range(0, 30):
        podInfo = getPodInfo(pod_namespace, podName)
        if podInfo is None:
            return False
        if 'hostNetwork' in podInfo['spec'] and \
           podInfo['spec']['hostNetwork']:
            return True
        if 'annotations' in podInfo["metadata"] and \
           'opencontrail.org/pod-state' in podInfo["metadata"]["annotations"]:
            podState = json.loads(podInfo["metadata"]["annotations"]
                                  ["opencontrail.org/pod-state"])
            break
        if not retry:
            return False
        time.sleep(1)

    # The lxc_manager uses the mac_address to setup the container interface.
    # Additionally the ip-address, prefixlen and gateway are also used.
    if podState is None:
        logging.error('No annotations in pod %s', podInfo["metadata"]["name"])
        return False

    nic_uuid = podState["uuid"]
    mac_address = podState["macAddress"]
    if opt_net_mode == 'none':
        ifname = manager.create_interface(short_id, instance_ifname,
                                          mac_address)
    else:
        ifname = manager.move_interface(short_id, pid, mac_address)

    api = ContrailVRouterApi()
    api.add_port(uid,
                 nic_uuid,
                 ifname,
                 mac_address,
                 port_type='NovaVMPort',
                 display_name=podName,
                 hostname=podName + '.' + pod_namespace)

    ip_address = podState["ipAddress"]
    gateway = podState["gateway"]
    Shell.run('ip netns exec %s ip addr add %s/32 peer %s dev %s' %
              (short_id, ip_address, gateway, instance_ifname))
    Shell.run('ip netns exec %s ip route add default via %s' %
              (short_id, gateway))
    Shell.run('ip netns exec %s ip link set %s up' %
              (short_id, instance_ifname))
    return True
예제 #2
0
def setup(pod_namespace, pod_name, docker_id):
    """
    project: pod_namespace
    network: pod_name
    netns: docker_id{12}
    """
    client = ContrailClient()

    # Kubelet::createPodInfraContainer ensures that State.Pid is set
    pid = docker_get_pid(docker_id)
    if pid == 0:
        raise Exception('Unable to read State.Pid')

    short_id = docker_id[0:11]

    if not os.path.exists('/var/run/netns'):
        os.mkdir('/var/run/netns')

    Shell.run('ln -sf /proc/%d/ns/net /var/run/netns/%s' % (pid, short_id))

    manager = LxcManager()

    if client._net_mode == 'none':
        instance_ifname = 'veth0'
    else:
        instance_ifname = 'eth0'

    uid, podName = getDockerPod(docker_id)

    podInfo = None
    for i in range(0, 120):
        podInfo = getPodInfo(podName)
        if 'annotations' in podInfo["metadata"] and \
           'nic_uuid' in podInfo["metadata"]["annotations"]:
            break
        time.sleep(1)
    
    # The lxc_manager uses the mac_address to setup the container interface.
    # Additionally the ip-address, prefixlen and gateway are also used.
    if not 'annotations' in podInfo["metadata"] or not 'nic_uuid' in podInfo["metadata"]["annotations"]:
        logging.error('No annotations in pod %s', podInfo["metadata"]["name"])
        sys.exit(1)


    podAnnotations = podInfo["metadata"]["annotations"]
    nic_uuid = podAnnotations["nic_uuid"]
    mac_address = podAnnotations["mac_address"]
    if client._net_mode == 'none':
        ifname = manager.create_interface(short_id, instance_ifname,
                                          mac_address)
    else:
        ifname = manager.move_interface(short_id, pid, instance_ifname,
                                        mac_address)

    api = ContrailVRouterApi()
    api.add_port(uid, nic_uuid, ifname, mac_address,
                 port_type='NovaVMPort',
                 display_name=podName,
                 hostname=podName+'.'+pod_namespace)

    ip_address = podAnnotations["ip_address"]
    gateway = podAnnotations["gateway"]
    Shell.run('ip netns exec %s ip addr add %s/32 peer %s dev %s' % \
              (short_id, ip_address, gateway, instance_ifname))
    Shell.run('ip netns exec %s ip route add default via %s' % \
              (short_id, gateway))
    Shell.run('ip netns exec %s ip link set %s up' %
              (short_id, instance_ifname))
예제 #3
0
def setup(pod_namespace, pod_name, docker_id, retry=True):
    """
    project: pod_namespace
    network: pod_name
    netns: docker_id{12}
    """

    # Kubelet::createPodInfraContainer ensures that State.Pid is set
    pid = docker_get_pid(docker_id)
    if pid == 0:
        raise Exception('Unable to read State.Pid')

    short_id = docker_id[0:12]

    if not os.path.exists('/var/run/netns'):
        os.mkdir('/var/run/netns')

    Shell.run('ln -sf /proc/%d/ns/net /var/run/netns/%s' % (pid, short_id))

    manager = LxcManager()

    if opt_net_mode == 'none':
        instance_ifname = 'veth0'
    else:
        instance_ifname = 'eth0'

    uid, podName = getDockerPod(docker_id)
    podInfo = None
    podState = None
    for i in range(0, 30):
        podInfo = getPodInfo(pod_namespace, podName)
        if podInfo is None:
            return False
        if 'hostNetwork' in podInfo['spec'] and \
           podInfo['spec']['hostNetwork']:
            return True
        if 'annotations' in podInfo["metadata"] and \
           'opencontrail.org/pod-state' in podInfo["metadata"]["annotations"]:
            podState = json.loads(
                podInfo["metadata"]["annotations"]
                ["opencontrail.org/pod-state"])
            break
        if not retry:
            return False
        time.sleep(1)

    # The lxc_manager uses the mac_address to setup the container interface.
    # Additionally the ip-address, prefixlen and gateway are also used.
    if podState is None:
        logging.error('No annotations in pod %s', podInfo["metadata"]["name"])
        return False

    nic_uuid = podState["uuid"]
    mac_address = podState["macAddress"]
    if opt_net_mode == 'none':
        ifname = manager.create_interface(short_id, instance_ifname,
                                          mac_address)
    else:
        ifname = manager.move_interface(short_id, pid, mac_address)

    api = ContrailVRouterApi()
    api.add_port(uid, nic_uuid, ifname, mac_address,
                 port_type='NovaVMPort',
                 display_name=podName,
                 hostname=podName+'.'+pod_namespace)

    ip_address = podState["ipAddress"]
    gateway = podState["gateway"]
    Shell.run('ip netns exec %s ip addr add %s/32 peer %s dev %s' %
              (short_id, ip_address, gateway, instance_ifname))
    Shell.run('ip netns exec %s ip route add default via %s' %
              (short_id, gateway))
    Shell.run('ip netns exec %s ip link set %s up' %
              (short_id, instance_ifname))
    return True
예제 #4
0
def setup(pod_namespace, pod_name, docker_id):
    """
    project: pod_namespace
    network: pod_name
    netns: docker_id{12}
    """
    client = ContrailClient()

    # Kubelet::createPodInfraContainer ensures that State.Pid is set
    pid = docker_get_pid(docker_id)
    if pid == 0:
        raise Exception('Unable to read State.Pid')

    short_id = docker_id[0:11]

    if not os.path.exists('/var/run/netns'):
        os.mkdir('/var/run/netns')

    Shell.run('ln -sf /proc/%d/ns/net /var/run/netns/%s' % (pid, short_id))

    manager = LxcManager()

    if client._net_mode == 'none':
        instance_ifname = 'veth0'
    else:
        instance_ifname = 'eth0'

    uid, podName = getDockerPod(docker_id)
    podInfo = None
    for i in range(0, 120):
        podInfo = getPodInfo(pod_namespace, podName)
        if 'annotations' in podInfo["metadata"] and \
           'nic_uuid' in podInfo["metadata"]["annotations"]:
            break
        time.sleep(1)
    
    # The lxc_manager uses the mac_address to setup the container interface.
    # Additionally the ip-address, prefixlen and gateway are also used.
    if not 'annotations' in podInfo["metadata"] or not 'nic_uuid' in podInfo["metadata"]["annotations"]:
        logging.error('No annotations in pod %s', podInfo["metadata"]["name"])
        sys.exit(1)


    podAnnotations = podInfo["metadata"]["annotations"]
    nic_uuid = podAnnotations["nic_uuid"]
    mac_address = podAnnotations["mac_address"]
    if client._net_mode == 'none':
        ifname = manager.create_interface(short_id, instance_ifname,
                                          mac_address)
    else:
        ifname = manager.move_interface(short_id, pid, instance_ifname,
                                        mac_address)

    api = ContrailVRouterApi()
    api.add_port(uid, nic_uuid, ifname, mac_address,
                 port_type='NovaVMPort',
                 display_name=podName,
                 hostname=podName+'.'+pod_namespace)

    ip_address = podAnnotations["ip_address"]
    gateway = podAnnotations["gateway"]
    Shell.run('ip netns exec %s ip addr add %s/32 peer %s dev %s' % \
              (short_id, ip_address, gateway, instance_ifname))
    Shell.run('ip netns exec %s ip route add default via %s' % \
              (short_id, gateway))
    Shell.run('ip netns exec %s ip link set %s up' %
              (short_id, instance_ifname))
    # TX checksum is broken on Fedora 21 testbed.
    # This may be an issue with kernel 3.17 or veth-pair code.
    if platform.linux_distribution()[0:2] == ('Fedora', '21'):
        Shell.run('nsenter -n -t %d ethtool -K %s tx off' % (pid, instance_ifname))