-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: cri manager could get the log of container
Signed-off-by: YaoZengzeng <[email protected]>
- Loading branch information
1 parent
fa50191
commit 357d342
Showing
6 changed files
with
161 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package containerio | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"io" | ||
"os" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" | ||
) | ||
|
||
const ( | ||
// delimiter used in cri logging format. | ||
delimiter = ' ' | ||
// eof is end-of-line. | ||
eol = '\n' | ||
// timestampFormat is the timestamp format used in cri logging format. | ||
timestampFormat = time.RFC3339Nano | ||
// pipeBufSize is the system PIPE_BUF size, on linux it is 4096 bytes. | ||
pipeBufSize = 4096 | ||
// bufSize is the size of the read buffer. | ||
bufSize = pipeBufSize - len(timestampFormat) - len(Stdout) - 2 /*2 delimiter*/ - 1 /*eol*/ | ||
) | ||
|
||
// StreamType is the type of the stream. | ||
type StreamType string | ||
|
||
const ( | ||
// Stdin stream type. | ||
Stdin StreamType = "stdin" | ||
// Stdout stream type. | ||
Stdout StreamType = "stdout" | ||
// Stderr stream type. | ||
Stderr StreamType = "stderr" | ||
) | ||
|
||
func init() { | ||
Register(func() Backend { | ||
return &criLogFile{} | ||
}) | ||
} | ||
|
||
type criLogFile struct { | ||
file *os.File | ||
pipeWriter *io.PipeWriter | ||
pipeReader *io.PipeReader | ||
closed bool | ||
} | ||
|
||
func (c *criLogFile) Name() string { | ||
return "cri-log-file" | ||
} | ||
|
||
func (c *criLogFile) Init(opt *Option) error { | ||
c.file = opt.criLogFile | ||
c.pipeReader, c.pipeWriter = io.Pipe() | ||
// TODO: redirect stderr. | ||
go redirectLogs(c.file, c.pipeReader, Stdout) | ||
return nil | ||
} | ||
|
||
func redirectLogs(w io.WriteCloser, r io.ReadCloser, stream StreamType) { | ||
defer r.Close() | ||
defer w.Close() | ||
streamBytes := []byte(stream) | ||
delimiterBytes := []byte{delimiter} | ||
partialBytes := []byte(runtime.LogTagPartial) | ||
fullBytes := []byte(runtime.LogTagFull) | ||
br := bufio.NewReaderSize(r, bufSize) | ||
for { | ||
lineBytes, isPrefix, err := br.ReadLine() | ||
if err != nil { | ||
if err == io.EOF { | ||
logrus.Infof("finish redirecting log file") | ||
} else { | ||
logrus.Errorf("failed to redirect log file: %v", err) | ||
} | ||
return | ||
} | ||
tagBytes := fullBytes | ||
if isPrefix { | ||
tagBytes = partialBytes | ||
} | ||
timestampBytes := time.Now().AppendFormat(nil, time.RFC3339Nano) | ||
data := bytes.Join([][]byte{timestampBytes, streamBytes, tagBytes, lineBytes}, delimiterBytes) | ||
data = append(data, eol) | ||
_, err = w.Write(data) | ||
if err != nil { | ||
logrus.Errorf("failed to write %q log to log file: %v", stream, err) | ||
} | ||
} | ||
} | ||
|
||
func (c *criLogFile) Out() io.Writer { | ||
return c.pipeWriter | ||
} | ||
|
||
func (c *criLogFile) In() io.Reader { | ||
// Log doesn't need stdin. | ||
return nil | ||
} | ||
|
||
func (c *criLogFile) Close() error { | ||
if c.closed { | ||
return nil | ||
} | ||
c.closed = true | ||
return c.pipeWriter.Close() | ||
} |
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
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