From 6dbccee90b6f53d593337c83ee98262cdb701317 Mon Sep 17 00:00:00 2001 From: Charlie Le <3375195+CharlieTLe@users.noreply.github.com> Date: Fri, 20 Sep 2024 14:48:46 -0700 Subject: [PATCH] Add logic for handling default filesystem root dir We started using a BucketClient for the filesystem where the default directory is an empty string: https://github.com/cortexproject/cortex/blob/af9e20c54ee97f409008f3e86541c5dfa5038e22/pkg/storage/bucket/filesystem/config.go#L17 When we create the new bucket, https://github.com/cortexproject/cortex/blob/526a6d935a948119cf74033a9f79391786022222/vendor/github.com/thanos-io/objstore/providers/filesystem/filesystem.go#L46 uses `filepath.Abs("")` which will use the current working directory as the root directory. Fixes: https://github.com/cortexproject/cortex/issues/6219 Signed-off-by: Charlie Le --- pkg/cortex/modules.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/cortex/modules.go b/pkg/cortex/modules.go index 9e72894f9e..9489fab2ec 100644 --- a/pkg/cortex/modules.go +++ b/pkg/cortex/modules.go @@ -160,6 +160,20 @@ func (t *Cortex) initRuntimeConfig() (services.Service, error) { registerer := prometheus.WrapRegistererWithPrefix("cortex_", prometheus.DefaultRegisterer) logger := util_log.Logger bucketClientFactory := func(ctx context.Context) (objstore.Bucket, error) { + // When directory is an empty string but the runtime-config.file is an absolute path, + // the filesystem.NewBucketClient will treat it as a relative path based on the current working directory + // that the process is running in. + if t.Cfg.RuntimeConfig.StorageConfig.Backend == bucket.Filesystem { + if t.Cfg.RuntimeConfig.StorageConfig.Filesystem.Directory == "" { + // Check if runtime-config.file is an absolute path + if t.Cfg.RuntimeConfig.LoadPath[0] == '/' { + // If it is, set the directory to the root directory so that the filesystem bucket + // will treat it as an absolute path. This is to maintain backwards compatibility + // with the previous behavior of the runtime-config.file of allowing relative and absolute paths. + t.Cfg.RuntimeConfig.StorageConfig.Filesystem.Directory = "/" + } + } + } return bucket.NewClient(ctx, t.Cfg.RuntimeConfig.StorageConfig, "runtime-config", logger, registerer) } serv, err := runtimeconfig.New(t.Cfg.RuntimeConfig, registerer, logger, bucketClientFactory)