forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetcd_config.go
174 lines (150 loc) · 3.9 KB
/
etcd_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
// Copyright 2018 The etcd 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 rpcpb
import (
"fmt"
"reflect"
"strings"
"github.com/coreos/etcd/embed"
"github.com/coreos/etcd/pkg/transport"
"github.com/coreos/etcd/pkg/types"
)
var etcdFields = []string{
"Name",
"DataDir",
"WALDir",
"HeartbeatIntervalMs",
"ElectionTimeoutMs",
"ListenClientURLs",
"AdvertiseClientURLs",
"ClientAutoTLS",
"ClientCertAuth",
"ClientCertFile",
"ClientKeyFile",
"ClientTrustedCAFile",
"ListenPeerURLs",
"AdvertisePeerURLs",
"PeerAutoTLS",
"PeerClientCertAuth",
"PeerCertFile",
"PeerKeyFile",
"PeerTrustedCAFile",
"InitialCluster",
"InitialClusterState",
"InitialClusterToken",
"SnapshotCount",
"QuotaBackendBytes",
"PreVote",
"InitialCorruptCheck",
"Logger",
"LogOutput",
"Debug",
}
// Flags returns etcd flags in string slice.
func (e *Etcd) Flags() (fs []string) {
tp := reflect.TypeOf(*e)
vo := reflect.ValueOf(*e)
for _, name := range etcdFields {
field, ok := tp.FieldByName(name)
if !ok {
panic(fmt.Errorf("field %q not found", name))
}
fv := reflect.Indirect(vo).FieldByName(name)
var sv string
switch fv.Type().Kind() {
case reflect.String:
sv = fv.String()
case reflect.Slice:
n := fv.Len()
sl := make([]string, n)
for i := 0; i < n; i++ {
sl[i] = fv.Index(i).String()
}
sv = strings.Join(sl, ",")
case reflect.Int64:
sv = fmt.Sprintf("%d", fv.Int())
case reflect.Bool:
sv = fmt.Sprintf("%v", fv.Bool())
default:
panic(fmt.Errorf("field %q (%v) cannot be parsed", name, fv.Type().Kind()))
}
fname := field.Tag.Get("yaml")
// TODO: remove this
if fname == "initial-corrupt-check" {
fname = "experimental-" + fname
}
if sv != "" {
fs = append(fs, fmt.Sprintf("--%s=%s", fname, sv))
}
}
return fs
}
// EmbedConfig returns etcd embed.Config.
func (e *Etcd) EmbedConfig() (cfg *embed.Config, err error) {
var lcURLs types.URLs
lcURLs, err = types.NewURLs(e.ListenClientURLs)
if err != nil {
return nil, err
}
var acURLs types.URLs
acURLs, err = types.NewURLs(e.AdvertiseClientURLs)
if err != nil {
return nil, err
}
var lpURLs types.URLs
lpURLs, err = types.NewURLs(e.ListenPeerURLs)
if err != nil {
return nil, err
}
var apURLs types.URLs
apURLs, err = types.NewURLs(e.AdvertisePeerURLs)
if err != nil {
return nil, err
}
cfg = embed.NewConfig()
cfg.Name = e.Name
cfg.Dir = e.DataDir
cfg.WalDir = e.WALDir
cfg.TickMs = uint(e.HeartbeatIntervalMs)
cfg.ElectionMs = uint(e.ElectionTimeoutMs)
cfg.LCUrls = lcURLs
cfg.ACUrls = acURLs
cfg.ClientAutoTLS = e.ClientAutoTLS
cfg.ClientTLSInfo = transport.TLSInfo{
ClientCertAuth: e.ClientCertAuth,
CertFile: e.ClientCertFile,
KeyFile: e.ClientKeyFile,
TrustedCAFile: e.ClientTrustedCAFile,
}
cfg.LPUrls = lpURLs
cfg.APUrls = apURLs
cfg.PeerAutoTLS = e.PeerAutoTLS
cfg.PeerTLSInfo = transport.TLSInfo{
ClientCertAuth: e.PeerClientCertAuth,
CertFile: e.PeerCertFile,
KeyFile: e.PeerKeyFile,
TrustedCAFile: e.PeerTrustedCAFile,
}
cfg.InitialCluster = e.InitialCluster
cfg.ClusterState = e.InitialClusterState
cfg.InitialClusterToken = e.InitialClusterToken
cfg.SnapCount = uint64(e.SnapshotCount)
cfg.QuotaBackendBytes = e.QuotaBackendBytes
cfg.PreVote = e.PreVote
cfg.ExperimentalInitialCorruptCheck = e.InitialCorruptCheck
cfg.Logger = e.Logger
cfg.LogOutput = e.LogOutput
cfg.Debug = e.Debug
return cfg, nil
}