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
//! Perform a version lookup.
use std::process::Stdio;

use tokio::process::Command;

use super::Result;

/// Exec a `kubectl` command to pull down the kubernetes version
/// metadata for a running cluster for use in the test framework
pub async fn get(kubectl_command: &str) -> Result<K8sVersion> {
    let mut command = Command::new(kubectl_command);
    command
        .stdin(Stdio::null())
        .stderr(Stdio::inherit())
        .stdout(Stdio::piped());

    command.arg("version");
    command.arg("-o").arg("json");

    command.kill_on_drop(true);

    let reader = command.output().await?;
    let json: serde_json::Value = serde_json::from_slice(&reader.stdout)?;

    Ok(K8sVersion {
        major: json["serverVersion"]["major"].to_string().replace('\"', ""),
        minor: json["serverVersion"]["minor"].to_string().replace('\"', ""),
        platform: json["serverVersion"]["platform"]
            .to_string()
            .replace('\"', ""),
        git_version: json["serverVersion"]["gitVersion"]
            .to_string()
            .replace('\"', ""),
    })
}

/// Maps K8s version metadata to struct to provide accessor
/// methods for use in testing framework
#[derive(Debug)]
pub struct K8sVersion {
    /// Server Major Version
    major: String,
    /// Server Minor Version
    minor: String,
    /// Server Platform Target
    platform: String,
    /// Fully Qualified Version Number
    git_version: String,
}

impl K8sVersion {
    /// Accessor method for returning major version
    pub fn major(&self) -> String {
        self.major.to_string()
    }

    /// Accessor method for returning minor version
    pub fn minor(&self) -> String {
        self.minor.to_string()
    }

    /// Accessor method for returning platform target
    pub fn platform(&self) -> String {
        self.platform.to_string()
    }

    /// Accessor method for returning fully qualified version
    pub fn version(&self) -> String {
        self.git_version.to_string()
    }
}