Skip to content

Commit

Permalink
libimage: wait to write an event until an image loaded
Browse files Browse the repository at this point in the history
Must wait to write to an event until an image has
finished loading.

Signed-off-by: Toshiki Sonoda <[email protected]>
  • Loading branch information
sstosh committed Feb 15, 2023
1 parent 3009dfe commit 8cf7839
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions libimage/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,13 @@ func (r *Runtime) Load(ctx context.Context, path string, options *LoadOptions) (
} {
loadedImages, transportName, err := f()
if err == nil {
return loadedImages, nil
if r.eventChannel != nil {
err = r.writeLoadEvents(path, loadedImages)
}
return loadedImages, err
}
logrus.Debugf("Error loading %s (%s): %v", path, transportName, err)
loadErrors = append(loadErrors, fmt.Errorf("%s: %v", transportName, err))

if r.eventChannel != nil {
for _, name := range loadedImages {
image, _, err := r.LookupImage(name, nil)
if err != nil {
return nil, fmt.Errorf("locating pulled image %q name in containers storage: %w", name, err)
}
r.writeEvent(&Event{ID: image.ID(), Name: path, Time: time.Now(), Type: EventTypeImageLoad})
}
}
}

// Give a decent error message if nothing above worked.
Expand All @@ -104,6 +97,18 @@ func (r *Runtime) Load(ctx context.Context, path string, options *LoadOptions) (
return nil, loadError
}

// writeLoadEvents writes the events of the loaded image.
func (r *Runtime) writeLoadEvents(path string, loadedImages []string) error {
for _, name := range loadedImages {
image, _, err := r.LookupImage(name, nil)
if err != nil {
return fmt.Errorf("locating pulled image %q name in containers storage: %w", name, err)
}
r.writeEvent(&Event{ID: image.ID(), Name: path, Time: time.Now(), Type: EventTypeImageLoad})
}
return nil
}

// loadMultiImageDockerArchive loads the docker archive specified by ref. In
// case the path@reference notation was used, only the specified image will be
// loaded. Otherwise, all images will be loaded.
Expand Down

0 comments on commit 8cf7839

Please sign in to comment.