Run implements Runnable.
(ctx context.Context, _ string, w io.Writer)
| 46 | |
| 47 | // Run implements Runnable. |
| 48 | func (r *Runner) Run(ctx context.Context, _ string, w io.Writer) error { |
| 49 | ctx, span := tracing.StartSpan(ctx) |
| 50 | defer span.End() |
| 51 | |
| 52 | logs := loadtestutil.NewSyncWriter(w) |
| 53 | defer logs.Close() |
| 54 | logger := slog.Make(sloghuman.Sink(logs)).Leveled(slog.LevelDebug) |
| 55 | r.client.SetLogger(logger) |
| 56 | r.client.SetLogBodies(true) |
| 57 | |
| 58 | _, _ = fmt.Fprintln(logs, "Opening connection to workspace agent") |
| 59 | switch r.cfg.ConnectionMode { |
| 60 | case ConnectionModeDirect: |
| 61 | _, _ = fmt.Fprintln(logs, "\tUsing direct connection...") |
| 62 | case ConnectionModeDerp: |
| 63 | _, _ = fmt.Fprintln(logs, "\tUsing proxied DERP connection through coder server...") |
| 64 | } |
| 65 | |
| 66 | conn, err := workspacesdk.New(r.client). |
| 67 | DialAgent(ctx, r.cfg.AgentID, &workspacesdk.DialAgentOptions{ |
| 68 | Logger: logger.Named("agentconn"), |
| 69 | // If the config requested DERP, then force DERP. |
| 70 | BlockEndpoints: r.cfg.ConnectionMode == ConnectionModeDerp, |
| 71 | }) |
| 72 | if err != nil { |
| 73 | return xerrors.Errorf("dial workspace agent: %w", err) |
| 74 | } |
| 75 | defer conn.Close() |
| 76 | |
| 77 | err = waitForDisco(ctx, logs, conn) |
| 78 | if err != nil { |
| 79 | return xerrors.Errorf("wait for discovery connection: %w", err) |
| 80 | } |
| 81 | |
| 82 | // Wait for a direct connection if requested. |
| 83 | if r.cfg.ConnectionMode == ConnectionModeDirect { |
| 84 | err = waitForDirectConnection(ctx, logs, conn) |
| 85 | if err != nil { |
| 86 | return xerrors.Errorf("wait for direct connection: %w", err) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Ensure DERP for completeness. |
| 91 | if r.cfg.ConnectionMode == ConnectionModeDerp { |
| 92 | status := conn.TailnetConn().Status() |
| 93 | if len(status.Peers()) != 1 { |
| 94 | return xerrors.Errorf("check connection mode: expected 1 peer, got %d", len(status.Peers())) |
| 95 | } |
| 96 | peer := status.Peer[status.Peers()[0]] |
| 97 | if peer.Relay == "" || peer.CurAddr != "" { |
| 98 | return xerrors.Errorf("check connection mode: peer is connected directly, not via DERP") |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | _, _ = fmt.Fprint(logs, "\nConnection established.\n\n") |
| 103 | |
| 104 | // HACK: even though the ping passed above, we still need to open a |
| 105 | // connection to the agent to ensure it's ready to accept connections. Not |