-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
runtime_config.go
135 lines (111 loc) · 3.31 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package loki
import (
"fmt"
"io"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/kv"
"github.com/grafana/dskit/runtimeconfig"
"gopkg.in/yaml.v2"
"github.com/grafana/loki/v3/pkg/runtime"
util_log "github.com/grafana/loki/v3/pkg/util/log"
"github.com/grafana/loki/v3/pkg/validation"
)
// runtimeConfigValues are values that can be reloaded from configuration file while Loki is running.
// Reloading is done by runtimeconfig.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"`
TenantConfig map[string]*runtime.Config `yaml:"configs"`
Multi kv.MultiRuntimeConfig `yaml:"multi_kv_config"`
}
func (r runtimeConfigValues) validate() error {
for t, c := range r.TenantLimits {
if c == nil {
level.Warn(util_log.Logger).Log("msg", "skipping empty tenant limit definition", "tenant", t)
continue
}
if err := c.Validate(); err != nil {
return fmt.Errorf("invalid override for tenant %s: %w", t, err)
}
}
return nil
}
func loadRuntimeConfig(r io.Reader) (interface{}, error) {
overrides := &runtimeConfigValues{}
decoder := yaml.NewDecoder(r)
decoder.SetStrict(true)
if err := decoder.Decode(&overrides); err != nil {
return nil, err
}
if err := overrides.validate(); err != nil {
return nil, err
}
return overrides, nil
}
// tenantLimitsFromRuntimeConfig implements validation.Limits
type tenantLimitsFromRuntimeConfig struct {
c *runtimeconfig.Manager
}
func (t *tenantLimitsFromRuntimeConfig) AllByUserID() map[string]*validation.Limits {
if t.c == nil {
return nil
}
cfg, ok := t.c.GetConfig().(*runtimeConfigValues)
if cfg != nil && ok {
return cfg.TenantLimits
}
return nil
}
func (t *tenantLimitsFromRuntimeConfig) TenantLimits(userID string) *validation.Limits {
allByUserID := t.AllByUserID()
if allByUserID == nil {
return nil
}
return allByUserID[userID]
}
func newtenantLimitsFromRuntimeConfig(c *runtimeconfig.Manager) validation.TenantLimits {
return &tenantLimitsFromRuntimeConfig{c: c}
}
type tenantConfigProvider struct {
c *runtimeconfig.Manager
}
func newTenantConfigProvider(c *runtimeconfig.Manager) runtime.TenantConfigProvider {
return &tenantConfigProvider{c: c}
}
// TenantConfig returns the user config or default config if none was defined.
func (t *tenantConfigProvider) TenantConfig(userID string) *runtime.Config {
if t.c == nil {
return nil
}
cfg, ok := t.c.GetConfig().(*runtimeConfigValues)
if !ok || cfg == nil {
return nil
}
if tenantCfg, ok := cfg.TenantConfig[userID]; ok {
return tenantCfg
}
return nil
}
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
}
}