-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
316 lines (267 loc) · 8.97 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
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package config provides functions to handle the configurations of job service.
package config
import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"strconv"
"strings"
"github.com/goharbor/harbor/src/jobservice/common/utils"
"gopkg.in/yaml.v2"
)
const (
jobServiceProtocol = "JOB_SERVICE_PROTOCOL"
jobServicePort = "JOB_SERVICE_PORT"
jobServiceHTTPCert = "JOB_SERVICE_HTTPS_CERT"
jobServiceHTTPKey = "JOB_SERVICE_HTTPS_KEY"
jobServiceWorkerPoolBackend = "JOB_SERVICE_POOL_BACKEND"
jobServiceWorkers = "JOB_SERVICE_POOL_WORKERS"
jobServiceRedisURL = "JOB_SERVICE_POOL_REDIS_URL"
jobServiceRedisNamespace = "JOB_SERVICE_POOL_REDIS_NAMESPACE"
jobServiceAuthSecret = "JOBSERVICE_SECRET"
// JobServiceProtocolHTTPS points to the 'https' protocol
JobServiceProtocolHTTPS = "https"
// JobServiceProtocolHTTP points to the 'http' protocol
JobServiceProtocolHTTP = "http"
// JobServicePoolBackendRedis represents redis backend
JobServicePoolBackendRedis = "redis"
// secret of UI
uiAuthSecret = "CORE_SECRET"
// redis protocol schema
redisSchema = "redis://"
)
// DefaultConfig is the default configuration reference
var DefaultConfig = &Configuration{}
// Configuration loads and keeps the related configuration items of job service.
type Configuration struct {
// Protocol server listening on: https/http
Protocol string `yaml:"protocol"`
// Server listening port
Port uint `yaml:"port"`
// Additional config when using https
HTTPSConfig *HTTPSConfig `yaml:"https_config,omitempty"`
// Configurations of worker worker
PoolConfig *PoolConfig `yaml:"worker_pool,omitempty"`
// Job logger configurations
JobLoggerConfigs []*LoggerConfig `yaml:"job_loggers,omitempty"`
// Logger configurations
LoggerConfigs []*LoggerConfig `yaml:"loggers,omitempty"`
}
// HTTPSConfig keeps additional configurations when using https protocol
type HTTPSConfig struct {
Cert string `yaml:"cert"`
Key string `yaml:"key"`
}
// RedisPoolConfig keeps redis worker info.
type RedisPoolConfig struct {
RedisURL string `yaml:"redis_url"`
Namespace string `yaml:"namespace"`
}
// PoolConfig keeps worker worker configurations.
type PoolConfig struct {
// Worker concurrency
WorkerCount uint `yaml:"workers"`
Backend string `yaml:"backend"`
RedisPoolCfg *RedisPoolConfig `yaml:"redis_pool,omitempty"`
}
// CustomizedSettings keeps the customized settings of logger
type CustomizedSettings map[string]interface{}
// LogSweeperConfig keeps settings of log sweeper
type LogSweeperConfig struct {
Duration int `yaml:"duration"`
Settings CustomizedSettings `yaml:"settings"`
}
// LoggerConfig keeps logger basic configurations.
type LoggerConfig struct {
Name string `yaml:"name"`
Level string `yaml:"level"`
Settings CustomizedSettings `yaml:"settings"`
Sweeper *LogSweeperConfig `yaml:"sweeper"`
}
// Load the configuration options from the specified yaml file.
// If the yaml file is specified and existing, load configurations from yaml file first;
// If detecting env variables is specified, load configurations from env variables;
// Please pay attentions, the detected env variable will override the same configuration item loading from file.
//
// yamlFilePath string: The path config yaml file
// readEnv bool : Whether detect the environment variables or not
func (c *Configuration) Load(yamlFilePath string, detectEnv bool) error {
if !utils.IsEmptyStr(yamlFilePath) {
// Try to load from file first
data, err := ioutil.ReadFile(yamlFilePath)
if err != nil {
return err
}
if err = yaml.Unmarshal(data, c); err != nil {
return err
}
}
if detectEnv {
// Load from env variables
c.loadEnvs()
}
// translate redis url if needed
if c.PoolConfig != nil && c.PoolConfig.RedisPoolCfg != nil {
redisAddress := c.PoolConfig.RedisPoolCfg.RedisURL
if !utils.IsEmptyStr(redisAddress) {
if _, err := url.Parse(redisAddress); err != nil {
if redisURL, ok := utils.TranslateRedisAddress(redisAddress); ok {
c.PoolConfig.RedisPoolCfg.RedisURL = redisURL
}
} else {
if !strings.HasPrefix(redisAddress, redisSchema) {
c.PoolConfig.RedisPoolCfg.RedisURL = fmt.Sprintf("%s%s", redisSchema, redisAddress)
}
}
}
}
// Validate settings
return c.validate()
}
// GetAuthSecret get the auth secret from the env
func GetAuthSecret() string {
return utils.ReadEnv(jobServiceAuthSecret)
}
// GetUIAuthSecret get the auth secret of UI side
func GetUIAuthSecret() string {
return utils.ReadEnv(uiAuthSecret)
}
// Load env variables
func (c *Configuration) loadEnvs() {
prot := utils.ReadEnv(jobServiceProtocol)
if !utils.IsEmptyStr(prot) {
c.Protocol = prot
}
p := utils.ReadEnv(jobServicePort)
if !utils.IsEmptyStr(p) {
if po, err := strconv.Atoi(p); err == nil {
c.Port = uint(po)
}
}
// Only when protocol is https
if c.Protocol == JobServiceProtocolHTTPS {
cert := utils.ReadEnv(jobServiceHTTPCert)
if !utils.IsEmptyStr(cert) {
if c.HTTPSConfig != nil {
c.HTTPSConfig.Cert = cert
} else {
c.HTTPSConfig = &HTTPSConfig{
Cert: cert,
}
}
}
certKey := utils.ReadEnv(jobServiceHTTPKey)
if !utils.IsEmptyStr(certKey) {
if c.HTTPSConfig != nil {
c.HTTPSConfig.Key = certKey
} else {
c.HTTPSConfig = &HTTPSConfig{
Key: certKey,
}
}
}
}
backend := utils.ReadEnv(jobServiceWorkerPoolBackend)
if !utils.IsEmptyStr(backend) {
if c.PoolConfig == nil {
c.PoolConfig = &PoolConfig{}
}
c.PoolConfig.Backend = backend
}
workers := utils.ReadEnv(jobServiceWorkers)
if !utils.IsEmptyStr(workers) {
if count, err := strconv.Atoi(workers); err == nil {
if c.PoolConfig == nil {
c.PoolConfig = &PoolConfig{}
}
c.PoolConfig.WorkerCount = uint(count)
}
}
if c.PoolConfig != nil && c.PoolConfig.Backend == JobServicePoolBackendRedis {
redisURL := utils.ReadEnv(jobServiceRedisURL)
if !utils.IsEmptyStr(redisURL) {
if c.PoolConfig.RedisPoolCfg == nil {
c.PoolConfig.RedisPoolCfg = &RedisPoolConfig{}
}
c.PoolConfig.RedisPoolCfg.RedisURL = redisURL
}
rn := utils.ReadEnv(jobServiceRedisNamespace)
if !utils.IsEmptyStr(rn) {
if c.PoolConfig.RedisPoolCfg == nil {
c.PoolConfig.RedisPoolCfg = &RedisPoolConfig{}
}
c.PoolConfig.RedisPoolCfg.Namespace = rn
}
}
}
// Check if the configurations are valid settings.
func (c *Configuration) validate() error {
if c.Protocol != JobServiceProtocolHTTPS &&
c.Protocol != JobServiceProtocolHTTP {
return fmt.Errorf("protocol should be %s or %s, but current setting is %s",
JobServiceProtocolHTTP,
JobServiceProtocolHTTPS,
c.Protocol)
}
if !utils.IsValidPort(c.Port) {
return fmt.Errorf("port number should be a none zero integer and less or equal 65535, but current is %d", c.Port)
}
if c.Protocol == JobServiceProtocolHTTPS {
if c.HTTPSConfig == nil {
return fmt.Errorf("certificate must be configured if serve with protocol %s", c.Protocol)
}
if utils.IsEmptyStr(c.HTTPSConfig.Cert) ||
!utils.FileExists(c.HTTPSConfig.Cert) ||
utils.IsEmptyStr(c.HTTPSConfig.Key) ||
!utils.FileExists(c.HTTPSConfig.Key) {
return fmt.Errorf("certificate for protocol %s is not correctly configured", c.Protocol)
}
}
if c.PoolConfig == nil {
return errors.New("no worker worker is configured")
}
if c.PoolConfig.Backend != JobServicePoolBackendRedis {
return fmt.Errorf("worker worker backend %s does not support", c.PoolConfig.Backend)
}
// When backend is redis
if c.PoolConfig.Backend == JobServicePoolBackendRedis {
if c.PoolConfig.RedisPoolCfg == nil {
return fmt.Errorf("redis worker must be configured when backend is set to '%s'", c.PoolConfig.Backend)
}
if utils.IsEmptyStr(c.PoolConfig.RedisPoolCfg.RedisURL) {
return errors.New("URL of redis worker is empty")
}
if !strings.HasPrefix(c.PoolConfig.RedisPoolCfg.RedisURL, redisSchema) {
return errors.New("invalid redis URL")
}
if _, err := url.Parse(c.PoolConfig.RedisPoolCfg.RedisURL); err != nil {
return fmt.Errorf("invalid redis URL: %s", err.Error())
}
if utils.IsEmptyStr(c.PoolConfig.RedisPoolCfg.Namespace) {
return errors.New("namespace of redis worker is required")
}
}
// Job service loggers
if len(c.LoggerConfigs) == 0 {
return errors.New("missing logger config of job service")
}
// Job loggers
if len(c.JobLoggerConfigs) == 0 {
return errors.New("missing logger config of job")
}
return nil // valid
}