-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from kumina/docker
Docker
- Loading branch information
Showing
15 changed files
with
781 additions
and
249 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,64 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/alecthomas/kingpin" | ||
) | ||
|
||
// A LogSourceFactory provides a repository of log sources that can be | ||
// instantiated from command line flags. | ||
type LogSourceFactory interface { | ||
// Init adds the factory's struct fields as flags in the | ||
// application. | ||
Init(*kingpin.Application) | ||
|
||
// New attempts to create a new log source. This is called after | ||
// flags have been parsed. Returning `nil, nil`, means the user | ||
// didn't want this log source. | ||
New(context.Context) (LogSourceCloser, error) | ||
} | ||
|
||
type LogSourceCloser interface { | ||
io.Closer | ||
LogSource | ||
} | ||
|
||
var logSourceFactories []LogSourceFactory | ||
|
||
// RegisterLogSourceFactory can be called from module `init` functions | ||
// to register factories. | ||
func RegisterLogSourceFactory(lsf LogSourceFactory) { | ||
logSourceFactories = append(logSourceFactories, lsf) | ||
} | ||
|
||
// InitLogSourceFactories runs Init on all factories. The | ||
// initialization order is arbitrary, except `fileLogSourceFactory` is | ||
// always last (the fallback). The file log source must be last since | ||
// it's enabled by default. | ||
func InitLogSourceFactories(app *kingpin.Application) { | ||
RegisterLogSourceFactory(&fileLogSourceFactory{}) | ||
|
||
for _, f := range logSourceFactories { | ||
f.Init(app) | ||
} | ||
} | ||
|
||
// NewLogSourceFromFactories iterates through the factories and | ||
// attempts to instantiate a log source. The first factory to return | ||
// success wins. | ||
func NewLogSourceFromFactories(ctx context.Context) (LogSourceCloser, error) { | ||
for _, f := range logSourceFactories { | ||
src, err := f.New(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if src != nil { | ||
return src, nil | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("no log source configured") | ||
} |
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,96 @@ | ||
// +build !nodocker | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"io" | ||
"log" | ||
"strings" | ||
|
||
"github.com/alecthomas/kingpin" | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/client" | ||
) | ||
|
||
// A DockerLogSource reads log records from the given Docker | ||
// journal. | ||
type DockerLogSource struct { | ||
client DockerClient | ||
containerID string | ||
reader *bufio.Reader | ||
} | ||
|
||
// A DockerClient is the client interface that client.Client | ||
// provides. See https://pkg.go.dev/github.com/docker/docker/client | ||
type DockerClient interface { | ||
io.Closer | ||
ContainerLogs(context.Context, string, types.ContainerLogsOptions) (io.ReadCloser, error) | ||
} | ||
|
||
// NewDockerLogSource returns a log source for reading Docker logs. | ||
func NewDockerLogSource(ctx context.Context, c DockerClient, containerID string) (*DockerLogSource, error) { | ||
r, err := c.ContainerLogs(ctx, containerID, types.ContainerLogsOptions{ | ||
ShowStdout: true, | ||
ShowStderr: true, | ||
Follow: true, | ||
Tail: "0", | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
logSrc := &DockerLogSource{ | ||
client: c, | ||
containerID: containerID, | ||
reader: bufio.NewReader(r), | ||
} | ||
|
||
return logSrc, nil | ||
} | ||
|
||
func (s *DockerLogSource) Close() error { | ||
return s.client.Close() | ||
} | ||
|
||
func (s *DockerLogSource) Path() string { | ||
return "docker:" + s.containerID | ||
} | ||
|
||
func (s *DockerLogSource) Read(ctx context.Context) (string, error) { | ||
line, err := s.reader.ReadString('\n') | ||
if err != nil { | ||
return "", err | ||
} | ||
return strings.TrimSpace(line), nil | ||
} | ||
|
||
// A dockerLogSourceFactory is a factory that can create | ||
// DockerLogSources from command line flags. | ||
type dockerLogSourceFactory struct { | ||
enable bool | ||
containerID string | ||
} | ||
|
||
func (f *dockerLogSourceFactory) Init(app *kingpin.Application) { | ||
app.Flag("docker.enable", "Read from Docker logs. Environment variable DOCKER_HOST can be used to change the address. See https://pkg.go.dev/github.com/docker/docker/client?tab=doc#NewEnvClient for more information.").Default("false").BoolVar(&f.enable) | ||
app.Flag("docker.container.id", "ID/name of the Postfix Docker container.").Default("postfix").StringVar(&f.containerID) | ||
} | ||
|
||
func (f *dockerLogSourceFactory) New(ctx context.Context) (LogSourceCloser, error) { | ||
if !f.enable { | ||
return nil, nil | ||
} | ||
|
||
log.Println("Reading log events from Docker") | ||
c, err := client.NewEnvClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewDockerLogSource(ctx, c, f.containerID) | ||
} | ||
|
||
func init() { | ||
RegisterLogSourceFactory(&dockerLogSourceFactory{}) | ||
} |
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,79 @@ | ||
// +build !nodocker | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewDockerLogSource(t *testing.T) { | ||
ctx := context.Background() | ||
c := &fakeDockerClient{} | ||
src, err := NewDockerLogSource(ctx, c, "acontainer") | ||
if err != nil { | ||
t.Fatalf("NewDockerLogSource failed: %v", err) | ||
} | ||
|
||
assert.Equal(t, []string{"acontainer"}, c.containerLogsCalls, "A call to ContainerLogs should be made.") | ||
|
||
if err := src.Close(); err != nil { | ||
t.Fatalf("Close failed: %v", err) | ||
} | ||
|
||
assert.Equal(t, 1, c.closeCalls, "A call to Close should be made.") | ||
} | ||
|
||
func TestDockerLogSource_Path(t *testing.T) { | ||
ctx := context.Background() | ||
c := &fakeDockerClient{} | ||
src, err := NewDockerLogSource(ctx, c, "acontainer") | ||
if err != nil { | ||
t.Fatalf("NewDockerLogSource failed: %v", err) | ||
} | ||
defer src.Close() | ||
|
||
assert.Equal(t, "docker:acontainer", src.Path(), "Path should be set by New.") | ||
} | ||
|
||
func TestDockerLogSource_Read(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
c := &fakeDockerClient{ | ||
logsReader: ioutil.NopCloser(strings.NewReader("Feb 13 23:31:30 ahost anid[123]: aline\n")), | ||
} | ||
src, err := NewDockerLogSource(ctx, c, "acontainer") | ||
if err != nil { | ||
t.Fatalf("NewDockerLogSource failed: %v", err) | ||
} | ||
defer src.Close() | ||
|
||
s, err := src.Read(ctx) | ||
if err != nil { | ||
t.Fatalf("Read failed: %v", err) | ||
} | ||
assert.Equal(t, "Feb 13 23:31:30 ahost anid[123]: aline", s, "Read should get data from the journal entry.") | ||
} | ||
|
||
type fakeDockerClient struct { | ||
logsReader io.ReadCloser | ||
|
||
containerLogsCalls []string | ||
closeCalls int | ||
} | ||
|
||
func (c *fakeDockerClient) ContainerLogs(ctx context.Context, containerID string, opts types.ContainerLogsOptions) (io.ReadCloser, error) { | ||
c.containerLogsCalls = append(c.containerLogsCalls, containerID) | ||
return c.logsReader, nil | ||
} | ||
|
||
func (c *fakeDockerClient) Close() error { | ||
c.closeCalls++ | ||
return nil | ||
} |
Oops, something went wrong.