Skip to content
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

Add the ability to configure agent logging #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,34 @@ type (
}

Agent struct {
Token string
Image string `default:"drone/drone-runner-docker:1"`
Concurrency int `default:"2"`
OS string `default:"linux"`
Arch string `default:"amd64"`
Version string
Kernel string
EnvironFile string `envconfig:"DRONE_AGENT_ENV_FILE"`
Environ []string
Volumes []string
Ports []string `envconfig:"DRONE_AGENT_PUBLISHED_PORTS"`
Labels map[string]string `envconfig:"DRONE_AGENT_LABELS"`
NamePrefix string `envconfig:"DRONE_AGENT_NAME_PREFIX" default:"agent-"`
Token string
Image string `default:"drone/drone-runner-docker:1"`
Concurrency int `default:"2"`
OS string `default:"linux"`
Arch string `default:"amd64"`
Version string
Kernel string
EnvironFile string `envconfig:"DRONE_AGENT_ENV_FILE"`
Environ []string
Volumes []string
Ports []string `envconfig:"DRONE_AGENT_PUBLISHED_PORTS"`
Labels map[string]string `envconfig:"DRONE_AGENT_LABELS"`
NamePrefix string `envconfig:"DRONE_AGENT_NAME_PREFIX" default:"agent-"`
LoggingDriver string `envconfig:"DRONE_AGENT_LOGGING_DRIVER"`
LoggingOptions map[string]string `envconfig:"DRONE_AGENT_LOGGING_OPTIONS"`
}

Runner Runner

GC struct {
Enabled bool `envconfig:"DRONE_GC_ENABLED"`
Image string `envconfig:"DRONE_GC_IMAGE" default:"drone/gc"`
Debug bool `envconfig:"DRONE_GC_DEBUG"`
Images []string `envconfig:"DRONE_GC_IGNORE_IMAGES"`
Interval time.Duration `envconfig:"DRONE_GC_INTERVAL" default:"30m"`
Cache string `envconfig:"DRONE_GC_CACHE" default:"10gb"`
Enabled bool `envconfig:"DRONE_GC_ENABLED"`
Image string `envconfig:"DRONE_GC_IMAGE" default:"drone/gc"`
Debug bool `envconfig:"DRONE_GC_DEBUG"`
Images []string `envconfig:"DRONE_GC_IGNORE_IMAGES"`
Interval time.Duration `envconfig:"DRONE_GC_INTERVAL" default:"30m"`
Cache string `envconfig:"DRONE_GC_CACHE" default:"10gb"`
LoggingDriver string `envconfig:"DRONE_GC_LOGGING_DRIVER"`
LoggingOptions map[string]string `envconfig:"DRONE_GC_LOGGING_OPTIONS"`
}

Reaper struct {
Expand All @@ -86,10 +90,12 @@ type (
}

Watchtower struct {
Enabled bool `envconfig:"DRONE_WATCHTOWER_ENABLED"`
Image string `envconfig:"DRONE_WATCHTOWER_IMAGE" default:"webhippie/watchtower"`
Interval int `envconfig:"DRONE_WATCHTOWER_INTERVAL" default:"300"`
Timeout time.Duration `envconfig:"DRONE_WATCHTOWER_TIMEOUT" default:"120m"`
Enabled bool `envconfig:"DRONE_WATCHTOWER_ENABLED"`
Image string `envconfig:"DRONE_WATCHTOWER_IMAGE" default:"webhippie/watchtower"`
Interval int `envconfig:"DRONE_WATCHTOWER_INTERVAL" default:"300"`
Timeout time.Duration `envconfig:"DRONE_WATCHTOWER_TIMEOUT" default:"120m"`
LoggingDriver string `envconfig:"DRONE_WATCHTOWER_LOGGING_DRIVER"`
LoggingOptions map[string]string `envconfig:"DRONE_WATCHTOWER_LOGGING_OPTIONS"`
}

HTTP struct {
Expand Down
2 changes: 1 addition & 1 deletion engine/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/drone/autoscaler"
)

// clientFunc defines a builder funciton used to build and return
// clientFunc defines a builder function used to build and return
// the docker client from a Server. This is primarily used for
// mock unit testing.
type clientFunc func(*autoscaler.Server) (docker.APIClient, io.Closer, error)
Expand Down
58 changes: 32 additions & 26 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,38 @@ func New(
client: newDockerClient,
},
installer: &installer{
metrics: metrics,
servers: servers,
os: config.Agent.OS,
arch: config.Agent.Arch,
image: config.Agent.Image,
secret: config.Agent.Token,
envs: config.Agent.Environ,
volumes: config.Agent.Volumes,
ports: config.Agent.Ports,
labels: config.Agent.Labels,
proto: config.Server.Proto,
host: config.Server.Host,
client: newDockerClient,
runner: config.Runner,
checkInterval: config.Check.Interval,
checkDeadline: config.Check.Deadline,
gcEnabled: config.GC.Enabled,
gcDebug: config.GC.Debug,
gcImage: config.GC.Image,
gcIgnore: config.GC.Images,
gcInterval: config.GC.Interval,
gcCache: config.GC.Cache,
watchtowerEnabled: config.Watchtower.Enabled,
watchtowerImage: config.Watchtower.Image,
watchtowerTimeout: config.Watchtower.Timeout,
watchtowerInterval: config.Watchtower.Interval,
metrics: metrics,
servers: servers,
os: config.Agent.OS,
arch: config.Agent.Arch,
image: config.Agent.Image,
secret: config.Agent.Token,
envs: config.Agent.Environ,
volumes: config.Agent.Volumes,
ports: config.Agent.Ports,
labels: config.Agent.Labels,
loggingDriver: config.Agent.LoggingDriver,
loggingOptions: config.Agent.LoggingOptions,
proto: config.Server.Proto,
host: config.Server.Host,
client: newDockerClient,
runner: config.Runner,
checkInterval: config.Check.Interval,
checkDeadline: config.Check.Deadline,
gcEnabled: config.GC.Enabled,
gcDebug: config.GC.Debug,
gcImage: config.GC.Image,
gcIgnore: config.GC.Images,
gcInterval: config.GC.Interval,
gcCache: config.GC.Cache,
gcLoggingDriver: config.GC.LoggingDriver,
gcLoggingOptions: config.GC.LoggingOptions,
watchtowerEnabled: config.Watchtower.Enabled,
watchtowerImage: config.Watchtower.Image,
watchtowerTimeout: config.Watchtower.Timeout,
watchtowerInterval: config.Watchtower.Interval,
watchtowerLoggingDriver: config.Watchtower.LoggingDriver,
watchtowerLoggingOptions: config.Watchtower.LoggingOptions,
},
pinger: &pinger{
servers: servers,
Expand Down
64 changes: 53 additions & 11 deletions engine/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,27 @@ type installer struct {
keepaliveTimeout time.Duration
runner config.Runner
labels map[string]string
loggingDriver string
loggingOptions map[string]string

checkInterval time.Duration
checkDeadline time.Duration

gcEnabled bool
gcDebug bool
gcImage string
gcIgnore []string
gcInterval time.Duration
gcCache string

watchtowerEnabled bool
watchtowerImage string
watchtowerInterval int
watchtowerTimeout time.Duration
gcEnabled bool
gcDebug bool
gcImage string
gcIgnore []string
gcInterval time.Duration
gcCache string
gcLoggingDriver string
gcLoggingOptions map[string]string

watchtowerEnabled bool
watchtowerImage string
watchtowerInterval int
watchtowerTimeout time.Duration
watchtowerLoggingDriver string
watchtowerLoggingOptions map[string]string

servers autoscaler.ServerStore
metrics metrics.Collector
Expand Down Expand Up @@ -225,6 +231,10 @@ poller:
return i.errorUpdate(ctx, instance, err)
}

if i.loggingDriver == "" {
i.loggingDriver = i.getDaemonLoggingDriver(ctx, client)
}

res, err := client.ContainerCreate(ctx,
&container.Config{
Image: i.image,
Expand All @@ -250,6 +260,10 @@ poller:
RestartPolicy: container.RestartPolicy{
Name: "always",
},
LogConfig: container.LogConfig{
Type: i.loggingDriver,
Config: i.loggingOptions,
},
}, nil, "agent")

if err != nil {
Expand Down Expand Up @@ -314,6 +328,11 @@ poller:

func (i *installer) setupWatchtower(ctx context.Context, client docker.APIClient) error {
vols := []string{"/var/run/docker.sock:/var/run/docker.sock"}

if i.watchtowerLoggingDriver == "" {
i.watchtowerLoggingDriver = i.getDaemonLoggingDriver(ctx, client)
}

res, err := client.ContainerCreate(ctx,
&container.Config{
Image: i.watchtowerImage,
Expand All @@ -332,6 +351,10 @@ func (i *installer) setupWatchtower(ctx context.Context, client docker.APIClient
RestartPolicy: container.RestartPolicy{
Name: "always",
},
LogConfig: container.LogConfig{
Type: i.watchtowerLoggingDriver,
Config: i.watchtowerLoggingOptions,
},
}, nil, "watchtower")
if err != nil {
return err
Expand Down Expand Up @@ -366,6 +389,10 @@ func (i *installer) setupGarbageCollector(ctx context.Context, client docker.API
io.Copy(ioutil.Discard, rc)
rc.Close()

if i.gcLoggingDriver == "" {
i.gcLoggingDriver = i.getDaemonLoggingDriver(ctx, client)
}

res, err := client.ContainerCreate(ctx,
&container.Config{
Image: i.gcImage,
Expand All @@ -382,13 +409,28 @@ func (i *installer) setupGarbageCollector(ctx context.Context, client docker.API
RestartPolicy: container.RestartPolicy{
Name: "always",
},
LogConfig: container.LogConfig{
Type: i.gcLoggingDriver,
Config: i.gcLoggingOptions,
},
}, nil, "drone-gc")
if err != nil {
return err
}
return client.ContainerStart(ctx, res.ID, types.ContainerStartOptions{})
}

func (i *installer) getDaemonLoggingDriver(ctx context.Context, client docker.APIClient) string {
info, err := client.Info(ctx)
if err != nil {
logger.FromContext(ctx).
WithError(err).
Warnln("cannot acquire logging driver, using 'json-file'")
return "json-file"
}
return info.LoggingDriver
}

func (i *installer) errorUpdate(ctx context.Context, server *autoscaler.Server, err error) error {
if err != nil {
server.State = autoscaler.StateError
Expand Down