-
Notifications
You must be signed in to change notification settings - Fork 237
/
config.go
277 lines (225 loc) · 8.34 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
package config
import (
"fmt"
"github.com/creasty/defaults"
"github.com/go-playground/validator/v10"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
var validate *validator.Validate
// Config represents all the parameters required for the app to be configured properly
type Config struct {
// Log configuration for the exporter
Log Log `yaml:"log" validate:"dive"`
// Server related configuration
Server Server `yaml:"server" validate:"dive"`
// GitLab related configuration
Gitlab Gitlab `yaml:"gitlab" validate:"dive"`
// Redis related configuration
Redis Redis `yaml:"redis" validate:"dive"`
// Pull configuration
Pull Pull `yaml:"pull" validate:"dive"`
// GarbageCollect configuration
GarbageCollect GarbageCollect `yaml:"garbage_collect" validate:"dive"`
// Default parameters which can be overridden at either the Project or Wildcard level
ProjectDefaults ProjectParameters `yaml:"project_defaults" validate:"dive"`
// List of projects to pull
Projects []Project `validate:"unique,at-least-1-project-or-wildcard,dive" yaml:"projects"`
// List of wildcards to search projects from
Wildcards []Wildcard `validate:"unique,at-least-1-project-or-wildcard,dive" yaml:"wildcards"`
}
// Log holds runtime logging configuration
type Log struct {
// Log level
Level string `default:"info" validate:"required,oneof=trace debug info warning error fatal panic"`
// Log format
Format string `default:"text" validate:"oneof=text json"`
}
// Server ..
type Server struct {
// Enable profiling pages
EnablePprof bool `default:"false" yaml:"enable_pprof"`
// [address:port] to make the process listen upon
ListenAddress string `default:":8080" yaml:"listen_address"`
Metrics ServerMetrics `yaml:"metrics"`
Webhook ServerWebhook `yaml:"webhook"`
}
// ServerMetrics ..
type ServerMetrics struct {
// Enable /metrics endpoint
Enabled bool `default:"true" yaml:"enabled"`
// Enable OpenMetrics content encoding in prometheus HTTP handler
EnableOpenmetricsEncoding bool `default:"false" yaml:"enable_openmetrics_encoding"`
}
// ServerWebhook ..
type ServerWebhook struct {
// Enable /webhook endpoint to support GitLab requests
Enabled bool `default:"false" yaml:"enabled"`
// Secret token to authenticate legitimate webhook requests coming from the GitLab server
SecretToken string `validate:"required_if=Enabled true" yaml:"secret_token"`
}
// Gitlab ..
type Gitlab struct {
// The URL of the GitLab server/api
URL string `default:"https://gitlab.com" validate:"required,url" yaml:"url"`
// Token to use to authenticate against the API
Token string `validate:"required" yaml:"token"`
// The URL of the GitLab server/api health endpoint (default to /users/sign_in which is publicly available on gitlab.com)
HealthURL string `default:"https://gitlab.com/explore" validate:"required,url" yaml:"health_url"`
// Whether to validate the service is reachable calling HealthURL
EnableHealthCheck bool `default:"true" yaml:"enable_health_check"`
// Whether to skip TLS validation when querying HealthURL
EnableTLSVerify bool `default:"true" yaml:"enable_tls_verify"`
// Rate limit for the GitLab API requests/sec
MaximumRequestsPerSecond int `default:"1" validate:"gte=1" yaml:"maximum_requests_per_second"`
}
// Redis ..
type Redis struct {
// URL used to connect onto the redis endpoint
// format: redis[s]://[:password@]host[:port][/db-number][?option=value])
URL string `yaml:"url"`
}
// Pull ..
type Pull struct {
// ProjectsFromWildcards configuration
ProjectsFromWildcards struct {
OnInit bool `default:"true" yaml:"on_init"`
Scheduled bool `default:"true" yaml:"scheduled"`
IntervalSeconds int `default:"1800" validate:"gte=1" yaml:"interval_seconds"`
} `yaml:"projects_from_wildcards"`
// EnvironmentsFromProjects configuration
EnvironmentsFromProjects struct {
OnInit bool `default:"true" yaml:"on_init"`
Scheduled bool `default:"true" yaml:"scheduled"`
IntervalSeconds int `default:"1800" validate:"gte=1" yaml:"interval_seconds"`
} `yaml:"environments_from_projects"`
// RefsFromProjects configuration
RefsFromProjects struct {
OnInit bool `default:"true" yaml:"on_init"`
Scheduled bool `default:"true" yaml:"scheduled"`
IntervalSeconds int `default:"300" validate:"gte=1" yaml:"interval_seconds"`
} `yaml:"refs_from_projects"`
// Metrics configuration
Metrics struct {
OnInit bool `default:"true" yaml:"on_init"`
Scheduled bool `default:"true" yaml:"scheduled"`
IntervalSeconds int `default:"30" validate:"gte=1" yaml:"interval_seconds"`
} `yaml:"metrics"`
}
// GarbageCollect ..
type GarbageCollect struct {
// Projects configuration
Projects struct {
OnInit bool `default:"false" yaml:"on_init"`
Scheduled bool `default:"true" yaml:"scheduled"`
IntervalSeconds int `default:"14400" validate:"gte=1" yaml:"interval_seconds"`
} `yaml:"projects"`
// Environments configuration
Environments struct {
OnInit bool `default:"false" yaml:"on_init"`
Scheduled bool `default:"true" yaml:"scheduled"`
IntervalSeconds int `default:"14400" validate:"gte=1" yaml:"interval_seconds"`
} `yaml:"environments"`
// Refs configuration
Refs struct {
OnInit bool `default:"false" yaml:"on_init"`
Scheduled bool `default:"true" yaml:"scheduled"`
IntervalSeconds int `default:"1800" validate:"gte=1" yaml:"interval_seconds"`
} `yaml:"refs"`
// Metrics configuration
Metrics struct {
OnInit bool `default:"false" yaml:"on_init"`
Scheduled bool `default:"true" yaml:"scheduled"`
IntervalSeconds int `default:"300" validate:"gte=1" yaml:"interval_seconds"`
} `yaml:"metrics"`
}
// UnmarshalYAML allows us to correctly hydrate our configuration using some
// custom logic
func (c *Config) UnmarshalYAML(v *yaml.Node) (err error) {
type localConfig struct {
Log Log `yaml:"log"`
Server Server `yaml:"server"`
Gitlab Gitlab `yaml:"gitlab"`
Redis Redis `yaml:"redis"`
Pull Pull `yaml:"pull"`
GarbageCollect GarbageCollect `yaml:"garbage_collect"`
ProjectDefaults ProjectParameters `yaml:"project_defaults"`
Projects []yaml.Node `yaml:"projects"`
Wildcards []yaml.Node `yaml:"wildcards"`
}
_cfg := localConfig{}
defaults.MustSet(&_cfg)
if err = v.Decode(&_cfg); err != nil {
return
}
c.Log = _cfg.Log
c.Server = _cfg.Server
c.Gitlab = _cfg.Gitlab
c.Redis = _cfg.Redis
c.Pull = _cfg.Pull
c.GarbageCollect = _cfg.GarbageCollect
c.ProjectDefaults = _cfg.ProjectDefaults
for _, n := range _cfg.Projects {
p := c.NewProject()
if err = n.Decode(&p); err != nil {
return
}
c.Projects = append(c.Projects, p)
}
for _, n := range _cfg.Wildcards {
w := c.NewWildcard()
if err = n.Decode(&w); err != nil {
return
}
c.Wildcards = append(c.Wildcards, w)
}
return
}
// Validate will throw an error if the Config parameters are whether incomplete or incorrects
func (c Config) Validate() error {
if validate == nil {
validate = validator.New()
_ = validate.RegisterValidation("at-least-1-project-or-wildcard", ValidateAtLeastOneProjectOrWildcard)
}
return validate.Struct(c)
}
// SchedulerConfig ..
type SchedulerConfig struct {
OnInit bool
Scheduled bool
IntervalSeconds int
}
// Log returns some logging fields to showcase the configuration to the enduser
func (sc SchedulerConfig) Log() log.Fields {
onInit, scheduled := "no", "no"
if sc.OnInit {
onInit = "yes"
}
if sc.Scheduled {
scheduled = fmt.Sprintf("every %vs", sc.IntervalSeconds)
}
return log.Fields{
"on-init": onInit,
"scheduled": scheduled,
}
}
// ValidateAtLeastOneProjectOrWildcard implements validator.Func
// assess that we have at least one projet or wildcard configured
func ValidateAtLeastOneProjectOrWildcard(v validator.FieldLevel) bool {
return v.Parent().FieldByName("Projects").Len() > 0 || v.Parent().FieldByName("Wildcards").Len() > 0
}
// New returns a new config with the default parameters
func New() (c Config) {
defaults.MustSet(&c)
return
}
// NewProject returns a new project with the config default parameters
func (c Config) NewProject() (p Project) {
p.ProjectParameters = c.ProjectDefaults
return
}
// NewWildcard returns a new wildcard with the config default parameters
func (c Config) NewWildcard() (w Wildcard) {
w.ProjectParameters = c.ProjectDefaults
return
}