pkg/trim-after-delete: make docker socket path configurable

Docker Desktop proxies the docker socket at its default location
(/var/run/docker.sock), but allows connecting to the non-proxied
socket through /var/run/docker.sock.raw.

This patch allows the trim-after-delete utility to customize
the docker socket path, so that it can connect to the non-proxied
socket.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-07-27 13:00:38 +02:00
parent 0dff43def5
commit a63ff3c480
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strings" "strings"
"time" "time"
) )
@ -63,17 +64,20 @@ func (a *DelayedAction) AtLeastOnceMore() {
} }
func main() { func main() {
// after-image-deletes --delay 10s -- /sbin/fstrim /var
delay := flag.Duration("delay", time.Second*10, "maximum time to wait after an image delete before triggering") delay := flag.Duration("delay", time.Second*10, "maximum time to wait after an image delete before triggering")
sockPath := flag.String("docker-socket", "/var/run/docker.sock", "location of the docker socket to connect to")
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s: run a command after images are deleted by Docker.\n\n", os.Args[0]) fmt.Fprintf(os.Stderr, `%[1]s: run a command after images are deleted by Docker.
fmt.Fprintf(os.Stderr, "Example usage:\n")
fmt.Fprintf(os.Stderr, "%s --delay 10s -- /sbin/fstrim /var\n", os.Args[0]) Example usage:
fmt.Fprintf(os.Stderr, " -- run the command /sbin/fstrim /var at most 10s after an image is deleted.\n") %[1]s --docker-socket=/var/run/docker.sock.raw --delay 10s -- /sbin/fstrim /var
fmt.Fprintf(os.Stderr, " This would allow large batches of image deletions to happen and amortise the\n") -- connect to the docker API through /var/run/docker.sock.raw, and run the
fmt.Fprintf(os.Stderr, " cost of the TRIM operation.\n\n") command /sbin/fstrim /var at most 10s after images are deleted. This
fmt.Fprintf(os.Stderr, "Arguments:\n") allows large batches of image deletions to happen and amortise the cost of
the TRIM operation.
Arguments:
`, filepath.Base(os.Args[0]))
flag.PrintDefaults() flag.PrintDefaults()
} }
@ -103,7 +107,7 @@ func main() {
httpc := http.Client{ httpc := http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", "/var/run/docker.sock") return net.Dial("unix", *sockPath)
}, },
}, },
} }
@ -113,7 +117,7 @@ RECONNECT:
for { for {
res, err := httpc.Get("http://unix/v1.24/events") res, err := httpc.Get("http://unix/v1.24/events")
if err != nil { if err != nil {
log.Printf("Failed to connect to the Docker daemon: will retry in 1s") log.Printf("Failed to connect to the Docker daemon at %s: will retry in 1s", *sockPath)
time.Sleep(time.Second) time.Sleep(time.Second)
continue RECONNECT continue RECONNECT
} }