Skip to content

Runtime-config: Handle absolute file paths when working directory is not / #6224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [ENHANCEMENT] Distributor: Add new `cortex_reduced_resolution_histogram_samples_total` metric to track the number of histogram samples which resolution was reduced. #6182
* [ENHANCEMENT] StoreGateway: Implement metadata API limit in queryable. #6195
* [ENHANCEMENT] Ingester: Add matchers to ingester LabelNames() and LabelNamesStream() RPC. #6209
* [BUGFIX] Runtime-config: Handle absolute file paths when working directory is not / #6224

## 1.18.0 2024-09-03

Expand Down
11 changes: 11 additions & 0 deletions integration/e2e/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type ConcreteService struct {
// docker NetworkName used to start this container.
// If empty it means service is stopped.
usedNetworkName string

// workDir is the working directory inside the container
workDir string
}

func NewConcreteService(
Expand Down Expand Up @@ -92,6 +95,10 @@ func (s *ConcreteService) SetUser(user string) {
s.user = user
}

func (s *ConcreteService) SetWorkDir(workDir string) {
s.workDir = workDir
}

func (s *ConcreteService) Start(networkName, sharedDir string) (err error) {
// In case of any error, if the container was already created, we
// have to cleanup removing it. We ignore the error of the "docker rm"
Expand Down Expand Up @@ -309,6 +316,10 @@ func (s *ConcreteService) buildDockerRunArgs(networkName, sharedDir string) []st
args = append(args, "--user", s.user)
}

if s.workDir != "" {
args = append(args, "--workdir", s.workDir)
}

// Published ports
for _, port := range s.networkPorts {
args = append(args, "-p", strconv.Itoa(port))
Expand Down
29 changes: 27 additions & 2 deletions integration/runtime_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ func TestLoadRuntimeConfigFromStorageBackend(t *testing.T) {

filePath := filepath.Join(e2e.ContainerSharedDir, runtimeConfigFile)
tests := []struct {
name string
flags map[string]string
name string
flags map[string]string
workDir string
}{
{
name: "no storage backend provided",
Expand All @@ -57,13 +58,37 @@ func TestLoadRuntimeConfigFromStorageBackend(t *testing.T) {
"-alertmanager-storage.local.path": filepath.Join(e2e.ContainerSharedDir, "alertmanager_configs"),
},
},
{
name: "runtime-config.file is a relative path",
flags: map[string]string{
"-runtime-config.file": runtimeConfigFile,
// alert manager
"-alertmanager.web.external-url": "http://localhost/alertmanager",
"-alertmanager-storage.backend": "local",
"-alertmanager-storage.local.path": filepath.Join(e2e.ContainerSharedDir, "alertmanager_configs"),
},
workDir: e2e.ContainerSharedDir,
},
{
name: "runtime-config.file is an absolute path but working directory is not /",
flags: map[string]string{
"-runtime-config.file": filePath,
// alert manager
"-alertmanager.web.external-url": "http://localhost/alertmanager",
"-alertmanager-storage.backend": "local",
"-alertmanager-storage.local.path": filepath.Join(e2e.ContainerSharedDir, "alertmanager_configs"),
},
workDir: "/var/lib/cortex",
},
}
// make alert manager config dir
require.NoError(t, writeFileToSharedDir(s, "alertmanager_configs", []byte{}))

for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cortexSvc := e2ecortex.NewSingleBinaryWithConfigFile(fmt.Sprintf("cortex-%d", i), cortexConfigFile, tt.flags, "", 9009, 9095)
cortexSvc.SetWorkDir(tt.workDir)

require.NoError(t, s.StartAndWaitReady(cortexSvc))

assertRuntimeConfigLoadedCorrectly(t, cortexSvc)
Expand Down
14 changes: 14 additions & 0 deletions pkg/cortex/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading