k8s_test_framework/
kubernetes_version.rs1use std::process::Stdio;
3
4use tokio::process::Command;
5
6use super::Result;
7
8pub 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#[derive(Debug)]
40pub struct K8sVersion {
41 major: String,
43 minor: String,
45 platform: String,
47 git_version: String,
49}
50
51impl K8sVersion {
52 pub fn major(&self) -> String {
54 self.major.to_string()
55 }
56
57 pub fn minor(&self) -> String {
59 self.minor.to_string()
60 }
61
62 pub fn platform(&self) -> String {
64 self.platform.to_string()
65 }
66
67 pub fn version(&self) -> String {
69 self.git_version.to_string()
70 }
71}