Skip to content

Commit

Permalink
Merge pull request #297 from leon3s/fix-attach-container
Browse files Browse the repository at this point in the history
bugfix: attach container
  • Loading branch information
fussybeaver authored Mar 16, 2023
2 parents 7b9673c + b96e959 commit 44ce77c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ impl Docker {
);

let (read, write) = self.process_upgraded(req).await?;
let log = FramedRead::new(read, NewlineLogOutputDecoder::new());
let log = FramedRead::new(read, NewlineLogOutputDecoder::new(true));

Ok(AttachContainerResults {
output: Box::pin(log),
Expand Down
2 changes: 1 addition & 1 deletion src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ impl Docker {
) -> impl Stream<Item = Result<LogOutput, Error>> {
FramedRead::new(
StreamReader::new(res.into_body().map_err(Error::from)),
NewlineLogOutputDecoder::new(),
NewlineLogOutputDecoder::new(false),
)
}

Expand Down
3 changes: 2 additions & 1 deletion src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ impl Docker {

let (read, write) = self.process_upgraded(req).await?;

let log = FramedRead::with_capacity(read, NewlineLogOutputDecoder::new(), capacity);
let log =
FramedRead::with_capacity(read, NewlineLogOutputDecoder::new(true), capacity);
Ok(StartExecResults::Attached {
output: Box::pin(log),
input: Box::pin(write),
Expand Down
26 changes: 21 additions & 5 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ enum NewlineLogOutputDecoderState {
#[derive(Debug, Copy, Clone)]
pub(crate) struct NewlineLogOutputDecoder {
state: NewlineLogOutputDecoderState,
is_tcp: bool,
}

impl NewlineLogOutputDecoder {
pub(crate) fn new() -> NewlineLogOutputDecoder {
pub(crate) fn new(is_tcp: bool) -> NewlineLogOutputDecoder {
NewlineLogOutputDecoder {
state: NewlineLogOutputDecoderState::WaitingHeader,
is_tcp,
}
}
}
Expand All @@ -45,9 +47,12 @@ impl Decoder for NewlineLogOutputDecoder {
NewlineLogOutputDecoderState::WaitingHeader => {
// `start_exec` API on unix socket will emit values without a header
if !src.is_empty() && src[0] > 2 {
debug!(
"NewlineLogOutputDecoder: no header found, return LogOutput::Console"
);
if self.is_tcp {
debug!("NewlineLogOutputDecoder: no header, but is_tcp is true returning raw data");
return Ok(Some(LogOutput::Console {
message: src.split().freeze(),
}));
}
let nl_index = src.iter().position(|b| *b == b'\n');
if let Some(pos) = nl_index {
debug!("NewlineLogOutputDecoder: newline found, pos = {}", pos + 1);
Expand Down Expand Up @@ -367,9 +372,20 @@ mod tests {

#[test]
fn newline_decode_no_header() {
let expected = &b"2023-01-14T23:17:27.496421984-05:00 [lighttpd] 2023/01/14 23"[..];
let mut buf = BytesMut::from(expected);
let mut codec: NewlineLogOutputDecoder = NewlineLogOutputDecoder::new(true);

assert_eq!(
codec.decode(&mut buf).unwrap(),
Some(LogOutput::Console {
message: bytes::Bytes::from(expected)
})
);

let mut buf =
BytesMut::from(&b"2023-01-14T23:17:27.496421984-05:00 [lighttpd] 2023/01/14 23"[..]);
let mut codec: NewlineLogOutputDecoder = NewlineLogOutputDecoder::new();
let mut codec: NewlineLogOutputDecoder = NewlineLogOutputDecoder::new(false);

assert_eq!(codec.decode(&mut buf).unwrap(), None);

Expand Down

0 comments on commit 44ce77c

Please sign in to comment.