Skip to content

Commit

Permalink
Special-case readCoreV1NamespacedPodLog to return an Iterator.
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
Arnavion committed Jun 23, 2018
1 parent 7137a03 commit e6bda30
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 41 deletions.
8 changes: 6 additions & 2 deletions k8s-openapi-tests/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ fn get() {
&client,
addon_manager_pod_name, "kube-system", Some("kube-addon-manager"), None, Some(4096), None, None, None, None, None)
.expect("couldn't get addon-manager pod logs");
let addon_manager_logs = match addon_manager_logs {
let mut addon_manager_logs = match addon_manager_logs {
api::ReadCoreV1NamespacedPodLogResponse::Ok(logs) => logs,
other => panic!("couldn't get addon-manager pod logs: {:?}", other),
};
assert!(addon_manager_logs.contains("INFO: == Kubernetes addon manager started at"));
let addon_manager_logs_first_line =
addon_manager_logs
.next().expect("addon-manager pod has no logs")
.expect("couldn't get first line of addon-manager pod logs");
assert!(addon_manager_logs_first_line.contains("INFO: == Kubernetes addon manager started at"), "{}", addon_manager_logs_first_line);
}
6 changes: 2 additions & 4 deletions k8s-openapi/src/v1_10/api/core/v1/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ impl Pod {

#[derive(Debug)]
pub enum ReadCoreV1NamespacedPodLogResponse<R> where R: ::std::io::Read {
Ok(String),
Ok(::std::io::Lines<::std::io::BufReader<R>>),
Unauthorized(R),
Other(::http::StatusCode, R),
}
Expand Down Expand Up @@ -1358,9 +1358,7 @@ impl Pod {

Ok(match ::Response::status_code(&response) {
::http::StatusCode::OK => {
let mut response = response;
let mut result = String::new();
::std::io::Read::read_to_string(&mut response, &mut result).map_err(::Error::IO)?;
let result = ::std::io::BufRead::lines(::std::io::BufReader::new(response));
ReadCoreV1NamespacedPodLogResponse::Ok(result)
},
::http::StatusCode::UNAUTHORIZED => ReadCoreV1NamespacedPodLogResponse::Unauthorized(response),
Expand Down
6 changes: 2 additions & 4 deletions k8s-openapi/src/v1_7/kubernetes/pkg/api/v1/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ impl Pod {

#[derive(Debug)]
pub enum ReadCoreV1NamespacedPodLogResponse<R> where R: ::std::io::Read {
Ok(String),
Ok(::std::io::Lines<::std::io::BufReader<R>>),
Unauthorized(R),
Other(::http::StatusCode, R),
}
Expand Down Expand Up @@ -1672,9 +1672,7 @@ impl Pod {

Ok(match ::Response::status_code(&response) {
::http::StatusCode::OK => {
let mut response = response;
let mut result = String::new();
::std::io::Read::read_to_string(&mut response, &mut result).map_err(::Error::IO)?;
let result = ::std::io::BufRead::lines(::std::io::BufReader::new(response));
ReadCoreV1NamespacedPodLogResponse::Ok(result)
},
::http::StatusCode::UNAUTHORIZED => ReadCoreV1NamespacedPodLogResponse::Unauthorized(response),
Expand Down
6 changes: 2 additions & 4 deletions k8s-openapi/src/v1_8/api/core/v1/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ impl Pod {

#[derive(Debug)]
pub enum ReadCoreV1NamespacedPodLogResponse<R> where R: ::std::io::Read {
Ok(String),
Ok(::std::io::Lines<::std::io::BufReader<R>>),
Unauthorized(R),
Other(::http::StatusCode, R),
}
Expand Down Expand Up @@ -1708,9 +1708,7 @@ impl Pod {

Ok(match ::Response::status_code(&response) {
::http::StatusCode::OK => {
let mut response = response;
let mut result = String::new();
::std::io::Read::read_to_string(&mut response, &mut result).map_err(::Error::IO)?;
let result = ::std::io::BufRead::lines(::std::io::BufReader::new(response));
ReadCoreV1NamespacedPodLogResponse::Ok(result)
},
::http::StatusCode::UNAUTHORIZED => ReadCoreV1NamespacedPodLogResponse::Unauthorized(response),
Expand Down
6 changes: 2 additions & 4 deletions k8s-openapi/src/v1_9/api/core/v1/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ impl Pod {

#[derive(Debug)]
pub enum ReadCoreV1NamespacedPodLogResponse<R> where R: ::std::io::Read {
Ok(String),
Ok(::std::io::Lines<::std::io::BufReader<R>>),
Unauthorized(R),
Other(::http::StatusCode, R),
}
Expand Down Expand Up @@ -1718,9 +1718,7 @@ impl Pod {

Ok(match ::Response::status_code(&response) {
::http::StatusCode::OK => {
let mut response = response;
let mut result = String::new();
::std::io::Read::read_to_string(&mut response, &mut result).map_err(::Error::IO)?;
let result = ::std::io::BufRead::lines(::std::io::BufReader::new(response));
ReadCoreV1NamespacedPodLogResponse::Ok(result)
},
::http::StatusCode::UNAUTHORIZED => ReadCoreV1NamespacedPodLogResponse::Unauthorized(response),
Expand Down
71 changes: 48 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,24 @@ fn get_rust_type(schema_kind: &swagger20::SchemaKind, replace_namespaces: &[(Vec
}
}

fn is_read_pod_log_operation_ok_response(
operation: &swagger20::Operation,
status_code: reqwest::StatusCode,
schema: &swagger20::Schema,
) -> Result<bool, Error> {
if operation.id == "readCoreV1NamespacedPodLog" && status_code == reqwest::StatusCode::Ok {
if let swagger20::SchemaKind::Ty(swagger20::Type::String { format: None }) = schema.kind {
Ok(true)
}
else {
Err(format!("expected operation {} to have String type for status code 200", operation.id).into())
}
}
else {
Ok(false)
}
}

fn replace_namespace<'a, I>(parts: I, replace_namespaces: &[(Vec<&str>, Vec<String>)]) -> Vec<String> where I: IntoIterator<Item = &'a str> {
let parts: Vec<_> = parts.into_iter().collect();

Expand Down Expand Up @@ -806,7 +824,7 @@ fn write_operation(

let operation_responses: Result<Vec<_>, _> =
operation.responses.iter()
.map(|(status_code, schema)| {
.map(|(&status_code, schema)| {
let http_status_code = match status_code {
reqwest::StatusCode::Accepted => "ACCEPTED",
reqwest::StatusCode::Created => "CREATED",
Expand All @@ -823,7 +841,7 @@ fn write_operation(
_ => return Err(format!("unrecognized status code {}", status_code)),
};

Ok((http_status_code, variant_name, schema))
Ok((status_code, http_status_code, variant_name, schema))
})
.collect();
let operation_responses = operation_responses?;
Expand All @@ -833,14 +851,17 @@ fn write_operation(
writeln!(file, "#[derive(Debug)]")?;
}
writeln!(file, "pub enum {}<R> where R: ::std::io::Read {{", operation_result_name)?;
for (_, variant_name, schema) in &operation_responses {
for &(status_code, _, variant_name, schema) in &operation_responses {
if let Some(schema) = schema {
if is_watch {
writeln!(
file,
" {}(::serde_json::StreamDeserializer<'static, ::serde_json::de::IoRead<R>, {}>),",
variant_name, get_rust_type(&schema.kind, replace_namespaces, mod_root))?;
}
else if is_read_pod_log_operation_ok_response(operation, status_code, schema)? {
writeln!(file, " {}(::std::io::Lines<::std::io::BufReader<R>>),", variant_name)?;
}
else {
writeln!(file, " {}({}),", variant_name, get_rust_type(&schema.kind, replace_namespaces, mod_root))?;
}
Expand Down Expand Up @@ -1019,31 +1040,35 @@ fn write_operation(
writeln!(file)?;

writeln!(file, "{} Ok(match ::Response::status_code(&response) {{", indent)?;
for (http_status_code, variant_name, schema) in &operation_responses {
for &(status_code, http_status_code, variant_name, schema) in &operation_responses {
write!(file, "{} ::http::StatusCode::{} => ", indent, http_status_code)?;
if let Some(schema) = schema {
writeln!(file, "{{")?;

match &schema.kind {
swagger20::SchemaKind::Ty(swagger20::Type::String { .. }) => {
writeln!(file, "{} let mut response = response;", indent)?;
writeln!(file, "{} let mut result = String::new();", indent)?;
// TODO: Better to return a Read rather than buffer up a whole String? `pods/{}/log` can get quite large...
writeln!(file, "{} ::std::io::Read::read_to_string(&mut response, &mut result).map_err(::Error::IO)?;", indent)?;
},

swagger20::SchemaKind::Ty(swagger20::Type::Boolean { .. }) |
swagger20::SchemaKind::Ty(swagger20::Type::Integer { .. }) |
swagger20::SchemaKind::Ty(swagger20::Type::Number { .. }) => {
return Err(format!("unsupported response type {:?}", schema.kind).into());
},

_ => if is_watch {
writeln!(file, "{} let result = ::serde_json::Deserializer::from_reader(response).into_iter();", indent)?;
if is_read_pod_log_operation_ok_response(operation, status_code, schema)? {
writeln!(file, "{} let result = ::std::io::BufRead::lines(::std::io::BufReader::new(response));", indent)?;
}
else {
match &schema.kind {
swagger20::SchemaKind::Ty(swagger20::Type::String { .. }) => {
writeln!(file, "{} let mut response = response;", indent)?;
writeln!(file, "{} let mut result = String::new();", indent)?;
writeln!(file, "{} ::std::io::Read::read_to_string(&mut response, &mut result).map_err(::Error::IO)?;", indent)?;
},

swagger20::SchemaKind::Ty(swagger20::Type::Boolean { .. }) |
swagger20::SchemaKind::Ty(swagger20::Type::Integer { .. }) |
swagger20::SchemaKind::Ty(swagger20::Type::Number { .. }) => {
return Err(format!("unsupported response type {:?}", schema.kind).into());
},

_ => if is_watch {
writeln!(file, "{} let result = ::serde_json::Deserializer::from_reader(response).into_iter();", indent)?;
}
else {
writeln!(file, "{} let result = ::serde_json::from_reader(response).map_err(::Error::JSON)?;", indent)?;
},
}
else {
writeln!(file, "{} let result = ::serde_json::from_reader(response).map_err(::Error::JSON)?;", indent)?;
},
}

writeln!(file, " {}::{}(result)", operation_result_name, variant_name)?;
Expand Down

0 comments on commit e6bda30

Please sign in to comment.