Repository: dragonflyoss/Dragonfly2
Pull request #1694 (2022-09-21 06:55:29)

refactor: listenIP and advertiseIP
Signed-off-by: Gaius <gaius.qi@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description
- Refactor listenIP and advertiseIP.
<!--- Describe your changes in detail -->

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.


codecov-commenter: # [Codecov](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1694?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) Report
> Merging [#1694](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1694?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (ed30bdc) into [main](https://codecov.io/gh/dragonflyoss/Dragonfly2/commit/67f6ef99af274d66cc4f7058d0f766ce9d24040c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (67f6ef9) will **decrease** coverage by `0.00%`.
> The diff coverage is `0.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1694/graphs/tree.svg?width=650&height=150&src=pr&token=PUICJYSAT7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1694?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)

```diff
@@            Coverage Diff             @@
##             main    #1694      +/-   ##
==========================================
- Coverage   41.99%   41.98%   -0.01%     
==========================================
  Files         125      125              
  Lines       15545    15558      +13     
==========================================
+ Hits         6528     6532       +4     
- Misses       8502     8514      +12     
+ Partials      515      512       -3     
```

| Flag | Coverage Δ | |
|---|---|---|
| Object-compatibility-e2etests | `?` | |
| e2etests | `?` | |
| unittests | `41.98% <0.00%> (-0.01%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1694?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | Coverage Δ | |
|---|---|---|
| [manager/config/config.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1694/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-bWFuYWdlci9jb25maWcvY29uZmlnLmdv) | `0.00% <0.00%> (ø)` | |
| [scheduler/config/config.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1694/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-c2NoZWR1bGVyL2NvbmZpZy9jb25maWcuZ28=) | `0.00% <0.00%> (ø)` | |
| [client/daemon/peer/piece\_manager.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1694/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BpZWNlX21hbmFnZXIuZ28=) | `52.75% <0.00%> (-0.41%)` | :arrow_down: |
| [client/daemon/peer/peertask\_conductor.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1694/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX2NvbmR1Y3Rvci5nbw==) | `57.17% <0.00%> (+0.29%)` | :arrow_up: |
| [...ent/daemon/peer/peertask\_piecetask\_synchronizer.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1694/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19zeW5jaHJvbml6ZXIuZ28=) | `51.41% <0.00%> (+0.94%)` | :arrow_up: |


Patch:
diff --git a/deploy/docker-compose/template/manager.template.yaml b/deploy/docker-compose/template/manager.template.yaml
index 741f2444..1b4f151b 100644
--- a/deploy/docker-compose/template/manager.template.yaml
+++ b/deploy/docker-compose/template/manager.template.yaml
@@ -2,8 +2,8 @@
 server:
   # grpc server configure
   grpc:
-    # listen address
-    listen: 0.0.0.0
+    # listen ip
+    listenIP: 0.0.0.0
     # listen port, manager will try to listen
     # when this port is not available, manager will try next port
     port:
diff --git a/deploy/docker-compose/template/scheduler.template.yaml b/deploy/docker-compose/template/scheduler.template.yaml
index 8f710fe2..0d7a7451 100644
--- a/deploy/docker-compose/template/scheduler.template.yaml
+++ b/deploy/docker-compose/template/scheduler.template.yaml
@@ -1,7 +1,9 @@
 # server scheduler instance configuration
 server:
-  # # ip
-  ip: 0.0.0.0
+  # # Advertise ip
+  advertiseIP: 127.0.0.1
+  # # Listen ip
+  listenIP: 0.0.0.0
   # # host
   # host: localhost
   # port is the ip and port scheduler server listens on.
diff --git a/manager/config/config.go b/manager/config/config.go
index d5b52c02..10a5a97e 100644
--- a/manager/config/config.go
+++ b/manager/config/config.go
@@ -17,13 +17,10 @@
 package config
 
 import (
-	"crypto/tls"
 	"errors"
 	"fmt"
 	"time"
 
-	"github.com/docker/go-connections/tlsconfig"
-
 	"d7y.io/dragonfly/v2/cmd/dependency/base"
 	"d7y.io/dragonfly/v2/pkg/objectstorage"
 	"d7y.io/dragonfly/v2/pkg/rpc"
@@ -152,16 +149,6 @@ type PostgresConfig struct {
 	Migrate bool `yaml:"migrate" mapstructure:"migrate"`
 }
 
-// Generate client tls config.
-func (t *TLSConfig) Client() (*tls.Config, error) {
-	return tlsconfig.Client(tlsconfig.Options{
-		CAFile:             t.CA,
-		CertFile:           t.Cert,
-		KeyFile:            t.Key,
-		InsecureSkipVerify: t.InsecureSkipVerify,
-	})
-}
-
 type RedisConfig struct {
 	// DEPRECATED: Please use the `addrs` field instead.
 	Host string `yaml:"host" mapstructure:"host"`
@@ -226,10 +213,13 @@ type MetricsConfig struct {
 }
 
 type TCPListenConfig struct {
-	// Listen is listen interface, like: 0.0.0.0, 192.168.0.1.
+	// DEPRECATED: Please use the `listenIP` field instead.
 	Listen string `mapstructure:"listen" yaml:"listen"`
 
-	// PortRange is listen port.
+	// ListenIP is listen ip, like: 0.0.0.0, 192.168.0.1.
+	ListenIP string `mapstructure:"listenIP" yaml:"listenIP"`
+
+	// Port is listen port.
 	PortRange TCPListenPortRange `yaml:"port" mapstructure:"port"`
 }
 
@@ -295,6 +285,7 @@ func New() *Config {
 		Server: &ServerConfig{
 			Name: DefaultServerName,
 			GRPC: &TCPListenConfig{
+				ListenIP: DefaultGRPCListenIP,
 				PortRange: TCPListenPortRange{
 					Start: DefaultGRPCPort,
 					End:   DefaultGRPCPort,
@@ -367,6 +358,10 @@ func (cfg *Config) Validate() error {
 		return errors.New("server requires parameter grpc")
 	}
 
+	if cfg.Server.GRPC.ListenIP == "" {
+		return errors.New("grpc requires parameter listenIP")
+	}
+
 	if cfg.Server.REST == nil {
 		return errors.New("server requires parameter rest")
 	}
@@ -570,5 +565,10 @@ func (cfg *Config) Convert() error {
 		cfg.Database.Redis.Addrs = []string{fmt.Sprintf("%s:%d", cfg.Database.Redis.Host, cfg.Database.Redis.Port)}
 	}
 
+	// TODO Compatible with deprecated fields listen.
+	if cfg.Server.GRPC.Listen != "" && cfg.Server.GRPC.ListenIP == "" {
+		cfg.Server.GRPC.ListenIP = cfg.Server.GRPC.Listen
+	}
+
 	return nil
 }
diff --git a/manager/config/constants.go b/manager/config/constants.go
index e6dd8a4a..7b2cd369 100644
--- a/manager/config/constants.go
+++ b/manager/config/constants.go
@@ -17,6 +17,7 @@
 package config
 
 import (
+	"net"
 	"time"
 
 	"d7y.io/dragonfly/v2/pkg/net/ip"
@@ -33,6 +34,9 @@ const (
 	DatabaseTypePostgres = "postgres"
 )
 
+// DefaultGRPCListenIP is default listen ip for grpc server.
+var DefaultGRPCListenIP = net.IPv4zero.String()
+
 const (
 	// DefaultServerName is default server name.
 	DefaultServerName = "d7y/manager"
diff --git a/manager/database/mysql.go b/manager/database/mysql.go
index 6e351a6b..91e286af 100644
--- a/manager/database/mysql.go
+++ b/manager/database/mysql.go
@@ -20,6 +20,7 @@ import (
 	"fmt"
 	"time"
 
+	"github.com/docker/go-connections/tlsconfig"
 	"github.com/go-sql-driver/mysql"
 	drivermysql "gorm.io/driver/mysql"
 	"gorm.io/gorm"
@@ -82,7 +83,12 @@ func formatMysqlDSN(cfg *config.MysqlConfig) (string, error) {
 	// Support TLS connection.
 	if cfg.TLS != nil {
 		mysqlCfg.TLSConfig = "custom"
-		tls, err := cfg.TLS.Client()
+		tls, err := tlsconfig.Client(tlsconfig.Options{
+			CAFile:             cfg.TLS.CA,
+			CertFile:           cfg.TLS.Cert,
+			KeyFile:            cfg.TLS.Key,
+			InsecureSkipVerify: cfg.TLS.InsecureSkipVerify,
+		})
 		if err != nil {
 			return "", err
 		}
diff --git a/manager/manager.go b/manager/manager.go
index 5de0cee5..dee43d90 100644
--- a/manager/manager.go
+++ b/manager/manager.go
@@ -252,7 +252,7 @@ func (s *Server) Serve() error {
 	}
 
 	// Generate GRPC listener
-	lis, _, err := rpc.ListenWithPortRange(s.config.Server.GRPC.Listen, s.config.Server.GRPC.PortRange.Start, s.config.Server.GRPC.PortRange.End)
+	lis, _, err := rpc.ListenWithPortRange(s.config.Server.GRPC.ListenIP, s.config.Server.GRPC.PortRange.Start, s.config.Server.GRPC.PortRange.End)
 	if err != nil {
 		logger.Fatalf("net listener failed to start: %v", err)
 	}
diff --git a/scheduler/config/config.go b/scheduler/config/config.go
index 84633d77..031255fb 100644
--- a/scheduler/config/config.go
+++ b/scheduler/config/config.go
@@ -65,18 +65,24 @@ type Config struct {
 }
 
 type ServerConfig struct {
-	// Server ip.
+	// DEPRECATED: Please use the `advertiseIP` field instead.
 	IP string `yaml:"ip" mapstructure:"ip"`
 
-	// Server hostname.
-	Host string `yaml:"host" mapstructure:"host"`
-
-	// Listen stands listen interface, like: 0.0.0.0, 192.168.0.1.
+	// DEPRECATED: Please use the `listenIP` field instead.
 	Listen string `yaml:"listen" mapstructure:"listen"`
 
+	// AdvertiseIP is advertise ip.
+	AdvertiseIP string `yaml:"advertiseIP" mapstructure:"advertiseIP"`
+
+	// ListenIP is listen ip, like: 0.0.0.0, 192.168.0.1.
+	ListenIP string `yaml:"listenIP" mapstructure:"listenIP"`
+
 	// Server port.
 	Port int `yaml:"port" mapstructure:"port"`
 
+	// Server hostname.
+	Host string `yaml:"host" mapstructure:"host"`
+
 	// Server work directory.
 	WorkHome string `yaml:"workHome" mapstructure:"workHome"`
 
@@ -277,10 +283,10 @@ type CertSpec struct {
 func New() *Config {
 	return &Config{
 		Server: &ServerConfig{
-			IP:     ip.IPv4,
-			Host:   fqdn.FQDNHostname,
-			Listen: DefaultServerListen,
-			Port:   DefaultServerPort,
+			AdvertiseIP: ip.IPv4,
+			ListenIP:    DefaultServerListenIP,
+			Port:        DefaultServerPort,
+			Host:        fqdn.FQDNHostname,
 		},
 		Scheduler: &SchedulerConfig{
 			Algorithm:            DefaultSchedulerAlgorithm,
@@ -353,20 +359,20 @@ func (cfg *Config) Validate() error {
 		return errors.New("server requires parameter server")
 	}
 
-	if cfg.Server.IP == "" {
-		return errors.New("server requires parameter ip")
+	if cfg.Server.AdvertiseIP == "" {
+		return errors.New("server requires parameter advertiseIP")
 	}
 
-	if cfg.Server.Host == "" {
-		return errors.New("server requires parameter host")
+	if cfg.Server.ListenIP == "" {
+		return errors.New("server requires parameter listenIP")
 	}
 
 	if cfg.Server.Port <= 0 {
 		return errors.New("server requires parameter port")
 	}
 
-	if cfg.Server.Listen == "" {
-		return errors.New("server requires parameter listen")
+	if cfg.Server.Host == "" {
+		return errors.New("server requires parameter host")
 	}
 
 	if cfg.Scheduler.Algorithm == "" {
@@ -500,5 +506,15 @@ func (cfg *Config) Convert() error {
 		cfg.Job.Redis.Addrs = []string{fmt.Sprintf("%s:%d", cfg.Job.Redis.Host, cfg.Job.Redis.Port)}
 	}
 
+	// TODO Compatible with deprecated fields ip.
+	if cfg.Server.IP != "" && cfg.Server.AdvertiseIP == "" {
+		cfg.Server.AdvertiseIP = cfg.Server.IP
+	}
+
+	// TODO Compatible with deprecated fields listen.
+	if cfg.Server.Listen != "" && cfg.Server.ListenIP == "" {
+		cfg.Server.ListenIP = cfg.Server.Listen
+	}
+
 	return nil
 }
diff --git a/scheduler/config/constants.go b/scheduler/config/constants.go
index ada8870e..5705e99c 100644
--- a/scheduler/config/constants.go
+++ b/scheduler/config/constants.go
@@ -38,8 +38,8 @@ const (
 	DefaultSchedulerFilterParentRangeLimit = 40
 )
 
-// DefaultServerListen is default listen for server.
-var DefaultServerListen = net.IPv4zero.String()
+// DefaultServerListenIP is default listen ip for server.
+var DefaultServerListenIP = net.IPv4zero.String()
 
 const (
 	// DefaultServerPort is default port for server.
diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go
index 9ad15726..9b08737f 100644
--- a/scheduler/scheduler.go
+++ b/scheduler/scheduler.go
@@ -113,7 +113,7 @@ func New(ctx context.Context, cfg *config.Config, d dfpath.Dfpath) (*Server, err
 	if _, err := s.managerClient.UpdateScheduler(context.Background(), &managerv1.UpdateSchedulerRequest{
 		SourceType:         managerv1.SourceType_SCHEDULER_SOURCE,
 		HostName:           s.config.Server.Host,
-		Ip:                 s.config.Server.IP,
+		Ip:                 s.config.Server.AdvertiseIP,
 		Port:               int32(s.config.Server.Port),
 		Idc:                s.config.Host.IDC,
 		Location:           s.config.Host.Location,
@@ -258,14 +258,14 @@ func (s *Server) Serve() error {
 			s.managerClient.KeepAlive(s.config.Manager.KeepAlive.Interval, &managerv1.KeepAliveRequest{
 				SourceType: managerv1.SourceType_SCHEDULER_SOURCE,
 				HostName:   s.config.Server.Host,
-				Ip:         s.config.Server.IP,
+				Ip:         s.config.Server.AdvertiseIP,
 				ClusterId:  uint64(s.config.Manager.SchedulerClusterID),
 			})
 		}()
 	}
 
 	// Generate GRPC limit listener.
-	listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.config.Server.Listen, s.config.Server.Port))
+	listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.config.Server.ListenIP, s.config.Server.Port))
 	if err != nil {
 		logger.Fatalf("net listener failed to start: %s", err.Error())
 	}


Test patch:
diff --git a/manager/config/config_test.go b/manager/config/config_test.go
index e3818f41..0fc80db0 100644
--- a/manager/config/config_test.go
+++ b/manager/config/config_test.go
@@ -36,7 +36,7 @@ func TestManagerConfig_Load(t *testing.T) {
 			CacheDir: "foo",
 			LogDir:   "foo",
 			GRPC: &TCPListenConfig{
-				Listen: "127.0.0.1",
+				ListenIP: "0.0.0.0",
 				PortRange: TCPListenPortRange{
 					Start: 65003,
 					End:   65003,
diff --git a/manager/config/testdata/manager.yaml b/manager/config/testdata/manager.yaml
index 75e0ca2c..131771e4 100644
--- a/manager/config/testdata/manager.yaml
+++ b/manager/config/testdata/manager.yaml
@@ -3,7 +3,7 @@ server:
   cacheDir: foo
   logDir: foo
   grpc:
-    listen: 127.0.0.1
+    listenIP: 0.0.0.0
     port:
       start: 65003
       end: 65003
diff --git a/scheduler/config/config_test.go b/scheduler/config/config_test.go
index f7a68097..c171cbce 100644
--- a/scheduler/config/config_test.go
+++ b/scheduler/config/config_test.go
@@ -52,14 +52,14 @@ func TestConfig_Load(t *testing.T) {
 			},
 		},
 		Server: &ServerConfig{
-			IP:       "127.0.0.1",
-			Host:     "foo",
-			Listen:   "0.0.0.0",
-			Port:     8002,
-			WorkHome: "home",
-			CacheDir: "foo",
-			LogDir:   "bar",
-			DataDir:  "baz",
+			AdvertiseIP: "127.0.0.1",
+			ListenIP:    "0.0.0.0",
+			Port:        8002,
+			Host:        "foo",
+			WorkHome:    "home",
+			CacheDir:    "foo",
+			LogDir:      "bar",
+			DataDir:     "baz",
 		},
 		DynConfig: &DynConfig{
 			RefreshInterval: 5 * time.Minute,
diff --git a/scheduler/config/testdata/scheduler.yaml b/scheduler/config/testdata/scheduler.yaml
index 91f98fd0..34018e01 100644
--- a/scheduler/config/testdata/scheduler.yaml
+++ b/scheduler/config/testdata/scheduler.yaml
@@ -1,8 +1,8 @@
 server:
-  ip: 127.0.0.1
-  host: foo
-  listen: 0.0.0.0
+  advertiseIP: 127.0.0.1
+  listenIP: 0.0.0.0
   port: 8002
+  host: foo
   workHome: home
   cacheDir: foo
   logDir: bar