Skip to content

Commit 3f9510f

Browse files
authored
Don't use Docker if resource has a YB_CONTAINER_*_IP environment variable set (#315)
[Fixes ch4961]
1 parent 03eed7b commit 3f9510f

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

cmd/yb/helpers.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"os/user"
1313
"path/filepath"
14+
"runtime"
1415
"sort"
1516
"strconv"
1617
"strings"
@@ -20,6 +21,7 @@ import (
2021
"github.com/spf13/pflag"
2122
"github.com/yourbase/yb"
2223
"github.com/yourbase/yb/internal/biome"
24+
"github.com/yourbase/yb/internal/build"
2325
"github.com/yourbase/yb/internal/config"
2426
"github.com/yourbase/yb/internal/ybdata"
2527
"zombiezen.com/go/log"
@@ -249,7 +251,7 @@ func newDockerNetwork(ctx context.Context, client *docker.Client, mode execution
249251
}
250252

251253
func showDockerWarningsIfNeeded(ctx context.Context, mode executionMode, targets []*yb.Target) {
252-
if !willUseDocker(mode, targets) {
254+
if !willUseDocker(mode, targets) || runtime.GOOS != biome.Linux {
253255
return
254256
}
255257
dockerGroup, err := user.LookupGroup("docker")
@@ -281,8 +283,10 @@ func showDockerWarningsIfNeeded(ctx context.Context, mode executionMode, targets
281283

282284
func willUseDocker(mode executionMode, targets []*yb.Target) bool {
283285
for _, target := range targets {
284-
if len(target.Resources) > 0 {
285-
return true
286+
for name := range target.Resources {
287+
if os.Getenv(build.ContainerIPEnvVar(name)) == "" {
288+
return true
289+
}
286290
}
287291
}
288292
return willUseDockerForCommands(mode, targets)
@@ -291,10 +295,14 @@ func willUseDocker(mode executionMode, targets []*yb.Target) bool {
291295
func willUseDockerForCommands(mode executionMode, targets []*yb.Target) bool {
292296
networkAvailable, _ := hostHasDockerNetwork()
293297
for _, target := range targets {
294-
if target.UseContainer ||
295-
len(target.Resources) > 0 && !networkAvailable {
298+
if target.UseContainer {
296299
return true
297300
}
301+
for name := range target.Resources {
302+
if os.Getenv(build.ContainerIPEnvVar(name)) == "" && !networkAvailable {
303+
return true
304+
}
305+
}
298306
}
299307
return mode >= useContainer
300308
}

cmd/yb/helpers_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,25 @@ package main
1818

1919
import (
2020
"fmt"
21+
"os"
2122
"strings"
2223
"testing"
2324

2425
"github.com/yourbase/yb"
2526
)
2627

2728
func TestWillUseDocker(t *testing.T) {
29+
const containerEnvVar = "YB_CONTAINER_ENV_IP"
30+
oldEnv, oldEnvExists := os.LookupEnv(containerEnvVar)
31+
os.Setenv(containerEnvVar, "1.2.3.4")
32+
t.Cleanup(func() {
33+
if !oldEnvExists {
34+
os.Unsetenv(containerEnvVar)
35+
} else {
36+
os.Setenv(containerEnvVar, oldEnv)
37+
}
38+
})
39+
2840
networkAvailable, _ := hostHasDockerNetwork()
2941
tests := []struct {
3042
mode executionMode
@@ -104,6 +116,16 @@ func TestWillUseDocker(t *testing.T) {
104116
want: true,
105117
forCommands: !networkAvailable,
106118
},
119+
{
120+
mode: preferHost,
121+
targets: []*yb.Target{
122+
// This is provided as YB_CONTAINER_ENV_IP.
123+
{Name: "default", UseContainer: false, Resources: map[string]*yb.ResourceDefinition{"env": {}}},
124+
{Name: "foo", UseContainer: false},
125+
},
126+
want: false,
127+
forCommands: false,
128+
},
107129
{
108130
mode: useContainer,
109131
targets: []*yb.Target{

internal/build/setup.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func startContainers(ctx context.Context, sys Sys, defs map[string]*yb.ResourceD
121121
}
122122
containers := make(map[string]*container)
123123
for name := range defs {
124-
ip := os.Getenv("YB_CONTAINER_" + strings.ToUpper(name) + "_IP")
124+
ip := os.Getenv(ContainerIPEnvVar(name))
125125
if ip != "" {
126126
log.Infof(ctx, "Using %s address from environment: %s", name, ip)
127127
exp.ips[name] = ip
@@ -179,6 +179,12 @@ func startContainers(ctx context.Context, sys Sys, defs map[string]*yb.ResourceD
179179
return exp, origCloseFunc, nil
180180
}
181181

182+
// ContainerIPEnvVar returns the name of the environment variable that
183+
// optionally provides the IP address of a target's resource.
184+
func ContainerIPEnvVar(resourceName string) string {
185+
return "YB_CONTAINER_" + strings.ToUpper(resourceName) + "_IP"
186+
}
187+
182188
// startContainer starts a single container with the given definition.
183189
func startContainer(ctx context.Context, sys Sys, resourceName string, cd *yb.ResourceDefinition) (_ *container, err error) {
184190
for _, mount := range cd.Mounts {

0 commit comments

Comments
 (0)