-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
240 lines (201 loc) · 6.67 KB
/
config.go
File metadata and controls
240 lines (201 loc) · 6.67 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
229
230
231
232
233
234
235
236
237
238
239
240
package config
import (
"errors"
"fmt"
"strings"
"sync/atomic"
"testing"
"github.com/go-playground/validator"
"github.com/go-viper/mapstructure/v2"
"github.com/spf13/viper"
)
var appConfig atomic.Pointer[CatteryConfig]
func init() {
appConfig.Store(&CatteryConfig{})
}
// Get returns the current config snapshot.
func Get() *CatteryConfig {
return appConfig.Load()
}
// Set atomically replaces the config. Used by LoadConfig and tests.
func Set(cfg *CatteryConfig) {
appConfig.Store(cfg)
}
// SetForTest sets the config for the duration of a test and restores it on cleanup.
func SetForTest(t *testing.T, cfg *CatteryConfig) {
cfg.InitMaps()
old := Get()
Set(cfg)
t.Cleanup(func() { Set(old) })
}
type CatteryConfig struct {
Server ServerConfig `yaml:"server" validate:"required"`
Database DatabaseConfig `yaml:"database" validate:"required"`
Github []*GitHubOrganization `yaml:"github" validate:"required,dive,required"`
Providers []*ProviderConfig `yaml:"providers" validate:"required,dive,required"`
TrayTypes []*TrayType `yaml:"trayTypes" validate:"required,dive,required"`
githubMap map[string]*GitHubOrganization
providerMap map[string]*ProviderConfig
trayTypesMap map[string]*TrayType
}
// InitMaps builds the internal lookup maps from the slice fields.
// Called automatically by LoadConfig; call manually when constructing CatteryConfig in tests.
func (c *CatteryConfig) InitMaps() {
c.githubMap = make(map[string]*GitHubOrganization)
for _, org := range c.Github {
c.githubMap[org.Name] = org
}
c.providerMap = make(map[string]*ProviderConfig)
for _, p := range c.Providers {
c.providerMap[p.Get("name")] = p
}
c.trayTypesMap = make(map[string]*TrayType)
for _, tt := range c.TrayTypes {
c.trayTypesMap[tt.Name] = tt
}
}
func LoadConfig(configPath *string) (*CatteryConfig, error) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
if *configPath == "" {
viper.AddConfigPath("/etc/cattery/")
viper.AddConfigPath("./")
} else {
viper.SetConfigFile(*configPath)
}
err := viper.ReadInConfig()
if err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError
if errors.As(err, &configFileNotFoundError) {
return nil, fmt.Errorf("config file not found")
} else {
return nil, fmt.Errorf("fatal error reading config file: %w", err)
}
}
cfg := &CatteryConfig{}
err = viper.Unmarshal(cfg)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal config file: %w", err)
}
switch cfg.Database.Type {
case "":
return nil, fmt.Errorf("database.type is required (supported: sqlite, mongodb)")
case "mongodb":
if cfg.Database.Uri == "" {
return nil, fmt.Errorf("database.uri is required for mongodb")
}
if cfg.Database.Database == "" {
return nil, fmt.Errorf("database.database is required for mongodb")
}
case "sqlite":
if cfg.Database.Path == "" {
return nil, fmt.Errorf("database.path is required for sqlite")
}
default:
return nil, fmt.Errorf("unsupported database type: %s", cfg.Database.Type)
}
cfg.githubMap = make(map[string]*GitHubOrganization)
for _, organization := range cfg.Github {
cfg.githubMap[organization.Name] = organization
}
cfg.providerMap = make(map[string]*ProviderConfig)
for _, provider := range cfg.Providers {
cfg.providerMap[provider.Get("name")] = provider
}
cfg.trayTypesMap = make(map[string]*TrayType)
for _, trayType := range cfg.TrayTypes {
cfg.trayTypesMap[trayType.Name] = trayType
providerConfig, ok := cfg.providerMap[trayType.Provider]
if !ok {
return nil, fmt.Errorf("provider %s for trayType %s not found", trayType.Provider, trayType.Name)
}
var decodeError error
switch providerConfig.Get("type") {
case "google":
var gc GoogleTrayConfig
decodeError = mapstructure.Decode(trayType.Config, &gc)
trayType.Config = gc
case "docker":
var dc DockerTrayConfig
decodeError = mapstructure.Decode(trayType.Config, &dc)
trayType.Config = dc
//case "scaleway":
default:
}
if decodeError != nil {
return nil, fmt.Errorf("failed to decode '%s' %w", providerConfig.Get("type"), decodeError)
}
}
validate := validator.New()
err = validate.Struct(cfg)
if err != nil {
// err is of type validator.ValidationErrors
for _, fieldErr := range err.(validator.ValidationErrors) {
return nil, fmt.Errorf("Validation failed on field '%s' for tag '%s'\n", fieldErr.Namespace(), fieldErr.Tag())
}
}
Set(cfg)
return cfg, nil
}
// GetGitHubOrg returns the GitHub organization by name
func (c *CatteryConfig) GetGitHubOrg(name string) *GitHubOrganization {
org, ok := c.githubMap[name]
if !ok {
return nil
}
return org
}
// GetProvider returns the provider by name
func (c *CatteryConfig) GetProvider(name string) *ProviderConfig {
provider, ok := c.providerMap[name]
if !ok {
return nil
}
return provider
}
// GetTrayType returns the tray type by name
func (c *CatteryConfig) GetTrayType(name string) *TrayType {
trayType, ok := c.trayTypesMap[name]
if !ok {
return nil
}
return trayType
}
type ServerConfig struct {
ListenAddress string `yaml:"listenAddress" validate:"required"`
// StatusListenAddress is the address for the /status and /metrics endpoints.
// If empty or equal to ListenAddress, these routes are served on the agent port.
StatusListenAddress string `yaml:"statusListenAddress"`
AdvertiseUrl string `yaml:"advertiseUrl" validate:"required"`
AgentSecret string `yaml:"agentSecret"`
}
type DatabaseConfig struct {
Type string `yaml:"type"` // "mongodb" (default) or "sqlite"
Uri string `yaml:"uri"`
Database string `yaml:"database"`
Path string `yaml:"path"` // SQLite file path
}
type GitHubOrganization struct {
Name string `yaml:"name" validate:"required"`
AppId int64 `yaml:"appId" validate:"required"`
AppClientId string `yaml:"appClientId" validate:"required"`
InstallationId int64 `yaml:"installationId" validate:"required"`
PrivateKeyPath string `yaml:"privateKeyPath"`
}
const DefaultMaxParallelCreation = 10
type TrayType struct {
Name string `yaml:"name" validate:"required"`
Provider string `yaml:"provider" validate:"required"`
RunnerGroupId int64 `yaml:"runnerGroupId" validate:"required"`
Shutdown bool `yaml:"shutdown"`
GitHubOrg string `yaml:"githubOrg" validate:"required"`
MaxTrays int `yaml:"maxTrays"`
MaxParallelCreation int `yaml:"maxParallelCreation"`
Config TrayConfig `yaml:"config"`
ExtraMetadata TrayExtraMetadata
}
type TrayExtraMetadata map[string]string
type ProviderConfig map[string]string
func (p ProviderConfig) Get(key string) string {
return p[strings.ToLower(key)]
}