forked from cortexproject/cortex
/
runtime_config.go
73 lines (59 loc) · 1.83 KB
/
runtime_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package cortex
import (
"io"
"gopkg.in/yaml.v2"
"github.com/cortexproject/cortex/pkg/ring/kv"
"github.com/cortexproject/cortex/pkg/util/runtimeconfig"
"github.com/cortexproject/cortex/pkg/util/validation"
)
// runtimeConfigValues are values that can be reloaded from configuration file while Cortex is running.
// Reloading is done by runtime_config.Manager, which also keeps the currently loaded config.
// These values are then pushed to the components that are interested in them.
type runtimeConfigValues struct {
TenantLimits map[string]*validation.Limits `yaml:"overrides"`
Multi kv.MultiRuntimeConfig `yaml:"multi_kv_config"`
}
func loadRuntimeConfig(r io.Reader) (interface{}, error) {
var overrides = &runtimeConfigValues{}
decoder := yaml.NewDecoder(r)
decoder.SetStrict(true)
if err := decoder.Decode(&overrides); err != nil {
return nil, err
}
return overrides, nil
}
func tenantLimitsFromRuntimeConfig(c *runtimeconfig.Manager) validation.TenantLimits {
if c == nil {
return nil
}
return func(userID string) *validation.Limits {
cfg, ok := c.GetConfig().(*runtimeConfigValues)
if !ok || cfg == nil {
return nil
}
return cfg.TenantLimits[userID]
}
}
func multiClientRuntimeConfigChannel(manager *runtimeconfig.Manager) func() <-chan kv.MultiRuntimeConfig {
if manager == nil {
return nil
}
// returns function that can be used in MultiConfig.ConfigProvider
return func() <-chan kv.MultiRuntimeConfig {
outCh := make(chan kv.MultiRuntimeConfig, 1)
// push initial config to the channel
val := manager.GetConfig()
if cfg, ok := val.(*runtimeConfigValues); ok && cfg != nil {
outCh <- cfg.Multi
}
ch := manager.CreateListenerChannel(1)
go func() {
for val := range ch {
if cfg, ok := val.(*runtimeConfigValues); ok && cfg != nil {
outCh <- cfg.Multi
}
}
}()
return outCh
}
}