コード例 #1
0
def kube_reset():
    lock_fd = _take_lock()
    if not lock_fd:
        log.log_error("Lock {} is active; Bail out".format(LOCK_FILE))
        return

    # Remove a key label and drain/delete self from cluster
    # If not, the next join would fail
    #
    if os.path.exists(KUBE_ADMIN_CONF):
        _label_node("enable_pods-")
        clicommon.run_command(
            "kubectl --kubeconfig {} --request-timeout 20s drain {} --ignore-daemonsets"
            .format(KUBE_ADMIN_CONF, device_info.get_hostname()),
            ignore_error=True)
        clicommon.run_command(
            "kubectl --kubeconfig {} --request-timeout 20s delete node {}".
            format(KUBE_ADMIN_CONF, device_info.get_hostname()),
            ignore_error=True)

    clicommon.run_command("kubeadm reset -f", ignore_error=True)
    clicommon.run_command("rm -rf /etc/cni/net.d")
    clicommon.run_command("rm -f {}".format(KUBE_ADMIN_CONF))
    clicommon.run_command("systemctl stop kubelet")
    clicommon.run_command("systemctl disable kubelet")
コード例 #2
0
def _do_join(server, port, insecure):
    KUBEADM_JOIN_CMD = "kubeadm join --discovery-file {} --node-name {}"
    err = ""
    out = ""
    try:
        ret = _download_file(server, port, insecure)
        log_debug("_download ret={}".format(ret))
        if ret == 0:
            _do_reset(True)
            _run_command("modprobe br_netfilter")
            # Copy flannel.conf
            _run_command("mkdir -p {}".format(CNI_DIR))
            _run_command("cp {} {}".format(FLANNEL_CONF_FILE, CNI_DIR))
            (ret, _, _) = _run_command("systemctl start kubelet")

        if ret == 0:
            (ret, out, err) = _run_command(KUBEADM_JOIN_CMD.format(
                KUBE_ADMIN_CONF, device_info.get_hostname()),
                                           timeout=60)
            log_debug("ret = {}".format(ret))

    except IOError as e:
        err = "Download failed: {}".format(str(e))
        ret = -1
        out = ""

    _troubleshoot_tips()

    if (ret != 0):
        log_error(err)

    return (ret, out, err)
コード例 #3
0
def _do_reset(pending_join=False):
    # Drain & delete self from cluster. If not, the next join would fail
    #
    if os.path.exists(KUBE_ADMIN_CONF):
        _run_command(
            "kubectl --kubeconfig {} --request-timeout 20s drain {} --ignore-daemonsets"
            .format(KUBE_ADMIN_CONF, device_info.get_hostname()))

        _run_command(
            "kubectl --kubeconfig {} --request-timeout 20s delete node {}".
            format(KUBE_ADMIN_CONF, device_info.get_hostname()))

    _run_command("kubeadm reset -f", 10)
    _run_command("rm -rf {}".format(CNI_DIR))
    if not pending_join:
        _run_command("rm -f {}".format(KUBE_ADMIN_CONF))
    _run_command("systemctl stop kubelet")
コード例 #4
0
def kube_write_labels(set_labels):
    """ Set given set_labels.
    """
    KUBECTL_SET_CMD = "kubectl --kubeconfig {} label --overwrite nodes {} {}"

    ret, node_labels = kube_read_labels()
    if ret != 0:
        log_debug("Read before set failed. Hence skipping set {}".format(
            str(set_labels)))
        return ret

    del_label_str = ""
    add_label_str = ""
    for (name, val) in set_labels.items():
        skip = False
        if name in node_labels:
            if val != node_labels[name]:
                # label value can't be modified. Remove it first
                # and then add
                del_label_str += "{}- ".format(name)
            else:
                # Already exists with same value.
                skip = True
        if not skip:
            # Add label
            add_label_str += "{}={} ".format(name, val)

    if add_label_str:
        # First remove if any
        if del_label_str:
            (ret, _, _) = _run_command(
                KUBECTL_SET_CMD.format(KUBE_ADMIN_CONF,
                                       device_info.get_hostname(),
                                       del_label_str.strip()))
        (ret, _, _) = _run_command(
            KUBECTL_SET_CMD.format(KUBE_ADMIN_CONF, device_info.get_hostname(),
                                   add_label_str.strip()))

        log_debug("{} kube labels {} ret={}".format(
            "Applied" if ret == 0 else "Failed to apply", add_label_str, ret))
    else:
        log_debug("Given labels are in sync with node labels. Hence no-op")

    return ret
コード例 #5
0
def kube_read_labels():
    """ Read current labels on node and return as dict. """
    KUBECTL_GET_CMD = "kubectl --kubeconfig {} get nodes --show-labels |\
 grep {} | tr -s ' ' | cut -f6 -d' '"

    labels = {}
    ret, out, _ = _run_command(
        KUBECTL_GET_CMD.format(KUBE_ADMIN_CONF, device_info.get_hostname()))

    if ret == 0:
        lst = out.split(",")

        for label in lst:
            tmp = label.split("=")
            labels[tmp[0]] = tmp[1]

    # log_debug("{} kube labels {} ret={}".format(
    # "Applied" if ret == 0 else "Failed to apply",
    # json.dumps(labels, indent=4), ret))

    return (ret, labels)
コード例 #6
0
def _do_join(server, insecure):
    try:
        _download_file(server, insecure)

        clicommon.run_command("systemctl enable kubelet")

        clicommon.run_command("modprobe br_netfilter")

        clicommon.run_command(KUBEADM_JOIN_CMD.format(
            KUBE_ADMIN_CONF, device_info.get_hostname()),
                              ignore_error=True)

        if _is_connected(server):
            labels = _get_labels()
            for label in labels:
                _label_node(label)

    except requests.exceptions.RequestException as e:
        clicommon.do_exit("Download failed: {}".format(str(e)))

    except OSError as e:
        clicommon.do_exit("Download failed: {}".format(str(e)))

    _troubleshoot_tips()
コード例 #7
0
ファイル: kube.py プロジェクト: zero804/sonic-utilities
def status():
    """Descibe this node"""
    run_kube_command("describe node {}".format(device_info.get_hostname()))
コード例 #8
0
ファイル: kube.py プロジェクト: zero804/sonic-utilities
def pods():
    """List all pods in this kubernetes cluster"""
    run_kube_command("get pods  --field-selector spec.nodeName={}".format(
        device_info.get_hostname()))
コード例 #9
0
def _label_node(label):
    cmd = "kubectl --kubeconfig {} label nodes {} {}".format(
        KUBE_ADMIN_CONF, device_info.get_hostname(), label)
    clicommon.run_command(cmd, ignore_error=True)
コード例 #10
0
def get_device_name():
    return str(device_info.get_hostname()).lower()