Skip to content

Commit

Permalink
feat(cloud-spanner): remove special handling of Cloud Build
Browse files Browse the repository at this point in the history
No longer supported.
  • Loading branch information
odsod committed Sep 17, 2024
1 parent fdbf8a4 commit 6b1ac61
Showing 1 changed file with 8 additions and 71 deletions.
79 changes: 8 additions & 71 deletions tools/sgcloudspanner/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package sgcloudspanner
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"os"
"os/exec"
"strings"
"time"

Expand All @@ -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.
Expand All @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

0 comments on commit 6b1ac61

Please sign in to comment.