Skip to content

Commit

Permalink
drivers/docker: Refactor infraImage pull to a func
Browse files Browse the repository at this point in the history
  • Loading branch information
tbehling committed Jun 29, 2022
1 parent 6440f3e commit c2218a6
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions drivers/docker/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,8 @@ func (d *Driver) CreateNetwork(allocID string, createSpec *drivers.NetworkCreate
return nil, false, fmt.Errorf("failed to connect to docker daemon: %s", err)
}

repo, tag := parseDockerImage(d.config.InfraImage)

// Pull the InfraImage only if it doesn't exist locally, or uses the "latest" tag
if dockerImage, _ := client.InspectImage(d.config.InfraImage); dockerImage == nil || tag == "latest" {
authOptions, err := firstValidAuth(repo, []authBackend{
authFromDockerConfig(d.config.Auth.Config),
authFromHelper(d.config.Auth.Helper),
})
if err != nil {
d.logger.Debug("auth failed for infra container image pull", "image", d.config.InfraImage, "error", err)
}
_, err = d.coordinator.PullImage(d.config.InfraImage, authOptions, allocID, noopLogEventFn, d.config.infraImagePullTimeoutDuration, d.config.pullActivityTimeoutDuration)
if err != nil {
return nil, false, err
}
} else {
// Image exists, so just increment its reference count
d.coordinator.IncrementImageReference(dockerImage.ID, d.config.InfraImage, allocID)
if err := d.pullInfraImage(allocID); err != nil {
return nil, false, err
}

config, err := d.createSandboxContainerConfig(allocID, createSpec)
Expand Down Expand Up @@ -131,3 +115,39 @@ func (d *Driver) createSandboxContainerConfig(allocID string, createSpec *driver
},
}, nil
}

// pullInfraImage conditionally pulls the `infra_image` from the Docker registry
// only if its name uses the "latest" tag or the image doesn't already exist locally.
func (d *Driver) pullInfraImage(allocID string) error {
name := d.config.InfraImage
repo, tag := parseDockerImage(name)

doPull := func() error {
authOptions, err := firstValidAuth(repo, []authBackend{
authFromDockerConfig(d.config.Auth.Config),
authFromHelper(d.config.Auth.Helper),
})
if err != nil {
d.logger.Debug("auth failed for infra_image container pull", "image", name, "error", err)
}
_, err = d.coordinator.PullImage(name, authOptions, allocID, noopLogEventFn, d.config.infraImagePullTimeoutDuration, d.config.pullActivityTimeoutDuration)

return err
}

if tag == "latest" {
return doPull()
}

dockerImage, err := client.InspectImage(name)
if err != nil {
d.logger.Debug("InspectImage failed for infra_image container pull", "image", name, "error", err)
return err
} else if dockerImage != nil {
// Image exists, so no pull is attempted; just increment its reference count
d.coordinator.IncrementImageReference(dockerImage.ID, name, allocID)
return nil
}

return doPull()
}

0 comments on commit c2218a6

Please sign in to comment.