99 "os"
1010 "os/signal"
1111 "path/filepath"
12+ "strings"
1213 "syscall"
1314 "time"
1415
@@ -75,20 +76,17 @@ func (s *EtcdService) configure(cfg *config.Config) {
7576 // based on https://github.com/openshift/cluster-etcd-operator/blob/master/bindata/bootkube/bootstrap-manifests/etcd-member-pod.yaml#L19
7677 s .etcdCfg = etcd .NewConfig ()
7778 s .etcdCfg .ClusterState = "new"
78- //s.etcdCfg.ForceNewCluster = true //TODO
7979 s .etcdCfg .Logger = "zap"
8080 s .etcdCfg .Dir = dataDir
8181 s .etcdCfg .QuotaBackendBytes = cfg .Etcd .QuotaBackendBytes
82- url2380 := setURL ([]string {"localhost" }, "2380" )
83- url2379 := setURL ([]string {"localhost" }, "2379" )
84- s .etcdCfg .AdvertisePeerUrls = url2380
85- s .etcdCfg .ListenPeerUrls = url2380
86- s .etcdCfg .AdvertiseClientUrls = url2379
87- s .etcdCfg .ListenClientUrls = url2379
88- s .etcdCfg .ListenMetricsUrls = setURL ([]string {"localhost" }, "2381" )
82+ s .etcdCfg .AdvertisePeerUrls = setURL ([]string {cfg .Node .NodeIP }, "2380" )
83+ s .etcdCfg .ListenPeerUrls = setURL ([]string {"0.0.0.0" }, "2380" )
84+ s .etcdCfg .AdvertiseClientUrls = setURL ([]string {cfg .Node .NodeIP }, "2379" )
85+ s .etcdCfg .ListenClientUrls = setURL ([]string {"0.0.0.0" }, "2379" )
86+ s .etcdCfg .ListenMetricsUrls = setURL ([]string {cfg .Node .NodeIP }, "2381" )
8987
9088 s .etcdCfg .Name = cfg .Node .HostnameOverride
91- s .etcdCfg .InitialCluster = fmt .Sprintf ("%s=https://%s:2380 " , cfg .Node .HostnameOverride , "localhost" )
89+ s .etcdCfg .InitialCluster = fmt .Sprintf ("%s=https://%s" , cfg .Node .HostnameOverride , net . JoinHostPort ( cfg . Node . NodeIP , "2380" ) )
9290
9391 s .etcdCfg .TlsMinVersion = getTLSMinVersion (cfg .ApiServer .TLS .MinVersion )
9492 if cfg .ApiServer .TLS .MinVersion != string (configv1 .VersionTLS13 ) {
@@ -103,6 +101,8 @@ func (s *EtcdService) configure(cfg *config.Config) {
103101 s .etcdCfg .PeerTLSInfo .TrustedCAFile = etcdSignerCertPath
104102
105103 s .etcdCfg .ExperimentalMaxLearners = MaxLearners
104+
105+ updateConfigFromFile (s .etcdCfg , getConfigFilePath ())
106106}
107107
108108func (s * EtcdService ) Run () error {
@@ -217,3 +217,35 @@ func checkFragmentationPercentage(ondisk, inuse int64) float64 {
217217 fragmentedPercentage := (diff / float64 (ondisk )) * 100
218218 return math .Round (fragmentedPercentage * 100 ) / 100
219219}
220+
221+ func getConfigFilePath () string {
222+ return filepath .Join (config .DataDir , "etcd" , "config" )
223+ }
224+
225+ func updateConfigFromFile (etcdCfg * etcd.Config , configPath string ) {
226+ data , err := os .ReadFile (configPath )
227+ if err != nil {
228+ klog .Errorf ("failed to read config file: %v" , err )
229+ return
230+ }
231+ lines := strings .Split (string (data ), "\n " )
232+ for _ , line := range lines {
233+ line = strings .TrimSpace (line )
234+ if line == "" || strings .HasPrefix (line , "#" ) {
235+ continue
236+ }
237+ eqIdx := strings .Index (line , "=" )
238+ if eqIdx == - 1 {
239+ continue
240+ }
241+ parts := []string {line [:eqIdx ], line [eqIdx + 1 :]}
242+ key := strings .TrimSpace (parts [0 ])
243+ val := strings .TrimSpace (parts [1 ])
244+ switch key {
245+ case "ETCD_INITIAL_CLUSTER" :
246+ etcdCfg .InitialCluster = val
247+ case "ETCD_INITIAL_CLUSTER_STATE" :
248+ etcdCfg .ClusterState = val
249+ }
250+ }
251+ }
0 commit comments