Skip to content

Commit

Permalink
Kubernetes server configurable using URL
Browse files Browse the repository at this point in the history
1) Dropped non-required IP update in admin.conf, as all masters use VIP only (sonic-net#7288)
2) Don't clear VERSION during stop, as it would overwrite new version pending to go.
3) subprocess, get return value from proc and do not imply with presence of data in stderr.
  • Loading branch information
renukamanavalan authored and Carl Keene committed Aug 7, 2021
1 parent b829347 commit e6a813e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 88 deletions.
1 change: 0 additions & 1 deletion src/sonic-ctrmgrd/ctrmgr/container
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ def container_stop(feature, **kwargs):
CURRENT_OWNER: "none",
UPD_TIMESTAMP: str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")),
CONTAINER_ID: "",
VERSION: "",
SYSTEM_STATE: "down"
}
if remote_state == "running":
Expand Down
40 changes: 11 additions & 29 deletions src/sonic-ctrmgrd/ctrmgr/kube_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ def _run_command(cmd, timeout=5):
(o, e) = proc.communicate(timeout)
output = to_str(o)
err = to_str(e)
if err:
ret = -1
else:
ret = proc.returncode
ret = proc.returncode
except subprocess.TimeoutExpired as error:
proc.kill()
output = ""
Expand Down Expand Up @@ -207,25 +204,11 @@ def _download_file(server, port, insecure):
data = r.read()
os.write(h, data)
os.close(h)
log_debug("Downloaded = {}".format(fname))

# Ensure the admin.conf has given VIP as server-IP.
update_file = "{}.upd".format(fname)
cmd = r'sed "s/server:.*:{}/server: https:\/\/{}:{}/" {} > {}'.format(
str(port), server, str(port), fname, update_file)
(ret, _, err) = _run_command(cmd)

log_debug("sed command: ret={}".format(ret))
if ret != 0:
log_error("sed update of downloaded file failed with ret={}".
format(ret))
log_debug("sed command failed: ret={}".format(ret))
return ret

shutil.copyfile(update_file, KUBE_ADMIN_CONF)
shutil.copyfile(fname, KUBE_ADMIN_CONF)

_run_command("rm -f {} {}".format(fname, update_file))
log_debug("{} downloaded".format(KUBE_ADMIN_CONF))
return ret


def _troubleshoot_tips():
Expand Down Expand Up @@ -284,16 +267,15 @@ def _do_join(server, port, insecure):
KUBEADM_JOIN_CMD = "kubeadm join --discovery-file {} --node-name {}"
err = ""
out = ""
ret = 0
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")
_download_file(server, port, insecure)
_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(
Expand Down
9 changes: 8 additions & 1 deletion src/sonic-ctrmgrd/tests/common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
DO_JOIN = "do_join"

# subproc key words

# List all subprocess commands expected within the test.
# Each call to subproc-side effect (mock_subproc_side_effect) increment index
# Other key words influence how this proc command to be processed
# PROC_RUN having true at that index, implies run it instead of mocking it
# PROC_OUT, ERR, FAIL THROW provide data on how to mock
#
PROC_CMD = "subproc_cmd"
PROC_RUN = "skip_mock"
PROC_FAIL = "proc_fail"
Expand Down Expand Up @@ -606,7 +613,7 @@ def communicate(self, timeout):
err = err_lst[self.index]
else:
err = ""
self.returncode = 0
self.returncode = 0 if not err else -1
return (out, err)

def kill(self):
Expand Down
10 changes: 6 additions & 4 deletions src/sonic-ctrmgrd/tests/container_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@
"remote_state": "none",
"system_state": "up",
"current_owner": "local",
"container_id": "snmp"
"container_id": "snmp",
"container_version": "20201230.0.15"
}
}
}
Expand All @@ -192,7 +193,7 @@
"system_state": "down",
"current_owner": "none",
"container_id": "",
"container_version": ""
"container_version": "20201230.0.15"
}
},
common_test.KUBE_LABEL_TABLE: {
Expand Down Expand Up @@ -222,7 +223,8 @@
"container_id": "xxx",
"system_state": "up",
"current_owner": "kube",
"remote_state": "running"
"remote_state": "running",
"container_version": "20201230.1.15"
}
}
}
Expand All @@ -235,7 +237,7 @@
"system_state": "down",
"current_owner": "none",
"container_id": "",
"container_version": ""
"container_version": "20201230.1.15"
}
},
common_test.KUBE_LABEL_TABLE: {
Expand Down
53 changes: 0 additions & 53 deletions src/sonic-ctrmgrd/tests/kube_commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@
common_test.RETVAL: 0,
common_test.ARGS: ["10.3.157.24", 6443, True, False],
common_test.PROC_CMD: [
'sed *',
'rm -f *',
"kubectl --kubeconfig {} --request-timeout 20s drain None \
--ignore-daemonsets".format(KUBE_ADMIN_CONF),
"kubectl --kubeconfig {} --request-timeout 20s delete node \
Expand All @@ -126,8 +124,6 @@
common_test.RETVAL: 0,
common_test.ARGS: ["10.3.157.24", 6443, False, False],
common_test.PROC_CMD: [
'sed *',
'rm -f *',
"kubectl --kubeconfig {} --request-timeout 20s drain None \
--ignore-daemonsets".format(KUBE_ADMIN_CONF),
"kubectl --kubeconfig {} --request-timeout 20s delete node \
Expand All @@ -154,55 +150,6 @@
]
},
3: {
common_test.DESCR: "Regular join: fail file update",
common_test.RETVAL: -1,
common_test.ARGS: ["10.3.157.24", 6443, False, False],
common_test.PROC_CMD: [
'sed *',
'rm -f *',
"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),
"kubeadm reset -f",
"rm -rf {}".format(CNI_DIR),
"systemctl stop kubelet",
"modprobe br_netfilter",
"mkdir -p {}".format(CNI_DIR),
"cp {} {}".format(FLANNEL_CONF_FILE, CNI_DIR),
"systemctl start kubelet",
"kubeadm join --discovery-file {} --node-name None".format(
KUBE_ADMIN_CONF)
],
common_test.PROC_RUN: [True, True],
common_test.PROC_FAIL: [True]
},
4: {
common_test.DESCR: "Regular join: fail file update",
common_test.RETVAL: -1,
common_test.ARGS: ["10.3.157.24", 6443, False, False],
common_test.PROC_CMD: [
'sed *',
'rm -f *',
"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),
"kubeadm reset -f",
"rm -rf {}".format(CNI_DIR),
"systemctl stop kubelet",
"modprobe br_netfilter",
"mkdir -p {}".format(CNI_DIR),
"cp {} {}".format(FLANNEL_CONF_FILE, CNI_DIR),
"systemctl start kubelet",
"kubeadm join --discovery-file {} --node-name None".format(
KUBE_ADMIN_CONF)
],
common_test.PROC_RUN: [True, True],
common_test.PROC_FAIL: [True],
common_test.PROC_THROW: [True]
},
5: {
common_test.DESCR: "Regular join: fail due to unable to lock",
common_test.RETVAL: -1,
common_test.ARGS: ["10.3.157.24", 6443, False, False],
Expand Down

0 comments on commit e6a813e

Please sign in to comment.