|
1 | 1 | use bytes::Bytes;
|
2 | 2 | use futures::Stream;
|
3 |
| -use serde::de::DeserializeOwned; |
| 3 | +use serde::{de::DeserializeOwned, Serialize}; |
4 | 4 |
|
5 | 5 | use crate::{
|
6 |
| - api::{Api, Patch, PatchParams, PostParams, Resource}, |
| 6 | + api::{Api, DeleteParams, Patch, PatchParams, PostParams, Resource}, |
| 7 | + client::Status, |
7 | 8 | Error, Result,
|
8 | 9 | };
|
9 | 10 |
|
@@ -221,6 +222,84 @@ where
|
221 | 222 | }
|
222 | 223 | }
|
223 | 224 |
|
| 225 | +// ---------------------------------------------------------------------------- |
| 226 | +// Eviction subresource |
| 227 | +// ---------------------------------------------------------------------------- |
| 228 | + |
| 229 | +use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta; |
| 230 | +/// Eviction body |
| 231 | +#[derive(Default, Clone, Serialize)] |
| 232 | +pub struct Eviction { |
| 233 | + /// How the eviction should occur |
| 234 | + pub delete_options: Option<DeleteParams>, |
| 235 | + /// Metadata describing the pod being evicted |
| 236 | + /// we somehow need this despite providing the name of the pod.. |
| 237 | + pub metadata: ObjectMeta, |
| 238 | +} |
| 239 | + |
| 240 | +impl Eviction { |
| 241 | + /// Create an eviction object for a named resource |
| 242 | + pub fn new(name: &str) -> Self { |
| 243 | + Self { |
| 244 | + metadata: ObjectMeta { |
| 245 | + name: Some(name.to_string()), |
| 246 | + ..ObjectMeta::default() |
| 247 | + }, |
| 248 | + delete_options: None, |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + /// Attach DeleteParams to the eviction object |
| 253 | + pub fn with_options(mut self, dp: DeleteParams) -> Self { |
| 254 | + self.delete_options = Some(dp); |
| 255 | + self |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +impl Resource { |
| 260 | + /// Create an eviction |
| 261 | + pub fn evict(&self, name: &str, pp: &PostParams, data: Vec<u8>) -> Result<http::Request<Vec<u8>>> { |
| 262 | + let base_url = self.make_url() + "/" + name + "/" + "eviction?"; |
| 263 | + // This is technically identical to Resource::create, but different url |
| 264 | + pp.validate()?; |
| 265 | + let mut qp = url::form_urlencoded::Serializer::new(base_url); |
| 266 | + if pp.dry_run { |
| 267 | + qp.append_pair("dryRun", "All"); |
| 268 | + } |
| 269 | + let urlstr = qp.finish(); |
| 270 | + let req = http::Request::post(urlstr); |
| 271 | + req.body(data).map_err(Error::HttpError) |
| 272 | + } |
| 273 | +} |
| 274 | + |
| 275 | +#[test] |
| 276 | +fn evict_path() { |
| 277 | + use crate::api::Resource; |
| 278 | + use k8s_openapi::api::core::v1 as corev1; |
| 279 | + let r = Resource::namespaced::<corev1::Pod>("ns"); |
| 280 | + let mut pp = PostParams::default(); |
| 281 | + pp.dry_run = true; |
| 282 | + let req = r.evict("foo", &pp, vec![]).unwrap(); |
| 283 | + assert_eq!(req.uri(), "/api/v1/namespaces/ns/pods/foo/eviction?&dryRun=All"); |
| 284 | +} |
| 285 | + |
| 286 | +/// Marker trait for objects that can be evicted |
| 287 | +pub trait EvictionObject {} |
| 288 | + |
| 289 | +impl EvictionObject for k8s_openapi::api::core::v1::Pod {} |
| 290 | + |
| 291 | +impl<K> Api<K> |
| 292 | +where |
| 293 | + K: Clone + DeserializeOwned + EvictionObject, |
| 294 | +{ |
| 295 | + /// Create an eviction |
| 296 | + pub async fn evict(&self, name: &str, pp: &PostParams, data: &Eviction) -> Result<Status> { |
| 297 | + let bytes = serde_json::to_vec(data)?; |
| 298 | + let req = self.resource.evict(name, pp, bytes)?; |
| 299 | + self.client.request::<Status>(req).await |
| 300 | + } |
| 301 | +} |
| 302 | + |
224 | 303 | // ----------------------------------------------------------------------------
|
225 | 304 | // Attach subresource
|
226 | 305 | // ----------------------------------------------------------------------------
|
|
0 commit comments