From a63ff3c480058ede32a18ef8d097f7967ce00f1b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 27 Jul 2020 13:00:38 +0200 Subject: [PATCH] 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 --- pkg/trim-after-delete/main.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/pkg/trim-after-delete/main.go b/pkg/trim-after-delete/main.go index bbcb154d0..0d7e53d9f 100644 --- a/pkg/trim-after-delete/main.go +++ b/pkg/trim-after-delete/main.go @@ -11,6 +11,7 @@ import ( "net/http" "os" "os/exec" + "path/filepath" "strings" "time" ) @@ -63,17 +64,20 @@ func (a *DelayedAction) AtLeastOnceMore() { } 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") + sockPath := flag.String("docker-socket", "/var/run/docker.sock", "location of the docker socket to connect to") 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, "Example usage:\n") - fmt.Fprintf(os.Stderr, "%s --delay 10s -- /sbin/fstrim /var\n", os.Args[0]) - fmt.Fprintf(os.Stderr, " -- run the command /sbin/fstrim /var at most 10s after an image is deleted.\n") - fmt.Fprintf(os.Stderr, " This would allow large batches of image deletions to happen and amortise the\n") - fmt.Fprintf(os.Stderr, " cost of the TRIM operation.\n\n") - fmt.Fprintf(os.Stderr, "Arguments:\n") + fmt.Fprintf(os.Stderr, `%[1]s: run a command after images are deleted by Docker. + +Example usage: +%[1]s --docker-socket=/var/run/docker.sock.raw --delay 10s -- /sbin/fstrim /var + -- connect to the docker API through /var/run/docker.sock.raw, and run the + command /sbin/fstrim /var at most 10s after images are deleted. This + allows large batches of image deletions to happen and amortise the cost of + the TRIM operation. + +Arguments: +`, filepath.Base(os.Args[0])) flag.PrintDefaults() } @@ -103,7 +107,7 @@ func main() { httpc := http.Client{ Transport: &http.Transport{ 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 { res, err := httpc.Get("http://unix/v1.24/events") 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) continue RECONNECT }