Skip to content

Commit 5682f66

Browse files
pracuccipstibrany
andauthored
Do not log "failed to load config" if runtime config file is empty (#3706)
* Do not log "failed to load config" if runtime config file is empty Signed-off-by: Marco Pracucci <[email protected]> * Do not allow to use a runtime config file containing multiple YAML documents Signed-off-by: Marco Pracucci <[email protected]> * Update pkg/cortex/runtime_config.go Signed-off-by: Marco Pracucci <[email protected]> Co-authored-by: Peter Štibraný <[email protected]> * Update pkg/cortex/runtime_config.go Signed-off-by: Marco Pracucci <[email protected]> Co-authored-by: Peter Štibraný <[email protected]> Co-authored-by: Peter Štibraný <[email protected]>
1 parent b33ae45 commit 5682f66

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
* [BUGFIX] Query-Frontend: avoid creating many small sub-queries by discarding cache extents under 5 minutes #3653
8080
* [BUGFIX] Ruler: Ensure the stale markers generated for evaluated rules respect the configured `-ruler.evaluation-delay-duration`. This will avoid issues with samples with NaN be persisted with timestamps set ahead of the next rule evaluation. #3687
8181
* [BUGFIX] Alertmanager: don't serve HTTP requests until Alertmanager has fully started. Serving HTTP requests earlier may result in loss of configuration for the user. #3679
82+
* [BUGFIX] Do not log "failed to load config" if runtime config file is empty. #3706
83+
* [BUGFIX] Do not allow to use a runtime config file containing multiple YAML documents. #3706
8284

8385
## 1.6.0
8486

pkg/cortex/runtime_config.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cortex
22

33
import (
4+
"errors"
45
"io"
56

67
"gopkg.in/yaml.v2"
@@ -10,6 +11,10 @@ import (
1011
"github.com/cortexproject/cortex/pkg/util/validation"
1112
)
1213

14+
var (
15+
errMultipleDocuments = errors.New("the provided runtime configuration contains multiple documents")
16+
)
17+
1318
// runtimeConfigValues are values that can be reloaded from configuration file while Cortex is running.
1419
// Reloading is done by runtime_config.Manager, which also keeps the currently loaded config.
1520
// These values are then pushed to the components that are interested in them.
@@ -24,10 +29,17 @@ func loadRuntimeConfig(r io.Reader) (interface{}, error) {
2429

2530
decoder := yaml.NewDecoder(r)
2631
decoder.SetStrict(true)
27-
if err := decoder.Decode(&overrides); err != nil {
32+
33+
// Decode the first document. An empty document (EOF) is OK.
34+
if err := decoder.Decode(&overrides); err != nil && !errors.Is(err, io.EOF) {
2835
return nil, err
2936
}
3037

38+
// Ensure the provided YAML config is not composed of multiple documents,
39+
if err := decoder.Decode(&runtimeConfigValues{}); !errors.Is(err, io.EOF) {
40+
return nil, errMultipleDocuments
41+
}
42+
3143
return overrides, nil
3244
}
3345

pkg/cortex/runtime_config_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55
"testing"
66

7+
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
89

910
"github.com/cortexproject/cortex/pkg/util/validation"
@@ -12,7 +13,7 @@ import (
1213
// Given limits are usually loaded via a config file, and that
1314
// a configmap is limited to 1MB, we need to minimise the limits file.
1415
// One way to do it is via YAML anchors.
15-
func TestLoadingAnchoredRuntimeYAML(t *testing.T) {
16+
func TestLoadRuntimeConfig_ShouldLoadAnchoredYAML(t *testing.T) {
1617
yamlFile := strings.NewReader(`
1718
overrides:
1819
'1234': &id001
@@ -49,3 +50,50 @@ overrides:
4950
require.Equal(t, limits, *loadedLimits["1235"])
5051
require.Equal(t, limits, *loadedLimits["1236"])
5152
}
53+
54+
func TestLoadRuntimeConfig_ShouldLoadEmptyFile(t *testing.T) {
55+
yamlFile := strings.NewReader(`
56+
# This is an empty YAML.
57+
`)
58+
actual, err := loadRuntimeConfig(yamlFile)
59+
require.NoError(t, err)
60+
assert.Equal(t, &runtimeConfigValues{}, actual)
61+
}
62+
63+
func TestLoadRuntimeConfig_ShouldReturnErrorOnMultipleDocumentsInTheConfig(t *testing.T) {
64+
cases := []string{
65+
`
66+
---
67+
---
68+
`, `
69+
---
70+
overrides:
71+
'1234':
72+
ingestion_burst_size: 123
73+
---
74+
overrides:
75+
'1234':
76+
ingestion_burst_size: 123
77+
`, `
78+
---
79+
# This is an empty YAML.
80+
---
81+
overrides:
82+
'1234':
83+
ingestion_burst_size: 123
84+
`, `
85+
---
86+
overrides:
87+
'1234':
88+
ingestion_burst_size: 123
89+
---
90+
# This is an empty YAML.
91+
`,
92+
}
93+
94+
for _, tc := range cases {
95+
actual, err := loadRuntimeConfig(strings.NewReader(tc))
96+
assert.Equal(t, errMultipleDocuments, err)
97+
assert.Nil(t, actual)
98+
}
99+
}

pkg/util/runtimeconfig/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ func (om *Manager) loadConfig() error {
146146
buf, err := ioutil.ReadFile(om.cfg.LoadPath)
147147
if err != nil {
148148
om.configLoadSuccess.Set(0)
149-
return err
149+
return errors.Wrap(err, "read file")
150150
}
151151
hash := sha256.Sum256(buf)
152152

153153
cfg, err := om.cfg.Loader(bytes.NewReader(buf))
154154
if err != nil {
155155
om.configLoadSuccess.Set(0)
156-
return err
156+
return errors.Wrap(err, "load file")
157157
}
158158
om.configLoadSuccess.Set(1)
159159

0 commit comments

Comments
 (0)