K8S handles hostname in lower case (#7694)

Why I did it
k8s handles in lower case, so the code ensures that it uses hostname in all lower case

How I did it
Wrapper for device_info.get_hostname that returns in lower case. This wrapper is used in all places that require hostname to use in kubectl commands.

How to verify it
Device joins successfully.
This commit is contained in:
Renuka Manavalan 2021-05-26 09:17:48 -07:00 committed by Ying Xie
parent c5d27750f2
commit cd7d04d08a
2 changed files with 27 additions and 27 deletions

View File

@ -25,9 +25,6 @@ LOCK_FILE = "/var/lock/kube_join.lock"
FLANNEL_CONF_FILE = "/usr/share/sonic/templates/kube_cni.10-flannel.conflist"
CNI_DIR = "/etc/cni/net.d"
# kubectl --kubeconfig <KUBE_ADMIN_CONF> label nodes
# <device_info.get_hostname()> <label to be added>
def log_debug(m):
msg = "{}: {}".format(inspect.stack()[1][3], m)
print(msg)
@ -49,6 +46,9 @@ def to_str(s):
return str(s)
def get_device_name():
return str(device_info.get_hostname()).lower()
def _run_command(cmd, timeout=5):
""" Run shell command and return exit code, along with stdout. """
@ -82,7 +82,7 @@ def kube_read_labels():
labels = {}
ret, out, _ = _run_command(KUBECTL_GET_CMD.format(
KUBE_ADMIN_CONF, device_info.get_hostname()))
KUBE_ADMIN_CONF, get_device_name()))
if ret == 0:
lst = out.split(",")
@ -130,9 +130,9 @@ def kube_write_labels(set_labels):
# 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()))
KUBE_ADMIN_CONF, get_device_name(), del_label_str.strip()))
(ret, _, _) = _run_command(KUBECTL_SET_CMD.format(
KUBE_ADMIN_CONF, device_info.get_hostname(), add_label_str.strip()))
KUBE_ADMIN_CONF, get_device_name(), add_label_str.strip()))
log_debug("{} kube labels {} ret={}".format(
"Applied" if ret == 0 else "Failed to apply", add_label_str, ret))
@ -251,10 +251,10 @@ def _do_reset(pending_join = False):
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()))
format(KUBE_ADMIN_CONF, get_device_name()))
_run_command("kubectl --kubeconfig {} --request-timeout 20s delete node {}".
format(KUBE_ADMIN_CONF, device_info.get_hostname()))
format(KUBE_ADMIN_CONF, get_device_name()))
_run_command("kubeadm reset -f", 10)
_run_command("rm -rf {}".format(CNI_DIR))
@ -279,7 +279,7 @@ def _do_join(server, port, insecure):
if ret == 0:
(ret, out, err) = _run_command(KUBEADM_JOIN_CMD.format(
KUBE_ADMIN_CONF, device_info.get_hostname()), timeout=60)
KUBE_ADMIN_CONF, get_device_name()), timeout=60)
log_debug("ret = {}".format(ret))
except IOError as e:

View File

@ -26,7 +26,7 @@ read_labels_test_data = {
common_test.RETVAL: 0,
common_test.PROC_CMD: ["\
kubectl --kubeconfig {} get nodes --show-labels |\
grep None | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF)],
grep none | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF)],
common_test.PROC_OUT: ["foo=bar,hello=world"],
common_test.POST: {
"foo": "bar",
@ -40,7 +40,7 @@ kubectl --kubeconfig {} get nodes --show-labels |\
common_test.RETVAL: -1,
common_test.PROC_CMD: ["\
kubectl --kubeconfig {} get nodes --show-labels |\
grep None | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF)],
grep none | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF)],
common_test.POST: {
},
common_test.PROC_KILLED: 1
@ -50,7 +50,7 @@ kubectl --kubeconfig {} get nodes --show-labels |\
common_test.RETVAL: -1,
common_test.PROC_CMD: ["\
kubectl --kubeconfig {} get nodes --show-labels |\
grep None | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF)],
grep none | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF)],
common_test.PROC_OUT: [""],
common_test.PROC_ERR: ["command failed"],
common_test.POST: {
@ -66,10 +66,10 @@ write_labels_test_data = {
common_test.ARGS: { "foo": "bar", "hello": "World!", "test": "ok" },
common_test.PROC_CMD: [
"kubectl --kubeconfig {} get nodes --show-labels |\
grep None | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF),
"kubectl --kubeconfig {} label --overwrite nodes None hello-".format(
grep none | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF),
"kubectl --kubeconfig {} label --overwrite nodes none hello-".format(
KUBE_ADMIN_CONF),
"kubectl --kubeconfig {} label --overwrite nodes None hello=World! test=ok".format(
"kubectl --kubeconfig {} label --overwrite nodes none hello=World! test=ok".format(
KUBE_ADMIN_CONF)
],
common_test.PROC_OUT: ["foo=bar,hello=world", "", ""]
@ -80,7 +80,7 @@ write_labels_test_data = {
common_test.ARGS: { "foo": "bar", "hello": "world" },
common_test.PROC_CMD: [
"kubectl --kubeconfig {} get nodes --show-labels |\
grep None | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF)
grep none | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF)
],
common_test.PROC_OUT: ["foo=bar,hello=world"]
},
@ -91,7 +91,7 @@ write_labels_test_data = {
common_test.RETVAL: -1,
common_test.PROC_CMD: [
"kubectl --kubeconfig {} get nodes --show-labels |\
grep None | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF)
grep none | tr -s ' ' | cut -f6 -d' '".format(KUBE_ADMIN_CONF)
],
common_test.PROC_ERR: ["read failed"]
}
@ -103,10 +103,10 @@ join_test_data = {
common_test.RETVAL: 0,
common_test.ARGS: ["10.3.157.24", 6443, True, False],
common_test.PROC_CMD: [
"kubectl --kubeconfig {} --request-timeout 20s drain None \
"kubectl --kubeconfig {} --request-timeout 20s drain none \
--ignore-daemonsets".format(KUBE_ADMIN_CONF),
"kubectl --kubeconfig {} --request-timeout 20s delete node \
None".format(KUBE_ADMIN_CONF),
none".format(KUBE_ADMIN_CONF),
"kubeadm reset -f",
"rm -rf {}".format(CNI_DIR),
"systemctl stop kubelet",
@ -114,7 +114,7 @@ None".format(KUBE_ADMIN_CONF),
"mkdir -p {}".format(CNI_DIR),
"cp {} {}".format(FLANNEL_CONF_FILE, CNI_DIR),
"systemctl start kubelet",
"kubeadm join --discovery-file {} --node-name None".format(
"kubeadm join --discovery-file {} --node-name none".format(
KUBE_ADMIN_CONF)
],
common_test.PROC_RUN: [True, True]
@ -124,10 +124,10 @@ None".format(KUBE_ADMIN_CONF),
common_test.RETVAL: 0,
common_test.ARGS: ["10.3.157.24", 6443, False, False],
common_test.PROC_CMD: [
"kubectl --kubeconfig {} --request-timeout 20s drain None \
"kubectl --kubeconfig {} --request-timeout 20s drain none \
--ignore-daemonsets".format(KUBE_ADMIN_CONF),
"kubectl --kubeconfig {} --request-timeout 20s delete node \
None".format(KUBE_ADMIN_CONF),
none".format(KUBE_ADMIN_CONF),
"kubeadm reset -f",
"rm -rf {}".format(CNI_DIR),
"systemctl stop kubelet",
@ -135,7 +135,7 @@ None".format(KUBE_ADMIN_CONF),
"mkdir -p {}".format(CNI_DIR),
"cp {} {}".format(FLANNEL_CONF_FILE, CNI_DIR),
"systemctl start kubelet",
"kubeadm join --discovery-file {} --node-name None".format(
"kubeadm join --discovery-file {} --node-name none".format(
KUBE_ADMIN_CONF)
],
common_test.PROC_RUN: [True, True]
@ -165,10 +165,10 @@ reset_test_data = {
common_test.DO_JOIN: True,
common_test.ARGS: [False],
common_test.PROC_CMD: [
"kubectl --kubeconfig {} --request-timeout 20s drain None \
"kubectl --kubeconfig {} --request-timeout 20s drain none \
--ignore-daemonsets".format(KUBE_ADMIN_CONF),
"kubectl --kubeconfig {} --request-timeout 20s delete node \
None".format(KUBE_ADMIN_CONF),
none".format(KUBE_ADMIN_CONF),
"kubeadm reset -f",
"rm -rf {}".format(CNI_DIR),
"rm -f {}".format(KUBE_ADMIN_CONF),
@ -180,10 +180,10 @@ None".format(KUBE_ADMIN_CONF),
common_test.RETVAL: 0,
common_test.ARGS: [False],
common_test.PROC_CMD: [
"kubectl --kubeconfig {} --request-timeout 20s drain None \
"kubectl --kubeconfig {} --request-timeout 20s drain none \
--ignore-daemonsets".format(KUBE_ADMIN_CONF),
"kubectl --kubeconfig {} --request-timeout 20s delete node \
None".format(KUBE_ADMIN_CONF),
none".format(KUBE_ADMIN_CONF),
"kubeadm reset -f",
"rm -rf {}".format(CNI_DIR),
"rm -f {}".format(KUBE_ADMIN_CONF),