Repository: buchgr/bazel-remote
Pull request #504 (2021-12-15 21:38:25)

Config refactor


Patch:
diff --git a/README.md b/README.md
index 4884a35..f7b8bbf 100644
--- a/README.md
+++ b/README.md
@@ -541,11 +541,12 @@ $ docker run -u 1000:1000 -v /path/to/cache/dir:/data -v $HOME/.aws:/aws-config
 
 ### Profiling
 
-To enable pprof profiling, specify a port with `--profile_port`.
+To enable pprof profiling, specify an address to listen to with
+`--profile_address`.
 
-If running inside docker, you will also need to set `--profile_host` to a
-value other than `127.0.0.1` (`--profile_host=` with an empty value should
-work) and add a `-p` mapping to the docker run commandline for the port.
+If running inside docker, you will need to use a profile_address value
+with a host other than `127.0.0.1` and add a `-p` mapping to the docker
+run commandline for the port.
 
 See [Profiling Go programs with pprof](https://jvns.ca/blog/2017/09/24/profiling-go-with-pprof/)
 for more details.
diff --git a/config/config.go b/config/config.go
index a651590..5cbfe11 100644
--- a/config/config.go
+++ b/config/config.go
@@ -68,8 +68,15 @@ type Config struct {
 	TLSConfig    *tls.Config
 	AccessLogger *log.Logger
 	ErrorLogger  *log.Logger
+}
+
+type YamlConfig struct {
+	Config `yaml:",inline"`
+
+	// Deprecated fields, retained for backwards compatibility when
+	// parsing config files.
 
-	// Deprecated fields. Retained for backwards compatibility.
+	// If set, these will be used to populate *Address fields.
 	Host        string `yaml:"host"`
 	Port        int    `yaml:"port"`
 	GRPCPort    int    `yaml:"grpc_port"`
@@ -163,30 +170,33 @@ func newFromYamlFile(path string) (*Config, error) {
 }
 
 func newFromYaml(data []byte) (*Config, error) {
-	c := Config{
-		StorageMode:            "zstd",
-		NumUploaders:           100,
-		MaxQueuedUploads:       1000000,
-		MaxBlobSize:            math.MaxInt64,
-		MetricsDurationBuckets: defaultDurationBuckets,
-		AccessLogLevel:         "all",
-	}
-
-	err := yaml.Unmarshal(data, &c)
+	yc := YamlConfig{
+		Config: Config{
+			StorageMode:            "zstd",
+			NumUploaders:           100,
+			MaxQueuedUploads:       1000000,
+			MaxBlobSize:            math.MaxInt64,
+			MetricsDurationBuckets: defaultDurationBuckets,
+			AccessLogLevel:         "all",
+		},
+	}
+
+	err := yaml.Unmarshal(data, &yc)
 	if err != nil {
 		return nil, fmt.Errorf("Failed to parse YAML config: %v", err)
 	}
+	c := yc.Config
 
 	if c.HTTPAddress == "" {
-		c.HTTPAddress = net.JoinHostPort(c.Host, strconv.Itoa(c.Port))
+		c.HTTPAddress = net.JoinHostPort(yc.Host, strconv.Itoa(yc.Port))
 	}
 
-	if c.GRPCAddress == "" && c.GRPCPort > 0 {
-		c.GRPCAddress = net.JoinHostPort(c.Host, strconv.Itoa(c.GRPCPort))
+	if c.GRPCAddress == "" && yc.GRPCPort > 0 {
+		c.GRPCAddress = net.JoinHostPort(yc.Host, strconv.Itoa(yc.GRPCPort))
 	}
 
-	if c.ProfileAddress == "" && c.ProfilePort > 0 {
-		c.ProfileAddress = net.JoinHostPort(c.ProfileHost, strconv.Itoa(c.ProfilePort))
+	if c.ProfileAddress == "" && yc.ProfilePort > 0 {
+		c.ProfileAddress = net.JoinHostPort(yc.ProfileHost, strconv.Itoa(yc.ProfilePort))
 	}
 
 	if c.MetricsDurationBuckets != nil {


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index 348144b..c0b1df2 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -36,9 +36,6 @@ access_log_level: none
 	expectedConfig := &Config{
 		HTTPAddress:                 "localhost:8080",
 		GRPCAddress:                 "localhost:9092",
-		Host:                        "localhost",
-		Port:                        8080,
-		GRPCPort:                    9092,
 		Dir:                         "/opt/cache-dir",
 		MaxSize:                     100,
 		StorageMode:                 "zstd",
@@ -82,9 +79,6 @@ gcs_proxy:
 	expectedConfig := &Config{
 		HTTPAddress: "localhost:8080",
 		GRPCAddress: "localhost:9092",
-		Host:        "localhost",
-		Port:        8080,
-		GRPCPort:    9092,
 		Dir:         "/opt/cache-dir",
 		MaxSize:     100,
 		StorageMode: "zstd",
@@ -123,9 +117,6 @@ http_proxy:
 	expectedConfig := &Config{
 		HTTPAddress: "localhost:8080",
 		GRPCAddress: "localhost:9092",
-		Host:        "localhost",
-		Port:        8080,
-		GRPCPort:    9092,
 		Dir:         "/opt/cache-dir",
 		MaxSize:     100,
 		StorageMode: "zstd",
@@ -194,8 +185,6 @@ s3_proxy:
 
 	expectedConfig := &Config{
 		HTTPAddress: "localhost:8080",
-		Host:        "localhost",
-		Port:        8080,
 		Dir:         "/opt/cache-dir",
 		MaxSize:     100,
 		StorageMode: "zstd",
@@ -233,8 +222,6 @@ profile_address: :7070
 
 	expectedConfig := &Config{
 		HTTPAddress:            "localhost:1234",
-		Host:                   "localhost",
-		Port:                   1234,
 		Dir:                    "/opt/cache-dir",
 		MaxSize:                42,
 		StorageMode:            "zstd",
@@ -280,8 +267,6 @@ endpoint_metrics_duration_buckets: [.005, .1, 5]
 
 	expectedConfig := &Config{
 		HTTPAddress:            "localhost:1234",
-		Host:                   "localhost",
-		Port:                   1234,
 		Dir:                    "/opt/cache-dir",
 		MaxSize:                42,
 		StorageMode:            "zstd",