etcd_config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2018 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rpcpb
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "github.com/coreos/etcd/embed"
  20. "github.com/coreos/etcd/pkg/transport"
  21. "github.com/coreos/etcd/pkg/types"
  22. )
  23. var etcdFields = []string{
  24. "Name",
  25. "DataDir",
  26. "WALDir",
  27. "HeartbeatIntervalMs",
  28. "ElectionTimeoutMs",
  29. "ListenClientURLs",
  30. "AdvertiseClientURLs",
  31. "ClientAutoTLS",
  32. "ClientCertAuth",
  33. "ClientCertFile",
  34. "ClientKeyFile",
  35. "ClientTrustedCAFile",
  36. "ListenPeerURLs",
  37. "AdvertisePeerURLs",
  38. "PeerAutoTLS",
  39. "PeerClientCertAuth",
  40. "PeerCertFile",
  41. "PeerKeyFile",
  42. "PeerTrustedCAFile",
  43. "InitialCluster",
  44. "InitialClusterState",
  45. "InitialClusterToken",
  46. "SnapshotCount",
  47. "QuotaBackendBytes",
  48. "PreVote",
  49. "InitialCorruptCheck",
  50. "Logger",
  51. "LogOutputs",
  52. "Debug",
  53. }
  54. // Flags returns etcd flags in string slice.
  55. func (e *Etcd) Flags() (fs []string) {
  56. tp := reflect.TypeOf(*e)
  57. vo := reflect.ValueOf(*e)
  58. for _, name := range etcdFields {
  59. field, ok := tp.FieldByName(name)
  60. if !ok {
  61. panic(fmt.Errorf("field %q not found", name))
  62. }
  63. fv := reflect.Indirect(vo).FieldByName(name)
  64. var sv string
  65. switch fv.Type().Kind() {
  66. case reflect.String:
  67. sv = fv.String()
  68. case reflect.Slice:
  69. n := fv.Len()
  70. sl := make([]string, n)
  71. for i := 0; i < n; i++ {
  72. sl[i] = fv.Index(i).String()
  73. }
  74. sv = strings.Join(sl, ",")
  75. case reflect.Int64:
  76. sv = fmt.Sprintf("%d", fv.Int())
  77. case reflect.Bool:
  78. sv = fmt.Sprintf("%v", fv.Bool())
  79. default:
  80. panic(fmt.Errorf("field %q (%v) cannot be parsed", name, fv.Type().Kind()))
  81. }
  82. fname := field.Tag.Get("yaml")
  83. // TODO: remove this
  84. if fname == "initial-corrupt-check" {
  85. fname = "experimental-" + fname
  86. }
  87. if sv != "" {
  88. fs = append(fs, fmt.Sprintf("--%s=%s", fname, sv))
  89. }
  90. }
  91. return fs
  92. }
  93. // EmbedConfig returns etcd embed.Config.
  94. func (e *Etcd) EmbedConfig() (cfg *embed.Config, err error) {
  95. var lcURLs types.URLs
  96. lcURLs, err = types.NewURLs(e.ListenClientURLs)
  97. if err != nil {
  98. return nil, err
  99. }
  100. var acURLs types.URLs
  101. acURLs, err = types.NewURLs(e.AdvertiseClientURLs)
  102. if err != nil {
  103. return nil, err
  104. }
  105. var lpURLs types.URLs
  106. lpURLs, err = types.NewURLs(e.ListenPeerURLs)
  107. if err != nil {
  108. return nil, err
  109. }
  110. var apURLs types.URLs
  111. apURLs, err = types.NewURLs(e.AdvertisePeerURLs)
  112. if err != nil {
  113. return nil, err
  114. }
  115. cfg = embed.NewConfig()
  116. cfg.Name = e.Name
  117. cfg.Dir = e.DataDir
  118. cfg.WalDir = e.WALDir
  119. cfg.TickMs = uint(e.HeartbeatIntervalMs)
  120. cfg.ElectionMs = uint(e.ElectionTimeoutMs)
  121. cfg.LCUrls = lcURLs
  122. cfg.ACUrls = acURLs
  123. cfg.ClientAutoTLS = e.ClientAutoTLS
  124. cfg.ClientTLSInfo = transport.TLSInfo{
  125. ClientCertAuth: e.ClientCertAuth,
  126. CertFile: e.ClientCertFile,
  127. KeyFile: e.ClientKeyFile,
  128. TrustedCAFile: e.ClientTrustedCAFile,
  129. }
  130. cfg.LPUrls = lpURLs
  131. cfg.APUrls = apURLs
  132. cfg.PeerAutoTLS = e.PeerAutoTLS
  133. cfg.PeerTLSInfo = transport.TLSInfo{
  134. ClientCertAuth: e.PeerClientCertAuth,
  135. CertFile: e.PeerCertFile,
  136. KeyFile: e.PeerKeyFile,
  137. TrustedCAFile: e.PeerTrustedCAFile,
  138. }
  139. cfg.InitialCluster = e.InitialCluster
  140. cfg.ClusterState = e.InitialClusterState
  141. cfg.InitialClusterToken = e.InitialClusterToken
  142. cfg.SnapCount = uint64(e.SnapshotCount)
  143. cfg.QuotaBackendBytes = e.QuotaBackendBytes
  144. cfg.PreVote = e.PreVote
  145. cfg.ExperimentalInitialCorruptCheck = e.InitialCorruptCheck
  146. cfg.Logger = e.Logger
  147. cfg.LogOutputs = e.LogOutputs
  148. cfg.Debug = e.Debug
  149. return cfg, nil
  150. }