github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/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  	"Logger",
    58  	"LogOutputs",
    59  	"LogLevel",
    60  
    61  	"SocketReuseAddress",
    62  	"SocketReusePort",
    63  }
    64  
    65  // Flags returns etcd flags in string slice.
    66  func (e *Etcd) Flags() (fs []string) {
    67  	tp := reflect.TypeOf(*e)
    68  	vo := reflect.ValueOf(*e)
    69  	for _, name := range etcdFields {
    70  		field, ok := tp.FieldByName(name)
    71  		if !ok {
    72  			panic(fmt.Errorf("field %q not found", name))
    73  		}
    74  		fv := reflect.Indirect(vo).FieldByName(name)
    75  		var sv string
    76  		switch fv.Type().Kind() {
    77  		case reflect.String:
    78  			sv = fv.String()
    79  		case reflect.Slice:
    80  			n := fv.Len()
    81  			sl := make([]string, n)
    82  			for i := 0; i < n; i++ {
    83  				sl[i] = fv.Index(i).String()
    84  			}
    85  			sv = strings.Join(sl, ",")
    86  		case reflect.Int64:
    87  			sv = fmt.Sprintf("%d", fv.Int())
    88  		case reflect.Bool:
    89  			sv = fmt.Sprintf("%v", fv.Bool())
    90  		default:
    91  			panic(fmt.Errorf("field %q (%v) cannot be parsed", name, fv.Type().Kind()))
    92  		}
    93  
    94  		fname := field.Tag.Get("yaml")
    95  
    96  		// TODO: remove this
    97  		if fname == "initial-corrupt-check" {
    98  			fname = "experimental-" + fname
    99  		}
   100  
   101  		if sv != "" {
   102  			fs = append(fs, fmt.Sprintf("--%s=%s", fname, sv))
   103  		}
   104  	}
   105  	return fs
   106  }