Skip to content

Commit

Permalink
removed unnecessary ARC and fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshrayas committed Apr 20, 2022
1 parent 009e6ee commit 5e59c64
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 107 deletions.
7 changes: 3 additions & 4 deletions src/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils::{ClusterOP, Finder, JsonDetails, TableDetails};
use anyhow::Result;
use async_trait::async_trait;
use kube::{
api::{Api, DynamicObject, ResourceExt},
core::GroupVersionKind,
Expand All @@ -11,7 +12,6 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::Arc;
use tokio::task::spawn;
use async_trait::async_trait;

#[derive(Serialize, Deserialize, Default, Debug)]
pub(crate) struct Cluster {
Expand All @@ -20,7 +20,7 @@ pub(crate) struct Cluster {
}

impl<'a> Cluster {
pub(crate) async fn new(version: String) -> anyhow::Result<Cluster> {
pub(crate) async fn new(version: String) -> anyhow::Result<Cluster> {
Ok(Cluster {
version: version.to_owned(),
deprecated_api_result: Self::get_deprecated_api(&version).await?,
Expand All @@ -30,7 +30,7 @@ impl<'a> Cluster {

#[async_trait]
impl Finder for Cluster {
async fn find_deprecated_api(&self) -> Result<Vec<TableDetails>> {
async fn find_deprecated_api(&self) -> Result<Vec<TableDetails>> {
let client = Client::try_default().await?;
let current_config = kube::config::Kubeconfig::read().unwrap();
info!(
Expand All @@ -44,7 +44,6 @@ impl Finder for Cluster {
.map(|resource| {
let arc_client = Arc::new(client.clone());
let client_clone = Arc::clone(&arc_client);
let resource = Arc::clone(&Arc::new(resource));
spawn(async move {
let mut temp_table: Vec<TableDetails> = vec![];
let kind = resource["kind"].as_str().unwrap().to_string();
Expand Down
179 changes: 89 additions & 90 deletions src/file.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
use crate::utils::{TableDetails,Finder};
use crate::utils::{Finder, TableDetails};
use async_trait::async_trait;
use jwalk::{Parallelism, WalkDir};
use rayon::iter::ParallelBridge;
use rayon::prelude::ParallelIterator;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fs::File;
use std::io::Read;
use std::sync::mpsc::{channel, Sender};
use yaml_rust::YamlLoader;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Debug)]
pub(crate) struct FileSystem {
version: String,
file_dir: String,
deprecated_apis: Vec<Value>,

}

impl<'a> FileSystem {
pub(crate) async fn new(file_dir: String, version: String) -> anyhow::Result<FileSystem> {
pub(crate) async fn new(file_dir: String, version: String) -> anyhow::Result<FileSystem> {
Ok(FileSystem {
file_dir,
version:version.to_owned(),
version: version.to_owned(),
deprecated_apis: Self::get_deprecated_api(&version).await?,
})
}
Expand All @@ -31,73 +30,74 @@ impl<'a> FileSystem {
#[async_trait]
impl<'a> Finder for FileSystem {
async fn find_deprecated_api(&self) -> anyhow::Result<Vec<TableDetails>> {
let (sender, receiver) = channel();
WalkDir::new(&self.file_dir)
.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 self.deprecated_apis.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()
let (sender, receiver) = channel();
WalkDir::new(&self.file_dir)
.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 self.deprecated_apis.iter() {
if z["kind"]
.as_str()
.unwrap()
.to_string();
if z["removed"].as_str().unwrap().eq("true") {
supported_api_version = "REMOVED".to_string();
api_version = "REMOVED";
.eq(doc["kind"].as_str().unwrap())
{
let mut supported_api_version = format!(
"{}/{}",
z["group"].as_str().unwrap(),
z["version"].as_str().unwrap()
);

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);
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);
}
}
}
}
Expand All @@ -106,24 +106,23 @@ impl<'a> Finder for FileSystem {
}
}
}
}
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);
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);
}
Ok(temp_table)
}
Ok(temp_table)
}
}
13 changes: 5 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use comfy_table::Table;
use file::{FileSystem};
use utils::{generate_csv_header, Output, Scrape, Finder};
use file::FileSystem;
use utils::{generate_csv_header, Finder, Output, Scrape};
mod cluster;
mod file;
mod utils;
Expand Down Expand Up @@ -40,7 +40,7 @@ impl Sunset {
async fn main() -> anyhow::Result<()> {
let cli = Sunset::parse();
// You can check the value provided by positional arguments, or option arguments
let version: String = if let Some(version) = &cli.target_version{
let version: String = if let Some(version) = &cli.target_version {
version.to_string()
} else {
"1.16".to_string()
Expand All @@ -55,12 +55,9 @@ async fn main() -> anyhow::Result<()> {

init_logger();



match cli.check_scrape_type() {
Scrape::Cluster => {
let c = Cluster::new(version).await?;
println!("hello");
let c = Cluster::new(version).await?;
let x = utils::VecTableDetails(c.find_deprecated_api().await?);
match cli.output {
Output::Csv => {
Expand All @@ -85,7 +82,7 @@ async fn main() -> anyhow::Result<()> {
}
}
Scrape::Dir(loc) => {
let c = FileSystem::new(loc, version).await?;
let c = FileSystem::new(loc, version).await?;
let x = utils::VecTableDetails(c.find_deprecated_api().await?);
match cli.output {
Output::Csv => {
Expand Down
8 changes: 3 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use comfy_table::{ContentArrangement, Table};
use csv::Writer;
use env_logger::{Builder, Env};

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{fs::File, io::Write};
use tokio::task::JoinHandle;
use async_trait::async_trait;

pub(crate) type ClusterOP = Vec<JoinHandle<Result<Vec<TableDetails>>>>;

Expand Down Expand Up @@ -113,17 +113,15 @@ pub(crate) enum Scrape {
Dir(String),
}


#[async_trait]
pub(crate) trait Finder {
async fn find_deprecated_api(&self) -> Result<Vec<TableDetails>>;
async fn get_deprecated_api(version:&String) -> anyhow::Result<Vec<Value>> {

async fn get_deprecated_api(version: &String) -> anyhow::Result<Vec<Value>> {
let url = format!(
"https://raw.githubusercontent.com/maheshrayas/k8s_deprecated_api/main/v{}/data.json",
version
);
let v: Value = reqwest::get(url).await?.json().await?;
Ok(v.as_array().unwrap().to_owned())
}
}
}

0 comments on commit 5e59c64

Please sign in to comment.