Skip to content

Commit

Permalink
feat: Check if deprecated apis are present in file location
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshrayas committed Apr 18, 2022
1 parent 640d9ca commit d2daf00
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 89 deletions.
115 changes: 115 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
serde = {version = "1.0", features = ["derive"] }
serde_json = "1.0"
yaml-rust = "0.4.5"
kube = {version="0.71.0", features = ["runtime","derive"]}
k8s-openapi = {version = "0.14.0",features=["v1_22"]}
tokio = { version = "1.17.0", features = ["full"] }
Expand All @@ -18,4 +19,6 @@ reqwest = { version = "0.11", features = ["json"] }
clap = { version = "3.1.8", features = ["derive"] }
comfy-table = "5.0.1"
openssl = { version = "0.10", features = ["vendored"] }
csv="1.1.6"
csv="1.1.6"
jwalk = "0.6.0"
rayon ="1.5.1"
13 changes: 3 additions & 10 deletions src/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{ClusterOP, Deprecated, JsonDetails, TableDetails};
use crate::utils::{ClusterOP, JsonDetails, TableDetails};
use anyhow::Result;
use kube::{
api::{Api, DynamicObject, ResourceExt},
Expand All @@ -7,24 +7,18 @@ use kube::{
Client,
};
use log::info;
use serde_json::Value;
use std::sync::Arc;
use tokio::task::spawn;

pub(crate) async fn get_cluster_resources(version: &str) -> Result<ClusterOP> {
pub(crate) async fn get_cluster_resources(version: &str, val: Vec<Value>) -> Result<ClusterOP> {
let client = Client::try_default().await?;
//let current_config = kube::config::Config::infer().await?;
let current_config = kube::config::Kubeconfig::read().unwrap();
info!(
"Connected to cluster {:?}",
current_config.current_context.unwrap()
);
info!("Target apiversions v{}", version);

let val = Deprecated::get_apiversion(format!("v{}", version).as_str())
.await?
.as_array()
.unwrap()
.to_owned();
Ok(val
.into_iter()
.map(|resource| {
Expand Down Expand Up @@ -82,7 +76,6 @@ pub(crate) async fn get_cluster_resources(version: &str) -> Result<ClusterOP> {
}
}
}

Ok(temp_table)
})
})
Expand Down
Empty file removed src/csv.rs
Empty file.
106 changes: 106 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use crate::utils::TableDetails;
use jwalk::{Parallelism, WalkDir};
use rayon::iter::ParallelBridge;
use rayon::prelude::ParallelIterator;
use serde_json::Value;
use std::fs::File;
use std::io::Read;
use std::sync::mpsc::{channel, Sender};
use yaml_rust::YamlLoader;

pub(crate) fn search_files(t: Vec<Value>, loc: String) -> Vec<TableDetails> {
let (sender, receiver) = channel();
WalkDir::new(loc)
.parallelism(Parallelism::RayonNewPool(0))
.into_iter()
.par_bridge()
.try_for_each_with(
sender,
|sed: &mut Sender<(String, String, String, String, String)>, op| {
let dir_entry = op.ok().unwrap();
if dir_entry.file_type().is_file() {
let path = dir_entry.path();
if let Some(yaml_file) = path.extension() {
if yaml_file.eq("yaml") {
let mut file = File::open(&path).expect("Unable to open file");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Unable to read file");
let docs = YamlLoader::load_from_str(&contents).unwrap();
// TODO: use combinators
for doc in docs {
if let Some(mut api_version) = doc["apiVersion"].as_str() {
for z in t.iter() {
if z["kind"]
.as_str()
.unwrap()
.eq(doc["kind"].as_str().unwrap())
{
let mut supported_api_version = format!(
"{}/{}",
z["group"].as_str().unwrap(),
z["version"].as_str().unwrap()
);

let p = path
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string();
if z["removed"].as_str().unwrap().eq("true") {
supported_api_version = "REMOVED".to_string();
api_version = "REMOVED";

if let Err(e) = sed.send((
doc["kind"].as_str().unwrap().to_string(),
supported_api_version,
api_version.to_string(),
doc["metadata"]["name"]
.as_str()
.unwrap()
.to_string(),
p,
)) {
return Err(e);
}
} else if supported_api_version.ne(api_version) {
if let Err(e) = sed.send((
doc["kind"].as_str().unwrap().to_string(),
supported_api_version,
api_version.to_string(),
doc["metadata"]["name"]
.as_str()
.unwrap()
.to_string(),
p,
)) {
return Err(e);
}
}
}
}
}
}
}
}
}
Ok(())
},
)
.expect("expected no send errors");
let res: Vec<_> = receiver.iter().collect();
let mut temp_table: Vec<TableDetails> = vec![];
for (kind, supported_api_version, deprecated_api_version, name, path) in res {
let p = path;
let t = TableDetails {
kind,
namespace: p.to_owned(),
name,
supported_api_version,
deprecated_api_version,
};
temp_table.push(t);
}
temp_table
}
Loading

0 comments on commit d2daf00

Please sign in to comment.