@@ -38,15 +38,29 @@ type ProxyOptions struct {
38
38
// ProxyURL is optional
39
39
ProxyURL * url.URL
40
40
41
+ // Token is optional. If specified, a new workspace proxy region will not be
42
+ // created, and the proxy will become a replica of the existing proxy
43
+ // region.
44
+ Token string
45
+
41
46
// FlushStats is optional
42
47
FlushStats chan chan <- struct {}
43
48
}
44
49
45
- // NewWorkspaceProxy will configure a wsproxy.Server with the given options.
46
- // The new wsproxy will register itself with the given coderd.API instance.
47
- // The first user owner client is required to create the wsproxy on the coderd
48
- // api server.
49
- func NewWorkspaceProxy (t * testing.T , coderdAPI * coderd.API , owner * codersdk.Client , options * ProxyOptions ) * wsproxy.Server {
50
+ type WorkspaceProxy struct {
51
+ * wsproxy.Server
52
+
53
+ ServerURL * url.URL
54
+ }
55
+
56
+ // NewWorkspaceProxyReplica will configure a wsproxy.Server with the given
57
+ // options. The new wsproxy replica will register itself with the given
58
+ // coderd.API instance.
59
+ //
60
+ // If a token is not provided, a new workspace proxy region is created using the
61
+ // owner client. If a token is provided, the proxy will become a replica of the
62
+ // existing proxy region.
63
+ func NewWorkspaceProxyReplica (t * testing.T , coderdAPI * coderd.API , owner * codersdk.Client , options * ProxyOptions ) WorkspaceProxy {
50
64
ctx , cancelFunc := context .WithCancel (context .Background ())
51
65
t .Cleanup (cancelFunc )
52
66
@@ -107,11 +121,15 @@ func NewWorkspaceProxy(t *testing.T, coderdAPI *coderd.API, owner *codersdk.Clie
107
121
options .Name = namesgenerator .GetRandomName (1 )
108
122
}
109
123
110
- proxyRes , err := owner .CreateWorkspaceProxy (ctx , codersdk.CreateWorkspaceProxyRequest {
111
- Name : options .Name ,
112
- Icon : "/emojis/flag.png" ,
113
- })
114
- require .NoError (t , err , "failed to create workspace proxy" )
124
+ token := options .Token
125
+ if token == "" {
126
+ proxyRes , err := owner .CreateWorkspaceProxy (ctx , codersdk.CreateWorkspaceProxyRequest {
127
+ Name : options .Name ,
128
+ Icon : "/emojis/flag.png" ,
129
+ })
130
+ require .NoError (t , err , "failed to create workspace proxy" )
131
+ token = proxyRes .ProxyToken
132
+ }
115
133
116
134
// Inherit collector options from coderd, but keep the wsproxy reporter.
117
135
statsCollectorOptions := coderdAPI .Options .WorkspaceAppsStatsCollectorOptions
@@ -121,7 +139,7 @@ func NewWorkspaceProxy(t *testing.T, coderdAPI *coderd.API, owner *codersdk.Clie
121
139
}
122
140
123
141
wssrv , err := wsproxy .New (ctx , & wsproxy.Options {
124
- Logger : slogtest .Make (t , nil ).Leveled (slog .LevelDebug ),
142
+ Logger : slogtest .Make (t , nil ).Leveled (slog .LevelDebug ). With ( slog . F ( "server_url" , serverURL . String ())) ,
125
143
Experiments : options .Experiments ,
126
144
DashboardURL : coderdAPI .AccessURL ,
127
145
AccessURL : accessURL ,
@@ -131,14 +149,14 @@ func NewWorkspaceProxy(t *testing.T, coderdAPI *coderd.API, owner *codersdk.Clie
131
149
Tracing : coderdAPI .TracerProvider ,
132
150
APIRateLimit : coderdAPI .APIRateLimit ,
133
151
SecureAuthCookie : coderdAPI .SecureAuthCookie ,
134
- ProxySessionToken : proxyRes . ProxyToken ,
152
+ ProxySessionToken : token ,
135
153
DisablePathApps : options .DisablePathApps ,
136
154
// We need a new registry to not conflict with the coderd internal
137
155
// proxy metrics.
138
156
PrometheusRegistry : prometheus .NewRegistry (),
139
157
DERPEnabled : ! options .DerpDisabled ,
140
158
DERPOnly : options .DerpOnly ,
141
- DERPServerRelayAddress : accessURL .String (),
159
+ DERPServerRelayAddress : serverURL .String (),
142
160
StatsCollectorOptions : statsCollectorOptions ,
143
161
})
144
162
require .NoError (t , err )
@@ -151,5 +169,8 @@ func NewWorkspaceProxy(t *testing.T, coderdAPI *coderd.API, owner *codersdk.Clie
151
169
handler = wssrv .Handler
152
170
mutex .Unlock ()
153
171
154
- return wssrv
172
+ return WorkspaceProxy {
173
+ Server : wssrv ,
174
+ ServerURL : serverURL ,
175
+ }
155
176
}
0 commit comments