click 0.4.2

A command-line REPL for Kubernetes that integrates into existing cli workflows
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
// Copyright 2017 Databricks, Inc.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//!  Utility functions for the Describe command, used to output
//!  information for supported kubernetes object types

use values::{val_str, val_str_opt, val_u64};

use ansi_term::Colour;
use chrono::DateTime;
use chrono::offset::Local;
use chrono::offset::Utc;
use serde_json::Value;

use std::borrow::Cow;
use std::fmt::Write;
use std::str::{self, FromStr};

pub enum DescItem<'a> {
    ValStr {
        path: &'a str,
        default: &'a str,
    },
    KeyValStr {
        parent: &'a str,
        secret_vals: bool,
    },
    MetadataValStr {
        path: &'a str,
        default: &'a str,
    },
    ObjectCreated,
    CustomFunc {
        path: Option<&'a str>,
        func: &'a (Fn(&Value) -> Cow<str>),
        default: &'a str,
    },
    StaticStr(Cow<'a, str>),
}

/// get key/vals out of a value
/// If secret_vals is true, the actual vals are hidden and we show only the size of the value
fn keyval_str<'a>(v: &'a Value, parent: &str, secret_vals: bool) -> Cow<'a, str> {
    let mut outstr = String::new();
    match v.pointer(parent) {
        Some(p) => {
            if let Some(keyvals) = p.as_object() {
                let mut first = true;
                for key in keyvals.keys() {
                    if !first {
                        outstr.push('\n');
                        if !secret_vals {
                            outstr.push('\t');
                        }
                    }
                    first = false;
                    outstr.push('\t');
                    outstr.push_str(key);

                    let is_service_token = if secret_vals && key == "token" {
                        let typ = v.pointer("/type").and_then(|t| t.as_str()).unwrap_or("");
                        typ == "kubernetes.io/service-account-token"
                    } else {
                        false
                    };

                    if is_service_token {
                        outstr.push_str(":\t");
                        match ::base64::decode(keyvals.get(key).unwrap().as_str().unwrap()) {
                            Ok(dec) => outstr
                                .push_str(str::from_utf8(&dec[..]).unwrap_or("Invalid utf-8 data")),
                            Err(_) => outstr.push_str("Could not decode secret"),
                        }
                    } else if secret_vals {
                        outstr.push_str(":\t");
                        match ::base64::decode(keyvals.get(key).unwrap().as_str().unwrap()) {
                            Ok(dec) => outstr.push_str(format!("{} bytes", dec.len()).as_str()),
                            Err(_) => outstr.push_str("Could not decode secret"),
                        }
                    } else {
                        outstr.push('=');
                        outstr.push_str(keyvals.get(key).unwrap().as_str().unwrap());
                    }
                }
            }
        }
        None => {
            outstr.push_str("\t<none>");
        }
    }
    outstr.into()
}

/// Generic describe function
/// TODO: Document
pub fn describe_object<'a, I>(v: &Value, fields: I) -> String
where
    I: Iterator<Item = (&'a str, DescItem<'a>)>,
{
    let mut res = String::new();
    let metadata = v.get("metadata").unwrap();
    for (title, item) in fields {
        let val = match item {
            DescItem::ValStr {
                ref path,
                ref default,
            } => val_str(path, v, default),
            DescItem::KeyValStr {
                ref parent,
                secret_vals,
            } => keyval_str(v, parent, secret_vals),
            DescItem::MetadataValStr {
                ref path,
                ref default,
            } => val_str(path, metadata, default),
            DescItem::ObjectCreated => {
                let created: DateTime<Utc> = DateTime::from_str(&val_str(
                    "/creationTimestamp",
                    metadata,
                    "<No CreationTime>",
                )).unwrap();
                format!("{} ({})", created, created.with_timezone(&Local)).into()
            }
            DescItem::CustomFunc {
                ref path,
                ref func,
                default,
            } => {
                let value = match path {
                    &Some(p) => v.pointer(p),
                    &None => Some(v),
                };
                match value {
                    Some(v) => func(v),
                    None => default.into(),
                }
            }
            DescItem::StaticStr(s) => s,
        };
        write!(&mut res, "{}{}\n", title, val).unwrap();
    }
    res
}

/// Utility function for describe to print out value
pub fn describe_format_pod(v: Value) -> String {
    let fields = vec![
        (
            "Name:\t\t",
            DescItem::MetadataValStr {
                path: "/name",
                default: "<No Name>",
            },
        ),
        (
            "Namespace:\t",
            DescItem::MetadataValStr {
                path: "/namespace",
                default: "<No Name>",
            },
        ),
        (
            "Node:\t\t",
            DescItem::ValStr {
                path: "/spec/nodeName",
                default: "<No NodeName>",
            },
        ),
        (
            "IP:\t\t",
            DescItem::ValStr {
                path: "/status/podIP",
                default: "<No PodIP>",
            },
        ),
        ("Created at:\t", DescItem::ObjectCreated),
        (
            "Status:\t\t",
            DescItem::CustomFunc {
                path: None,
                func: &pod_phase,
                default: "<No Phase>",
            },
        ),
        (
            "Labels:\t",
            DescItem::KeyValStr {
                parent: "/metadata/labels",
                secret_vals: false,
            },
        ),
        (
            "Annotations:",
            DescItem::KeyValStr {
                parent: "/metadata/annotations",
                secret_vals: false,
            },
        ),
        (
            "Volumes:\n",
            DescItem::CustomFunc {
                path: Some("/spec/volumes"),
                func: &get_volume_str,
                default: "<No Volumes>",
            },
        ),
    ];
    describe_object(&v, fields.into_iter())
}

/// Get volume info out of volume array
fn get_volume_str<'a>(v: &'a Value) -> Cow<'a, str> {
    let mut buf = String::new();
    if let Some(vol_arry) = v.as_array() {
        for vol in vol_arry.iter() {
            buf.push_str(format!("  Name: {}\n", val_str("/name", vol, "<No Name>")).as_str());
            if vol.get("emptyDir").is_some() {
                buf.push_str(
                    "    Type:\tEmptyDir (a temporary directory that shares a pod's lifetime)\n",
                )
            }
            if let Some(conf_map) = vol.get("configMap") {
                buf.push_str("    Type:\tConfigMap (a volume populated by a ConfigMap)\n");
                buf.push_str(
                    format!("    Name:\t{}\n", val_str("/name", conf_map, "<No Name>")).as_str(),
                );
            }
            if let Some(secret) = vol.get("secret") {
                buf.push_str("    Type:\tSecret (a volume populated by a Secret)\n");
                buf.push_str(
                    format!(
                        "    SecretName:\t{}\n",
                        val_str("/secretName", secret, "<No SecretName>")
                    ).as_str(),
                );
            }
            if let Some(aws) = vol.get("awsElasticBlockStore") {
                buf.push_str(
                    "    Type:\tAWS Block Store (An AWS Disk resource exposed to the pod)\n",
                );
                buf.push_str(
                    format!(
                        "    VolumeId:\t{}\n",
                        val_str("/volumeID", aws, "<No VolumeID>")
                    ).as_str(),
                );
                buf.push_str(
                    format!("    FSType:\t{}\n", val_str("/fsType", aws, "<No FsType>")).as_str(),
                );
                let mut pnum = 0;
                if let Some(part) = aws.get("partition") {
                    if let Some(p) = part.as_u64() {
                        pnum = p;
                    }
                }
                buf.push_str(format!("    Partition#:\t{}\n", pnum).as_str());
                if let Some(read_only) = aws.get("readOnly") {
                    if read_only.as_bool().unwrap() {
                        buf.push_str("    Read-Only:\tTrue\n");
                    } else {
                        buf.push_str("    Read-Only:\tFalse\n");
                    }
                } else {
                    buf.push_str("    Read-Only:\tFalse\n");
                }
            }
        }
    }
    buf.into()
}

fn pod_phase<'a>(v: &Value) -> Cow<str> {
    let phase_str = val_str("/status/phase", v, "<No Phase>");
    let colour = match &*phase_str {
        "Pending" | "Unknown" => Colour::Yellow,
        "Running" | "Succeeded" => Colour::Green,
        "Failed" => Colour::Red,
        _ => Colour::Yellow,
    };
    colour.paint(phase_str).to_string().into()
}

/// Utility function for describe to print out value
pub fn describe_format_node(v: Value) -> String {
    let fields = vec![
        (
            "Name:\t\t",
            DescItem::MetadataValStr {
                path: "/name",
                default: "<No Name>",
            },
        ),
        (
            "Labels:\t",
            DescItem::KeyValStr {
                parent: "/metadata/labels",
                secret_vals: false,
            },
        ),
        (
            "Annotations:",
            DescItem::KeyValStr {
                parent: "/metadata/annotations",
                secret_vals: false,
            },
        ),
        ("Created at:\t", DescItem::ObjectCreated),
        (
            "Provider Id:\t",
            DescItem::ValStr {
                path: "/spec/providerID",
                default: "<No Provider Id>",
            },
        ),
        (
            "External URL:\t",
            DescItem::CustomFunc {
                path: None,
                func: &node_access_url,
                default: "N/A>",
            },
        ),
        (
            "\nSystem Info:",
            DescItem::KeyValStr {
                parent: "/status/nodeInfo",
                secret_vals: false,
            },
        ),
    ];
    describe_object(&v, fields.into_iter())
}

fn node_access_url<'a>(v: &'a Value) -> Cow<'a, str> {
    match val_str_opt("/spec/providerID", v) {
        Some(provider) => {
            if provider.starts_with("aws://") {
                let ip_opt = v.pointer("/status/addresses").and_then(|addr| {
                    addr.as_array().and_then(|addr_vec| {
                        addr_vec
                            .into_iter()
                            .find(|&aval| {
                                aval.as_object().map_or(false, |addr| {
                                    addr["type"].as_str().map_or(false, |t| t == "ExternalIP")
                                })
                            })
                            .and_then(|v| v.pointer("/address").and_then(|a| a.as_str()))
                    })
                });
                ip_opt.map_or("Not Found".into(), |ip| {
                    let octs: Vec<&str> = ip.split(".").collect();
                    if octs.len() < 4 {
                        format!("Unexpected ip format: {}", ip).into()
                    } else {
                        format!(
                            "ec2-{}-{}-{}-{}.us-west-2.compute.amazonaws.com ({})",
                            octs[0], octs[1], octs[2], octs[3], ip
                        ).into()
                    }
                })
            } else {
                "N/A".into()
            }
        }
        None => "N/A".into(),
    }
}

/// Utility function for describe to print service info
pub fn describe_format_service(v: Value, endpoint_val: Option<Value>) -> String {
    let port_str = get_ports_str(v.pointer("/spec/ports"), endpoint_val);
    let fields = vec![
        (
            "Name:\t\t",
            DescItem::MetadataValStr {
                path: "/name",
                default: "<No Name>",
            },
        ),
        (
            "Labels:\t",
            DescItem::KeyValStr {
                parent: "/metadata/labels",
                secret_vals: false,
            },
        ),
        (
            "Annotations:",
            DescItem::KeyValStr {
                parent: "/metadata/annotations",
                secret_vals: false,
            },
        ),
        ("Created at:\t", DescItem::ObjectCreated),
        (
            "Selector:",
            DescItem::KeyValStr {
                parent: "/spec/selector",
                secret_vals: false,
            },
        ),
        (
            "Type:\t\t",
            DescItem::ValStr {
                path: "/spec/type",
                default: "<No Type>",
            },
        ),
        (
            "IP:\t\t",
            DescItem::ValStr {
                path: "/spec/clusterIP",
                default: "<No Type>",
            },
        ),
        (
            "LoadBalIngress:\t",
            DescItem::ValStr {
                path: "/status/loadBalancer/ingress/0/hostname",
                default: "<No Ingress>",
            },
        ),
        ("Ports:\n", DescItem::StaticStr(port_str)),
        (
            "SessionAffnity:\t",
            DescItem::ValStr {
                path: "/spec/sessionAffnity",
                default: "<none>",
            },
        ),
    ];
    describe_object(&v, fields.into_iter())
}

/// Get ports info out of ports array
fn get_ports_str<'a>(v: Option<&'a Value>, endpoint_val: Option<Value>) -> Cow<'a, str> {
    if v.is_none() {
        return "<none>".into();
    }
    let mut buf = String::new();
    match v.unwrap().as_array() {
        // safe unwrap, checked above
        Some(port_array) => {
            for port in port_array.iter() {
                let proto = val_str("/protocol", port, "<Unknown>");
                let name = val_str("/name", port, "<No Name>");
                let port_num = val_u64("/port", port, 0);
                let endpoints = match endpoint_val {
                    Some(ref ep) => {
                        // to get all the endpoints, we need to check all subsets this port is in
                        // TODO: This is complex, simplify and/or abstract
                        let mut epbuf = String::from_str("  Endpoints:\t").unwrap();
                        let mut found_one = false;
                        ep.pointer("/subsets").map(|s| {
                            s.as_array().map(|subsets| {
                                for subset in subsets.iter() {
                                    // see if this subset has this port by checking if any port in
                                    // the ports array has the same port number
                                    let contains = subset
                                        .pointer("/ports")
                                        .map(|p| {
                                            p.as_array()
                                                .map(|ports_array| {
                                                    let mut c = false;
                                                    for port in ports_array.iter() {
                                                        if port_num == val_u64("/port", port, 0) {
                                                            c = true;
                                                        }
                                                    }
                                                    c
                                                })
                                                .unwrap_or(false)
                                        })
                                        .unwrap_or(false);
                                    if contains {
                                        // we do have this port, need to add all addresses as
                                        // endpoints
                                        found_one = true;
                                        let port_num = val_u64("/targetPort", port, 0);
                                        subset.pointer("/addresses").map(|a| {
                                            a.as_array().map(|addr_array| {
                                                let mut first = true;
                                                for addr in addr_array.iter() {
                                                    if first {
                                                        first = false;
                                                    } else {
                                                        epbuf.push_str(", ");
                                                    }
                                                    epbuf.push_str(
                                                        format!("{}:{}",
                                                                val_str("/ip", addr, "<No IP>"),
                                                                port_num).as_str());
                                                }
                                            })
                                        });
                                    }
                                }
                            })
                        });
                        if !found_one {
                            epbuf.push_str("<none>");
                        }
                        epbuf
                    }
                    None => "<No Endpoints>".to_owned(),
                };
                buf.push_str(format!("  Port:\t\t{} {}/{}\n", name, port_num, proto).as_str());
                buf.push_str(
                    format!(
                        "  NodePort:\t{} {}/{}\n",
                        val_str("/name", port, "<No Name>"),
                        val_u64("/nodePort", port, 0),
                        proto
                    ).as_str(),
                );
                buf.push_str(endpoints.as_str());
                buf.push('\n');
                buf.push('\n');
            }
        }
        None => buf.push_str("<none>"),
    }
    buf.into()
}

/// Utility function to describe a secret
pub fn describe_format_secret(v: Value) -> String {
    let fields = vec![
        (
            "Name:\t\t",
            DescItem::MetadataValStr {
                path: "/name",
                default: "<No Name>",
            },
        ),
        (
            "Namespace:\t",
            DescItem::MetadataValStr {
                path: "/namespace",
                default: "<No Name>",
            },
        ),
        (
            "Labels:\t",
            DescItem::KeyValStr {
                parent: "/metadata/labels",
                secret_vals: false,
            },
        ),
        (
            "Annotations:",
            DescItem::KeyValStr {
                parent: "/metadata/annotations",
                secret_vals: false,
            },
        ),
        (
            "\nType:\t\t",
            DescItem::ValStr {
                path: "/type",
                default: "<No Type>",
            },
        ),
        (
            "\nData:\n",
            DescItem::KeyValStr {
                parent: "/data",
                secret_vals: true,
            },
        ),
    ];
    describe_object(&v, fields.into_iter())
}