-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add StdoutConfig and StderrConfig to steps.
#3103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
39fa1f9
Added `-stdout_path` and `-stderr_path` flags to entrypoint.
2688e38
Renamed tests.
988794b
Made copyAsync copy stream until EOF if stopCh is nil.
739d6d7
Use File.Stat to get file size in test.
96e8e5b
Removed asyncWriwer wrapper.
d342aef
Added README.
8ab5b33
Added `StdoutConfig` and `StderrConfig` fields in `v1beta1.Step`.
6e78d3b
Passed `StepOutputConfig` to entrypoint.
a99a19a
Fixed race condition when asynchronously copying outputs.
a52c86f
Added an integration test for redirecting step outputs.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
| "math" | ||
| "os" | ||
| "time" | ||
| ) | ||
|
|
||
| type ioResult struct { | ||
| numBytes int | ||
| err error | ||
| } | ||
|
|
||
| // readAsync implements a non-blocking read. | ||
| func readAsync(r io.Reader, p []byte) <-chan ioResult { | ||
| resultCh := make(chan ioResult, 1) | ||
| go func() { | ||
| defer close(resultCh) | ||
| n, err := r.Read(p) | ||
| resultCh <- ioResult{n, err} | ||
| }() | ||
| return resultCh | ||
| } | ||
|
|
||
| // copyAsync performs a non-blocking copy from src to dst. | ||
| func copyAsync(dst io.Writer, src io.Reader, stopCh <-chan struct{}) <-chan ioResult { | ||
| resultCh := make(chan ioResult, 1) | ||
| go func() { | ||
| defer close(resultCh) | ||
|
|
||
| buf := make([]byte, 1024) | ||
| result := ioResult{} | ||
| readCh := readAsync(src, buf) | ||
| stopped := false | ||
| done := false | ||
| timer := time.NewTimer(time.Duration(math.MaxInt64)) | ||
| defer timer.Stop() | ||
|
|
||
| for !done { | ||
| // If the stop channel is signalled, continue the loop to read the rest of the available | ||
| // data with a short timeout instead of a non-blocking read to mitigate the race between | ||
| // this loop and Read() running in another goroutine. | ||
| if stopped { | ||
| if !timer.Stop() { | ||
| <-timer.C | ||
| } | ||
| timer.Reset(100 * time.Millisecond) | ||
| } | ||
| select { | ||
| case r := <-readCh: | ||
| if r.numBytes != 0 { | ||
| nw, err := dst.Write(buf[:r.numBytes]) | ||
| result.numBytes += nw | ||
| if err != nil { | ||
| result.err = err | ||
| done = true | ||
| } else if nw < r.numBytes { | ||
| result.err = io.ErrShortWrite | ||
| done = true | ||
| } | ||
| } | ||
| if r.err != nil { | ||
| if !errors.Is(r.err, io.EOF) { | ||
| result.err = r.err | ||
| } | ||
| done = true | ||
| } | ||
| if !done { | ||
| readCh = readAsync(src, buf) | ||
| } | ||
| case <-stopCh: | ||
| stopped = true | ||
| stopCh = nil | ||
| case <-timer.C: | ||
| done = true | ||
| } | ||
| } | ||
|
|
||
| resultCh <- result | ||
| }() | ||
| return resultCh | ||
| } | ||
|
|
||
| // asyncWriter creates a write that duplicates its writes to the provided writer asynchronously. | ||
| func asyncWriter(w io.Writer, stopCh <-chan struct{}) (io.Writer, <-chan error, error) { | ||
| pr, pw, err := os.Pipe() | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| doneCh := make(chan error, 1) | ||
| go func() { | ||
| defer close(doneCh) | ||
|
|
||
| if err := (<-copyAsync(w, pr, stopCh)).err; err != nil { | ||
| doneCh <- err | ||
| } | ||
| pr.Close() | ||
| pw.Close() | ||
| }() | ||
|
|
||
| return pw, doneCh, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "io" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestCopyAsyncEOF(t *testing.T) { | ||
| stopCh := make(chan struct{}, 1) | ||
| defer close(stopCh) | ||
|
|
||
| pr, pw := io.Pipe() | ||
| defer pr.Close() | ||
|
|
||
| buf := &bytes.Buffer{} | ||
| copyCh := copyAsync(buf, pr, stopCh) | ||
|
|
||
| expectedString := "hello world" | ||
| pw.Write([]byte(expectedString)) | ||
| pw.Close() | ||
|
|
||
| if c := <-copyCh; c.err != nil { | ||
| t.Fatalf("Unexpected error: %v", c.err) | ||
| } | ||
| if buf.String() != expectedString { | ||
| t.Errorf("got: %v, wanted: %v", buf.String(), expectedString) | ||
| } | ||
| } | ||
|
|
||
| func TestCopyAsyncStop(t *testing.T) { | ||
| stopCh := make(chan struct{}, 1) | ||
|
|
||
| pr, pw := io.Pipe() | ||
| defer pr.Close() | ||
| defer pw.Close() | ||
|
|
||
| buf := &bytes.Buffer{} | ||
| copyCh := copyAsync(buf, pr, stopCh) | ||
|
|
||
| expectedString := "hello world" | ||
| pw.Write([]byte(expectedString)) | ||
|
|
||
| close(stopCh) | ||
|
|
||
| if c := <-copyCh; c.err != nil { | ||
| t.Fatalf("Unexpected error: %v", c.err) | ||
| } | ||
| if buf.String() != expectedString { | ||
| t.Errorf("got: %v, wanted: %v", buf.String(), expectedString) | ||
| } | ||
| } | ||
|
|
||
| func TestCopyAsyncError(t *testing.T) { | ||
| stopCh := make(chan struct{}, 1) | ||
| defer close(stopCh) | ||
|
|
||
| pr, pw := io.Pipe() | ||
| defer pr.Close() | ||
|
|
||
| buf := &bytes.Buffer{} | ||
| copyCh := copyAsync(buf, pr, stopCh) | ||
|
|
||
| expectedString := "hello world" | ||
| expectedError := errors.New("test error") | ||
| pw.Write([]byte(expectedString)) | ||
| pw.CloseWithError(expectedError) | ||
|
|
||
| if c := <-copyCh; !errors.Is(c.err, expectedError) { | ||
| t.Errorf("Expected error %v but got %v", expectedError, c.err) | ||
| } | ||
| if buf.String() != expectedString { | ||
| t.Errorf("got: %v, wanted: %v", buf.String(), expectedString) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest putting
1024into a named constant with any relevant info about its value in an accompanying comment.