etcd_config.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "Logger",
  48. "LogOutputs",
  49. "LogLevel",
  50. }
  51. // Flags returns etcd flags in string slice.
  52. func (e *Etcd) Flags() (fs []string) {
  53. tp := reflect.TypeOf(*e)
  54. vo := reflect.ValueOf(*e)
  55. for _, name := range etcdFields {
  56. field, ok := tp.FieldByName(name)
  57. if !ok {
  58. panic(fmt.Errorf("field %q not found", name))
  59. }
  60. fv := reflect.Indirect(vo).FieldByName(name)
  61. var sv string
  62. switch fv.Type().Kind() {
  63. case reflect.String:
  64. sv = fv.String()
  65. case reflect.Slice:
  66. n := fv.Len()
  67. sl := make([]string, n)
  68. for i := 0; i < n; i++ {
  69. sl[i] = fv.Index(i).String()
  70. }
  71. sv = strings.Join(sl, ",")
  72. case reflect.Int64:
  73. sv = fmt.Sprintf("%d", fv.Int())
  74. case reflect.Bool:
  75. sv = fmt.Sprintf("%v", fv.Bool())
  76. default:
  77. panic(fmt.Errorf("field %q (%v) cannot be parsed", name, fv.Type().Kind()))
  78. }
  79. fname := field.Tag.Get("yaml")
  80. // TODO: remove this
  81. if fname == "initial-corrupt-check" {
  82. fname = "experimental-" + fname
  83. }
  84. if sv != "" {
  85. fs = append(fs, fmt.Sprintf("--%s=%s", fname, sv))
  86. }
  87. }
  88. return fs
  89. }