mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 08:23:45 +00:00
fix: propagate allocated transport ports
This commit is contained in:
@@ -30,6 +30,7 @@ type Config struct {
|
||||
Environment []string
|
||||
SDKBaseURL string
|
||||
ReadyURL string
|
||||
Transport string
|
||||
ReadyTimeout time.Duration
|
||||
PollInterval time.Duration
|
||||
HTTPClient *http.Client
|
||||
@@ -51,6 +52,12 @@ func New(config Config) (*Supervisor, error) {
|
||||
if config.PollInterval <= 0 {
|
||||
config.PollInterval = 100 * time.Millisecond
|
||||
}
|
||||
if config.Transport == "" {
|
||||
config.Transport = "enet"
|
||||
}
|
||||
if config.Transport != "enet" && config.Transport != "steam_sdr" {
|
||||
return nil, fmt.Errorf("unsupported transport %q", config.Transport)
|
||||
}
|
||||
if config.HTTPClient == nil {
|
||||
config.HTTPClient = http.DefaultClient
|
||||
}
|
||||
@@ -68,9 +75,14 @@ func (s *Supervisor) Start(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
env = append(env, "SDR_LISTEN_PORT="+strconv.Itoa(port), "SDR_IP="+address+":"+strconv.Itoa(port))
|
||||
if s.config.Transport == "steam_sdr" {
|
||||
env = append(env, "SDR_LISTEN_PORT="+strconv.Itoa(port), "SDR_IP="+address+":"+strconv.Itoa(port))
|
||||
}
|
||||
command := withPort(s.config.Command, port)
|
||||
s.cmd = exec.CommandContext(ctx, command[0], command[1:]...)
|
||||
} else {
|
||||
s.cmd = exec.CommandContext(ctx, s.config.Command[0], s.config.Command[1:]...)
|
||||
}
|
||||
s.cmd = exec.CommandContext(ctx, s.config.Command[0], s.config.Command[1:]...)
|
||||
s.cmd.Env = env
|
||||
if err := s.cmd.Start(); err != nil {
|
||||
return err
|
||||
@@ -85,6 +97,17 @@ func (s *Supervisor) Start(ctx context.Context) error {
|
||||
return s.sdkPost(ctx, "/ready")
|
||||
}
|
||||
|
||||
func withPort(command []string, port int) []string {
|
||||
result := append([]string(nil), command...)
|
||||
for i, arg := range result {
|
||||
if strings.HasPrefix(arg, "--port=") {
|
||||
result[i] = "--port=" + strconv.Itoa(port)
|
||||
return result
|
||||
}
|
||||
}
|
||||
return append(result, "--port="+strconv.Itoa(port))
|
||||
}
|
||||
|
||||
func (s *Supervisor) Wait() error {
|
||||
if s.cmd == nil {
|
||||
return fmt.Errorf("supervisor has not started")
|
||||
|
||||
@@ -35,8 +35,9 @@ func TestAllocatedStartInjectsDynamicEndpointAndCallsReadyAfterProbe(t *testing.
|
||||
|
||||
ready = true
|
||||
path := filepath.Join(t.TempDir(), "env.txt")
|
||||
command := []string{"/bin/sh", "-c", "env > " + path}
|
||||
s, err := New(Config{Command: command, SDKBaseURL: server.URL, ReadyURL: server.URL + "/ready-probe", ReadyTimeout: time.Second, PollInterval: time.Millisecond})
|
||||
argsPath := filepath.Join(t.TempDir(), "args.txt")
|
||||
command := []string{"/bin/sh", "-c", "env > " + path + "; printf '%s' \"$@\" > " + argsPath, "shell"}
|
||||
s, err := New(Config{Command: command, SDKBaseURL: server.URL, ReadyURL: server.URL + "/ready-probe", Transport: "steam_sdr", ReadyTimeout: time.Second, PollInterval: time.Millisecond})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -53,11 +54,51 @@ func TestAllocatedStartInjectsDynamicEndpointAndCallsReadyAfterProbe(t *testing.
|
||||
if !strings.Contains(string(contents), "SDR_LISTEN_PORT=31001") || !strings.Contains(string(contents), "SDR_IP=203.0.113.9:31001") {
|
||||
t.Fatalf("dynamic endpoint not injected: %s", contents)
|
||||
}
|
||||
args, err := os.ReadFile(argsPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(args), "--port=31001") {
|
||||
t.Fatalf("dynamic port argument not injected: %s", args)
|
||||
}
|
||||
if !readyCalled {
|
||||
t.Fatal("Agones Ready was called before process-ready probe")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllocatedENetDoesNotReceiveSDRVariables(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/gameserver" {
|
||||
_, _ = w.Write([]byte(`{"status":{"address":"127.0.0.1","ports":[{"name":"game","port":31002}]}}`))
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/ready-probe" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
path := filepath.Join(t.TempDir(), "env.txt")
|
||||
s, err := New(Config{Command: []string{"/bin/sh", "-c", "env > " + path}, SDKBaseURL: server.URL, ReadyURL: server.URL + "/ready-probe", Transport: "enet", ReadyTimeout: time.Second})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Start(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Wait(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
contents, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(contents), "SDR_LISTEN_PORT=") || strings.Contains(string(contents), "SDR_IP=") {
|
||||
t.Fatalf("ENet received SDR variables: %s", contents)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectModeDoesNotRequireAgonesReadiness(t *testing.T) {
|
||||
s, err := New(Config{Command: []string{"/bin/sh", "-c", "exit 0"}})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user