Skip to content

Commit

Permalink
Add option scan stopped containers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslorentz committed Sep 6, 2023
1 parent 6fedb34 commit e2160ae
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
10 changes: 10 additions & 0 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func init() {
fs.Bool("process-caddyfile", true,
"Process Caddyfile before loading it, removing invalid servers")

fs.Bool("scan-stopped-containers", true,
"Scan stopped containers and use its labels for caddyfile generation")

fs.Duration("polling-interval", 30*time.Second,
"Interval caddy should manually check docker for a new caddyfile")

Expand Down Expand Up @@ -138,6 +141,7 @@ func createOptions(flags caddycmd.Flags) *config.Options {
labelPrefixFlag := flags.String("label-prefix")
proxyServiceTasksFlag := flags.Bool("proxy-service-tasks")
processCaddyfileFlag := flags.Bool("process-caddyfile")
scanStoppedContainersFlag := flags.Bool("scan-stopped-containers")
pollingIntervalFlag := flags.Duration("polling-interval")
modeFlag := flags.String("mode")
controllerSubnetFlag := flags.String("controller-network")
Expand Down Expand Up @@ -233,6 +237,12 @@ func createOptions(flags caddycmd.Flags) *config.Options {
options.ProcessCaddyfile = processCaddyfileFlag
}

if scanStoppedContainersEnv := os.Getenv("CADDY_DOCKER_SCAN_STOPPED_CONTAINERS"); scanStoppedContainersEnv != "" {
options.ScanStoppedContainers = isTrue.MatchString(scanStoppedContainersEnv)
} else {
options.ScanStoppedContainers = scanStoppedContainersFlag
}

if pollingIntervalEnv := os.Getenv("CADDY_DOCKER_POLLING_INTERVAL"); pollingIntervalEnv != "" {
if p, err := time.ParseDuration(pollingIntervalEnv); err != nil {
log.Error("Failed to parse CADDY_DOCKER_POLLING_INTERVAL", zap.String("CADDY_DOCKER_POLLING_INTERVAL", pollingIntervalEnv), zap.Error(err))
Expand Down
5 changes: 3 additions & 2 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
type Options struct {
CaddyfilePath string
DockerSockets []string
DockerCertsPath []string
DockerAPIsVersion []string
DockerCertsPath []string
DockerAPIsVersion []string
LabelPrefix string
ControlledServersLabel string
ProxyServiceTasks bool
ProcessCaddyfile bool
ScanStoppedContainers bool
PollingInterval time.Duration
Mode Mode
Secret string
Expand Down
2 changes: 1 addition & 1 deletion generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (g *CaddyfileGenerator) GenerateCaddyfile(logger *zap.Logger) ([]byte, []st
}

// Add containers
containers, err := dockerClient.ContainerList(context.Background(), types.ContainerListOptions{})
containers, err := dockerClient.ContainerList(context.Background(), types.ContainerListOptions{All: g.options.ScanStoppedContainers})
if err == nil {
for _, container := range containers {
if _, isControlledServer := container.Labels[g.options.ControlledServersLabel]; isControlledServer {
Expand Down
3 changes: 2 additions & 1 deletion loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ func (dockerLoader *DockerLoader) Start() error {
zap.String("CaddyfilePath", dockerLoader.options.CaddyfilePath),
zap.String("LabelPrefix", dockerLoader.options.LabelPrefix),
zap.Duration("PollingInterval", dockerLoader.options.PollingInterval),
zap.Bool("ProcessCaddyfile", dockerLoader.options.ProcessCaddyfile),
zap.Bool("ProxyServiceTasks", dockerLoader.options.ProxyServiceTasks),
zap.Bool("ProcessCaddyfile", dockerLoader.options.ProcessCaddyfile),
zap.Bool("ScanStoppedContainers", dockerLoader.options.ScanStoppedContainers),
zap.String("IngressNetworks", fmt.Sprintf("%v", dockerLoader.options.IngressNetworks)),
zap.Strings("DockerSockets", dockerLoader.options.DockerSockets),
zap.Strings("DockerCertsPath", dockerLoader.options.DockerCertsPath),
Expand Down
1 change: 1 addition & 0 deletions tests/caddyfile+config/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
caddy
21 changes: 21 additions & 0 deletions tests/containers/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

set -e

. ../functions.sh

trap "docker rm -f caddy whoami0 whoami1 whoami_stopped" EXIT

{
docker run --name caddy -d -p 4443:443 -e CADDY_DOCKER_SCAN_STOPPED_CONTAINERS=true -v /var/run/docker.sock:/var/run/docker.sock caddy-docker-proxy:local &&
docker run --name whoami0 -d -l caddy=whoami0.example.com -l "caddy.reverse_proxy={{upstreams 80}}" -l caddy.tls=internal containous/whoami &&
docker run --name whoami1 -d -l caddy=whoami1.example.com -l "caddy.reverse_proxy={{upstreams 80}}" -l caddy.tls=internal containous/whoami &&
docker create --name whoami_stopped -l caddy=whoami_stopped.example.com -l "caddy.respond=\"I'm a stopped container!\" 200" -l caddy.tls=internal containous/whoami &&

retry curl -k --resolve whoami0.example.com:4443:127.0.0.1 https://whoami0.example.com:4443 &&
retry curl -k --resolve whoami1.example.com:4443:127.0.0.1 https://whoami1.example.com:4443 &&
retry curl -k --resolve whoami_stopped.example.com:4443:127.0.0.1 https://whoami_stopped.example.com:4443
} || {
echo "Test failed"
exit 1
}
Empty file modified tests/ingress-networks/run.sh
100644 → 100755
Empty file.
5 changes: 3 additions & 2 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

set -e

trap "exit 1" INT
trap "docker stack rm caddy_test" EXIT

docker network create --driver overlay --attachable caddy_test || true

for d in */
Expand All @@ -14,5 +17,3 @@ do
echo ""
(cd $d && . run.sh)
done

docker stack rm caddy_test

0 comments on commit e2160ae

Please sign in to comment.