From 3f9510fb390f197ed7d314cb9ad2744c83160e1f Mon Sep 17 00:00:00 2001 From: Ross Light Date: Wed, 2 Jun 2021 11:28:36 -0700 Subject: [PATCH] Don't use Docker if resource has a `YB_CONTAINER_*_IP` environment variable set (#315) [Fixes ch4961] --- cmd/yb/helpers.go | 18 +++++++++++++----- cmd/yb/helpers_test.go | 22 ++++++++++++++++++++++ internal/build/setup.go | 8 +++++++- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/cmd/yb/helpers.go b/cmd/yb/helpers.go index a669a0be..297128ed 100644 --- a/cmd/yb/helpers.go +++ b/cmd/yb/helpers.go @@ -11,6 +11,7 @@ import ( "os" "os/user" "path/filepath" + "runtime" "sort" "strconv" "strings" @@ -20,6 +21,7 @@ import ( "github.com/spf13/pflag" "github.com/yourbase/yb" "github.com/yourbase/yb/internal/biome" + "github.com/yourbase/yb/internal/build" "github.com/yourbase/yb/internal/config" "github.com/yourbase/yb/internal/ybdata" "zombiezen.com/go/log" @@ -249,7 +251,7 @@ func newDockerNetwork(ctx context.Context, client *docker.Client, mode execution } func showDockerWarningsIfNeeded(ctx context.Context, mode executionMode, targets []*yb.Target) { - if !willUseDocker(mode, targets) { + if !willUseDocker(mode, targets) || runtime.GOOS != biome.Linux { return } dockerGroup, err := user.LookupGroup("docker") @@ -281,8 +283,10 @@ func showDockerWarningsIfNeeded(ctx context.Context, mode executionMode, targets func willUseDocker(mode executionMode, targets []*yb.Target) bool { for _, target := range targets { - if len(target.Resources) > 0 { - return true + for name := range target.Resources { + if os.Getenv(build.ContainerIPEnvVar(name)) == "" { + return true + } } } return willUseDockerForCommands(mode, targets) @@ -291,10 +295,14 @@ func willUseDocker(mode executionMode, targets []*yb.Target) bool { func willUseDockerForCommands(mode executionMode, targets []*yb.Target) bool { networkAvailable, _ := hostHasDockerNetwork() for _, target := range targets { - if target.UseContainer || - len(target.Resources) > 0 && !networkAvailable { + if target.UseContainer { return true } + for name := range target.Resources { + if os.Getenv(build.ContainerIPEnvVar(name)) == "" && !networkAvailable { + return true + } + } } return mode >= useContainer } diff --git a/cmd/yb/helpers_test.go b/cmd/yb/helpers_test.go index 7d8e2f09..6f856412 100644 --- a/cmd/yb/helpers_test.go +++ b/cmd/yb/helpers_test.go @@ -18,6 +18,7 @@ package main import ( "fmt" + "os" "strings" "testing" @@ -25,6 +26,17 @@ import ( ) func TestWillUseDocker(t *testing.T) { + const containerEnvVar = "YB_CONTAINER_ENV_IP" + oldEnv, oldEnvExists := os.LookupEnv(containerEnvVar) + os.Setenv(containerEnvVar, "1.2.3.4") + t.Cleanup(func() { + if !oldEnvExists { + os.Unsetenv(containerEnvVar) + } else { + os.Setenv(containerEnvVar, oldEnv) + } + }) + networkAvailable, _ := hostHasDockerNetwork() tests := []struct { mode executionMode @@ -104,6 +116,16 @@ func TestWillUseDocker(t *testing.T) { want: true, forCommands: !networkAvailable, }, + { + mode: preferHost, + targets: []*yb.Target{ + // This is provided as YB_CONTAINER_ENV_IP. + {Name: "default", UseContainer: false, Resources: map[string]*yb.ResourceDefinition{"env": {}}}, + {Name: "foo", UseContainer: false}, + }, + want: false, + forCommands: false, + }, { mode: useContainer, targets: []*yb.Target{ diff --git a/internal/build/setup.go b/internal/build/setup.go index 9475409f..79527b9d 100644 --- a/internal/build/setup.go +++ b/internal/build/setup.go @@ -121,7 +121,7 @@ func startContainers(ctx context.Context, sys Sys, defs map[string]*yb.ResourceD } containers := make(map[string]*container) for name := range defs { - ip := os.Getenv("YB_CONTAINER_" + strings.ToUpper(name) + "_IP") + ip := os.Getenv(ContainerIPEnvVar(name)) if ip != "" { log.Infof(ctx, "Using %s address from environment: %s", name, ip) exp.ips[name] = ip @@ -179,6 +179,12 @@ func startContainers(ctx context.Context, sys Sys, defs map[string]*yb.ResourceD return exp, origCloseFunc, nil } +// ContainerIPEnvVar returns the name of the environment variable that +// optionally provides the IP address of a target's resource. +func ContainerIPEnvVar(resourceName string) string { + return "YB_CONTAINER_" + strings.ToUpper(resourceName) + "_IP" +} + // startContainer starts a single container with the given definition. func startContainer(ctx context.Context, sys Sys, resourceName string, cd *yb.ResourceDefinition) (_ *container, err error) { for _, mount := range cd.Mounts {