Skip to content

Commit

Permalink
auto update: pass through a context
Browse files Browse the repository at this point in the history
Pass a single context.Context through the call stack.  If auto-updates
will ever be made available for REST calls, the context will help
supporting disconnected clients.

Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Jul 13, 2021
1 parent a8847c0 commit a90a4ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions pkg/autoupdate/autoupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func ValidateImageReference(imageName string) error {
//
// It returns a slice of successfully restarted systemd units and a slice of
// errors encountered during auto update.
func AutoUpdate(runtime *libpod.Runtime, options Options) ([]string, []error) {
func AutoUpdate(ctx context.Context, runtime *libpod.Runtime, options Options) ([]string, []error) {
// Create a map from `image ID -> []*Container`.
containerMap, errs := imageContainersMap(runtime)
if len(containerMap) == 0 {
Expand All @@ -129,7 +129,7 @@ func AutoUpdate(runtime *libpod.Runtime, options Options) ([]string, []error) {
listOptions := &libimage.ListImagesOptions{
Filters: []string{"readonly=false"},
}
imagesSlice, err := runtime.LibimageRuntime().ListImages(context.Background(), nil, listOptions)
imagesSlice, err := runtime.LibimageRuntime().ListImages(ctx, nil, listOptions)
if err != nil {
return nil, []error{err}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func AutoUpdate(runtime *libpod.Runtime, options Options) ([]string, []error) {
errs = append(errs, errors.Errorf("error registry auto-updating container %q: raw-image name is empty", cid))
}
authfile := getAuthfilePath(registryCtr, options)
needsUpdate, err := newerRemoteImageAvailable(runtime, image, rawImageName, authfile)
needsUpdate, err := newerRemoteImageAvailable(ctx, runtime, image, rawImageName, authfile)
if err != nil {
errs = append(errs, errors.Wrapf(err, "error registry auto-updating container %q: image check for %q failed", cid, rawImageName))
continue
Expand All @@ -174,7 +174,7 @@ func AutoUpdate(runtime *libpod.Runtime, options Options) ([]string, []error) {
if needsUpdate {
logrus.Infof("Auto-updating container %q using registry image %q", cid, rawImageName)
if _, updated := updatedRawImages[rawImageName]; !updated {
_, err = updateImage(runtime, rawImageName, options)
_, err = updateImage(ctx, runtime, rawImageName, options)
if err != nil {
errs = append(errs, errors.Wrapf(err, "error registry auto-updating container %q: image update for %q failed", cid, rawImageName))
continue
Expand Down Expand Up @@ -292,13 +292,13 @@ func getAuthfilePath(ctr *libpod.Container, options Options) string {

// newerRemoteImageAvailable returns true if there corresponding image on the remote
// registry is newer.
func newerRemoteImageAvailable(runtime *libpod.Runtime, img *libimage.Image, origName string, authfile string) (bool, error) {
func newerRemoteImageAvailable(ctx context.Context, runtime *libpod.Runtime, img *libimage.Image, origName string, authfile string) (bool, error) {
remoteRef, err := docker.ParseReference("//" + origName)
if err != nil {
return false, err
}

return img.HasDifferentDigest(context.Background(), remoteRef)
return img.HasDifferentDigest(ctx, remoteRef)
}

// newerLocalImageAvailable returns true if the container and local image have different digests
Expand All @@ -316,12 +316,12 @@ func newerLocalImageAvailable(runtime *libpod.Runtime, img *libimage.Image, rawI
}

// updateImage pulls the specified image.
func updateImage(runtime *libpod.Runtime, name string, options Options) (*libimage.Image, error) {
func updateImage(ctx context.Context, runtime *libpod.Runtime, name string, options Options) (*libimage.Image, error) {
pullOptions := &libimage.PullOptions{}
pullOptions.AuthFilePath = options.Authfile
pullOptions.Writer = os.Stderr

pulledImages, err := runtime.LibimageRuntime().Pull(context.Background(), name, config.PullPolicyAlways, pullOptions)
pulledImages, err := runtime.LibimageRuntime().Pull(ctx, name, config.PullPolicyAlways, pullOptions)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/infra/abi/auto-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func (ic *ContainerEngine) AutoUpdate(ctx context.Context, options entities.Auto
// them in the entities package as low-level packages must not leak
// into the remote client.
autoOpts := autoupdate.Options{Authfile: options.Authfile}
units, failures := autoupdate.AutoUpdate(ic.Libpod, autoOpts)
units, failures := autoupdate.AutoUpdate(ctx, ic.Libpod, autoOpts)
return &entities.AutoUpdateReport{Units: units}, failures
}

0 comments on commit a90a4ec

Please sign in to comment.