-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
326 lines (272 loc) · 8.25 KB
/
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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package config
import (
"errors"
"fmt"
"io/fs"
"os"
"strconv"
"strings"
"github.com/rs/zerolog"
)
const (
// AccessKeyEnvVar is the name of the environment variable where the access
// key to the Infralight App Server must be provided
AccessKeyEnvVar = "INFRALIGHT_ACCESS_KEY"
// SecretKeyEnvVar is the name of the environment variable where the secret
// key to the Infralight App Server must be provided
SecretKeyEnvVar = "INFRALIGHT_SECRET_KEY" // nolint: gosec
// DefaultConfigDir is the path to the default directory where configuration
// files (generally mounted from a Kubernetes ConfigMap) must be present.
DefaultConfigDir = "/etc/config"
// DefaultFireflyAPI is the default URL for Firefly's API
DefaultFireflyAPI = "https://k8s-api.prod.external.api.infralight.cloud"
// DefaultFireflyLoginAPI is the default URL for Firefly's Login API
DefaultFireflyLoginAPI = "https://prod.external.api.infralight.cloud"
)
var (
// ErrAccessKeys is an error returned when the environment variables for the
// access and secret keys are not provided or empty.
ErrAccessKeys = errors.New("access and secret keys must be provided")
// ErrEndpoint is an error returned when the configuration directory is
// missing an endpoint setting (endpoint is the URL to the Infralight App
// Server).
ErrEndpoint = errors.New("Infralight endpoint must be provided")
// DefaultResourceTypes is the list of Kubernetes resources that are
// to be collected by default (i.e. if there is no configuration at all)
DefaultResourceTypes = []string{
"apiservices",
"analysistemplates",
"clusteranalysistemplates",
"clusterroles",
"clusterrolebindings",
"configmaps",
"controllerrevisions",
"csinodes",
"cronjobs",
"customresourcedefinitions",
"daemonsets",
"deployments",
"endpoints",
"endpointslices",
"flowschemas",
"ingresses",
"jobs",
"leases",
"namespaces",
"networkpolicies",
"nodes",
"persistentvolumeclaims",
"persistentvolumes",
"pods",
"priorityclasses",
"prioritylevelconfigurations",
"replicasets",
"replicationcontrollers",
"roles",
"rolebindings",
"rollouts",
"rollouts/finalizers",
"rollouts/status",
"serviceaccounts",
"services",
"services/status",
"statefulsets",
"storageclasses",
}
)
// Config represents configuration to the collector library. It is shared
// between the different data collectors (impementing the collector.DataCollector
// interface).
type Config struct {
// File system object from which configuration files are read. by default,
// this is the local file system; an in-memory file system is used in the
// unit tests
FS fs.FS
// The directory inside fs where configuration files are stored. by default,
// this is /etc/config
ConfigDir string
// DryRun indicates whether the collector should only perform local read
// operations. When true, authentication against the Firefly API is not
// made, as is sending of collected data. Data is printed to standard output
// instead
DryRun bool
// The logger instance
Log *zerolog.Logger
// AccessKey is the Infralight access key
AccessKey string
// SecretKey is the Infralight secret key
SecretKey string
// Endpoint is the URL to the Infralight App Server
Endpoint string
// LoginEndpoint is the URL to login Infralight Service
LoginEndpoint string
// Namespace is the Kubernets namespace we're collecting data from (if empty,
// all namespaces are collected)
Namespace string
// IgnoreNamespaces is a list of namespaces to ignore (only taken into
// account when Namespace is empty)
IgnoreNamespaces []string
// AllowedResources is a list of resource types (named by their "Kind" value)
// that the collector is allowed to collect
AllowedResources map[string]bool
// OverrideUniqueClusterId is a boolean indicating whether to override the master url of the Kubernetes integration
OverrideUniqueClusterId bool
// PageSize is an integer for max page size in KB
PageSize int
// MaxGoRoutines is an integer for max goroutines running at ones sending the chunks.
MaxGoRoutines int
}
// LoadConfig creates a new configuration object. A logger object, a file-system
// object (where configuration files are stored), and a path to the configuration
// directory may be provided. All parameters are optional. If not provided,
// a noop logger is used, the local file system is used, and DefaultConfigDir is
// used.
func LoadConfig(
log *zerolog.Logger,
cfs fs.FS,
configDir string,
dryRun bool,
) (conf *Config, err error) {
if log == nil {
l := zerolog.Nop()
log = &l
}
if cfs == nil {
log.Debug().Msg("No file system object provided, using default one")
cfs = &localFS{}
}
if configDir == "" {
configDir = DefaultConfigDir
}
// load Infralight API Key from the environment, this is required
accessKey := os.Getenv(AccessKeyEnvVar)
secretKey := os.Getenv(SecretKeyEnvVar)
if !dryRun && (accessKey == "" || secretKey == "") {
return conf, ErrAccessKeys
}
conf = &Config{
FS: cfs,
ConfigDir: configDir,
Log: log,
DryRun: dryRun,
}
conf.Endpoint = strings.TrimSuffix(
parseOne(conf.etcConfig("endpoint"), ""),
"/",
)
if conf.Endpoint == "" || isOldEndpoint(conf.Endpoint) {
conf.Endpoint = DefaultFireflyAPI
}
conf.LoginEndpoint = strings.TrimSuffix(
parseOne(conf.etcConfig("loginEndpoint"), ""),
"/",
)
if conf.LoginEndpoint == "" {
conf.LoginEndpoint = DefaultFireflyLoginAPI
}
conf.AccessKey = accessKey
conf.SecretKey = secretKey
conf.Namespace = parseOne(conf.etcConfig("collector.watchNamespace"), "")
conf.IgnoreNamespaces = parseMultiple(conf.etcConfig("collector.ignoreNamespaces"), nil)
conf.AllowedResources = make(map[string]bool)
conf.backwardsCompatibilityResources()
for _, resource := range parseMultiple(conf.etcConfig("collector.resources"), DefaultResourceTypes) {
conf.AllowedResources[resource] = true
}
conf.OverrideUniqueClusterId = parseBool(
conf.etcConfig("collector.OverrideUniqueClusterId"),
false,
)
conf.PageSize = parseInt(conf.etcConfig("collector.PageSize"), 500)
conf.MaxGoRoutines = parseInt(conf.etcConfig("collector.MaxGoRoutines"), 50)
return conf, nil
}
func (conf *Config) backwardsCompatibilityResources() {
entries, err := fs.ReadDir(conf.FS, conf.ConfigDir)
if err != nil {
return
}
for _, entry := range entries {
if strings.HasPrefix(entry.Name(), "collector.resources.") {
name := strings.ToLower(strings.TrimPrefix(entry.Name(), "collector.resources."))
conf.AllowedResources[name] = parseBool(conf.etcConfig(entry.Name()), false)
}
}
}
// IgnoreNamespace accepts a namespace and returns a boolean value indicating
// whether the namespace should be ignored
func (conf *Config) IgnoreNamespace(ns string) bool {
if conf.Namespace != "" && ns != conf.Namespace {
return false
}
if len(conf.IgnoreNamespaces) > 0 {
return includes(conf.IgnoreNamespaces, ns)
}
return false
}
func parseOne(str, defVal string) string {
str = strings.TrimSpace(str)
if str == "" {
return defVal
}
return str
}
func parseInt(str string, defVal int) int {
str = strings.TrimSpace(str)
asInt, err := strconv.Atoi(str)
if err != nil {
return defVal
}
return asInt
}
func parseMultiple(str string, defVal []string) []string {
str = strings.TrimSpace(str)
if str == "" {
return defVal
}
return strings.Split(str, "\n")
}
func parseBool(str string, defVal bool) bool {
str = strings.TrimSpace(str)
if str == "" {
return defVal
}
asBool, err := strconv.ParseBool(str)
if err != nil {
return defVal
}
return asBool
}
func includes(list []string, value string) bool {
for _, val := range list {
if val == value {
return true
}
}
return false
}
func (conf *Config) etcConfig(name string) string {
data, err := fs.ReadFile(
conf.FS,
fmt.Sprintf("%s/%s", strings.TrimPrefix(conf.ConfigDir, "/"), name),
)
if err != nil {
// only log this error if it's _not_ a "no such file or directory"
// error
if !os.IsNotExist(err) {
conf.Log.Warn().
Err(err).
Str("key", name).
Msg("Failed loading configuration key")
}
return ""
}
return string(data)
}
type localFS struct{}
func (fs *localFS) Open(name string) (fs.File, error) {
return os.Open("/" + name)
}
func isOldEndpoint(endpoint string) bool {
return endpoint == "https://prodapi.infralight.cloud/api"
}