forked from easegress-io/easegress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
447 lines (389 loc) · 16.3 KB
/
option.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* 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
*
* https://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 option
import (
"fmt"
"net"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/mitchellh/mapstructure"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/megaease/easegress/pkg/common"
"github.com/megaease/easegress/pkg/util/codectool"
"github.com/megaease/easegress/pkg/version"
)
// ClusterOptions defines the cluster members.
type ClusterOptions struct {
// Primary members define following URLs to form a cluster.
ListenPeerURLs []string `yaml:"listen-peer-urls"`
ListenClientURLs []string `yaml:"listen-client-urls"`
AdvertiseClientURLs []string `yaml:"advertise-client-urls"`
InitialAdvertisePeerURLs []string `yaml:"initial-advertise-peer-urls"`
InitialCluster map[string]string `yaml:"initial-cluster"`
StateFlag string `yaml:"state-flag"`
// Secondary members define URLs to connect to cluster formed by primary members.
PrimaryListenPeerURLs []string `yaml:"primary-listen-peer-urls"`
MaxCallSendMsgSize int `yaml:"max-call-send-msg-size"`
}
// Options is the start-up options.
type Options struct {
flags *pflag.FlagSet
viper *viper.Viper
yamlStr string
// Flags from command line only.
ShowVersion bool `yaml:"-"`
ShowHelp bool `yaml:"-"`
ShowConfig bool `yaml:"-"`
ConfigFile string `yaml:"-"`
ForceNewCluster bool `yaml:"-"`
SignalUpgrade bool `yaml:"-"`
// If a config file is specified, below command line flags will be ignored.
// meta
Name string `yaml:"name" env:"EG_NAME"`
Labels map[string]string `yaml:"labels" env:"EG_LABELS"`
APIAddr string `yaml:"api-addr"`
TLS bool `yaml:"tls"`
CertFile string `yaml:"cert-file"`
KeyFile string `yaml:"key-file"`
Debug bool `yaml:"debug"`
DisableAccessLog bool `yaml:"disable-access-log"`
InitialObjectConfigFiles []string `yaml:"initial-object-config-files"`
ObjectsDumpInterval string `yaml:"objects-dump-interval"`
// cluster options
UseStandaloneEtcd bool `yaml:"use-standalone-etcd"`
ClusterName string `yaml:"cluster-name"`
ClusterRole string `yaml:"cluster-role"`
ClusterRequestTimeout string `yaml:"cluster-request-timeout"`
Cluster ClusterOptions `yaml:"cluster"`
// Path.
HomeDir string `yaml:"home-dir"`
DataDir string `yaml:"data-dir"`
WALDir string `yaml:"wal-dir"`
LogDir string `yaml:"log-dir"`
MemberDir string `yaml:"member-dir"`
// Profile.
CPUProfileFile string `yaml:"cpu-profile-file"`
MemoryProfileFile string `yaml:"memory-profile-file"`
// Status
StatusUpdateMaxBatchSize int `yaml:"status-update-max-batch-size"`
// Prepare the items below in advance.
AbsHomeDir string `yaml:"-"`
AbsDataDir string `yaml:"-"`
AbsWALDir string `yaml:"-"`
AbsLogDir string `yaml:"-"`
AbsMemberDir string `yaml:"-"`
}
// addClusterVars introduces cluster arguments.
func addClusterVars(opt *Options) {
opt.flags.StringVar(&opt.ClusterName, "cluster-name", "eg-cluster-default-name", "Human-readable name for the new cluster, ignored while joining an existed cluster.")
opt.flags.StringVar(&opt.ClusterRole, "cluster-role", "primary", "Cluster role for this member (primary, secondary).")
opt.flags.StringVar(&opt.ClusterRequestTimeout, "cluster-request-timeout", "10s", "Timeout to handle request in the cluster.")
// Cluster connection configuration
opt.flags.StringSliceVar(&opt.Cluster.ListenClientURLs, "listen-client-urls", []string{"https://localhost:2379"}, "List of URLs to listen on for cluster client traffic.")
opt.flags.StringSliceVar(&opt.Cluster.ListenPeerURLs, "listen-peer-urls", []string{"https://localhost:2380"}, "List of URLs to listen on for cluster peer traffic.")
opt.flags.StringSliceVar(&opt.Cluster.AdvertiseClientURLs, "advertise-client-urls", []string{"https://localhost:2379"}, "List of this member's client URLs to advertise to the rest of the cluster.")
opt.flags.StringSliceVar(&opt.Cluster.InitialAdvertisePeerURLs, "initial-advertise-peer-urls", []string{"https://localhost:2380"}, "List of this member's peer URLs to advertise to the rest of the cluster.")
opt.flags.StringToStringVarP(&opt.Cluster.InitialCluster, "initial-cluster", "", nil, "List of (member name, URL) pairs that will form the cluster. E.g. primary-1=https://localhost:2380.")
opt.flags.StringVar(&opt.Cluster.StateFlag, "state-flag", "new", "Cluster state (new, existing)")
opt.flags.StringSliceVar(&opt.Cluster.PrimaryListenPeerURLs, "primary-listen-peer-urls", []string{"https://localhost:2380"}, "List of peer URLs of primary members. Define this only, when cluster-role is secondary.")
opt.flags.IntVar(&opt.Cluster.MaxCallSendMsgSize, "max-call-send-msg-size", 10*1024*1024, "Maximum size in bytes for cluster synchronization messages.")
}
// New creates a default Options.
func New() *Options {
opt := &Options{
flags: pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError),
viper: viper.New(),
}
opt.flags.BoolVarP(&opt.ShowVersion, "version", "v", false, "Print the version and exit.")
opt.flags.BoolVarP(&opt.ShowHelp, "help", "h", false, "Print the helper message and exit.")
opt.flags.BoolVarP(&opt.ShowConfig, "print-config", "c", false, "Print the configuration.")
opt.flags.StringVarP(&opt.ConfigFile, "config-file", "f", "", "Load server configuration from a file(yaml format), other command line flags will be ignored if specified.")
opt.flags.BoolVar(&opt.ForceNewCluster, "force-new-cluster", false, "Force to create a new one-member cluster.")
opt.flags.BoolVar(&opt.SignalUpgrade, "signal-upgrade", false, "Send an upgrade signal to the server based on the local pid file, then exit. The original server will start a graceful upgrade after signal received.")
opt.flags.StringVar(&opt.Name, "name", "eg-default-name", "Human-readable name for this member.")
opt.flags.StringToStringVar(&opt.Labels, "labels", nil, "The labels for the instance of Easegress.")
opt.flags.BoolVar(&opt.UseStandaloneEtcd, "use-standalone-etcd", false, "Use standalone etcd instead of embedded .")
addClusterVars(opt)
opt.flags.StringVar(&opt.APIAddr, "api-addr", "localhost:2381", "Address([host]:port) to listen on for administration traffic.")
opt.flags.BoolVar(&opt.TLS, "tls", false, "Flag to use secure transport protocol(https).")
opt.flags.StringVar(&opt.CertFile, "cert-file", "", "Flag to set the certificate file for https.")
opt.flags.StringVar(&opt.KeyFile, "key-file", "", "Flag to set the private key file for https.")
opt.flags.BoolVar(&opt.Debug, "debug", false, "Flag to set lowest log level from INFO downgrade DEBUG.")
opt.flags.StringSliceVar(&opt.InitialObjectConfigFiles, "initial-object-config-files", nil, "List of configuration files for initial objects, these objects will be created at startup if not already exist.")
opt.flags.StringVar(&opt.ObjectsDumpInterval, "objects-dump-interval", "", "The time interval to dump running objects config, for example: 30m")
opt.flags.StringVar(&opt.HomeDir, "home-dir", "./", "Path to the home directory.")
opt.flags.StringVar(&opt.DataDir, "data-dir", "data", "Path to the data directory.")
opt.flags.StringVar(&opt.WALDir, "wal-dir", "", "Path to the WAL directory.")
opt.flags.StringVar(&opt.LogDir, "log-dir", "", "Path to the log directory.")
opt.flags.StringVar(&opt.MemberDir, "member-dir", "member", "Path to the member directory.")
opt.flags.StringVar(&opt.CPUProfileFile, "cpu-profile-file", "", "Path to the CPU profile file.")
opt.flags.StringVar(&opt.MemoryProfileFile, "memory-profile-file", "", "Path to the memory profile file.")
opt.flags.IntVar(&opt.StatusUpdateMaxBatchSize, "status-update-max-batch-size", 20, "Number of object statuses to update at maximum in one transaction.")
opt.viper.BindPFlags(opt.flags)
return opt
}
// YAML returns yaml string of option, need to be called after calling Parse.
func (opt *Options) YAML() string {
return opt.yamlStr
}
// UseInitialCluster returns true if the cluster.initial-cluster is defined. If it is, the ClusterJoinUrls is ignored.
func (opt *Options) UseInitialCluster() bool {
return len(opt.Cluster.InitialCluster) > 0
}
// renameLegacyClusterRoles renames legacy writer/reader --> primary/secondary and raises warning.
func (opt *Options) renameLegacyClusterRoles() {
warning := "Cluster roles writer/reader are deprecated. \n" +
"Renamed cluster role '%s' to '%s'. Please use primary/secondary instead. \n"
fmtLogger := fmt.Printf // Importing logger here is an import cycle, so use fmt instead.
if opt.ClusterRole == "writer" {
opt.ClusterRole = "primary"
fmtLogger(warning, "writer", "primary")
}
if opt.ClusterRole == "reader" {
opt.ClusterRole = "secondary"
fmtLogger(warning, "reader", "secondary")
}
}
// Parse parses all arguments, returns normal message without error if --help/--version set.
func (opt *Options) Parse() (string, error) {
err := opt.flags.Parse(os.Args[1:])
if err != nil {
return "", err
}
if opt.ShowVersion {
return version.Short, nil
}
if opt.ShowHelp {
return opt.flags.FlagUsages(), nil
}
opt.viper.AutomaticEnv()
opt.viper.SetEnvPrefix("EG")
opt.viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
if opt.ConfigFile != "" {
opt.viper.SetConfigFile(opt.ConfigFile)
opt.viper.SetConfigType("yaml")
err := opt.viper.ReadInConfig()
if err != nil {
return "", fmt.Errorf("read config file %s failed: %v",
opt.ConfigFile, err)
}
}
// NOTE: Workaround because viper does not treat env vars the same as other config.
// Reference: https://github.com/spf13/viper/issues/188#issuecomment-399518663
for _, key := range opt.viper.AllKeys() {
val := opt.viper.Get(key)
// NOTE: We need to handle map[string]string
// Reference: https://github.com/spf13/viper/issues/911
if key == "labels" {
val = opt.viper.GetStringMapString(key)
}
opt.viper.Set(key, val)
}
err = opt.viper.Unmarshal(opt, func(c *mapstructure.DecoderConfig) {
c.TagName = "yaml"
})
if err != nil {
return "", fmt.Errorf("yaml file unmarshal failed, please make sure you provide valid yaml file, %v", err)
}
opt.renameLegacyClusterRoles()
if opt.UseStandaloneEtcd {
opt.ClusterRole = "secondary" // when using external standalone etcd, the cluster role cannot be "primary"
}
if opt.ClusterRole == "primary" && len(opt.Cluster.InitialCluster) == 0 {
opt.Cluster.InitialCluster = map[string]string{opt.Name: opt.Cluster.InitialAdvertisePeerURLs[0]}
}
err = opt.validate()
if err != nil {
return "", err
}
err = opt.prepare()
if err != nil {
return "", err
}
buff, err := codectool.MarshalYAML(opt)
if err != nil {
return "", fmt.Errorf("marshal config to yaml failed: %v", err)
}
opt.yamlStr = string(buff)
if opt.ShowConfig {
fmt.Printf("%s", opt.yamlStr)
}
return "", nil
}
// ParseURLs parses list of strings to url.URL objects.
func ParseURLs(urlStrings []string) ([]url.URL, error) {
urls := make([]url.URL, len(urlStrings))
for i, urlString := range urlStrings {
parsedURL, err := url.Parse(urlString)
if err != nil {
return nil, fmt.Errorf(" %s: %v", urlString, err)
}
urls[i] = *parsedURL
}
return urls, nil
}
func (opt *Options) validate() error {
if opt.ClusterName == "" {
return fmt.Errorf("empty cluster-name")
} else if err := common.ValidateName(opt.ClusterName); err != nil {
return err
}
switch opt.ClusterRole {
case "secondary":
if opt.ForceNewCluster {
return fmt.Errorf("secondary got force-new-cluster")
}
if len(opt.Cluster.PrimaryListenPeerURLs) == 0 {
return fmt.Errorf("secondary got empty cluster.primary-listen-peer-urls")
}
case "primary":
argumentsToValidate := map[string][]string{
"listen-client-urls": opt.Cluster.ListenClientURLs,
"listen-peer-urls": opt.Cluster.ListenPeerURLs,
"advertise-client-urls": opt.Cluster.AdvertiseClientURLs,
"initial-advertise-peer-urls": opt.Cluster.InitialAdvertisePeerURLs,
}
initialClusterUrls := make([]string, 0, len(opt.Cluster.InitialCluster))
for _, value := range opt.Cluster.InitialCluster {
initialClusterUrls = append(initialClusterUrls, value)
}
if _, err := ParseURLs(initialClusterUrls); err != nil {
return fmt.Errorf("invalid initial-cluster: %v", err)
}
for arg, urls := range argumentsToValidate {
if len(urls) == 0 {
return fmt.Errorf("empty %s", arg)
}
if _, err := ParseURLs(urls); err != nil {
return fmt.Errorf("invalid %s: %v", arg, err)
}
}
default:
return fmt.Errorf("invalid cluster-role: supported roles are primary/secondary")
}
_, err := time.ParseDuration(opt.ClusterRequestTimeout)
if err != nil {
return fmt.Errorf("invalid cluster-request-timeout: %v", err)
}
_, _, err = net.SplitHostPort(opt.APIAddr)
if err != nil {
return fmt.Errorf("invalid api-addr: %v", err)
}
if err != nil {
return fmt.Errorf("invalid api-url: %v", err)
}
// dirs
if opt.HomeDir == "" {
return fmt.Errorf("empty home-dir")
}
if opt.DataDir == "" {
return fmt.Errorf("empty data-dir")
}
if !opt.UseInitialCluster() && opt.MemberDir == "" {
return fmt.Errorf("empty member-dir")
}
if opt.TLS && (opt.CertFile == "" || opt.KeyFile == "") {
return fmt.Errorf("empty cert file or key file")
}
// profile: nothing to validate
// meta
if opt.Name == "" {
name, err := generateMemberName(opt.APIAddr)
if err != nil {
return err
}
opt.Name = name
}
return common.ValidateName(opt.Name)
}
func (opt *Options) prepare() error {
abs, isAbs, clean, join := filepath.Abs, filepath.IsAbs, filepath.Clean, filepath.Join
if isAbs(opt.HomeDir) {
opt.AbsHomeDir = clean(opt.HomeDir)
} else {
absHomeDir, err := abs(opt.HomeDir)
if err != nil {
return err
}
opt.AbsHomeDir = absHomeDir
}
type dirItem struct {
dir string
absDir *string
}
table := []dirItem{
{dir: opt.DataDir, absDir: &opt.AbsDataDir},
{dir: opt.WALDir, absDir: &opt.AbsWALDir},
{dir: opt.MemberDir, absDir: &opt.AbsMemberDir},
}
if opt.LogDir != "" {
table = append(table, dirItem{dir: opt.LogDir, absDir: &opt.AbsLogDir})
}
for _, di := range table {
if di.dir == "" {
continue
}
if filepath.IsAbs(di.dir) {
*di.absDir = clean(di.dir)
} else {
*di.absDir = clean(join(opt.AbsHomeDir, di.dir))
}
}
return nil
}
// InitialClusterToString returns initial clusters string representation.
func (opt *Options) InitialClusterToString() string {
ss := make([]string, 0)
for name, peerURL := range opt.Cluster.InitialCluster {
ss = append(ss, fmt.Sprintf("%s=%s", name, peerURL))
}
return strings.Join(ss, ",")
}
// GetPeerURLs returns URLs listed in cluster.initial-cluster for primary (a.k.a writer) and
// for secondary (a.k.a reader) the ones listed in cluster.primary-listen-peer-url.
func (opt *Options) GetPeerURLs() []string {
if opt.ClusterRole == "secondary" {
return opt.Cluster.PrimaryListenPeerURLs
}
peerURLs := make([]string, 0)
for _, peerURL := range opt.Cluster.InitialCluster {
peerURLs = append(peerURLs, peerURL)
}
return peerURLs
}
// GetFirstAdvertiseClientURL returns the first advertised client url.
func (opt *Options) GetFirstAdvertiseClientURL() (string, error) {
if len(opt.Cluster.AdvertiseClientURLs) == 0 {
return "", fmt.Errorf("cluster.advertise-client-URLs is empty")
}
return opt.Cluster.AdvertiseClientURLs[0], nil
}
func generateMemberName(apiAddr string) (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}
memberName := hostname + "-" + apiAddr
memberName = strings.Replace(memberName, ",", "-", -1)
memberName = strings.Replace(memberName, ":", "-", -1)
memberName = strings.Replace(memberName, "=", "-", -1)
return memberName, nil
}