From d79ad863e4f3441e63505b505807b8192a7777dd Mon Sep 17 00:00:00 2001 From: Ramkumar Chinchani <45800463+rchincha@users.noreply.github.com> Date: Tue, 14 Feb 2023 09:16:37 -0800 Subject: [PATCH] fix: set GC delay defaults for storage subPaths (#1189) Signed-off-by: Ramkumar Chinchani --- examples/config-multiple-cve.json | 3 ++- pkg/cli/root.go | 21 ++++++++++++++++++++- pkg/cli/root_test.go | 20 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/examples/config-multiple-cve.json b/examples/config-multiple-cve.json index 6f087946..fcb35bc3 100644 --- a/examples/config-multiple-cve.json +++ b/examples/config-multiple-cve.json @@ -7,7 +7,8 @@ "subPaths": { "/infra": { "rootDirectory": "/tmp/zot1", - "dedupe": true + "dedupe": true, + "gc": true }, "/b": { "rootDirectory": "/tmp/zot2", diff --git a/pkg/cli/root.go b/pkg/cli/root.go index 1da69784..1267d0b3 100644 --- a/pkg/cli/root.go +++ b/pkg/cli/root.go @@ -143,6 +143,7 @@ func newVerifyCmd(conf *config.Config) *cobra.Command { Run: func(cmd *cobra.Command, args []string) { if len(args) > 0 { if err := LoadConfiguration(conf, args[0]); err != nil { + log.Error().Msgf("Config file %s is invalid", args[0]) panic(err) } @@ -506,7 +507,7 @@ func applyDefaultValues(config *config.Config, viperInstance *viper.Viper) { config.Storage.GCDelay = 0 } - // cache + // cache settings // global storage @@ -549,6 +550,12 @@ func applyDefaultValues(config *config.Config, viperInstance *viper.Viper) { storageConfig.RemoteCache = false } } + + // if gc is enabled and gcDelay is not set, it is set to default value + if storageConfig.GC && !viperInstance.IsSet("storage::subpaths::"+name+"::gcdelay") { + storageConfig.GCDelay = storage.DefaultGCDelay + config.Storage.SubPaths[name] = storageConfig + } } } @@ -729,6 +736,18 @@ func validateGC(config *config.Config) error { } } + // subpaths + for name, subPath := range config.Storage.SubPaths { + if subPath.GC && subPath.GCDelay <= 0 { + log.Error().Err(errors.ErrBadConfig). + Str("subPath", name). + Interface("gcDelay", subPath.GCDelay). + Msg("invalid GC delay configuration - cannot be negative or zero") + + return errors.ErrBadConfig + } + } + return nil } diff --git a/pkg/cli/root_test.go b/pkg/cli/root_test.go index f871f20b..61e11fa2 100644 --- a/pkg/cli/root_test.go +++ b/pkg/cli/root_test.go @@ -1053,6 +1053,26 @@ func TestLoadConfig(t *testing.T) { err = cli.LoadConfiguration(config, tmpfile.Name()) So(err, ShouldNotBeNil) + content = []byte(`{"storage":{"rootDirectory":"/tmp/zot", + "subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true","gcDelay":"0s"}, + "/b": {"rootDirectory": "/zot-a","dedupe":"true"}}}, + "http":{"address":"127.0.0.1","port":"8080","realm":"zot", + "auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`) + err = os.WriteFile(tmpfile.Name(), content, 0o0600) + So(err, ShouldBeNil) + err = cli.LoadConfiguration(config, tmpfile.Name()) + So(err, ShouldNotBeNil) + + content = []byte(`{"storage":{"rootDirectory":"/tmp/zot", + "subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true","gc":"true"}, + "/b": {"rootDirectory": "/b","dedupe":"true"}}}, + "http":{"address":"127.0.0.1","port":"8080","realm":"zot", + "auth":{"htpasswd":{"path":"test/data/htpasswd"},"failDelay":1}}}`) + err = os.WriteFile(tmpfile.Name(), content, 0o0600) + So(err, ShouldBeNil) + err = cli.LoadConfiguration(config, tmpfile.Name()) + So(err, ShouldBeNil) + content = []byte(`{"storage":{"rootDirectory":"/tmp/zot", "subPaths": {"/a": {"rootDirectory": "/zot-a","dedupe":"true"}, "/b": {"rootDirectory": "/zot-a","dedupe":"true"}}},