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

Prevent concurrent image pulls of same imageRef #159

Merged
merged 2 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 16 additions & 4 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/hashicorp/nomad/plugins/shared/hclspec"
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
spec "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sync/singleflight"
)

const (
Expand Down Expand Up @@ -104,6 +105,9 @@ type Driver struct {
systemInfo api.Info
// Queried from systemInfo: is podman running on a cgroupv2 system?
cgroupV2 bool

// singleflight group to prevent parallel image downloads
pullGroup singleflight.Group
}

// TaskState is the state which is encoded in the handle returned in
Expand Down Expand Up @@ -819,12 +823,20 @@ func (d *Driver) createImage(image string, auth *AuthConfig, forcePull bool, ima
Password: auth.Password,
}

ctx, cancel := context.WithTimeout(context.Background(), imagePullTimeout)
defer cancel()
result, err, _ := d.pullGroup.Do(imageName, func() (interface{}, error) {

if imageID, err = d.podman.ImagePull(ctx, imageName, imageAuth); err != nil {
return imageID, fmt.Errorf("failed to start task, unable to pull image %s : %w", imageName, err)
ctx, cancel := context.WithTimeout(context.Background(), imagePullTimeout)
defer cancel()

if imageID, err = d.podman.ImagePull(ctx, imageName, imageAuth); err != nil {
return imageID, fmt.Errorf("failed to start task, unable to pull image %s : %w", imageName, err)
}
return imageID, nil
})
if err != nil {
return "", err
}
imageID = result.(string)
d.logger.Debug("Pulled image ID", "imageID", imageID)
return imageID, nil
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/hashicorp/nomad/api v0.0.0-20220519231241-2b054e38e91a
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
github.com/stretchr/testify v1.7.1
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
)

require (
Expand Down