From 1cc933c6bb1dbbab56bc1918db4ae358c56bba8b Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Wed, 3 Aug 2022 13:49:04 +0200 Subject: pkg/autoupdate: introduce status constants To replace redundant string scattered across the code with proper constants. The "status" will further be useful in a future change as it can be moved into a `task`. Signed-off-by: Valentin Rothberg --- pkg/autoupdate/autoupdate.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/pkg/autoupdate/autoupdate.go b/pkg/autoupdate/autoupdate.go index 89d0a1ac3..2db85df5d 100644 --- a/pkg/autoupdate/autoupdate.go +++ b/pkg/autoupdate/autoupdate.go @@ -60,6 +60,14 @@ type updater struct { runtime *libpod.Runtime } +const ( + statusFailed = "failed" // The update has failed + statusUpdated = "true" // The update succeeded + statusNotUpdated = "false" // No update was needed + statusPending = "pending" // The update is pending (see options.DryRun) + statusRolledBack = "rolled back" // Rollback after a failed update +) + // task includes data and state for updating a container type task struct { authfile string // Container-specific authfile @@ -233,7 +241,7 @@ func (t *task) updateRegistry(ctx context.Context) (*entities.AutoUpdateReport, ImageName: rawImageName, Policy: PolicyRegistryImage, SystemdUnit: t.unit, - Updated: "failed", + Updated: statusFailed, } if _, updated := t.auto.updatedRawImages[rawImageName]; updated { @@ -241,7 +249,7 @@ func (t *task) updateRegistry(ctx context.Context) (*entities.AutoUpdateReport, if err := t.auto.restartSystemdUnit(ctx, t.container, t.unit); err != nil { return report, err } - report.Updated = "true" + report.Updated = statusUpdated return report, nil } @@ -251,12 +259,12 @@ func (t *task) updateRegistry(ctx context.Context) (*entities.AutoUpdateReport, } if !needsUpdate { - report.Updated = "false" + report.Updated = statusNotUpdated return report, nil } if t.auto.options.DryRun { - report.Updated = "pending" + report.Updated = statusPending return report, nil } @@ -268,7 +276,7 @@ func (t *task) updateRegistry(ctx context.Context) (*entities.AutoUpdateReport, logrus.Infof("Auto-updating container %q using registry image %q", cid, rawImageName) updateErr := t.auto.restartSystemdUnit(ctx, t.container, t.unit) if updateErr == nil { - report.Updated = "true" + report.Updated = statusUpdated return report, nil } @@ -286,7 +294,7 @@ func (t *task) updateRegistry(ctx context.Context) (*entities.AutoUpdateReport, return report, fmt.Errorf("restarting unit with old image during fallback: %w", err) } - report.Updated = "rolled back" + report.Updated = statusRolledBack return report, nil } @@ -308,7 +316,7 @@ func (t *task) updateLocally(ctx context.Context) (*entities.AutoUpdateReport, e ImageName: rawImageName, Policy: PolicyLocalImage, SystemdUnit: t.unit, - Updated: "failed", + Updated: statusFailed, } needsUpdate, err := newerLocalImageAvailable(t.auto.runtime, t.image, rawImageName) @@ -317,19 +325,19 @@ func (t *task) updateLocally(ctx context.Context) (*entities.AutoUpdateReport, e } if !needsUpdate { - report.Updated = "false" + report.Updated = statusNotUpdated return report, nil } if t.auto.options.DryRun { - report.Updated = "pending" + report.Updated = statusPending return report, nil } logrus.Infof("Auto-updating container %q using local image %q", cid, rawImageName) updateErr := t.auto.restartSystemdUnit(ctx, t.container, t.unit) if updateErr == nil { - report.Updated = "true" + report.Updated = statusUpdated return report, nil } @@ -345,7 +353,7 @@ func (t *task) updateLocally(ctx context.Context) (*entities.AutoUpdateReport, e return report, fmt.Errorf("restarting unit with old image during fallback: %w", err) } - report.Updated = "rolled back" + report.Updated = statusRolledBack return report, nil } -- cgit v1.2.3-54-g00ecf