@@ -3,7 +3,8 @@ use futures::Stream;
3
3
use serde:: de:: DeserializeOwned ;
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,67 @@ where
221
222
}
222
223
}
223
224
225
+ // ----------------------------------------------------------------------------
226
+ // Eviction subresource
227
+ // ----------------------------------------------------------------------------
228
+
229
+ /// Params for evictable objects
230
+ #[ derive( Default , Clone ) ]
231
+ pub struct EvictParams {
232
+ /// How the eviction should occur
233
+ pub delete_options : Option < DeleteParams > ,
234
+ /// How the http post should occur
235
+ pub post_options : PostParams ,
236
+ }
237
+
238
+ impl Resource {
239
+ /// Create an eviction
240
+ pub fn evict ( & self , name : & str , ep : & EvictParams ) -> Result < http:: Request < Vec < u8 > > > {
241
+ let base_url = self . make_url ( ) + "/" + name + "/" + "eviction?" ;
242
+ // This is technically identical to Resource::create, but different url
243
+ let pp = & ep. post_options ;
244
+ pp. validate ( ) ?;
245
+ let mut qp = url:: form_urlencoded:: Serializer :: new ( base_url) ;
246
+ if pp. dry_run {
247
+ qp. append_pair ( "dryRun" , "All" ) ;
248
+ }
249
+ let urlstr = qp. finish ( ) ;
250
+ // eviction body parameters are awkward, need metadata with name
251
+ let data = serde_json:: to_vec ( & serde_json:: json!( {
252
+ "delete_options" : ep. delete_options,
253
+ "metadata" : { "name" : name }
254
+ } ) ) ?;
255
+ let req = http:: Request :: post ( urlstr) ;
256
+ req. body ( data) . map_err ( Error :: HttpError )
257
+ }
258
+ }
259
+
260
+ #[ test]
261
+ fn evict_path ( ) {
262
+ use crate :: api:: Resource ;
263
+ use k8s_openapi:: api:: core:: v1 as corev1;
264
+ let r = Resource :: namespaced :: < corev1:: Pod > ( "ns" ) ;
265
+ let ep = EvictParams :: default ( ) ;
266
+ let req = r. evict ( "foo" , & ep) . unwrap ( ) ;
267
+ assert_eq ! ( req. uri( ) , "/api/v1/namespaces/ns/pods/foo/eviction?" ) ;
268
+ }
269
+
270
+ /// Marker trait for objects that can be evicted
271
+ pub trait Evictable { }
272
+
273
+ impl Evictable for k8s_openapi:: api:: core:: v1:: Pod { }
274
+
275
+ impl < K > Api < K >
276
+ where
277
+ K : Clone + DeserializeOwned + Evictable ,
278
+ {
279
+ /// Create an eviction
280
+ pub async fn evict ( & self , name : & str , ep : & EvictParams ) -> Result < Status > {
281
+ let req = self . resource . evict ( name, ep) ?;
282
+ self . client . request :: < Status > ( req) . await
283
+ }
284
+ }
285
+
224
286
// ----------------------------------------------------------------------------
225
287
// Attach subresource
226
288
// ----------------------------------------------------------------------------
0 commit comments