Skip to content

Commit b08e633

Browse files
committed
Merge branch 'main' of github.com:/coder/coder into dk/cliq
2 parents 4df8a3b + 53b58ed commit b08e633

File tree

230 files changed

+5906
-7237
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

230 files changed

+5906
-7237
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ jobs:
142142
143143
# Check for any typos
144144
- name: Check for typos
145-
uses: crate-ci/typos@v1.18.2
145+
uses: crate-ci/typos@v1.19.0
146146
with:
147147
config: .github/workflows/typos.toml
148148

@@ -155,7 +155,7 @@ jobs:
155155
156156
# Needed for helm chart linting
157157
- name: Install helm
158-
uses: azure/setup-helm@v3
158+
uses: azure/setup-helm@v4
159159
with:
160160
version: v3.9.2
161161

.github/workflows/security.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ jobs:
114114
echo "image=$(cat "$image_job")" >> $GITHUB_OUTPUT
115115
116116
- name: Run Trivy vulnerability scanner
117-
uses: aquasecurity/trivy-action@84384bd6e777ef152729993b8145ea352e9dd3ef
117+
uses: aquasecurity/trivy-action@062f2592684a31eb3aa050cc61e7ca1451cecd3d
118118
with:
119119
image-ref: ${{ steps.build.outputs.image }}
120120
format: sarif

agent/agent.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/go-chi/chi/v5"
2626
"github.com/google/uuid"
2727
"github.com/prometheus/client_golang/prometheus"
28+
"github.com/prometheus/common/expfmt"
2829
"github.com/spf13/afero"
2930
"go.uber.org/atomic"
3031
"golang.org/x/exp/slices"
@@ -34,6 +35,7 @@ import (
3435
"tailscale.com/net/speedtest"
3536
"tailscale.com/tailcfg"
3637
"tailscale.com/types/netlogtype"
38+
"tailscale.com/util/clientmetric"
3739

3840
"cdr.dev/slog"
3941
"github.com/coder/retry"
@@ -1714,18 +1716,6 @@ func (a *agent) HandleHTTPDebugManifest(w http.ResponseWriter, r *http.Request)
17141716
}
17151717
}
17161718

1717-
func (a *agent) HandleHTTPDebugToken(w http.ResponseWriter, r *http.Request) {
1718-
tok := a.sessionToken.Load()
1719-
if tok == nil {
1720-
a.logger.Error(r.Context(), "no session token in-memory")
1721-
w.WriteHeader(http.StatusInternalServerError)
1722-
_, _ = fmt.Fprintf(w, "no session token in-memory")
1723-
return
1724-
}
1725-
w.WriteHeader(http.StatusOK)
1726-
_, _ = fmt.Fprintf(w, *tok)
1727-
}
1728-
17291719
func (a *agent) HandleHTTPDebugLogs(w http.ResponseWriter, r *http.Request) {
17301720
logPath := filepath.Join(a.logDir, "coder-agent.log")
17311721
f, err := os.Open(logPath)
@@ -1753,7 +1743,6 @@ func (a *agent) HTTPDebug() http.Handler {
17531743
r.Get("/debug/magicsock", a.HandleHTTPDebugMagicsock)
17541744
r.Get("/debug/magicsock/debug-logging/{state}", a.HandleHTTPMagicsockDebugLoggingState)
17551745
r.Get("/debug/manifest", a.HandleHTTPDebugManifest)
1756-
r.Get("/debug/token", a.HandleHTTPDebugToken)
17571746
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
17581747
w.WriteHeader(http.StatusNotFound)
17591748
_, _ = w.Write([]byte("404 not found"))
@@ -1993,3 +1982,26 @@ func (a *apiConnRoutineManager) start(name string, b gracefulShutdownBehavior, f
19931982
func (a *apiConnRoutineManager) wait() error {
19941983
return a.eg.Wait()
19951984
}
1985+
1986+
func PrometheusMetricsHandler(prometheusRegistry *prometheus.Registry, logger slog.Logger) http.Handler {
1987+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1988+
w.Header().Set("Content-Type", "text/plain")
1989+
1990+
// Based on: https://github.com/tailscale/tailscale/blob/280255acae604796a1113861f5a84e6fa2dc6121/ipn/localapi/localapi.go#L489
1991+
clientmetric.WritePrometheusExpositionFormat(w)
1992+
1993+
metricFamilies, err := prometheusRegistry.Gather()
1994+
if err != nil {
1995+
logger.Error(context.Background(), "prometheus handler failed to gather metric families", slog.Error(err))
1996+
return
1997+
}
1998+
1999+
for _, metricFamily := range metricFamilies {
2000+
_, err = expfmt.MetricFamilyToText(w, metricFamily)
2001+
if err != nil {
2002+
logger.Error(context.Background(), "expfmt.MetricFamilyToText failed", slog.Error(err))
2003+
return
2004+
}
2005+
}
2006+
})
2007+
}

agent/agent_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,22 +2088,6 @@ func TestAgent_DebugServer(t *testing.T) {
20882088
require.NotNil(t, v)
20892089
})
20902090

2091-
t.Run("Token", func(t *testing.T) {
2092-
t.Parallel()
2093-
2094-
ctx := testutil.Context(t, testutil.WaitLong)
2095-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+"/debug/token", nil)
2096-
require.NoError(t, err)
2097-
2098-
res, err := srv.Client().Do(req)
2099-
require.NoError(t, err)
2100-
require.Equal(t, http.StatusOK, res.StatusCode)
2101-
defer res.Body.Close()
2102-
resBody, err := io.ReadAll(res.Body)
2103-
require.NoError(t, err)
2104-
require.Equal(t, "token", string(resBody))
2105-
})
2106-
21072091
t.Run("Logs", func(t *testing.T) {
21082092
t.Parallel()
21092093

agent/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ func (a *agent) apiHandler() http.Handler {
3535
ignorePorts: cpy,
3636
cacheDuration: cacheDuration,
3737
}
38+
promHandler := PrometheusMetricsHandler(a.prometheusRegistry, a.logger)
3839
r.Get("/api/v0/listening-ports", lp.handler)
3940
r.Get("/debug/logs", a.HandleHTTPDebugLogs)
4041
r.Get("/debug/magicsock", a.HandleHTTPDebugMagicsock)
4142
r.Get("/debug/magicsock/debug-logging/{state}", a.HandleHTTPMagicsockDebugLoggingState)
4243
r.Get("/debug/manifest", a.HandleHTTPDebugManifest)
43-
r.Get("/debug/token", a.HandleHTTPDebugToken)
44+
r.Get("/debug/prometheus", promHandler.ServeHTTP)
4445

4546
return r
4647
}

cli/agent.go

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import (
1818
"cloud.google.com/go/compute/metadata"
1919
"golang.org/x/xerrors"
2020
"gopkg.in/natefinch/lumberjack.v2"
21-
"tailscale.com/util/clientmetric"
2221

2322
"github.com/prometheus/client_golang/prometheus"
24-
"github.com/prometheus/common/expfmt"
2523

2624
"cdr.dev/slog"
2725
"cdr.dev/slog/sloggers/sloghuman"
@@ -31,12 +29,12 @@ import (
3129
"github.com/coder/coder/v2/agent/agentproc"
3230
"github.com/coder/coder/v2/agent/reaper"
3331
"github.com/coder/coder/v2/buildinfo"
34-
"github.com/coder/coder/v2/cli/clibase"
3532
"github.com/coder/coder/v2/codersdk"
3633
"github.com/coder/coder/v2/codersdk/agentsdk"
34+
"github.com/coder/serpent"
3735
)
3836

39-
func (r *RootCmd) workspaceAgent() *clibase.Cmd {
37+
func (r *RootCmd) workspaceAgent() *serpent.Command {
4038
var (
4139
auth string
4240
logDir string
@@ -51,12 +49,12 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
5149
slogJSONPath string
5250
slogStackdriverPath string
5351
)
54-
cmd := &clibase.Cmd{
52+
cmd := &serpent.Command{
5553
Use: "agent",
5654
Short: `Starts the Coder workspace agent.`,
5755
// This command isn't useful to manually execute.
5856
Hidden: true,
59-
Handler: func(inv *clibase.Invocation) error {
57+
Handler: func(inv *serpent.Invocation) error {
6058
ctx, cancel := context.WithCancel(inv.Context())
6159
defer cancel()
6260

@@ -125,7 +123,7 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
125123
args := append(os.Args, "--no-reap")
126124
err := reaper.ForkReap(
127125
reaper.WithExecArgs(args...),
128-
reaper.WithCatchSignals(InterruptSignals...),
126+
reaper.WithCatchSignals(StopSignals...),
129127
)
130128
if err != nil {
131129
logger.Error(ctx, "agent process reaper unable to fork", slog.Error(err))
@@ -144,7 +142,7 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
144142
// Note that we don't want to handle these signals in the
145143
// process that runs as PID 1, that's why we do this after
146144
// the reaper forked.
147-
ctx, stopNotify := inv.SignalNotifyContext(ctx, InterruptSignals...)
145+
ctx, stopNotify := inv.SignalNotifyContext(ctx, StopSignals...)
148146
defer stopNotify()
149147

150148
// DumpHandler does signal handling, so we call it after the
@@ -315,7 +313,8 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
315313
ModifiedProcesses: nil,
316314
})
317315

318-
prometheusSrvClose := ServeHandler(ctx, logger, prometheusMetricsHandler(prometheusRegistry, logger), prometheusAddress, "prometheus")
316+
promHandler := agent.PrometheusMetricsHandler(prometheusRegistry, logger)
317+
prometheusSrvClose := ServeHandler(ctx, logger, promHandler, prometheusAddress, "prometheus")
319318
defer prometheusSrvClose()
320319

321320
debugSrvClose := ServeHandler(ctx, logger, agnt.HTTPDebug(), debugAddress, "debug")
@@ -326,69 +325,69 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
326325
},
327326
}
328327

329-
cmd.Options = clibase.OptionSet{
328+
cmd.Options = serpent.OptionSet{
330329
{
331330
Flag: "auth",
332331
Default: "token",
333332
Description: "Specify the authentication type to use for the agent.",
334333
Env: "CODER_AGENT_AUTH",
335-
Value: clibase.StringOf(&auth),
334+
Value: serpent.StringOf(&auth),
336335
},
337336
{
338337
Flag: "log-dir",
339338
Default: os.TempDir(),
340339
Description: "Specify the location for the agent log files.",
341340
Env: "CODER_AGENT_LOG_DIR",
342-
Value: clibase.StringOf(&logDir),
341+
Value: serpent.StringOf(&logDir),
343342
},
344343
{
345344
Flag: "script-data-dir",
346345
Default: os.TempDir(),
347346
Description: "Specify the location for storing script data.",
348347
Env: "CODER_AGENT_SCRIPT_DATA_DIR",
349-
Value: clibase.StringOf(&scriptDataDir),
348+
Value: serpent.StringOf(&scriptDataDir),
350349
},
351350
{
352351
Flag: "pprof-address",
353352
Default: "127.0.0.1:6060",
354353
Env: "CODER_AGENT_PPROF_ADDRESS",
355-
Value: clibase.StringOf(&pprofAddress),
354+
Value: serpent.StringOf(&pprofAddress),
356355
Description: "The address to serve pprof.",
357356
},
358357
{
359358
Flag: "no-reap",
360359

361360
Env: "",
362361
Description: "Do not start a process reaper.",
363-
Value: clibase.BoolOf(&noReap),
362+
Value: serpent.BoolOf(&noReap),
364363
},
365364
{
366365
Flag: "ssh-max-timeout",
367366
// tcpip.KeepaliveIdleOption = 72h + 1min (forwardTCPSockOpts() in tailnet/conn.go)
368367
Default: "72h",
369368
Env: "CODER_AGENT_SSH_MAX_TIMEOUT",
370369
Description: "Specify the max timeout for a SSH connection, it is advisable to set it to a minimum of 60s, but no more than 72h.",
371-
Value: clibase.DurationOf(&sshMaxTimeout),
370+
Value: serpent.DurationOf(&sshMaxTimeout),
372371
},
373372
{
374373
Flag: "tailnet-listen-port",
375374
Default: "0",
376375
Env: "CODER_AGENT_TAILNET_LISTEN_PORT",
377376
Description: "Specify a static port for Tailscale to use for listening.",
378-
Value: clibase.Int64Of(&tailnetListenPort),
377+
Value: serpent.Int64Of(&tailnetListenPort),
379378
},
380379
{
381380
Flag: "prometheus-address",
382381
Default: "127.0.0.1:2112",
383382
Env: "CODER_AGENT_PROMETHEUS_ADDRESS",
384-
Value: clibase.StringOf(&prometheusAddress),
383+
Value: serpent.StringOf(&prometheusAddress),
385384
Description: "The bind address to serve Prometheus metrics.",
386385
},
387386
{
388387
Flag: "debug-address",
389388
Default: "127.0.0.1:2113",
390389
Env: "CODER_AGENT_DEBUG_ADDRESS",
391-
Value: clibase.StringOf(&debugAddress),
390+
Value: serpent.StringOf(&debugAddress),
392391
Description: "The bind address to serve a debug HTTP server.",
393392
},
394393
{
@@ -397,23 +396,23 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
397396
Flag: "log-human",
398397
Env: "CODER_AGENT_LOGGING_HUMAN",
399398
Default: "/dev/stderr",
400-
Value: clibase.StringOf(&slogHumanPath),
399+
Value: serpent.StringOf(&slogHumanPath),
401400
},
402401
{
403402
Name: "JSON Log Location",
404403
Description: "Output JSON logs to a given file.",
405404
Flag: "log-json",
406405
Env: "CODER_AGENT_LOGGING_JSON",
407406
Default: "",
408-
Value: clibase.StringOf(&slogJSONPath),
407+
Value: serpent.StringOf(&slogJSONPath),
409408
},
410409
{
411410
Name: "Stackdriver Log Location",
412411
Description: "Output Stackdriver compatible logs to a given file.",
413412
Flag: "log-stackdriver",
414413
Env: "CODER_AGENT_LOGGING_STACKDRIVER",
415414
Default: "",
416-
Value: clibase.StringOf(&slogStackdriverPath),
415+
Value: serpent.StringOf(&slogStackdriverPath),
417416
},
418417
}
419418

@@ -501,26 +500,3 @@ func urlPort(u string) (int, error) {
501500
}
502501
return -1, xerrors.Errorf("invalid port: %s", u)
503502
}
504-
505-
func prometheusMetricsHandler(prometheusRegistry *prometheus.Registry, logger slog.Logger) http.Handler {
506-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
507-
w.Header().Set("Content-Type", "text/plain")
508-
509-
// Based on: https://github.com/tailscale/tailscale/blob/280255acae604796a1113861f5a84e6fa2dc6121/ipn/localapi/localapi.go#L489
510-
clientmetric.WritePrometheusExpositionFormat(w)
511-
512-
metricFamilies, err := prometheusRegistry.Gather()
513-
if err != nil {
514-
logger.Error(context.Background(), "Prometheus handler can't gather metric families", slog.Error(err))
515-
return
516-
}
517-
518-
for _, metricFamily := range metricFamilies {
519-
_, err = expfmt.MetricFamilyToText(w, metricFamily)
520-
if err != nil {
521-
logger.Error(context.Background(), "expfmt.MetricFamilyToText failed", slog.Error(err))
522-
return
523-
}
524-
}
525-
})
526-
}

cli/autoupdate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import (
66

77
"golang.org/x/xerrors"
88

9-
"github.com/coder/coder/v2/cli/clibase"
109
"github.com/coder/coder/v2/cli/cliui"
1110
"github.com/coder/coder/v2/codersdk"
11+
"github.com/coder/serpent"
1212
)
1313

14-
func (r *RootCmd) autoupdate() *clibase.Cmd {
14+
func (r *RootCmd) autoupdate() *serpent.Command {
1515
client := new(codersdk.Client)
16-
cmd := &clibase.Cmd{
16+
cmd := &serpent.Command{
1717
Annotations: workspaceCommand,
1818
Use: "autoupdate <workspace> <always|never>",
1919
Short: "Toggle auto-update policy for a workspace",
20-
Middleware: clibase.Chain(
21-
clibase.RequireNArgs(2),
20+
Middleware: serpent.Chain(
21+
serpent.RequireNArgs(2),
2222
r.InitClient(client),
2323
),
24-
Handler: func(inv *clibase.Invocation) error {
24+
Handler: func(inv *serpent.Invocation) error {
2525
policy := strings.ToLower(inv.Args[1])
2626
err := validateAutoUpdatePolicy(policy)
2727
if err != nil {

0 commit comments

Comments
 (0)