diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2023-02-23 12:46:01 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2023-03-21 00:38:57 +0300 |
commit | 940a01804178355bc93665561eae633a10b5c065 (patch) | |
tree | 17a51a9c15a772aefec0256e5384e8255c3b3189 /internal/gitaly/config/config.go | |
parent | 2f8b790bd825d18ab848a6f909e8937d254f5dbd (diff) |
gitaly: Validation of DailyJob
The 'DailyJob' configuration should be checked before it is used.
The new 'Validate()' method now added, but it is not yet used
to keep the old behaviour.
The 'cfgerror' package extended with a new 'InRange()' validation
function that is used to verify the value belongs to the expected
range.
Diffstat (limited to 'internal/gitaly/config/config.go')
-rw-r--r-- | internal/gitaly/config/config.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/internal/gitaly/config/config.go b/internal/gitaly/config/config.go index fef4851b4..276a21f3c 100644 --- a/internal/gitaly/config/config.go +++ b/internal/gitaly/config/config.go @@ -53,6 +53,35 @@ type DailyJob struct { Disabled bool `toml:"disabled,omitempty" json:"disabled"` } +// Validate runs validation on all fields and compose all found errors. +func (dj DailyJob) Validate(allowedStorages []string) error { + if dj.Disabled { + return nil + } + + inRangeOpts := []cfgerror.InRangeOpt{cfgerror.InRangeOptIncludeMin, cfgerror.InRangeOptIncludeMax} + errs := cfgerror.New(). + Append(cfgerror.InRange(0, 23, dj.Hour, inRangeOpts...), "start_hour"). + Append(cfgerror.InRange(0, 59, dj.Minute, inRangeOpts...), "start_minute"). + Append(cfgerror.InRange(time.Duration(0), 24*time.Hour, dj.Duration.Duration(), inRangeOpts...), "duration") + + for i, storage := range dj.Storages { + var found bool + for _, allowed := range allowedStorages { + if allowed == storage { + found = true + break + } + } + if !found { + cause := fmt.Errorf("%w: %q", cfgerror.ErrDoesntExist, storage) + errs = errs.Append(cfgerror.NewValidationError(cause, "storages", fmt.Sprintf("[%d]", i))) + } + } + + return errs.AsError() +} + // Cfg is a container for all config derived from config.toml. type Cfg struct { // ConfigCommand specifies the path to an executable that Gitaly will run after loading the |