keel 0.0.1

A Kubernetes client library for Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
pub mod config;
mod resplit;

use failure::{format_err, Error, Fail, ResultExt};
use futures::{future, prelude::*};
use hyper::{Body, Chunk, Request, Response, StatusCode};
use hyper_tls::HttpsConnector;
use log::{debug, error, warn};
use native_tls::{Certificate, Identity, TlsConnector};
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::{
    env,
    fmt::{Debug, Display},
    path::PathBuf,
    sync::Arc,
};
use url::Url;

use self::config::ConfigContext;

#[derive(Fail, Debug)]
#[fail(display = "Unexpected HTTP response status: {}", status)]
pub struct HttpStatusError {
    status: StatusCode,
}

#[derive(Fail, Debug)]
#[fail(display = "Unable to parse response: {}", reason)]
pub struct ParseError {
    reason: String,
}

#[derive(Debug, Clone)]
pub struct Client<C> {
    client: Arc<hyper::Client<C>>,
    config: ConfigContext,
}

impl Client<HttpsConnector<hyper::client::HttpConnector>> {
    pub fn new() -> Result<Self, Error> {
        let dns_threads = 1; // Only need a single DNS lookup
        let http = hyper::client::HttpConnector::new(dns_threads);
        Client::new_from_http(http)
    }

    pub fn new_from_http(http: hyper::client::HttpConnector) -> Result<Self, Error> {
        let config_path = env::var_os(config::CONFIG_ENV)
            .map(PathBuf::from)
            .or_else(config::default_path)
            .ok_or_else(|| format_err!("Unable to find config"))?;
        debug!("Reading config from {}", config_path.display());
        let config = config::load_from_file(&config_path)
            .with_context(|e| format!("Unable to read {}: {}", config_path.display(), e))?;
        let context = config.config_context(&config.current_context)?;
        Client::new_from_context(http, context)
    }

    pub fn new_from_context(
        mut http: hyper::client::HttpConnector,
        config: ConfigContext,
    ) -> Result<Self, Error> {
        http.enforce_http(false);
        let mut tls = TlsConnector::builder();
        if let (Some(certdata), Some(keydata)) = (
            config.user.client_certificate_read(),
            config.user.client_key_read(),
        ) {
            debug!("Setting user client cert");
            let cert = openssl::x509::X509::from_pem(&certdata?)?;
            let pkey = openssl::pkey::PKey::private_key_from_pem(&keydata?)?;
            // openssl pkcs12 -export -clcerts -inkey kubecfg.key -in kubecfg.crt -out kubecfg.p12 -name "kubecfg"
            let password = "";
            let p12 =
                openssl::pkcs12::Pkcs12::builder().build(password, "kubeconfig", &pkey, &cert)?;
            tls.identity(Identity::from_pkcs12(&p12.to_der()?, password)?);
        }

        if let Some(data) = config.cluster.certificate_authority_read() {
            debug!("Setting cluster CA cert");
            let cert = Certificate::from_pem(&data?)?;
            // FIXME: want to validate against _only_ this cert ..
            tls.add_root_certificate(cert);
        }

        if config.cluster.insecure_skip_tls_verify {
            warn!("Disabling CA verification");

            tls.danger_accept_invalid_certs(true);
        }

        let hyper_client =
            hyper::Client::builder().build(HttpsConnector::from((http, tls.build()?)));

        Self::new_with_client(hyper_client, config)
    }
}

impl<C> Client<C> {
    pub fn new_with_client(client: hyper::Client<C>, config: ConfigContext) -> Result<Self, Error> {
        Ok(Client {
            client: Arc::new(client),
            config,
        })
    }

    pub fn client(&self) -> &hyper::Client<C> {
        &self.client
    }
}

#[derive(Clone, Debug)]
pub struct StatusError<S: Debug>(S);

impl<S: Debug> std::error::Error for StatusError<S> {
    fn description(&self) -> &str {
        "request failed"
    }
}

impl<S: Debug> Display for StatusError<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let StatusError(status) = self;
        write!(f, "{:?}", status)
    }
}

pub enum Observed<L, I> {
    List(L),
    ListPart(L),
    Item(I),
}

pub struct ListState<L> {
    list: L,
    continu_: Option<String>,
    resource_version: String,
}

pub struct WatchState<I> {
    item: I,
    resource_version: String,
}

pub enum KubernetesError<S> {
    Status(S),
    Other(Error),
}

impl<S> From<Error> for KubernetesError<S> {
    fn from(error: Error) -> Self {
        KubernetesError::Other(error)
    }
}

pub enum ObserverState<List, Item, Status> {
    None,
    Listing(Box<dyn Future<Item = ListState<List>, Error = KubernetesError<Status>> + Send>),
    Watching(Box<dyn Stream<Item = WatchState<Item>, Error = KubernetesError<Status>> + Send>),
}

pub struct Observer<L, I, S, B>
where
    L: DeserializeOwned + Debug + Send + Sync + 'static,
    I: DeserializeOwned + Debug + Send + Sync + 'static,
    S: DeserializeOwned + Debug + Send + Sync + 'static,
    B: Into<hyper::Body> + Send + 'static,
{
    state: ObserverState<L, I, S>,
    last_resource_version: Option<String>,
    client: Client<HttpsConnector<hyper::client::HttpConnector>>,
    request_factory: Box<Fn(RequestOpts) -> K8sRequest<B> + Send>,
}

fn get_resource_version(value: &Value) -> Option<String> {
    value
        .get("metadata")
        .and_then(|m| m.get("resourceVersion"))
        .and_then(|rv| rv.as_str())
        .map(|str| str.to_string())
}

fn get_continue(value: &Value) -> Option<String> {
    value
        .get("metadata")
        .and_then(|m| m.get("continue"))
        .and_then(|rv| rv.as_str())
        .map(|str| str.to_string())
}

pub struct RequestOpts<'a> {
    pub watch: bool,
    pub resource_version: Option<&'a str>,
    pub continu_: Option<&'a str>,
}

impl<L, I, S, B> Observer<L, I, S, B>
where
    L: DeserializeOwned + Debug + Send + Sync + 'static,
    I: DeserializeOwned + Debug + Send + Sync + 'static,
    S: DeserializeOwned + Debug + Send + Sync + 'static,
    B: Into<hyper::Body> + Send + 'static,
{
    fn list(
        &mut self,
        continu_: &Option<String>,
    ) -> impl Future<Item = ListState<L>, Error = KubernetesError<S>> + Send {
        let req = (*self.request_factory)(RequestOpts {
            watch: false,
            resource_version: None,
            continu_: match continu_ {
                Some(ref str) => Some(str.as_str()),
                None => None,
            },
        });

        self.client.simple_req_chunk(req).then(
            |result| -> Result<ListState<L>, KubernetesError<S>> {
                match result {
                    Ok(chunk) => {
                        let o: Value = serde_json::from_slice(chunk.as_ref())
                            .map_err(|e| KubernetesError::Other(e.into()))?;

                        let resource_version: String = match get_resource_version(&o) {
                            Some(rv) => Ok(rv),
                            None => Err(KubernetesError::Other(
                                ParseError {
                                    reason: format!(
                                        "missing resourceVersion in response object {:?}",
                                        o
                                    ),
                                }.into(),
                            )),
                        }?;
                        let continu_ = get_continue(&o);

                        warn!(
                            "Got resource version {:?}, continu_ {:?} from list operation",
                            resource_version, continu_
                        );

                        Ok(ListState {
                            list: serde_json::from_value(o).map_err(|e| {
                                error!(
                                    "Parsing response body: {}",
                                    String::from_utf8_lossy(chunk.as_ref())
                                );
                                KubernetesError::Other(e.into())
                            })?,
                            resource_version,
                            continu_,
                        })
                    }
                    Err(KubernetesError::Status((chunk, httpstatus))) => {
                        let status: S = serde_json::from_slice(chunk.as_ref()).map_err(|e| {
                            error!(
                                "Failed to parse error Status ({}), falling back to HTTP status",
                                e
                            );
                            KubernetesError::Other(HttpStatusError { status: httpstatus }.into())
                        })?;

                        Err(KubernetesError::Status(status))
                    }
                    Err(KubernetesError::Other(e)) => Err(KubernetesError::Other(e)),
                }
            },
        )
    }

    fn watch(&mut self) -> impl Stream<Item = WatchState<I>, Error = KubernetesError<S>> + Send {
        let req = (*self.request_factory)(RequestOpts {
            watch: true,
            continu_: None,
            resource_version: match self.last_resource_version {
                Some(ref str) => Some(str.as_str()),
                None => None,
            },
        });

        self.client.watch_as_chunk(req).then(
            |result| -> Result<WatchState<I>, KubernetesError<S>> {
                match result {
                    Ok(chunk) => {
                        let o: Value = serde_json::from_slice(chunk.as_ref())
                            .map_err(|e| KubernetesError::Other(e.into()))?;

                        let resource_version: String =
                            match o.get("object").and_then(get_resource_version) {
                                Some(rv) => Ok(rv),
                                None => Err(KubernetesError::Other(
                                    ParseError {
                                        reason: format!(
                                            "missing resourceVersion in response object {:?}",
                                            o
                                        ),
                                    }.into(),
                                )),
                            }?;

                        Ok(WatchState {
                            item: serde_json::from_value(o).map_err(|e| {
                                error!(
                                    "Parsing response body: {}",
                                    String::from_utf8_lossy(chunk.as_ref())
                                );
                                KubernetesError::Other(e.into())
                            })?,
                            resource_version,
                        })
                    }
                    Err(KubernetesError::Status((chunk, httpstatus))) => {
                        let status: S = serde_json::from_slice(chunk.as_ref()).map_err(|e| {
                            error!(
                                "Failed to parse error Status ({}), falling back to HTTP status",
                                e
                            );
                            KubernetesError::Other(HttpStatusError { status: httpstatus }.into())
                        })?;

                        Err(KubernetesError::Status(status))
                    }
                    Err(KubernetesError::Other(e)) => Err(KubernetesError::Other(e)),
                }
            },
        )
    }
}

impl<L, I, S, B> Stream for Observer<L, I, S, B>
where
    L: DeserializeOwned + Debug + Send + Sync + 'static,
    I: DeserializeOwned + Debug + Send + Sync + 'static,
    S: DeserializeOwned + Debug + Send + Sync + 'static,
    B: Into<hyper::Body> + Send + 'static,
{
    type Error = KubernetesError<S>;
    type Item = Observed<L, I>;

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        match &mut self.state {
            ObserverState::None => {
                let future = Box::new(self.list(&None));
                self.state = ObserverState::Listing(future);
                self.poll()
            }
            ObserverState::Listing(future) => (*future)
                .poll()
                .map(|async_value| {
                    async_value.map(|list_state| {
                        if let Some(continu_) = list_state.continu_ {
                            let future = Box::new(self.list(&Some(continu_)));
                            self.state = ObserverState::Listing(future);
                            Some(Observed::ListPart(list_state.list))
                        } else {
                            self.last_resource_version = Some(list_state.resource_version);
                            let stream = Box::new(self.watch());
                            self.state = ObserverState::Watching(stream);
                            Some(Observed::List(list_state.list))
                        }
                    })
                }).map_err(|e| {
                    self.state = ObserverState::None;
                    self.last_resource_version = None;
                    e
                }),
            ObserverState::Watching(stream) => (*stream)
                .poll()
                .and_then(|async_value| match async_value {
                    Async::Ready(result) => match result {
                        Some(watch_state) => {
                            self.last_resource_version = Some(watch_state.resource_version);
                            Ok(Async::Ready(Some(Observed::Item(watch_state.item))))
                        }
                        None => {
                            let stream = Box::new(self.watch());
                            self.state = ObserverState::Watching(stream);
                            self.poll()
                        }
                    },
                    Async::NotReady => Ok(Async::NotReady),
                }).map_err(|e| {
                    self.state = ObserverState::None;
                    self.last_resource_version = None;
                    e
                }),
        }
    }
}

type K8sRequest<B> = Result<Request<B>, k8s_openapi::RequestError>;

impl<C: hyper::client::connect::Connect + 'static> Client<C> {
    fn rebase_url<B>(&self, mut req: Request<B>) -> Result<Request<hyper::Body>, Error>
    where
        B: Into<hyper::Body> + Send + 'static,
    {
        if req.uri().host().is_none() {
            let req_uri = format!("{}", req.uri());
            let base_uri: Url = self.config.cluster.server.parse()?;
            let joined_uri = base_uri.join(&req_uri)?;

            *req.uri_mut() = joined_uri.to_string().parse()?;
        }

        Ok(req.map(<B>::into))
    }

    fn wrap_req<B>(
        &self,
        result_req: Result<Request<B>, k8s_openapi::RequestError>,
    ) -> Result<Request<hyper::Body>, Error>
    where
        B: Into<hyper::Body> + Send + 'static,
    {
        match result_req {
            Ok(req) => Ok(self.rebase_url(req)?),
            Err(e) => Err(e.into()),
        }
    }

    pub fn observe<L, I, S, B>(
        &self,
        request_factory: Box<Fn(RequestOpts) -> K8sRequest<B> + Send>,
    ) -> Result<impl Stream<Item = Observed<L, I>, Error = KubernetesError<S>> + Send, Error>
    where
        L: DeserializeOwned + Debug + Send + Sync + 'static,
        I: DeserializeOwned + Debug + Send + Sync + 'static,
        S: DeserializeOwned + Debug + Send + Sync + 'static,
        B: Into<hyper::Body> + Send + 'static,
    {
        Ok(Observer {
            state: ObserverState::None,
            last_resource_version: None,
            client: Client::new()?,
            request_factory,
        })
    }

    pub fn simple_req_chunk<B>(
        &self,
        req: Result<Request<B>, k8s_openapi::RequestError>,
    ) -> impl Future<Item = Chunk, Error = KubernetesError<(Chunk, StatusCode)>> + Send
    where
        B: Into<hyper::Body> + Send + 'static,
    {
        let client = Arc::clone(&self.client);
        future::result(self.wrap_req(req))
            .inspect(|req|
                    // Avoid body, since it may not be Debug
                    debug!("Request: {} {}", req.method(), req.uri()))
            .and_then(move |req|
                    // TODO: add method/uri context to error
                    client.request(req).from_err::<Error>())
            .inspect(|res| debug!("Response: {} {:?}", res.status(), res.headers()))
            // Verbose!
            //.inspect(|res| debug!("Response: {:#?}", res))
            .and_then(|res| {
                let status = res.status();
                res.into_body().concat2().map(move |body| (status, body)).from_err()
            })
            // Verbose!
            //.inspect(|(_, body)| debug!("Response body: {:?}", ::std::str::from_utf8(body.as_ref())))
            .map_err(KubernetesError::Other)
            .and_then(move |(httpstatus, body)| -> Result<Chunk, KubernetesError<(Chunk, StatusCode)>> {
                if httpstatus.is_success() {
                    Ok(body)
                } else {
                    error!("failure body: {:#?}", ::std::str::from_utf8(body.as_ref()));
                    Err(KubernetesError::Status((body, httpstatus)))
                }
            })
    }

    pub fn simple_req<T, S, B>(
        &self,
        req: Result<Request<B>, k8s_openapi::RequestError>,
    ) -> impl Future<Item = T, Error = KubernetesError<S>> + Send
    where
        T: DeserializeOwned + Debug + Send + Sync + 'static,
        S: DeserializeOwned + Debug + Send + Sync + 'static,
        B: Into<hyper::Body> + Send + 'static,
    {
        self.simple_req_chunk(req).then(|result| match result {
            Ok(chunk) => {
                let o: T = serde_json::from_slice(chunk.as_ref()).map_err(|e| {
                    error!(
                        "Parsing response body: {}",
                        String::from_utf8_lossy(chunk.as_ref())
                    );
                    KubernetesError::Other(e.into())
                })?;
                Ok(o)
            }
            Err(KubernetesError::Status((chunk, httpstatus))) => {
                let status: S = serde_json::from_slice(chunk.as_ref()).map_err(|e| {
                    error!(
                        "Failed to parse error Status ({}), falling back to HTTP status",
                        e
                    );
                    KubernetesError::Other(HttpStatusError { status: httpstatus }.into())
                })?;

                Err(KubernetesError::Status(status))
            }
            Err(KubernetesError::Other(e)) => Err(KubernetesError::Other(e)),
        })
    }

    pub(crate) fn watch_as_chunk<B>(
        &self,
        req: Result<Request<B>, k8s_openapi::RequestError>,
    ) -> impl Stream<Item = Vec<u8>, Error = KubernetesError<(Chunk, StatusCode)>> + Send
    where
        B: Into<hyper::Body> + Send + 'static,
    {
        let client = Arc::clone(&self.client);
        future::result(self.wrap_req(req))
            .inspect(|req| debug!("Watch request: {} {}", req.method(), req.uri()))
            .and_then(move |req|
                    // TODO: add method/uri context to error
                    client.request(req).from_err::<Error>())
            .inspect(|res| debug!("Response: {:#?}", res))
            .map_err(KubernetesError::Other)
            .and_then(|res| {
                let httpstatus = res.status();
                let r = if httpstatus.is_success() { Ok(res) } else { Err(res) };
                future::result(r)
                    .or_else(move |res| {
                        res.into_body()
                            .concat2()
                            .map_err(|e| KubernetesError::Other(e.into()))
                            .and_then(move |body| -> Result<Response<Body>, KubernetesError<(Chunk, StatusCode)>> {
                                error!("failure body: {:#?}", ::std::str::from_utf8(body.as_ref()));
                                Err(KubernetesError::Status((body, httpstatus)))
                            })
                    })
                    .map(|res| {
                        resplit::new(res.into_body(), |&c| c == b'\n')
                            .inspect(|line| debug!("Got line: {:#?}", ::std::str::from_utf8(line).unwrap_or("<invalid utf8>")))
                            .map_err(|e| KubernetesError::Other(e.into()))
                            .and_then(move |line| -> Result<Vec<u8>, KubernetesError<(Chunk, StatusCode)>> {
                                Ok(line)
                            })
                    })
            })
            .flatten_stream()
    }

    pub fn watch<T, S, B>(
        &self,
        req: Result<Request<B>, k8s_openapi::RequestError>,
    ) -> impl Stream<Item = T, Error = KubernetesError<S>> + Send
    where
        T: DeserializeOwned + Debug + Send + Sync + 'static,
        S: DeserializeOwned + Debug + Send + Sync + 'static,
        B: Into<hyper::Body> + Send + 'static,
    {
        self.watch_as_chunk(req).then(|result| match result {
            Ok(chunk) => {
                let o: T = serde_json::from_slice(chunk.as_ref()).map_err(|e| {
                    error!(
                        "Parsing response body: {}",
                        String::from_utf8_lossy(chunk.as_ref())
                    );
                    KubernetesError::Other(e.into())
                })?;
                Ok(o)
            }
            Err(KubernetesError::Status((chunk, httpstatus))) => {
                let status: S = serde_json::from_slice(chunk.as_ref()).map_err(|e| {
                    error!(
                        "Failed to parse error Status ({}), falling back to HTTP status",
                        e
                    );
                    KubernetesError::Other(HttpStatusError { status: httpstatus }.into())
                })?;

                Err(KubernetesError::Status(status))
            }
            Err(KubernetesError::Other(e)) => Err(KubernetesError::Other(e)),
        })
        // let client = Arc::clone(&self.client);
        // future::result(self.wrap_req(req))
        //     .inspect(|req| debug!("Watch request: {} {}", req.method(), req.uri()))
        //     .and_then(move |req|
        //             // TODO: add method/uri context to error
        //             client.request(req).from_err::<Error>())
        //     .inspect(|res| debug!("Response: {:#?}", res))
        //     .map_err(|e| -> KubernetesError<S> { KubernetesError::Other(e) })
        //     .and_then(|res| {
        //         let httpstatus = res.status();
        //         let r = if httpstatus.is_success() { Ok(res) } else { Err(res) };
        //         future::result(r)
        //             .or_else(move |res| {
        //                 res.into_body()
        //                     .concat2()
        //                     .map_err(|e| KubernetesError::Other(e.into()))
        //                     .and_then(move |body| -> Result<Response<Body>, KubernetesError<S>> {
        //                         error!("failure body: {:#?}", ::std::str::from_utf8(body.as_ref()));
        //                         let status: S = serde_json::from_slice(body.as_ref())
        //                             .map_err(|e| {
        //                                 error!("Failed to parse error Status ({}), falling back to HTTP status", e);
        //                                 KubernetesError::Other(HttpStatusError{status: httpstatus}.into())
        //                             })?;

        //                         Err(KubernetesError::Status(status))
        //                     })
        //             })
        //             .map(|res| {
        //                 resplit::new(res.into_body(), |&c| c == b'\n')
        //                     .inspect(|line| debug!("Got line: {:#?}", ::std::str::from_utf8(line).unwrap_or("<invalid utf8>")))
        //                     .map_err(|e| KubernetesError::Other(e.into()))
        //                     .and_then(move |line| -> Result<T, KubernetesError<S>> {
        //                         let o: T = serde_json::from_slice(line.as_ref())
        //                             .map_err(|e| {
        //                                 error!("Parsing response body: {}", String::from_utf8_lossy(line.as_ref()));
        //                                 KubernetesError::Other(e.into())
        //                             })?;
        //                         Ok(o)
        //                     })
        //             })
        //     })
        //     .flatten_stream()
    }
}