Skip to content

Commit

Permalink
driver: emit a driver event when we pull or load an image (hashicorp#138
Browse files Browse the repository at this point in the history
)
  • Loading branch information
towe75 authored Nov 2, 2021
1 parent 06906f2 commit 7e3d018
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
6 changes: 6 additions & 0 deletions api/image_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)

var ImageNotFound = errors.New("No such Image")

type inspectIDImageResponse struct {
Id string `json:"Id"`
}
Expand All @@ -26,6 +29,9 @@ func (c *API) ImageInspectID(ctx context.Context, image string) (string, error)
if err != nil {
return "", err
}
if res.StatusCode == http.StatusNotFound {
return "", ImageNotFound
}

if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("unknown error, status code: %d: %s", res.StatusCode, body)
Expand Down
24 changes: 20 additions & 4 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
}

if !recoverRunningContainer {
imageID, err := d.createImage(createOpts.Image, &driverConfig.Auth, driverConfig.ForcePull)
imageID, err := d.createImage(createOpts.Image, &driverConfig.Auth, driverConfig.ForcePull, cfg)
if err != nil {
return nil, nil, fmt.Errorf("failed to create image: %s: %w", createOpts.Image, err)
}
Expand Down Expand Up @@ -668,7 +668,7 @@ func memoryInBytes(strmem string) (int64, error) {

// Creates the requested image if missing from storage
// returns the 64-byte image ID as an unique image identifier
func (d *Driver) createImage(image string, auth *AuthConfig, forcePull bool) (string, error) {
func (d *Driver) createImage(image string, auth *AuthConfig, forcePull bool, cfg *drivers.TaskConfig) (string, error) {
var imageID string
imageName := image
// If it is a shortname, we should not have to worry
Expand All @@ -688,6 +688,14 @@ func (d *Driver) createImage(image string, auth *AuthConfig, forcePull bool) (st
archiveData := imageRef.StringWithinTransport()
path := strings.Split(archiveData, ":")[0]
d.logger.Debug("Load image archive", "path", path)
//nolint // ignore returned error, can't react in a good way
d.eventer.EmitEvent(&drivers.TaskEvent{
TaskID: cfg.ID,
TaskName: cfg.Name,
AllocID: cfg.AllocID,
Timestamp: time.Now(),
Message: "Loading image " + path,
})
imageName, err = d.podman.ImageLoad(d.ctx, path)
if err != nil {
return imageID, fmt.Errorf("error while loading image: %w", err)
Expand All @@ -696,7 +704,7 @@ func (d *Driver) createImage(image string, auth *AuthConfig, forcePull bool) (st
}

imageID, err := d.podman.ImageInspectID(d.ctx, imageName)
if err != nil {
if err != nil && err != api.ImageNotFound {
// If ImageInspectID errors, continue the operation and try
// to pull the image instead
d.logger.Warn("Unable to check for local image", "image", imageName, "error", err)
Expand All @@ -706,7 +714,15 @@ func (d *Driver) createImage(image string, auth *AuthConfig, forcePull bool) (st
return imageID, nil
}

d.logger.Debug("Pull image", "image", imageName)
d.logger.Info("Pulling image", "image", imageName)
//nolint // ignore returned error, can't react in a good way
d.eventer.EmitEvent(&drivers.TaskEvent{
TaskID: cfg.ID,
TaskName: cfg.Name,
AllocID: cfg.AllocID,
Timestamp: time.Now(),
Message: "Pulling image " + imageName,
})
imageAuth := api.ImageAuthConfig{
Username: auth.Username,
Password: auth.Password,
Expand Down
16 changes: 14 additions & 2 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,13 @@ func startDestroyInspectImage(t *testing.T, image string, taskName string) {

d := podmanDriverHarness(t, nil)

imageID, err := getPodmanDriver(t, d).createImage(image, &AuthConfig{}, false)
task := &drivers.TaskConfig{
ID: uuid.Generate(),
Name: taskName,
AllocID: uuid.Generate(),
Resources: createBasicResources(),
}
imageID, err := getPodmanDriver(t, d).createImage(image, &AuthConfig{}, false, task)
require.NoError(t, err)
require.Equal(t, imageID, inspectData.Image)
}
Expand Down Expand Up @@ -1800,7 +1806,13 @@ func Test_createImageArchives(t *testing.T) {
func createInspectImage(t *testing.T, image, reference string) {
d := podmanDriverHarness(t, nil)

idTest, err := getPodmanDriver(t, d).createImage(image, &AuthConfig{}, false)
task := &drivers.TaskConfig{
ID: uuid.Generate(),
Name: "inspectImage",
AllocID: uuid.Generate(),
Resources: createBasicResources(),
}
idTest, err := getPodmanDriver(t, d).createImage(image, &AuthConfig{}, false, task)
require.NoError(t, err)

idRef, err := getPodmanDriver(t, d).podman.ImageInspectID(context.Background(), reference)
Expand Down

0 comments on commit 7e3d018

Please sign in to comment.