go.etcd.io/etcd@v3.3.27+incompatible/functional/rpcpb/etcd_config.go (about)

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