Skip to content

Commit

Permalink
pivot to cleanup of docker client when out of scope
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman committed Jul 24, 2023
1 parent 4cf42ac commit 97f3237
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
43 changes: 31 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func GetImageFromSource(ctx context.Context, imgStr string, source image.Source,
}
}

provider, err := selectImageProvider(imgStr, source, cfg)
provider, cleanup, err := selectImageProvider(imgStr, source, cfg)
if cleanup != nil {
defer cleanup()
}
if err != nil {
return nil, err
}
Expand All @@ -99,58 +102,74 @@ func GetImageFromSource(ctx context.Context, imgStr string, source image.Source,
return img, nil
}

func selectImageProvider(imgStr string, source image.Source, cfg config) (image.Provider, error) {
func selectImageProvider(imgStr string, source image.Source, cfg config) (image.Provider, func(), error) {
var provider image.Provider
tempDirGenerator := rootTempDirGenerator.NewGenerator()
platformSelectionUnsupported := fmt.Errorf("specified platform=%q however image source=%q does not support selecting platform", cfg.Platform.String(), source.String())

cleanup := func() {}

switch source {
case image.DockerTarballSource:
if cfg.Platform != nil {
return nil, platformSelectionUnsupported
return nil, cleanup, platformSelectionUnsupported
}
// note: the imgStr is the path on disk to the tar file
provider = docker.NewProviderFromTarball(imgStr, tempDirGenerator)
case image.DockerDaemonSource:
c, err := dockerClient.GetClient()
if err != nil {
return nil, err
return nil, cleanup, err
}

cleanup = func() {
if err := c.Close(); err != nil {
log.Errorf("unable to close docker client: %+v", err)
}
}

provider, err = docker.NewProviderFromDaemon(imgStr, tempDirGenerator, c, cfg.Platform)
if err != nil {
return nil, err
return nil, cleanup, err
}
case image.PodmanDaemonSource:
c, err := podman.GetClient()
if err != nil {
return nil, err
return nil, cleanup, err
}

cleanup = func() {
if err := c.Close(); err != nil {
log.Errorf("unable to close docker client: %+v", err)
}
}

provider, err = docker.NewProviderFromDaemon(imgStr, tempDirGenerator, c, cfg.Platform)
if err != nil {
return nil, err
return nil, cleanup, err
}
case image.OciDirectorySource:
if cfg.Platform != nil {
return nil, platformSelectionUnsupported
return nil, cleanup, platformSelectionUnsupported
}
provider = oci.NewProviderFromPath(imgStr, tempDirGenerator)
case image.OciTarballSource:
if cfg.Platform != nil {
return nil, platformSelectionUnsupported
return nil, cleanup, platformSelectionUnsupported
}
provider = oci.NewProviderFromTarball(imgStr, tempDirGenerator)
case image.OciRegistrySource:
defaultPlatformIfNil(&cfg)
provider = oci.NewProviderFromRegistry(imgStr, tempDirGenerator, cfg.Registry, cfg.Platform)
case image.SingularitySource:
if cfg.Platform != nil {
return nil, platformSelectionUnsupported
return nil, cleanup, platformSelectionUnsupported
}
provider = sif.NewProviderFromPath(imgStr, tempDirGenerator)
default:
return nil, fmt.Errorf("unable to determine image source")
return nil, cleanup, fmt.Errorf("unable to determine image source")
}
return provider, nil
return provider, cleanup, nil
}

// defaultPlatformIfNil sets the platform to use the host's architecture
Expand Down
17 changes: 0 additions & 17 deletions internal/docker/client.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package docker

import (
"context"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"

"github.com/docker/cli/cli/connhelper"
"github.com/docker/docker/client"
Expand Down Expand Up @@ -51,20 +48,6 @@ func GetClient() (*client.Client, error) {
}
}

clientOpts = append(clientOpts, func(c *client.Client) error {
httpClient := &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
IdleConnTimeout: 10 * time.Second,
},
}
return client.WithHTTPClient(httpClient)(c)
})

clientOpts = append(clientOpts, client.WithDialContext(func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial("unix", "/var/run/docker.sock") // In some cases, the path of unix socket is not /var/run/docker.sock
}))

dockerClient, err := client.NewClientWithOpts(clientOpts...)
if err != nil {
return nil, fmt.Errorf("failed create docker client: %w", err)
Expand Down

0 comments on commit 97f3237

Please sign in to comment.