diff --git a/tools/sgcloudspanner/emulator.go b/tools/sgcloudspanner/emulator.go index fa046d2d..9ef868e9 100644 --- a/tools/sgcloudspanner/emulator.go +++ b/tools/sgcloudspanner/emulator.go @@ -3,11 +3,9 @@ package sgcloudspanner import ( "bytes" "context" - "encoding/json" "fmt" "net" "os" - "os/exec" "strings" "time" @@ -18,10 +16,9 @@ import ( // Cloud Spanner Emulator versions can be found here, // https://console.cloud.google.com/gcr/images/cloud-spanner-emulator/global/emulator const ( - cloudbuildNetwork = "cloudbuild" - url = "gcr.io/cloud-spanner-emulator/emulator" - version = "1.5.17" - image = url + ":" + version + url = "gcr.io/cloud-spanner-emulator/emulator" + version = "1.5.17" + image = url + ":" + version ) // RunEmulator runs the Cloud Spanner emulator in Docker. @@ -39,12 +36,7 @@ func RunEmulator(ctx context.Context) (_ func(), err error) { if !isDockerDaemonRunning(ctx) { return nil, fmt.Errorf("the Docker daemon does not seem to be running") } - var dockerRunCmd *exec.Cmd - if isRunningOnCloudBuild(ctx) { - dockerRunCmd = sgdocker.Command(ctx, "run", "-d", "--network", cloudbuildNetwork, "--publish-all", image) - } else { - dockerRunCmd = sgdocker.Command(ctx, "run", "-d", "--publish-all", image) - } + dockerRunCmd := sgdocker.Command(ctx, "run", "-d", "--publish-all", image) var dockerRunStdout strings.Builder dockerRunCmd.Stdout = &dockerRunStdout if err := dockerRunCmd.Run(); err != nil { @@ -67,19 +59,10 @@ func RunEmulator(ctx context.Context) (_ func(), err error) { sg.Logger(ctx).Printf("failed to unset SPANNER_EMULATOR_HOST: %v", err) } } - var emulatorHost string - if isRunningOnCloudBuild(ctx) { - emulatorHost, err = inspectPortAddressCloudbuild(ctx, containerID, "9010/tcp", image) - if err != nil { - cleanup() - return nil, err - } - } else { - emulatorHost, err = inspectPortAddress(ctx, containerID, "9010/tcp") - if err != nil { - cleanup() - return nil, err - } + emulatorHost, err := inspectPortAddress(ctx, containerID, "9010/tcp") + if err != nil { + cleanup() + return nil, err } if err := os.Setenv("SPANNER_EMULATOR_HOST", emulatorHost); err != nil { cleanup() @@ -118,45 +101,6 @@ func inspectPortAddress(ctx context.Context, containerID, containerPort string) return "", fmt.Errorf("no mapping found for %s in container %s", containerPort, containerID) } -func inspectPortAddressCloudbuild(ctx context.Context, containerID, containerPort, image string) (string, error) { - var stdout bytes.Buffer - cmd := sgdocker.Command(ctx, "inspect", containerID) - cmd.Stdout = &stdout - if err := cmd.Run(); err != nil { - return "", err - } - var containers []struct { - Config struct { - Image string - } - NetworkSettings struct { - Ports map[string][]struct { - HostPort string - } - Networks map[string]struct { - Gateway string - } - } - } - if err := json.NewDecoder(&stdout).Decode(&containers); err != nil { - return "", err - } - var host, port string - - for _, container := range containers { - if container.Config.Image == image { - port = container.NetworkSettings.Ports[containerPort][0].HostPort // prefer first option - } - if network, ok := container.NetworkSettings.Networks[cloudbuildNetwork]; ok { - host = network.Gateway - } - } - if host == "" || port == "" { - return "", fmt.Errorf("failed to inspect container %s for port %s", containerID, containerPort) - } - return fmt.Sprintf("%s:%s", host, port), nil -} - func awaitReachable(ctx context.Context, addr string, wait, maxWait time.Duration) error { deadline := time.Now().Add(maxWait) for time.Now().Before(deadline) { @@ -173,10 +117,3 @@ func awaitReachable(ctx context.Context, addr string, wait, maxWait time.Duratio } return fmt.Errorf("%s was unreachable for %v", addr, maxWait) } - -func isRunningOnCloudBuild(ctx context.Context) bool { - cmd := sgdocker.Command(ctx, "network", "inspect", cloudbuildNetwork) - var stdout bytes.Buffer - cmd.Stdout, cmd.Stderr = &stdout, nil - return cmd.Run() == nil -}