k8s_test_framework/
kubernetes_version.rs

1//! Perform a version lookup.
2use std::process::Stdio;
3
4use tokio::process::Command;
5
6use super::Result;
7
8/// Exec a `kubectl` command to pull down the kubernetes version
9/// metadata for a running cluster for use in the test framework
10pub async fn get(kubectl_command: &str) -> Result<K8sVersion> {
11    let mut command = Command::new(kubectl_command);
12    command
13        .stdin(Stdio::null())
14        .stderr(Stdio::inherit())
15        .stdout(Stdio::piped());
16
17    command.arg("version");
18    command.arg("-o").arg("json");
19
20    command.kill_on_drop(true);
21
22    let reader = command.output().await?;
23    let json: serde_json::Value = serde_json::from_slice(&reader.stdout)?;
24
25    Ok(K8sVersion {
26        major: json["serverVersion"]["major"].to_string().replace('\"', ""),
27        minor: json["serverVersion"]["minor"].to_string().replace('\"', ""),
28        platform: json["serverVersion"]["platform"]
29            .to_string()
30            .replace('\"', ""),
31        git_version: json["serverVersion"]["gitVersion"]
32            .to_string()
33            .replace('\"', ""),
34    })
35}
36
37/// Maps K8s version metadata to struct to provide accessor
38/// methods for use in testing framework
39#[derive(Debug)]
40pub struct K8sVersion {
41    /// Server Major Version
42    major: String,
43    /// Server Minor Version
44    minor: String,
45    /// Server Platform Target
46    platform: String,
47    /// Fully Qualified Version Number
48    git_version: String,
49}
50
51impl K8sVersion {
52    /// Accessor method for returning major version
53    pub fn major(&self) -> String {
54        self.major.to_string()
55    }
56
57    /// Accessor method for returning minor version
58    pub fn minor(&self) -> String {
59        self.minor.to_string()
60    }
61
62    /// Accessor method for returning platform target
63    pub fn platform(&self) -> String {
64        self.platform.to_string()
65    }
66
67    /// Accessor method for returning fully qualified version
68    pub fn version(&self) -> String {
69        self.git_version.to_string()
70    }
71}