-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interface out log writer for docker/podman formats
- Loading branch information
Showing
4 changed files
with
122 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,56 @@ | ||
package containerclient | ||
|
||
import ( | ||
"errors" | ||
"encoding/json" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/quay/quay-builder/rpc" | ||
) | ||
|
||
// PodmanRPCWriter implements a RPCWriter. | ||
// Unlike the Docker daemon, Podman's build call outputs plain string, and not JSON encoded data, | ||
// so we need to serialize each line into a Response struct before logging it to an rpc.Client. | ||
type PodmanRPCWriter struct { | ||
client rpc.Client | ||
errResponse *Response | ||
partialBuffer *partialBuffer | ||
hasPartialBuffer bool | ||
} | ||
|
||
// Write implements the io.Writer interface for RPCWriter. | ||
func (w * PodmanRPCWriter) Write(p []byte) (n int, err error) { | ||
// Unlike docker, libpod parses the JSON encoded data from stream before writing the output, | ||
// without the option of returning the raw data instead. | ||
// Instead of decoding the stream into a Response, we set the Response's "Stream" before | ||
// marshaling it into JSON to be logged. | ||
originalLength := len(p) | ||
|
||
var m Response | ||
m.Stream = string(p) | ||
|
||
jsonData, err := json.Marshal(&m) | ||
if err != nil { | ||
log.Fatalf("Error when marshaling logs: %v", err) | ||
} | ||
|
||
err = w.client.PublishBuildLogEntry(string(jsonData)) | ||
if err != nil { | ||
log.Fatalf("Failed to publish log entry: %v", err) | ||
} | ||
|
||
return originalLength, nil | ||
} | ||
|
||
func (w *PodmanRPCWriter) ErrResponse() (error, bool) { | ||
// libpod already parses the JSON stream before writing to output. | ||
// So the error would not be returned from the output stream,. but as | ||
// the return value of the API call instead. | ||
// See https://github.com/containers/podman/blob/master/pkg/bindings/images/build.go#L175 | ||
return nil, false | ||
} | ||
|
||
func (w *PodmanRPCWriter) ResetError() { | ||
w.errResponse = nil | ||
} |