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

change image pull function signature, only node image name is needed #81

Merged
merged 1 commit into from
Oct 4, 2020
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
20 changes: 10 additions & 10 deletions clab/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (c *cLab) CreateContainer(ctx context.Context, node *Node) (err error) {
labels["group"] = node.Group
}

err = c.PullImageIfRequired(nctx, node)
err = c.PullImageIfRequired(nctx, node.Image)
if err != nil {
return err
}
Expand Down Expand Up @@ -185,16 +185,16 @@ func (c *cLab) CreateContainer(ctx context.Context, node *Node) (err error) {
return linkContainerNS(node.Pid, node.LongName)
}

func (c *cLab) PullImageIfRequired(ctx context.Context, node *Node) (err error) {
func (c *cLab) PullImageIfRequired(ctx context.Context, imageName string) error {
filter := filters.NewArgs()
filter.Add("reference", node.Image)
filter.Add("reference", imageName)

ilo := types.ImageListOptions{
All: false,
Filters: filter,
}

log.Debugf("Looking up %s Docker image", node.Image)
log.Debugf("Looking up %s Docker image", imageName)

images, err := c.DockerClient.ImageList(ctx, ilo)
if err != nil {
Expand All @@ -203,25 +203,25 @@ func (c *cLab) PullImageIfRequired(ctx context.Context, node *Node) (err error)

// If Image doesn't exist, we need to pull it
if len(images) > 0 {
log.Debugf("Image %s present, skip pulling", node.Image)
log.Debugf("Image %s present, skip pulling", imageName)
return nil
}

// might need canonical name e.g.
// -> alpine == docker.io/library/alpine
// -> foo/bar == docker.io/foo/bar
// -> docker.elastic.co/elasticsearch/elasticsearch == docker.elastic.co/elasticsearch/elasticsearch
canonicalImageName := node.Image
slashCount := strings.Count(node.Image, "/")
canonicalImageName := imageName
slashCount := strings.Count(imageName, "/")

switch slashCount {
case 0:
canonicalImageName = "docker.io/library/" + node.Image
canonicalImageName = "docker.io/library/" + imageName
case 1:
canonicalImageName = "docker.io/" + node.Image
canonicalImageName = "docker.io/" + imageName
}

log.Infof("Pulling %s Docker image", node.Image)
log.Infof("Pulling %s Docker image", canonicalImageName)
reader, err := c.DockerClient.ImagePull(ctx, canonicalImageName, types.ImagePullOptions{})
if err != nil {
return err
Expand Down