From 94f57db00b1e57a7b99ef58605db733fc1f59868 Mon Sep 17 00:00:00 2001 From: William Zoid Stewart Date: Tue, 8 Feb 2022 16:13:23 +0100 Subject: [PATCH] Enable support for proxies needed in the Docker environment --- cmd/rbe_configs_gen/rbe_configs_gen.go | 5 +++++ pkg/rbeconfigsgen/options.go | 2 ++ pkg/rbeconfigsgen/rbeconfigsgen.go | 10 ++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/cmd/rbe_configs_gen/rbe_configs_gen.go b/cmd/rbe_configs_gen/rbe_configs_gen.go index 222ffed5c..58a8516a2 100644 --- a/cmd/rbe_configs_gen/rbe_configs_gen.go +++ b/cmd/rbe_configs_gen/rbe_configs_gen.go @@ -58,6 +58,7 @@ var ( enableMonitoring = flag.Bool("enable_monitoring", false, "(Optional) Enables reporting reporting results to Google Cloud Monitoring. Defaults to false.") monitoringProjectID = flag.String("monitoring_project_id", "", "GCP Project ID where monitoring results will be reported. Required if --enable_monitoring is true.") monitoringDockerImage = flag.String("monitoring_docker_image", "", "Name of the toolchain docker image to be reported as a string label to monitoring. Required if --enable_monitoring is true.") + httpsProxy = flag.String("https_proxy", "", "The proxy argument to send to Docker.") ) // printFlag prints flag values with the intent of allowing easy copy paste of flags to rerun this @@ -110,6 +111,9 @@ func printFlags() { if len(*monitoringDockerImage) != 0 { log.Printf("--monitoring_docker_image=%q \\", *monitoringDockerImage) } + if len(*httpsProxy) != 0 { + log.Printf("--https_proxy=%q \\", *httpsProxy) + } } func initMonitoringClient(ctx context.Context) (*monitoring.Client, error) { @@ -171,6 +175,7 @@ func main() { JavaUseLocalRuntime: *javaUseLocalRuntime, TempWorkDir: *tempWorkDir, Cleanup: *cleanup, + HttpsProxy: *httpsProxy, } result := true diff --git a/pkg/rbeconfigsgen/options.go b/pkg/rbeconfigsgen/options.go index 6fcab5f85..a620553cb 100644 --- a/pkg/rbeconfigsgen/options.go +++ b/pkg/rbeconfigsgen/options.go @@ -99,6 +99,8 @@ type Options struct { // Cleanup determines whether the running container & intermediate files will be deleted once // config generation is done. Setting it to false is useful for debugging intermediate state. Cleanup bool + // HttpsProxy set the proxy to pass to the Docker daemon + HttpsProxy string } // DefaultOptions are some option values that are populated as default values for certain fields diff --git a/pkg/rbeconfigsgen/rbeconfigsgen.go b/pkg/rbeconfigsgen/rbeconfigsgen.go index a752642c1..7c8ec60c0 100644 --- a/pkg/rbeconfigsgen/rbeconfigsgen.go +++ b/pkg/rbeconfigsgen/rbeconfigsgen.go @@ -166,6 +166,8 @@ type dockerRunner struct { // Parameters that affect how commands are executed inside the running toolchain container. // These parameters can be changed between calls to the execCmd function. + // httpsProxy is the proxy to use by commands inside the container. + httpsProxy string // workdir is the working directory to use to run commands inside the container. workdir string // env is the environment variables to set when executing commands specified in the given order @@ -251,7 +253,7 @@ func BazeliskDownloadInfo(os string) (string, string, error) { // newDockerRunner creates a new running container of the given containerImage. stopContainer // determines if the cleanup function on the dockerRunner will stop the running container when // called. -func newDockerRunner(containerImage string, stopContainer bool) (*dockerRunner, error) { +func newDockerRunner(containerImage string, stopContainer bool, httpsProxy string) (*dockerRunner, error) { if containerImage == "" { return nil, fmt.Errorf("container image was not specified") } @@ -259,6 +261,7 @@ func newDockerRunner(containerImage string, stopContainer bool) (*dockerRunner, containerImage: containerImage, stopContainer: stopContainer, dockerPath: "docker", + httpsProxy: httpsProxy, } if _, err := runCmd(d.dockerPath, "pull", d.containerImage); err != nil { return nil, fmt.Errorf("docker was unable to pull the toolchain container image %q: %w", d.containerImage, err) @@ -294,6 +297,9 @@ func (d *dockerRunner) execCmd(args ...string) (string, error) { if d.workdir != "" { a = append(a, "-w", d.workdir) } + if len(d.httpsProxy) != 0 { + a = append(a, "-e", fmt.Sprintf("https_proxy=%s", d.httpsProxy)) + } for _, e := range d.env { a = append(a, "-e", e) } @@ -982,7 +988,7 @@ func Run(o Options) error { if err := processTempDir(&o); err != nil { return fmt.Errorf("unable to initialize a local temporary working directory to store intermediate files: %w", err) } - d, err := newDockerRunner(o.ToolchainContainer, o.Cleanup) + d, err := newDockerRunner(o.ToolchainContainer, o.Cleanup, o.HttpsProxy) if err != nil { return fmt.Errorf("failed to initialize a docker container: %w", err) }