mirror of
https://github.com/containers/podman.git
synced 2025-06-24 19:42:56 +08:00
pkg/autoupdate: move policy map into updater
[NO NEW TESTS NEEDED] - should not change behavior. Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
@ -53,6 +53,15 @@ var supportedPolicies = map[string]Policy{
|
|||||||
// policyMapper is used for tying a container to it's autoupdate policy
|
// policyMapper is used for tying a container to it's autoupdate policy
|
||||||
type policyMapper map[Policy][]*libpod.Container
|
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
|
// LookupPolicy looks up the corresponding Policy for the specified
|
||||||
// string. If none is found, an errors is returned including the list of
|
// string. If none is found, an errors is returned including the list of
|
||||||
// supported policies.
|
// supported policies.
|
||||||
@ -116,12 +125,22 @@ func ValidateImageReference(imageName string) error {
|
|||||||
// It returns a slice of successfully restarted systemd units and a slice of
|
// It returns a slice of successfully restarted systemd units and a slice of
|
||||||
// errors encountered during auto update.
|
// errors encountered during auto update.
|
||||||
func AutoUpdate(ctx context.Context, runtime *libpod.Runtime, options entities.AutoUpdateOptions) ([]*entities.AutoUpdateReport, []error) {
|
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`.
|
// Create a map from `image ID -> []*Container`.
|
||||||
containerMap, errs := imageContainersMap(runtime)
|
if errs := auto.imageContainersMap(); len(errs) > 0 {
|
||||||
if len(containerMap) == 0 {
|
|
||||||
return nil, errs
|
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.
|
// Create a map from `image ID -> *libimage.Image` for image lookups.
|
||||||
listOptions := &libimage.ListImagesOptions{
|
listOptions := &libimage.ListImagesOptions{
|
||||||
Filters: []string{"readonly=false"},
|
Filters: []string{"readonly=false"},
|
||||||
@ -142,19 +161,14 @@ func AutoUpdate(ctx context.Context, runtime *libpod.Runtime, options entities.A
|
|||||||
return nil, []error{err}
|
return nil, []error{err}
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
auto.conn = conn
|
||||||
|
|
||||||
runtime.NewSystemEvent(events.AutoUpdate)
|
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.
|
// Update all images/container according to their auto-update policy.
|
||||||
var allReports []*entities.AutoUpdateReport
|
var allReports []*entities.AutoUpdateReport
|
||||||
for imageID, policyMapper := range containerMap {
|
var errs []error
|
||||||
|
for imageID, policyMapper := range auto.imageToPolicyMapper {
|
||||||
image, exists := imageMap[imageID]
|
image, exists := imageMap[imageID]
|
||||||
if !exists {
|
if !exists {
|
||||||
errs = append(errs, fmt.Errorf("container image ID %q not found in local storage", imageID))
|
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
|
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.
|
// 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) {
|
func (u *updater) updateRegistry(ctx context.Context, image *libimage.Image, ctr *libpod.Container) (*entities.AutoUpdateReport, error) {
|
||||||
cid := ctr.ID()
|
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]
|
// imageContainersMap generates a map[image ID] -> [containers using the image]
|
||||||
// of all containers with a valid auto-update policy.
|
// of all containers with a valid auto-update policy.
|
||||||
func imageContainersMap(runtime *libpod.Runtime) (map[string]policyMapper, []error) {
|
func (u *updater) imageContainersMap() []error {
|
||||||
allContainers, err := runtime.GetAllContainers()
|
allContainers, err := u.runtime.GetAllContainers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, []error{err}
|
return []error{err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u.imageToPolicyMapper = make(map[string]policyMapper)
|
||||||
|
|
||||||
errors := []error{}
|
errors := []error{}
|
||||||
containerMap := make(map[string]policyMapper)
|
|
||||||
for _, ctr := range allContainers {
|
for _, ctr := range allContainers {
|
||||||
state, err := ctr.State()
|
state, err := ctr.State()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -390,17 +397,17 @@ func imageContainersMap(runtime *libpod.Runtime) (map[string]policyMapper, []err
|
|||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
id, _ := ctr.Image()
|
id, _ := ctr.Image()
|
||||||
policyMap, exists := containerMap[id]
|
policyMap, exists := u.imageToPolicyMapper[id]
|
||||||
if !exists {
|
if !exists {
|
||||||
policyMap = make(map[Policy][]*libpod.Container)
|
policyMap = make(map[Policy][]*libpod.Container)
|
||||||
}
|
}
|
||||||
policyMap[policy] = append(policyMap[policy], ctr)
|
policyMap[policy] = append(policyMap[policy], ctr)
|
||||||
containerMap[id] = policyMap
|
u.imageToPolicyMapper[id] = policyMap
|
||||||
// Now we know that `ctr` is configured for auto updates.
|
// 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
|
// getAuthfilePath returns an authfile path, if set. The authfile label in the
|
||||||
|
Reference in New Issue
Block a user