Merge pull request #3542 from thaJeztah/configurable_socket

pkg/trim-after-delete: make docker socket path configurable
This commit is contained in:
David Scott 2022-07-17 03:26:52 -07:00 committed by GitHub
commit 8070434cad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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
} }