From 328c8ba7b4567f69602cf6bc71f7a2f0efb5c969 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Mon, 1 Aug 2022 14:22:37 +0200 Subject: pkg/autoupdate: move policy map into updater [NO NEW TESTS NEEDED] - should not change behavior. Signed-off-by: Valentin Rothberg --- pkg/autoupdate/autoupdate.go | 57 +++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 25 deletions(-) (limited to 'pkg/autoupdate') diff --git a/pkg/autoupdate/autoupdate.go b/pkg/autoupdate/autoupdate.go index c792ee4e9..7bda0ac27 100644 --- a/pkg/autoupdate/autoupdate.go +++ b/pkg/autoupdate/autoupdate.go @@ -53,6 +53,15 @@ var supportedPolicies = map[string]Policy{ // policyMapper is used for tying a container to it's autoupdate policy type policyMapper map[Policy][]*libpod.Container +// updater includes shared state for auto-updating one or more containers. +type updater struct { + conn *dbus.Conn + imageToPolicyMapper map[string]policyMapper + options *entities.AutoUpdateOptions + updatedRawImages map[string]bool + runtime *libpod.Runtime +} + // LookupPolicy looks up the corresponding Policy for the specified // string. If none is found, an errors is returned including the list of // supported policies. @@ -116,12 +125,22 @@ 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(ctx context.Context, runtime *libpod.Runtime, options entities.AutoUpdateOptions) ([]*entities.AutoUpdateReport, []error) { + auto := updater{ + options: &options, + runtime: runtime, + updatedRawImages: make(map[string]bool), + } + // Create a map from `image ID -> []*Container`. - containerMap, errs := imageContainersMap(runtime) - if len(containerMap) == 0 { + if errs := auto.imageContainersMap(); len(errs) > 0 { return nil, errs } + // Nothing to do. + if len(auto.imageToPolicyMapper) == 0 { + return nil, nil + } + // Create a map from `image ID -> *libimage.Image` for image lookups. listOptions := &libimage.ListImagesOptions{ Filters: []string{"readonly=false"}, @@ -142,19 +161,14 @@ func AutoUpdate(ctx context.Context, runtime *libpod.Runtime, options entities.A return nil, []error{err} } defer conn.Close() + auto.conn = conn runtime.NewSystemEvent(events.AutoUpdate) - auto := updater{ - conn: conn, - options: &options, - runtime: runtime, - updatedRawImages: make(map[string]bool), - } - // Update all images/container according to their auto-update policy. var allReports []*entities.AutoUpdateReport - for imageID, policyMapper := range containerMap { + var errs []error + for imageID, policyMapper := range auto.imageToPolicyMapper { image, exists := imageMap[imageID] if !exists { errs = append(errs, fmt.Errorf("container image ID %q not found in local storage", imageID)) @@ -185,14 +199,6 @@ func AutoUpdate(ctx context.Context, runtime *libpod.Runtime, options entities.A return allReports, errs } -// updater includes shared state for auto-updating one or more containers. -type updater struct { - conn *dbus.Conn - options *entities.AutoUpdateOptions - updatedRawImages map[string]bool - runtime *libpod.Runtime -} - // updateRegistry updates the image/container according to the "registry" policy. func (u *updater) updateRegistry(ctx context.Context, image *libimage.Image, ctr *libpod.Container) (*entities.AutoUpdateReport, error) { cid := ctr.ID() @@ -353,14 +359,15 @@ func (u *updater) restartSystemdUnit(ctx context.Context, ctr *libpod.Container, // imageContainersMap generates a map[image ID] -> [containers using the image] // of all containers with a valid auto-update policy. -func imageContainersMap(runtime *libpod.Runtime) (map[string]policyMapper, []error) { - allContainers, err := runtime.GetAllContainers() +func (u *updater) imageContainersMap() []error { + allContainers, err := u.runtime.GetAllContainers() if err != nil { - return nil, []error{err} + return []error{err} } + u.imageToPolicyMapper = make(map[string]policyMapper) + errors := []error{} - containerMap := make(map[string]policyMapper) for _, ctr := range allContainers { state, err := ctr.State() if err != nil { @@ -390,17 +397,17 @@ func imageContainersMap(runtime *libpod.Runtime) (map[string]policyMapper, []err continue } else { id, _ := ctr.Image() - policyMap, exists := containerMap[id] + policyMap, exists := u.imageToPolicyMapper[id] if !exists { policyMap = make(map[Policy][]*libpod.Container) } policyMap[policy] = append(policyMap[policy], ctr) - containerMap[id] = policyMap + u.imageToPolicyMapper[id] = policyMap // Now we know that `ctr` is configured for auto updates. } } - return containerMap, errors + return errors } // getAuthfilePath returns an authfile path, if set. The authfile label in the -- cgit v1.2.3-54-g00ecf