Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uncordon the node #80

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
47 changes: 47 additions & 0 deletions piqe_ocp_lib/api/resources/ocp_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,53 @@ def mark_node_unschedulable(self, node_name: str) -> Optional[ResourceInstance]:
logger.error("Exception encountered while marking node unschedulable: %s\n", e)
return api_response

def make_node_cordon(self, node_name: str) -> bool:
"""
Make node cordon and return whether it's cordon or not
:param node_name:
:return:
"""
api_response = None
body = {
"spec": {
"taints": [{"effect": "NoSchedule", "key": "node.kubernetes.io/unschedulable"}],
"unschedulable": True,
}
}
try:
api_response = self.ocp_nodes.patch(name=node_name, body=body)
logger.info("Node %s maked cordon" % node_name)
except ApiException as e:
logger.error("Exception encountered while making node cordon: %s\n", e)
if "'unschedulable': True" in str(api_response.spec):
made_cordon = True
else:
made_cordon = False
return made_cordon

def make_node_uncordon(self, node_name: str) -> bool:
"""
Make node uncordon and return whether it's uncordon or not
:param node_name:
:return:
"""
body = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's sufficient to just set 'unschedulable' to False. We should be removing the whole 'taints' element from 'spec'. We can do this by passing "op": "remove", "path": "....
Please refer to the following:
https://stackoverflow.com/questions/27439986/what-is-the-json-patch-format-to-remove-an-element-from-an-array

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, I do agree, thanks!

Copy link
Collaborator Author

@adtandale adtandale Feb 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this jsonpatch module needs to be installed explicitly using pip.

"spec": {
"taints": [{"effect": "NoSchedule", "key": "node.kubernetes.io/unschedulable"}],
"unschedulable": False,
}
}
try:
api_response = self.ocp_nodes.patch(name=node_name, body=body)
logger.info("Node %s maked uncordon" % node_name)
except ApiException as e:
logger.error("Exception encountered while making node uncordon: %s\n", e)
if "'unschedulable': True" not in str(api_response.spec):
made_uncordon = True
else:
made_uncordon = False
return made_uncordon

def are_all_nodes_ready(self) -> bool:
"""
Return the status of all node based on the condition type Ready.
Expand Down
16 changes: 16 additions & 0 deletions piqe_ocp_lib/tests/resources/test_ocp_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,22 @@ def test_mark_node_schedulable(self, setup_params):
api_response = node_api_obj.mark_node_schedulable(node_name=worker_node_name.metadata.name)
assert api_response.kind == "Node"

def test_make_node_cordon(self, setup_params):
"""
Check if we can make node cordon
and make the node uncordon again
:param setup_params:
:return:
"""
node_api_obj = setup_params["node_api_obj"]
worker_node_list = node_api_obj.get_worker_nodes()
for worker_node_name in worker_node_list.items:
made_cordon = node_api_obj.make_node_cordon(node_name=worker_node_name.metadata.name)
assert made_cordon is True
for worker_node_name in worker_node_list.items:
made_uncordon = node_api_obj.make_node_uncordon(node_name=worker_node_name.metadata.name)
assert made_uncordon is True

def test_are_all_nodes_ready(self, setup_params):
"""
Verify that all nodes status is Ready.
Expand Down