Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: goroutine leak with docker daemon #183

Merged
merged 4 commits into from
Jul 27, 2023
Merged
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
44 changes: 32 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,75 @@ 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) {
// nolint:funlen
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
Loading