-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
driver/docker: pull image with digest #4298
Merged
dadgar
merged 5 commits into
hashicorp:master
from
justenwalker:docker-driver-digest-tags
May 21, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e77d8a5
driver/docker: pull image with digest
justenwalker ce63b79
driver/docker: cleanup parseDockerImage
justenwalker 4492333
driver/docker: fix TestDockerDriver_ForcePull_RepoDigest
justenwalker f4670f6
driver/docker: fix up TestParseDockerImage
justenwalker ba2d957
driver/docker: add test for dockerImageRef
justenwalker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1500,18 +1500,15 @@ func (d *DockerDriver) Periodic() (bool, time.Duration) { | |
// loading it from the file system | ||
func (d *DockerDriver) createImage(driverConfig *DockerDriverConfig, client *docker.Client, taskDir *allocdir.TaskDir) (string, error) { | ||
image := driverConfig.ImageName | ||
repo, tag := docker.ParseRepositoryTag(image) | ||
if tag == "" { | ||
tag = "latest" | ||
} | ||
repo, tag := parseDockerImage(image) | ||
|
||
coordinator, callerID := d.getDockerCoordinator(client) | ||
|
||
// We're going to check whether the image is already downloaded. If the tag | ||
// is "latest", or ForcePull is set, we have to check for a new version every time so we don't | ||
// bother to check and cache the id here. We'll download first, then cache. | ||
if driverConfig.ForcePull { | ||
d.logger.Printf("[DEBUG] driver.docker: force pull image '%s:%s' instead of inspecting local", repo, tag) | ||
d.logger.Printf("[DEBUG] driver.docker: force pull image '%s' instead of inspecting local", dockerImageRef(repo, tag)) | ||
} else if tag != "latest" { | ||
if dockerImage, _ := client.InspectImage(image); dockerImage != nil { | ||
// Image exists so just increment its reference count | ||
|
@@ -1544,7 +1541,7 @@ func (d *DockerDriver) pullImage(driverConfig *DockerDriverConfig, client *docke | |
d.logger.Printf("[DEBUG] driver.docker: did not find docker auth for repo %q", repo) | ||
} | ||
|
||
d.emitEvent("Downloading image %s:%s", repo, tag) | ||
d.emitEvent("Downloading image %s", dockerImageRef(repo, tag)) | ||
coordinator, callerID := d.getDockerCoordinator(client) | ||
|
||
return coordinator.PullImage(driverConfig.ImageName, authOptions, callerID, d.emitEvent) | ||
|
@@ -2183,3 +2180,25 @@ type createContainerClient interface { | |
ListContainers(docker.ListContainersOptions) ([]docker.APIContainers, error) | ||
RemoveContainer(opts docker.RemoveContainerOptions) error | ||
} | ||
|
||
func parseDockerImage(image string) (repo, tag string) { | ||
repo, tag = docker.ParseRepositoryTag(image) | ||
if tag != "" { | ||
return repo, tag | ||
} | ||
if i := strings.IndexRune(image, '@'); i > -1 { // Has digest (@sha256:...) | ||
// when pulling images with a digest, the repository contains the sha hash, and the tag is empty | ||
// see: https://github.com/fsouza/go-dockerclient/blob/master/image_test.go#L471 | ||
repo = image | ||
} else { | ||
tag = "latest" | ||
} | ||
return repo, tag | ||
} | ||
|
||
func dockerImageRef(repo string, tag string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind adding a quick test for this. Could even just be a line in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if tag == "" { | ||
return repo | ||
} | ||
return fmt.Sprintf("%s:%s", repo, tag) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a risk of someone using
@
in their repo name/url/tag? wonder if its more safe to actually sniff for@sha256:
in its entirely - not aware of the docker repo spec if its invalid or notThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure that '@' is not a valid repo name. The official spec is codified here:
https://github.com/docker/distribution/blob/master/reference/regexp.go