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

Implement cordon/uncordon for Node #762

Merged
merged 4 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions kube-client/src/api/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
api::{Api, Resource},
Error, Result,
};
use k8s_openapi::api::core::v1::Node;
use kube_core::util::Restart;
use serde::de::DeserializeOwned;

Expand All @@ -16,3 +17,64 @@ where
self.client.request::<K>(req).await
}
}

impl Api<Node> {
/// Cordon a Node.
pub async fn cordon(&self, name: &str) -> Result<Node> {
let mut req = self.request.cordon(name).map_err(Error::BuildRequest)?;
req.extensions_mut().insert("cordon");
self.client.request::<Node>(req).await
}

/// Uncordon a Node.
pub async fn uncordon(&self, name: &str) -> Result<Node> {
let mut req = self.request.uncordon(name).map_err(Error::BuildRequest)?;
req.extensions_mut().insert("cordon");
self.client.request::<Node>(req).await
}
}

// Tests that require a cluster and the complete feature set
// Can be run with `cargo test -p kube-client --lib -- --ignored`
#[cfg(test)]
#[cfg(all(feature = "client"))]
mod test {
use crate::{
api::{Api, DeleteParams, ListParams, PostParams},
Client,
};
use k8s_openapi::api::core::v1::Node;
use serde_json::json;

#[tokio::test]
#[ignore] // needs kubeconfig
async fn node_cordon_and_uncordon_works() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::try_default().await?;

let node_name = "fakenode";
let fake_node = serde_json::from_value(json!({
"apiVersion": "v1",
"kind": "Node",
"metadata": {
"name": node_name,
},
}))?;

let nodes: Api<Node> = Api::all(client.clone());
nodes.create(&PostParams::default(), &fake_node).await?;

let schedulables = ListParams::default().fields(&format!("spec.unschedulable==false"));
let nodes_init = nodes.list(&schedulables).await?;
let num_nodes_before_cordon = nodes_init.items.len();

nodes.cordon(node_name).await?;
let nodes_after_cordon = nodes.list(&schedulables).await?;
assert_eq!(nodes_after_cordon.items.len(), num_nodes_before_cordon - 1);

nodes.uncordon(node_name).await?;
let nodes_after_uncordon = nodes.list(&schedulables).await?;
assert_eq!(nodes_after_uncordon.items.len(), num_nodes_before_cordon);
nodes.delete(node_name, &DeleteParams::default()).await?;
Ok(())
}
}
41 changes: 40 additions & 1 deletion kube-core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,37 @@ impl Request {
}
}

impl Request {
/// Cordon a resource
pub fn cordon(&self, name: &str) -> Result<http::Request<Vec<u8>>, request::Error> {
self.set_unschedulable(name, true)
}

/// Uncordon a resource
pub fn uncordon(&self, name: &str) -> Result<http::Request<Vec<u8>>, request::Error> {
self.set_unschedulable(name, false)
}

fn set_unschedulable(
&self,
node_name: &str,
value: bool,
) -> Result<http::Request<Vec<u8>>, request::Error> {
self.patch(
node_name,
&PatchParams::default(),
&Patch::Strategic(serde_json::json!({ "spec": { "unschedulable": value } })),
)
}
}


#[cfg(test)]
mod test {
use crate::{params::Patch, request::Request, resource::Resource};

#[test]
fn restart_patch_is_correct() {
use crate::{params::Patch, request::Request, resource::Resource};
use k8s_openapi::api::apps::v1 as appsv1;

let url = appsv1::Deployment::url_path(&(), Some("ns"));
Expand All @@ -52,4 +77,18 @@ mod test {
Patch::Merge(()).content_type()
);
}

#[test]
fn cordon_patch_is_correct() {
use k8s_openapi::api::core::v1::Node;

let url = Node::url_path(&(), Some("ns"));
let req = Request::new(url).cordon("mynode").unwrap();
assert_eq!(req.uri(), "/api/v1/namespaces/ns/nodes/mynode?");
assert_eq!(req.method(), "PATCH");
assert_eq!(
req.headers().get("Content-Type").unwrap().to_str().unwrap(),
Patch::Strategic(()).content_type()
);
}
}