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

feat: Pull busybox image if we need to (backport #79) #89

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions provider/docker/docker_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Provider struct {
dockerNetworkName string
networkMu sync.RWMutex
listeners map[string]Listeners
builderImageName string
}

func NewDockerProvider(ctx context.Context, logger *zap.Logger, providerName string, dockerOpts ...client.Opt) (*Provider, error) {
Expand All @@ -41,10 +42,11 @@ func NewDockerProvider(ctx context.Context, logger *zap.Logger, providerName str
}

dockerProvider := &Provider{
dockerClient: dockerClient,
listeners: map[string]Listeners{},
logger: logger.Named("docker_provider"),
name: providerName,
dockerClient: dockerClient,
listeners: map[string]Listeners{},
logger: logger.Named("docker_provider"),
name: providerName,
builderImageName: "busybox:latest",
}

dockerProvider.dockerNetworkName = fmt.Sprintf("petri-network-%s", util.RandomString(5))
Expand Down
28 changes: 12 additions & 16 deletions provider/docker/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ import (
func (p *Provider) CreateTask(ctx context.Context, logger *zap.Logger, definition provider.TaskDefinition) (string, error) {
logger = logger.Named("docker_provider")

_, _, err := p.dockerClient.ImageInspectWithRaw(ctx, definition.Image.Image)
if err != nil {
logger.Info("image not found, pulling", zap.String("image", definition.Image.Image))
if err := p.pullImage(ctx, definition); err != nil {
return "", err
}
if err := p.pullImage(ctx, definition.Image.Image); err != nil {
return "", err
}

portSet := convertTaskDefinitionPortsToPortSet(definition)
Expand Down Expand Up @@ -98,18 +94,18 @@ func (p *Provider) CreateTask(ctx context.Context, logger *zap.Logger, definitio
return createdContainer.ID, nil
}

func (p *Provider) pullImage(ctx context.Context, definition provider.TaskDefinition) error {
p.logger.Info("pulling image", zap.String("image", definition.Image.Image))
resp, err := p.dockerClient.ImagePull(ctx, definition.Image.Image, types.ImagePullOptions{})
func (p *Provider) pullImage(ctx context.Context, imageName string) error {
_, _, err := p.dockerClient.ImageInspectWithRaw(ctx, imageName)
if err != nil {
return err
}

defer resp.Close()
// throw away the image pull stdout response
_, err = io.Copy(io.Discard, resp)
p.logger.Info("image not found, pulling", zap.String("image", imageName))
resp, err := p.dockerClient.ImagePull(ctx, imageName, types.ImagePullOptions{})
if err != nil {
return err
}
defer resp.Close()

if err != nil {
// throw away the image pull stdout response
_, err = io.Copy(io.Discard, resp)
return err
}
return nil
Expand Down
24 changes: 20 additions & 4 deletions provider/docker/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ func (p *Provider) WriteFile(ctx context.Context, volumeName, relPath string, co

containerName := fmt.Sprintf("petri-writefile-%d", time.Now().UnixNano())

if err := p.pullImage(ctx, p.builderImageName); err != nil {
return err
}

logger.Debug("creating writefile container")

cc, err := p.dockerClient.ContainerCreate(
ctx,
&container.Config{
Image: "busybox:latest",
Image: p.builderImageName,

Entrypoint: []string{"sh", "-c"},
Cmd: []string{
Expand Down Expand Up @@ -178,12 +182,16 @@ func (p *Provider) ReadFile(ctx context.Context, volumeName, relPath string) ([]

containerName := fmt.Sprintf("petri-getfile-%d", time.Now().UnixNano())

if err := p.pullImage(ctx, p.builderImageName); err != nil {
return nil, err
}

logger.Debug("creating getfile container")

cc, err := p.dockerClient.ContainerCreate(
ctx,
&container.Config{
Image: "busybox:latest",
Image: p.builderImageName,

Labels: map[string]string{
providerLabelName: p.name,
Expand Down Expand Up @@ -254,10 +262,14 @@ func (p *Provider) DownloadDir(ctx context.Context, volumeName, relPath, localPa

logger.Debug("creating getdir container")

if err := p.pullImage(ctx, p.builderImageName); err != nil {
return err
}

cc, err := p.dockerClient.ContainerCreate(
ctx,
&container.Config{
Image: "busybox:latest",
Image: p.builderImageName,

Labels: map[string]string{
providerLabelName: p.name,
Expand Down Expand Up @@ -334,12 +346,16 @@ func (p *Provider) SetVolumeOwner(ctx context.Context, volumeName, uid, gid stri

containerName := fmt.Sprintf("petri-setowner-%d", time.Now().UnixNano())

if err := p.pullImage(ctx, p.builderImageName); err != nil {
return err
}

logger.Debug("creating volume-owner container")

cc, err := p.dockerClient.ContainerCreate(
ctx,
&container.Config{
Image: "busybox:latest",
Image: p.builderImageName,
Entrypoint: []string{"sh", "-c"},
Cmd: []string{
`chown "$2:$3" "$1" && chmod 0700 "$1"`,
Expand Down
Loading