2018-10-06 03:32:43 +00:00
|
|
|
package docklog
|
|
|
|
|
|
|
|
import (
|
2022-07-28 18:46:56 +00:00
|
|
|
"context"
|
2018-10-06 03:32:43 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-03-14 20:23:11 +00:00
|
|
|
"math/rand"
|
|
|
|
"strings"
|
2019-04-16 01:20:09 +00:00
|
|
|
"sync"
|
2019-02-19 13:38:39 +00:00
|
|
|
"time"
|
2018-10-06 03:32:43 +00:00
|
|
|
|
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
2022-07-28 18:46:56 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
|
2018-10-06 03:32:43 +00:00
|
|
|
"github.com/hashicorp/nomad/client/lib/fifo"
|
|
|
|
)
|
|
|
|
|
2018-10-16 17:49:50 +00:00
|
|
|
// DockerLogger is a small utility to forward logs from a docker container to a target
|
2018-10-06 03:32:43 +00:00
|
|
|
// destination
|
2018-10-16 17:49:50 +00:00
|
|
|
type DockerLogger interface {
|
2018-10-06 03:32:43 +00:00
|
|
|
Start(*StartOpts) error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartOpts are the options needed to start docker log monitoring
|
|
|
|
type StartOpts struct {
|
|
|
|
// Endpoint sets the docker client endpoint, defaults to environment if not set
|
|
|
|
Endpoint string
|
|
|
|
|
|
|
|
// ContainerID of the container to monitor logs for
|
|
|
|
ContainerID string
|
2019-04-25 02:00:00 +00:00
|
|
|
TTY bool
|
2018-10-06 03:32:43 +00:00
|
|
|
|
|
|
|
// Stdout path to fifo
|
|
|
|
Stdout string
|
|
|
|
//Stderr path to fifo
|
|
|
|
Stderr string
|
|
|
|
|
2019-02-19 13:38:39 +00:00
|
|
|
// StartTime is the Unix time that the docker logger should fetch logs beginning
|
|
|
|
// from
|
|
|
|
StartTime int64
|
|
|
|
|
2018-10-06 03:32:43 +00:00
|
|
|
// TLS settings for docker client
|
|
|
|
TLSCert string
|
|
|
|
TLSKey string
|
|
|
|
TLSCA string
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:49:50 +00:00
|
|
|
// NewDockerLogger returns an implementation of the DockerLogger interface
|
|
|
|
func NewDockerLogger(logger hclog.Logger) DockerLogger {
|
2019-03-14 20:23:11 +00:00
|
|
|
return &dockerLogger{
|
|
|
|
logger: logger,
|
|
|
|
doneCh: make(chan interface{}),
|
|
|
|
}
|
2018-10-06 03:32:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 17:49:50 +00:00
|
|
|
// dockerLogger implements the DockerLogger interface
|
2018-10-06 03:32:43 +00:00
|
|
|
type dockerLogger struct {
|
|
|
|
logger hclog.Logger
|
|
|
|
|
2019-04-16 01:20:09 +00:00
|
|
|
stdout io.WriteCloser
|
|
|
|
stderr io.WriteCloser
|
|
|
|
stdLock sync.Mutex
|
2019-03-14 20:23:11 +00:00
|
|
|
|
2019-04-16 01:20:09 +00:00
|
|
|
cancelCtx context.CancelFunc
|
|
|
|
doneCh chan interface{}
|
2018-10-06 03:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start log monitoring
|
|
|
|
func (d *dockerLogger) Start(opts *StartOpts) error {
|
|
|
|
client, err := d.getDockerClient(opts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open docker client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
d.cancelCtx = cancel
|
|
|
|
|
2019-02-19 13:38:39 +00:00
|
|
|
go func() {
|
2019-03-14 20:23:11 +00:00
|
|
|
defer close(d.doneCh)
|
|
|
|
|
2019-04-16 14:48:25 +00:00
|
|
|
stdout, stderr, err := d.openStreams(ctx, opts)
|
2019-04-16 01:20:09 +00:00
|
|
|
if err != nil {
|
|
|
|
d.logger.Error("log streaming ended with terminal error", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-19 13:38:39 +00:00
|
|
|
sinceTime := time.Unix(opts.StartTime, 0)
|
2019-03-14 20:23:11 +00:00
|
|
|
backoff := 0.0
|
2019-02-19 13:38:39 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
logOpts := docker.LogsOptions{
|
|
|
|
Context: ctx,
|
|
|
|
Container: opts.ContainerID,
|
2019-04-16 01:20:09 +00:00
|
|
|
OutputStream: stdout,
|
|
|
|
ErrorStream: stderr,
|
2019-02-19 13:38:39 +00:00
|
|
|
Since: sinceTime.Unix(),
|
|
|
|
Follow: true,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
2019-04-28 21:26:15 +00:00
|
|
|
|
|
|
|
// When running in TTY, we must use a raw terminal.
|
|
|
|
// If not, we set RawTerminal to false to allow docker client
|
|
|
|
// to interpret special stdout/stderr messages
|
|
|
|
RawTerminal: opts.TTY,
|
2019-02-19 13:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := client.Logs(logOpts)
|
2019-02-19 14:12:28 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
// If context is terminated then we can safely break the loop
|
|
|
|
return
|
2019-03-14 20:23:11 +00:00
|
|
|
} else if err == nil {
|
|
|
|
backoff = 0.0
|
|
|
|
} else if isLoggingTerminalError(err) {
|
|
|
|
d.logger.Error("log streaming ended with terminal error", "error", err)
|
|
|
|
return
|
2019-02-19 14:12:28 +00:00
|
|
|
} else if err != nil {
|
2019-03-14 20:23:11 +00:00
|
|
|
backoff = nextBackoff(backoff)
|
|
|
|
d.logger.Error("log streaming ended with error", "error", err, "retry_in", backoff)
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(backoff) * time.Second)
|
2019-02-19 13:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sinceTime = time.Now()
|
|
|
|
|
2020-12-10 15:29:18 +00:00
|
|
|
container, err := client.InspectContainerWithOptions(docker.InspectContainerOptions{
|
|
|
|
ID: opts.ContainerID,
|
|
|
|
})
|
2019-02-19 13:38:39 +00:00
|
|
|
if err != nil {
|
|
|
|
_, notFoundOk := err.(*docker.NoSuchContainer)
|
|
|
|
if !notFoundOk {
|
|
|
|
return
|
|
|
|
}
|
2019-02-19 16:23:23 +00:00
|
|
|
} else if !container.State.Running {
|
2019-02-19 13:38:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-10-06 03:32:43 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:48:25 +00:00
|
|
|
// openStreams open logger stdout/stderr; should be called in a background goroutine to avoid locking up
|
|
|
|
// process to avoid locking goroutine process
|
|
|
|
func (d *dockerLogger) openStreams(ctx context.Context, opts *StartOpts) (stdout, stderr io.WriteCloser, err error) {
|
2019-04-16 01:20:09 +00:00
|
|
|
d.stdLock.Lock()
|
|
|
|
stdoutF, stderrF := d.stdout, d.stderr
|
|
|
|
d.stdLock.Unlock()
|
|
|
|
|
|
|
|
if stdoutF != nil && stderrF != nil {
|
|
|
|
return stdoutF, stderrF, nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:48:25 +00:00
|
|
|
// opening a fifo may block indefinitely until a reader end opens, so
|
|
|
|
// we preform open() without holding the stdLock, so Stop and interleave.
|
|
|
|
// This a defensive measure - logmon (the reader end) should be up and
|
|
|
|
// started before dockerLogger is started
|
2019-04-16 01:20:09 +00:00
|
|
|
if stdoutF == nil {
|
|
|
|
stdoutF, err = fifo.OpenWriter(opts.Stdout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if stderrF == nil {
|
|
|
|
stderrF, err = fifo.OpenWriter(opts.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:48:25 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
// Stop was called and don't need files anymore
|
|
|
|
stdoutF.Close()
|
|
|
|
stderrF.Close()
|
|
|
|
return nil, nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
2019-04-16 01:20:09 +00:00
|
|
|
d.stdLock.Lock()
|
|
|
|
d.stdout, d.stderr = stdoutF, stderrF
|
|
|
|
d.stdLock.Unlock()
|
|
|
|
|
|
|
|
return stdoutF, stderrF, nil
|
|
|
|
}
|
|
|
|
|
2018-10-06 03:32:43 +00:00
|
|
|
// Stop log monitoring
|
|
|
|
func (d *dockerLogger) Stop() error {
|
|
|
|
if d.cancelCtx != nil {
|
|
|
|
d.cancelCtx()
|
|
|
|
}
|
2019-04-16 01:20:09 +00:00
|
|
|
|
|
|
|
d.stdLock.Lock()
|
|
|
|
stdout, stderr := d.stdout, d.stderr
|
|
|
|
d.stdLock.Unlock()
|
|
|
|
|
|
|
|
if stdout != nil {
|
|
|
|
stdout.Close()
|
2018-10-06 03:32:43 +00:00
|
|
|
}
|
2019-04-16 01:20:09 +00:00
|
|
|
if stderr != nil {
|
|
|
|
stderr.Close()
|
2018-10-06 03:32:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dockerLogger) getDockerClient(opts *StartOpts) (*docker.Client, error) {
|
|
|
|
var err error
|
|
|
|
var merr multierror.Error
|
|
|
|
var newClient *docker.Client
|
|
|
|
|
|
|
|
// Default to using whatever is configured in docker.endpoint. If this is
|
|
|
|
// not specified we'll fall back on NewClientFromEnv which reads config from
|
|
|
|
// the DOCKER_* environment variables DOCKER_HOST, DOCKER_TLS_VERIFY, and
|
|
|
|
// DOCKER_CERT_PATH. This allows us to lock down the config in production
|
|
|
|
// but also accept the standard ENV configs for dev and test.
|
|
|
|
if opts.Endpoint != "" {
|
|
|
|
if opts.TLSCert+opts.TLSKey+opts.TLSCA != "" {
|
|
|
|
d.logger.Debug("using TLS client connection to docker", "endpoint", opts.Endpoint)
|
|
|
|
newClient, err = docker.NewTLSClient(opts.Endpoint, opts.TLSCert, opts.TLSKey, opts.TLSCA)
|
|
|
|
if err != nil {
|
|
|
|
merr.Errors = append(merr.Errors, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d.logger.Debug("using plaintext client connection to docker", "endpoint", opts.Endpoint)
|
|
|
|
newClient, err = docker.NewClient(opts.Endpoint)
|
|
|
|
if err != nil {
|
|
|
|
merr.Errors = append(merr.Errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d.logger.Debug("using client connection initialized from environment")
|
|
|
|
newClient, err = docker.NewClientFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
merr.Errors = append(merr.Errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newClient, merr.ErrorOrNil()
|
|
|
|
}
|
2019-03-14 20:23:11 +00:00
|
|
|
|
|
|
|
func isLoggingTerminalError(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if apiErr, ok := err.(*docker.Error); ok {
|
|
|
|
switch apiErr.Status {
|
|
|
|
case 501:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
terminals := []string{
|
|
|
|
"configured logging driver does not support reading",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range terminals {
|
|
|
|
if strings.Contains(err.Error(), c) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// nextBackoff returns the next backoff period in seconds given current backoff
|
|
|
|
func nextBackoff(backoff float64) float64 {
|
|
|
|
if backoff < 0.5 {
|
|
|
|
backoff = 0.5
|
|
|
|
}
|
|
|
|
|
|
|
|
backoff = backoff * 1.15 * (1.0 + rand.Float64())
|
|
|
|
if backoff > 120 {
|
|
|
|
backoff = 120
|
|
|
|
} else if backoff < 0.5 {
|
|
|
|
backoff = 0.5
|
|
|
|
}
|
|
|
|
|
|
|
|
return backoff
|
|
|
|
}
|