Skip to content
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
23 changes: 18 additions & 5 deletions images/hook-docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -37,13 +38,13 @@ func run() error {
fmt.Println("Starting the Docker Engine")

d := dockerConfig{
Debug: true,
LogDriver: "syslog",
LogOpts: map[string]string{
"syslog-address": fmt.Sprintf("udp://%v:514", cfg.syslogHost),
},
Debug: true,
InsecureRegistries: cfg.insecureRegistries,
}
if addr := syslogAddress(cfg.syslogHost); addr != "" {
d.LogDriver = "syslog"
d.LogOpts = map[string]string{"syslog-address": addr}
}
path := "/etc/docker"
// Create the directory for the docker config
err = os.MkdirAll(path, os.ModeDir)
Expand Down Expand Up @@ -89,6 +90,18 @@ func main() {
}
}

// syslogAddress builds the syslog log driver's UDP address for the given host.
// It returns an empty string when host is empty (caller should skip configuring
// the syslog log driver in that case). IPv6 addresses are bracketed via
// net.JoinHostPort so the resulting URL parses correctly (e.g.
// "udp://[fd00::1]:514" rather than "udp://fd00::1:514").
func syslogAddress(host string) string {
if host == "" {
return ""
}
return "udp://" + net.JoinHostPort(host, "514")
}

// writeToDisk writes the dockerConfig to loc.
func (d dockerConfig) writeToDisk(loc string) error {
b, err := json.Marshal(d)
Expand Down
20 changes: 20 additions & 0 deletions images/hook-docker/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ import (
"testing"
)

func TestSyslogAddress(t *testing.T) {
tests := map[string]struct {
host string
want string
}{
"empty host returns empty": {host: "", want: ""},
"ipv4 host": {host: "192.168.1.10", want: "udp://192.168.1.10:514"},
"ipv6 host is bracketed": {host: "fd00:80:66::1", want: "udp://[fd00:80:66::1]:514"},
"ipv6 loopback": {host: "::1", want: "udp://[::1]:514"},
"hostname passes through": {host: "syslog.example.com", want: "udp://syslog.example.com:514"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := syslogAddress(tt.host); got != tt.want {
t.Fatalf("syslogAddress(%q) = %q, want %q", tt.host, got, tt.want)
}
})
}
}

func TestWriteToDisk(t *testing.T) {
tests := map[string]struct {
cfg dockerConfig
Expand Down
Loading