Repository: buchgr/bazel-remote
Pull request #277 (2020-05-19 06:49:19)

asset: add env var and finish config setup


Patch:
diff --git a/README.md b/README.md
index 5baa79c..26d6cdb 100644
--- a/README.md
+++ b/README.md
@@ -109,7 +109,7 @@ GLOBAL OPTIONS:
    --max_size value                 The maximum size of the remote cache in GiB. This flag is required. (default: -1) [$BAZEL_REMOTE_MAX_SIZE]
    --host value                     Address to listen on. Listens on all network interfaces by default. [$BAZEL_REMOTE_HOST]
    --port value                     The port the HTTP server listens on. (default: 8080) [$BAZEL_REMOTE_PORT]
-   --grpc_port value                The port the EXPERIMENTAL gRPC server listens on. Set to 0 to disable. (default: 9092) [$BAZEL_REMOTE_GRPC_PORT]
+   --grpc_port value                The port the gRPC server listens on. Set to 0 to disable. (default: 9092) [$BAZEL_REMOTE_GRPC_PORT]
    --profile_host value             A host address to listen on for profiling, if enabled by a valid --profile_port setting. (default: "127.0.0.1") [$BAZEL_REMOTE_PROFILE_HOST]
    --profile_port value             If a positive integer, serve /debug/pprof/* URLs from http://profile_host:profile_port. (default: 0, ie profiling disabled) [$BAZEL_REMOTE_PROFILE_PORT]
    --htpasswd_file value            Path to a .htpasswd file. This flag is optional. Please read https://httpd.apache.org/docs/2.4/programs/htpasswd.html. [$BAZEL_REMOTE_HTPASSWD_FILE]
@@ -128,7 +128,7 @@ GLOBAL OPTIONS:
    --disable_http_ac_validation     Whether to disable ActionResult validation for HTTP requests. (default: false, ie enable validation) [$BAZEL_REMOTE_DISABLE_HTTP_AC_VALIDATION]
    --disable_grpc_ac_deps_check     Whether to disable ActionResult dependency checks for gRPC GetActionResult requests. (default: false, ie enable ActionCache dependency checks) [$BAZEL_REMOTE_DISABLE_GRPS_AC_DEPS_CHECK]
    --enable_endpoint_metrics        Whether to enable metrics for each HTTP/gRPC endpoint. (default: false, ie disable metrics) [$BAZEL_REMOTE_ENABLE_ENDPOINT_METRICS]
-   --experimental_remote_asset_api  Whether to enable the experimental remote asset API implementation. (default: false, ie remote asset API disabled)
+   --experimental_remote_asset_api  Whether to enable the experimental remote asset API implementation. (default: false, ie disable remote asset API) [$BAZEL_REMOTE_EXPERIMENTAL_REMOTE_ASSET_API]
    --help, -h                       show help (default: false)
 ```
 
@@ -197,6 +197,9 @@ host: localhost
 #profile_port: 7070
 # IP address to use, if profiling is enabled:
 #profile_host: 127.0.0.1
+
+# If true, enable experimental remote asset API support:
+#experimental_remote_asset_api: true
 ```
 
 ## Docker
diff --git a/config/config.go b/config/config.go
index 2ad6b3c..21d2052 100644
--- a/config/config.go
+++ b/config/config.go
@@ -36,23 +36,24 @@ type HTTPBackendConfig struct {
 
 // Config holds the top-level configuration for bazel-remote.
 type Config struct {
-	Host                    string                    `yaml:"host"`
-	Port                    int                       `yaml:"port"`
-	GRPCPort                int                       `yaml:"grpc_port"`
-	ProfileHost             string                    `yaml:"profile_host"`
-	ProfilePort             int                       `yaml:"profile_port"`
-	Dir                     string                    `yaml:"dir"`
-	MaxSize                 int                       `yaml:"max_size"`
-	HtpasswdFile            string                    `yaml:"htpasswd_file"`
-	TLSCertFile             string                    `yaml:"tls_cert_file"`
-	TLSKeyFile              string                    `yaml:"tls_key_file"`
-	S3CloudStorage          *S3CloudStorageConfig     `yaml:"s3_proxy"`
-	GoogleCloudStorage      *GoogleCloudStorageConfig `yaml:"gcs_proxy"`
-	HTTPBackend             *HTTPBackendConfig        `yaml:"http_proxy"`
-	IdleTimeout             time.Duration             `yaml:"idle_timeout"`
-	DisableHTTPACValidation bool                      `yaml:"disable_http_ac_validation"`
-	DisableGRPCACDepsCheck  bool                      `yaml:"disable_grpc_ac_deps_check"`
-	EnableEndpointMetrics   bool                      `yaml:"enable_endpoint_metrics"`
+	Host                       string                    `yaml:"host"`
+	Port                       int                       `yaml:"port"`
+	GRPCPort                   int                       `yaml:"grpc_port"`
+	ProfileHost                string                    `yaml:"profile_host"`
+	ProfilePort                int                       `yaml:"profile_port"`
+	Dir                        string                    `yaml:"dir"`
+	MaxSize                    int                       `yaml:"max_size"`
+	HtpasswdFile               string                    `yaml:"htpasswd_file"`
+	TLSCertFile                string                    `yaml:"tls_cert_file"`
+	TLSKeyFile                 string                    `yaml:"tls_key_file"`
+	S3CloudStorage             *S3CloudStorageConfig     `yaml:"s3_proxy"`
+	GoogleCloudStorage         *GoogleCloudStorageConfig `yaml:"gcs_proxy"`
+	HTTPBackend                *HTTPBackendConfig        `yaml:"http_proxy"`
+	IdleTimeout                time.Duration             `yaml:"idle_timeout"`
+	DisableHTTPACValidation    bool                      `yaml:"disable_http_ac_validation"`
+	DisableGRPCACDepsCheck     bool                      `yaml:"disable_grpc_ac_deps_check"`
+	EnableEndpointMetrics      bool                      `yaml:"enable_endpoint_metrics"`
+	ExperimentalRemoteAssetAPI bool                      `yaml:"experimental_remote_asset_api"`
 }
 
 // New returns a validated Config with the specified values, and an error
@@ -61,25 +62,27 @@ func New(dir string, maxSize int, host string, port int, grpcPort int,
 	profileHost string, profilePort int, htpasswdFile string,
 	tlsCertFile string, tlsKeyFile string, idleTimeout time.Duration,
 	s3 *S3CloudStorageConfig, disableHTTPACValidation bool,
-	disableGRPCACDepsCheck bool, enableEndpointMetrics bool) (*Config, error) {
+	disableGRPCACDepsCheck bool, enableEndpointMetrics bool,
+	experimentalRemoteAssetAPI bool) (*Config, error) {
 	c := Config{
-		Host:                    host,
-		Port:                    port,
-		GRPCPort:                grpcPort,
-		ProfileHost:             profileHost,
-		ProfilePort:             profilePort,
-		Dir:                     dir,
-		MaxSize:                 maxSize,
-		HtpasswdFile:            htpasswdFile,
-		TLSCertFile:             tlsCertFile,
-		TLSKeyFile:              tlsKeyFile,
-		S3CloudStorage:          s3,
-		GoogleCloudStorage:      nil,
-		HTTPBackend:             nil,
-		IdleTimeout:             idleTimeout,
-		DisableHTTPACValidation: disableHTTPACValidation,
-		DisableGRPCACDepsCheck:  disableGRPCACDepsCheck,
-		EnableEndpointMetrics:   enableEndpointMetrics,
+		Host:                       host,
+		Port:                       port,
+		GRPCPort:                   grpcPort,
+		ProfileHost:                profileHost,
+		ProfilePort:                profilePort,
+		Dir:                        dir,
+		MaxSize:                    maxSize,
+		HtpasswdFile:               htpasswdFile,
+		TLSCertFile:                tlsCertFile,
+		TLSKeyFile:                 tlsKeyFile,
+		S3CloudStorage:             s3,
+		GoogleCloudStorage:         nil,
+		HTTPBackend:                nil,
+		IdleTimeout:                idleTimeout,
+		DisableHTTPACValidation:    disableHTTPACValidation,
+		DisableGRPCACDepsCheck:     disableGRPCACDepsCheck,
+		EnableEndpointMetrics:      enableEndpointMetrics,
+		ExperimentalRemoteAssetAPI: experimentalRemoteAssetAPI,
 	}
 
 	err := validateConfig(&c)
@@ -138,6 +141,10 @@ func validateConfig(c *Config) error {
 		return errors.New("The 'grpc_port' flag/key must be 0 (disabled) or a positive integer")
 	}
 
+	if c.GRPCPort == 0 && c.ExperimentalRemoteAssetAPI {
+		return errors.New("Remote Asset API support depends on gRPC being enabled")
+	}
+
 	if (c.TLSCertFile != "" && c.TLSKeyFile == "") || (c.TLSCertFile == "" && c.TLSKeyFile != "") {
 		return errors.New("When enabling TLS one must specify both " +
 			"'tls_key_file' and 'tls_cert_file'")
diff --git a/main.go b/main.go
index 309d27f..549df0b 100644
--- a/main.go
+++ b/main.go
@@ -94,7 +94,7 @@ func main() {
 		&cli.IntFlag{
 			Name:    "grpc_port",
 			Value:   9092,
-			Usage:   "The port the EXPERIMENTAL gRPC server listens on. Set to 0 to disable.",
+			Usage:   "The port the gRPC server listens on. Set to 0 to disable.",
 			EnvVars: []string{"BAZEL_REMOTE_GRPC_PORT"},
 		},
 		&cli.StringFlag{
@@ -210,6 +210,7 @@ func main() {
 			Name:        "experimental_remote_asset_api",
 			Usage:       "Whether to enable the experimental remote asset API implementation.",
 			DefaultText: "false, ie disable remote asset API",
+			EnvVars:     []string{"BAZEL_REMOTE_EXPERIMENTAL_REMOTE_ASSET_API"},
 		},
 	}
 
@@ -249,6 +250,7 @@ func main() {
 				ctx.Bool("disable_http_ac_validation"),
 				ctx.Bool("disable_grpc_ac_deps_check"),
 				ctx.Bool("enable_endpoint_metrics"),
+				ctx.Bool("experimental_remote_asset_api"),
 			)
 		}
 
@@ -388,7 +390,7 @@ func main() {
 				}
 				log.Println("gRPC AC dependency checks:", validateStatus)
 
-				enableRemoteAssetAPI := ctx.Bool("experimental_remote_asset_api")
+				enableRemoteAssetAPI := c.ExperimentalRemoteAssetAPI
 				remoteAssetStatus := "disabled"
 				if enableRemoteAssetAPI {
 					remoteAssetStatus = "enabled"


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index c933bc3..d7feef3 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -19,6 +19,7 @@ tls_cert_file: /opt/tls.cert
 tls_key_file:  /opt/tls.key
 disable_http_ac_validation: true
 enable_endpoint_metrics: true
+experimental_remote_asset_api: true
 `
 
 	config, err := newFromYaml([]byte(yaml))
@@ -27,16 +28,17 @@ enable_endpoint_metrics: true
 	}
 
 	expectedConfig := &Config{
-		Host:                    "localhost",
-		Port:                    8080,
-		GRPCPort:                9092,
-		Dir:                     "/opt/cache-dir",
-		MaxSize:                 100,
-		HtpasswdFile:            "/opt/.htpasswd",
-		TLSCertFile:             "/opt/tls.cert",
-		TLSKeyFile:              "/opt/tls.key",
-		DisableHTTPACValidation: true,
-		EnableEndpointMetrics:   true,
+		Host:                       "localhost",
+		Port:                       8080,
+		GRPCPort:                   9092,
+		Dir:                        "/opt/cache-dir",
+		MaxSize:                    100,
+		HtpasswdFile:               "/opt/.htpasswd",
+		TLSCertFile:                "/opt/tls.cert",
+		TLSKeyFile:                 "/opt/tls.key",
+		DisableHTTPACValidation:    true,
+		EnableEndpointMetrics:      true,
+		ExperimentalRemoteAssetAPI: true,
 	}
 
 	if !reflect.DeepEqual(config, expectedConfig) {