forked from microsoft/retina
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
228 lines (200 loc) · 7.07 KB
/
config.go
File metadata and controls
228 lines (200 loc) · 7.07 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package config
import (
"errors"
"fmt"
"log"
"reflect"
"strings"
"time"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
// Level defines the level of monitor aggregation.
type Level int
const MinTelemetryInterval time.Duration = 2 * time.Minute
const (
Low Level = iota
High
)
type PacketParserRingBufferMode string
const (
PacketParserRingBufferDisabled PacketParserRingBufferMode = "disabled"
PacketParserRingBufferEnabled PacketParserRingBufferMode = "enabled"
PacketParserRingBufferAuto PacketParserRingBufferMode = "auto"
)
var (
ErrPacketParserRingBufferAutoNotSupported = errors.New("packetParserRingBuffer mode auto is not supported yet")
ErrPacketParserRingBufferInvalid = errors.New("packetParserRingBuffer must be set to enabled or disabled")
ErrPacketParserRingBufferInvalidBool = errors.New(
"packetParserRingBuffer must be enabled or disabled, got boolean",
)
)
var (
ErrorTelemetryIntervalTooSmall = fmt.Errorf(
"telemetryInterval smaller than %v is not allowed",
MinTelemetryInterval,
)
DefaultTelemetryInterval = 15 * time.Minute
DefaultSamplingRate uint32 = 1
)
func (l *Level) UnmarshalText(text []byte) error {
s := strings.ToLower(string(text))
switch s {
case "low":
*l = Low
case "high":
*l = High
default:
// Default to Low if the text is not recognized.
*l = Low
}
return nil
}
func (l *Level) String() string {
switch *l {
case Low:
return "low"
case High:
return "high"
default:
return ""
}
}
func (m *PacketParserRingBufferMode) UnmarshalText(text []byte) error {
s := strings.ToLower(strings.TrimSpace(string(text)))
switch s {
case string(PacketParserRingBufferEnabled):
*m = PacketParserRingBufferEnabled
return nil
case string(PacketParserRingBufferDisabled):
*m = PacketParserRingBufferDisabled
return nil
case string(PacketParserRingBufferAuto):
return ErrPacketParserRingBufferAutoNotSupported
case "":
return ErrPacketParserRingBufferInvalid
default:
return fmt.Errorf("invalid packetParserRingBuffer %q: %w", s, ErrPacketParserRingBufferInvalid)
}
}
func (m *PacketParserRingBufferMode) IsEnabled() bool {
return *m == PacketParserRingBufferEnabled
}
type Server struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
}
type Config struct {
APIServer Server `yaml:"apiServer"`
LogLevel string `yaml:"logLevel"`
EnabledPlugin []string `yaml:"enabledPlugin"`
MetricsInterval time.Duration `yaml:"metricsInterval"`
// Deprecated: Use only MetricsInterval instead in the go code.
MetricsIntervalDuration time.Duration `yaml:"metricsIntervalDuration"`
EnableTelemetry bool `yaml:"enableTelemetry"`
EnableRetinaEndpoint bool `yaml:"enableRetinaEndpoint"`
EnablePodLevel bool `yaml:"enablePodLevel"`
EnableConntrackMetrics bool `yaml:"enableConntrackMetrics"`
RemoteContext bool `yaml:"remoteContext"`
EnableAnnotations bool `yaml:"enableAnnotations"`
BypassLookupIPOfInterest bool `yaml:"bypassLookupIPOfInterest"`
DataAggregationLevel Level `yaml:"dataAggregationLevel"`
MonitorSockPath string `yaml:"monitorSockPath"`
TelemetryInterval time.Duration `yaml:"telemetryInterval"`
DataSamplingRate uint32 `yaml:"dataSamplingRate"`
PacketParserRingBuffer PacketParserRingBufferMode `yaml:"packetParserRingBuffer"`
PacketParserRingBufferSize uint32 `yaml:"packetParserRingBufferSize"`
FilterMapMaxEntries uint32 `yaml:"filterMapMaxEntries"`
}
func GetConfig(cfgFilename string) (*Config, error) {
if cfgFilename != "" {
viper.SetConfigFile(cfgFilename)
} else {
viper.SetConfigName("config")
viper.AddConfigPath("/retina/config")
}
viper.SetEnvPrefix("retina")
viper.AutomaticEnv()
// NOTE(mainred): RetinaEndpoint is currently the only supported solution to cache Pod, and before an alternative is implemented,
// we make EnableRetinaEndpoint true and cannot be configurable.
viper.SetDefault("EnableRetinaEndpoint", true)
err := viper.ReadInConfig()
if err != nil {
return nil, fmt.Errorf("fatal error config file: %s", err)
}
var config Config
decoderConfigOption := viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(), // default hook.
mapstructure.StringToSliceHookFunc(","), // default hook.
decodeLevelHook,
decodePacketParserRingBufferModeHook,
))
err = viper.Unmarshal(&config, decoderConfigOption)
if err != nil {
return nil, fmt.Errorf("fatal error config file: %s", err)
}
if config.MetricsIntervalDuration != 0 {
config.MetricsInterval = config.MetricsIntervalDuration
} else if config.MetricsInterval != 0 {
config.MetricsInterval *= time.Second
log.Print("metricsInterval is deprecated, please use metricsIntervalDuration instead")
}
// If unset, default telemetry interval to 15 minutes.
if config.TelemetryInterval == 0 {
log.Printf("telemetryInterval is not set, defaulting to %v", DefaultTelemetryInterval)
config.TelemetryInterval = DefaultTelemetryInterval
} else if config.TelemetryInterval < MinTelemetryInterval {
return nil, ErrorTelemetryIntervalTooSmall
}
// If unset, default sampling rate to 1
if config.DataSamplingRate == 0 {
log.Printf("dataSamplingRate is not set, defaulting to %v", DefaultSamplingRate)
config.DataSamplingRate = DefaultSamplingRate
}
// Default filter map max entries to 255 if not set.
if config.FilterMapMaxEntries == 0 {
config.FilterMapMaxEntries = 255
}
switch config.PacketParserRingBuffer { //nolint:exhaustive // we only care about Auto and empty (default) here
case "":
config.PacketParserRingBuffer = PacketParserRingBufferDisabled
case PacketParserRingBufferAuto:
return nil, ErrPacketParserRingBufferAutoNotSupported
}
return &config, nil
}
func decodeLevelHook(field, target reflect.Type, data interface{}) (interface{}, error) {
// Check if the field we are decoding is a string.
if field.Kind() != reflect.String {
return data, nil
}
// Check if the type we are decoding to is a Level.
if target != reflect.TypeOf(Level(0)) {
return data, nil
}
var level Level
err := level.UnmarshalText([]byte(data.(string)))
if err != nil {
return nil, err
}
return level, nil
}
func decodePacketParserRingBufferModeHook(field, target reflect.Type, data interface{}) (interface{}, error) {
if target != reflect.TypeOf(PacketParserRingBufferMode("")) {
return data, nil
}
switch field.Kind() { //nolint:exhaustive // we only care about String and Bool
case reflect.String:
var mode PacketParserRingBufferMode
if err := mode.UnmarshalText([]byte(data.(string))); err != nil {
return nil, err
}
return mode, nil
case reflect.Bool:
return nil, ErrPacketParserRingBufferInvalidBool
default:
return data, nil
}
}