Skip to content

Commit 296fd47

Browse files
authored
Merge pull request #6224 from cortexproject/fix-default-root-path-for-runtime-config
Runtime-config: Handle absolute file paths when working directory is not /
2 parents 61e749e + 44853ee commit 296fd47

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* [ENHANCEMENT] Distributor: Add new `cortex_reduced_resolution_histogram_samples_total` metric to track the number of histogram samples which resolution was reduced. #6182
1717
* [ENHANCEMENT] StoreGateway: Implement metadata API limit in queryable. #6195
1818
* [ENHANCEMENT] Ingester: Add matchers to ingester LabelNames() and LabelNamesStream() RPC. #6209
19+
* [BUGFIX] Runtime-config: Handle absolute file paths when working directory is not / #6224
1920

2021
## 1.18.0 2024-09-03
2122

integration/e2e/service.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ type ConcreteService struct {
4848
// docker NetworkName used to start this container.
4949
// If empty it means service is stopped.
5050
usedNetworkName string
51+
52+
// workDir is the working directory inside the container
53+
workDir string
5154
}
5255

5356
func NewConcreteService(
@@ -92,6 +95,10 @@ func (s *ConcreteService) SetUser(user string) {
9295
s.user = user
9396
}
9497

98+
func (s *ConcreteService) SetWorkDir(workDir string) {
99+
s.workDir = workDir
100+
}
101+
95102
func (s *ConcreteService) Start(networkName, sharedDir string) (err error) {
96103
// In case of any error, if the container was already created, we
97104
// have to cleanup removing it. We ignore the error of the "docker rm"
@@ -319,6 +326,10 @@ func (s *ConcreteService) buildDockerRunArgs(networkName, sharedDir string) []st
319326
args = append(args, "--user", s.user)
320327
}
321328

329+
if s.workDir != "" {
330+
args = append(args, "--workdir", s.workDir)
331+
}
332+
322333
// Published ports
323334
for _, port := range s.networkPorts {
324335
args = append(args, "-p", strconv.Itoa(port))

integration/runtime_config_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ func TestLoadRuntimeConfigFromStorageBackend(t *testing.T) {
3333

3434
filePath := filepath.Join(e2e.ContainerSharedDir, runtimeConfigFile)
3535
tests := []struct {
36-
name string
37-
flags map[string]string
36+
name string
37+
flags map[string]string
38+
workDir string
3839
}{
3940
{
4041
name: "no storage backend provided",
@@ -57,13 +58,37 @@ func TestLoadRuntimeConfigFromStorageBackend(t *testing.T) {
5758
"-alertmanager-storage.local.path": filepath.Join(e2e.ContainerSharedDir, "alertmanager_configs"),
5859
},
5960
},
61+
{
62+
name: "runtime-config.file is a relative path",
63+
flags: map[string]string{
64+
"-runtime-config.file": runtimeConfigFile,
65+
// alert manager
66+
"-alertmanager.web.external-url": "http://localhost/alertmanager",
67+
"-alertmanager-storage.backend": "local",
68+
"-alertmanager-storage.local.path": filepath.Join(e2e.ContainerSharedDir, "alertmanager_configs"),
69+
},
70+
workDir: e2e.ContainerSharedDir,
71+
},
72+
{
73+
name: "runtime-config.file is an absolute path but working directory is not /",
74+
flags: map[string]string{
75+
"-runtime-config.file": filePath,
76+
// alert manager
77+
"-alertmanager.web.external-url": "http://localhost/alertmanager",
78+
"-alertmanager-storage.backend": "local",
79+
"-alertmanager-storage.local.path": filepath.Join(e2e.ContainerSharedDir, "alertmanager_configs"),
80+
},
81+
workDir: "/var/lib/cortex",
82+
},
6083
}
6184
// make alert manager config dir
6285
require.NoError(t, writeFileToSharedDir(s, "alertmanager_configs", []byte{}))
6386

6487
for i, tt := range tests {
6588
t.Run(tt.name, func(t *testing.T) {
6689
cortexSvc := e2ecortex.NewSingleBinaryWithConfigFile(fmt.Sprintf("cortex-%d", i), cortexConfigFile, tt.flags, "", 9009, 9095)
90+
cortexSvc.SetWorkDir(tt.workDir)
91+
6792
require.NoError(t, s.StartAndWaitReady(cortexSvc))
6893

6994
assertRuntimeConfigLoadedCorrectly(t, cortexSvc)

pkg/cortex/modules.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ func (t *Cortex) initRuntimeConfig() (services.Service, error) {
160160
registerer := prometheus.WrapRegistererWithPrefix("cortex_", prometheus.DefaultRegisterer)
161161
logger := util_log.Logger
162162
bucketClientFactory := func(ctx context.Context) (objstore.Bucket, error) {
163+
// When directory is an empty string but the runtime-config.file is an absolute path,
164+
// the filesystem.NewBucketClient will treat it as a relative path based on the current working directory
165+
// that the process is running in.
166+
if t.Cfg.RuntimeConfig.StorageConfig.Backend == bucket.Filesystem {
167+
if t.Cfg.RuntimeConfig.StorageConfig.Filesystem.Directory == "" {
168+
// Check if runtime-config.file is an absolute path
169+
if t.Cfg.RuntimeConfig.LoadPath[0] == '/' {
170+
// If it is, set the directory to the root directory so that the filesystem bucket
171+
// will treat it as an absolute path. This is to maintain backwards compatibility
172+
// with the previous behavior of the runtime-config.file of allowing relative and absolute paths.
173+
t.Cfg.RuntimeConfig.StorageConfig.Filesystem.Directory = "/"
174+
}
175+
}
176+
}
163177
return bucket.NewClient(ctx, t.Cfg.RuntimeConfig.StorageConfig, "runtime-config", logger, registerer)
164178
}
165179
serv, err := runtimeconfig.New(t.Cfg.RuntimeConfig, registerer, logger, bucketClientFactory)

0 commit comments

Comments
 (0)