etcd_config.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. )
  20. var etcdFields = []string{
  21. "Name",
  22. "DataDir",
  23. "WALDir",
  24. "HeartbeatIntervalMs",
  25. "ElectionTimeoutMs",
  26. "ListenClientURLs",
  27. "AdvertiseClientURLs",
  28. "ClientAutoTLS",
  29. "ClientCertAuth",
  30. "ClientCertFile",
  31. "ClientKeyFile",
  32. "ClientTrustedCAFile",
  33. "ListenPeerURLs",
  34. "AdvertisePeerURLs",
  35. "PeerAutoTLS",
  36. "PeerClientCertAuth",
  37. "PeerCertFile",
  38. "PeerKeyFile",
  39. "PeerTrustedCAFile",
  40. "InitialCluster",
  41. "InitialClusterState",
  42. "InitialClusterToken",
  43. "SnapshotCount",
  44. "QuotaBackendBytes",
  45. // "PreVote",
  46. // "InitialCorruptCheck",
  47. }
  48. // Flags returns etcd flags in string slice.
  49. func (cfg *Etcd) Flags() (fs []string) {
  50. tp := reflect.TypeOf(*cfg)
  51. vo := reflect.ValueOf(*cfg)
  52. for _, name := range etcdFields {
  53. field, ok := tp.FieldByName(name)
  54. if !ok {
  55. panic(fmt.Errorf("field %q not found", name))
  56. }
  57. fv := reflect.Indirect(vo).FieldByName(name)
  58. var sv string
  59. switch fv.Type().Kind() {
  60. case reflect.String:
  61. sv = fv.String()
  62. case reflect.Slice:
  63. n := fv.Len()
  64. sl := make([]string, n)
  65. for i := 0; i < n; i++ {
  66. sl[i] = fv.Index(i).String()
  67. }
  68. sv = strings.Join(sl, ",")
  69. case reflect.Int64:
  70. sv = fmt.Sprintf("%d", fv.Int())
  71. case reflect.Bool:
  72. sv = fmt.Sprintf("%v", fv.Bool())
  73. default:
  74. panic(fmt.Errorf("field %q (%v) cannot be parsed", name, fv.Type().Kind()))
  75. }
  76. fname := field.Tag.Get("yaml")
  77. // not supported in old etcd
  78. if fname == "pre-vote" || fname == "initial-corrupt-check" {
  79. continue
  80. }
  81. if sv != "" {
  82. fs = append(fs, fmt.Sprintf("--%s=%s", fname, sv))
  83. }
  84. }
  85. return fs
  86. }