Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit46b2f3d

Browse files
authored
fix(cli): allow disabling debug listening ports for agent (#20671)
A customer reported unexpected port allocation in their workspace. Whenlooking into it I noticed we always hijack these ports and there is no way to disable them entirely.This change allows the servers to be disabled by setting them to theempty string. Previously they would still listen on ephemeral ports.```console❯ coder agent --help | grep -E '211[2-3]|6060' --debug-address string, $CODER_AGENT_DEBUG_ADDRESS (default: 127.0.0.1:2113) --pprof-address string, $CODER_AGENT_PPROF_ADDRESS (default: 127.0.0.1:6060) --prometheus-address string, $CODER_AGENT_PROMETHEUS_ADDRESS (default: 127.0.0.1:2112)```There are now two ways to disable, either via CLI or env variables:```console# Flags.coder agent --debug-address= --pprof-address= --prometheus-address=# Environment variables.export CODER_AGENT_DEBUG_ADDRESS=export CODER_AGENT_PPROF_ADDRESS=export CODER_AGENT_PROMETHEUS_ADDRESS=coder agent```
1 parentdec2c4c commit46b2f3d

File tree

2 files changed

+90
-17
lines changed

2 files changed

+90
-17
lines changed

‎cli/agent.go‎

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313
"runtime"
14+
"slices"
1415
"strconv"
1516
"strings"
1617
"time"
@@ -201,18 +202,15 @@ func workspaceAgent() *serpent.Command {
201202
// Enable pprof handler
202203
// This prevents the pprof import from being accidentally deleted.
203204
_=pprof.Handler
204-
pprofSrvClose:=ServeHandler(ctx,logger,nil,pprofAddress,"pprof")
205-
deferpprofSrvClose()
206-
ifport,err:=extractPort(pprofAddress);err==nil {
207-
ignorePorts[port]="pprof"
208-
}
209-
210-
ifport,err:=extractPort(prometheusAddress);err==nil {
211-
ignorePorts[port]="prometheus"
212-
}
205+
ifpprofAddress!="" {
206+
pprofSrvClose:=ServeHandler(ctx,logger,nil,pprofAddress,"pprof")
207+
deferpprofSrvClose()
213208

214-
ifport,err:=extractPort(debugAddress);err==nil {
215-
ignorePorts[port]="debug"
209+
ifport,err:=extractPort(pprofAddress);err==nil {
210+
ignorePorts[port]="pprof"
211+
}
212+
}else {
213+
logger.Debug(ctx,"pprof address is empty, disabling pprof server")
216214
}
217215

218216
executablePath,err:=os.Executable()
@@ -276,6 +274,28 @@ func workspaceAgent() *serpent.Command {
276274
for {
277275
prometheusRegistry:=prometheus.NewRegistry()
278276

277+
promHandler:=agent.PrometheusMetricsHandler(prometheusRegistry,logger)
278+
varserverClose []func()
279+
ifprometheusAddress!="" {
280+
prometheusSrvClose:=ServeHandler(ctx,logger,promHandler,prometheusAddress,"prometheus")
281+
serverClose=append(serverClose,prometheusSrvClose)
282+
283+
ifport,err:=extractPort(prometheusAddress);err==nil {
284+
ignorePorts[port]="prometheus"
285+
}
286+
}else {
287+
logger.Debug(ctx,"prometheus address is empty, disabling prometheus server")
288+
}
289+
290+
ifdebugAddress!="" {
291+
// ServerHandle depends on `agnt.HTTPDebug()`, but `agnt`
292+
// depends on `ignorePorts`. Keep this if statement in sync
293+
// with below.
294+
ifport,err:=extractPort(debugAddress);err==nil {
295+
ignorePorts[port]="debug"
296+
}
297+
}
298+
279299
agnt:=agent.New(agent.Options{
280300
Client:client,
281301
Logger:logger,
@@ -299,10 +319,15 @@ func workspaceAgent() *serpent.Command {
299319
},
300320
})
301321

302-
promHandler:=agent.PrometheusMetricsHandler(prometheusRegistry,logger)
303-
prometheusSrvClose:=ServeHandler(ctx,logger,promHandler,prometheusAddress,"prometheus")
304-
305-
debugSrvClose:=ServeHandler(ctx,logger,agnt.HTTPDebug(),debugAddress,"debug")
322+
ifdebugAddress!="" {
323+
// ServerHandle depends on `agnt.HTTPDebug()`, but `agnt`
324+
// depends on `ignorePorts`. Keep this if statement in sync
325+
// with above.
326+
debugSrvClose:=ServeHandler(ctx,logger,agnt.HTTPDebug(),debugAddress,"debug")
327+
serverClose=append(serverClose,debugSrvClose)
328+
}else {
329+
logger.Debug(ctx,"debug address is empty, disabling debug server")
330+
}
306331

307332
select {
308333
case<-ctx.Done():
@@ -314,8 +339,11 @@ func workspaceAgent() *serpent.Command {
314339
}
315340

316341
lastErr=agnt.Close()
317-
debugSrvClose()
318-
prometheusSrvClose()
342+
343+
slices.Reverse(serverClose)
344+
for_,closeFunc:=rangeserverClose {
345+
closeFunc()
346+
}
319347

320348
ifmustExit {
321349
break

‎cli/agent_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,51 @@ func TestWorkspaceAgent(t *testing.T) {
178178
require.Greater(t,atomic.LoadInt64(&called),int64(0),"expected coderd to be reached with custom headers")
179179
require.Greater(t,atomic.LoadInt64(&derpCalled),int64(0),"expected /derp to be called with custom headers")
180180
})
181+
182+
t.Run("DisabledServers",func(t*testing.T) {
183+
t.Parallel()
184+
185+
client,db:=coderdtest.NewWithDatabase(t,nil)
186+
user:=coderdtest.CreateFirstUser(t,client)
187+
r:=dbfake.WorkspaceBuild(t,db, database.WorkspaceTable{
188+
OrganizationID:user.OrganizationID,
189+
OwnerID:user.UserID,
190+
}).WithAgent().Do()
191+
192+
logDir:=t.TempDir()
193+
inv,_:=clitest.New(t,
194+
"agent",
195+
"--auth","token",
196+
"--agent-token",r.AgentToken,
197+
"--agent-url",client.URL.String(),
198+
"--log-dir",logDir,
199+
"--pprof-address","",
200+
"--prometheus-address","",
201+
"--debug-address","",
202+
)
203+
204+
clitest.Start(t,inv)
205+
206+
// Verify the agent is connected and working.
207+
resources:=coderdtest.NewWorkspaceAgentWaiter(t,client,r.Workspace.ID).
208+
MatchResources(matchAgentWithVersion).Wait()
209+
require.Len(t,resources,1)
210+
require.Len(t,resources[0].Agents,1)
211+
require.NotEmpty(t,resources[0].Agents[0].Version)
212+
213+
// Verify the servers are not listening by checking the log for disabled
214+
// messages.
215+
require.Eventually(t,func()bool {
216+
logContent,err:=os.ReadFile(filepath.Join(logDir,"coder-agent.log"))
217+
iferr!=nil {
218+
returnfalse
219+
}
220+
logStr:=string(logContent)
221+
returnstrings.Contains(logStr,"pprof address is empty, disabling pprof server")&&
222+
strings.Contains(logStr,"prometheus address is empty, disabling prometheus server")&&
223+
strings.Contains(logStr,"debug address is empty, disabling debug server")
224+
},testutil.WaitLong,testutil.IntervalMedium)
225+
})
181226
}
182227

183228
funcmatchAgentWithVersion(rs []codersdk.WorkspaceResource)bool {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp