From e6f5623627478acb83745cde64bd20aa6db9186d Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 1 Feb 2023 20:05:57 +0200 Subject: [PATCH] chore: Rename agent statistics server to http api server (#5961) --- agent/agent.go | 16 +++++------ agent/{statsendpoint.go => api.go} | 2 +- codersdk/workspaceagentconn.go | 44 ++++++++++++++---------------- scaletest/agentconn/run.go | 2 +- 4 files changed, 31 insertions(+), 33 deletions(-) rename agent/{statsendpoint.go => api.go} (95%) diff --git a/agent/agent.go b/agent/agent.go index e71affaf13..6da2db0098 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -526,23 +526,23 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (_ return nil, err } - statisticsListener, err := network.Listen("tcp", ":"+strconv.Itoa(codersdk.WorkspaceAgentStatisticsPort)) + apiListener, err := network.Listen("tcp", ":"+strconv.Itoa(codersdk.WorkspaceAgentHTTPAPIServerPort)) if err != nil { - return nil, xerrors.Errorf("listen for statistics: %w", err) + return nil, xerrors.Errorf("api listener: %w", err) } defer func() { if err != nil { - _ = statisticsListener.Close() + _ = apiListener.Close() } }() if err = a.trackConnGoroutine(func() { - defer statisticsListener.Close() + defer apiListener.Close() server := &http.Server{ - Handler: a.statisticsHandler(), + Handler: a.apiHandler(), ReadTimeout: 20 * time.Second, ReadHeaderTimeout: 20 * time.Second, WriteTimeout: 20 * time.Second, - ErrorLog: slog.Stdlib(ctx, a.logger.Named("statistics_http_server"), slog.LevelInfo), + ErrorLog: slog.Stdlib(ctx, a.logger.Named("http_api_server"), slog.LevelInfo), } go func() { select { @@ -552,9 +552,9 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (_ _ = server.Close() }() - err := server.Serve(statisticsListener) + err := server.Serve(apiListener) if err != nil && !xerrors.Is(err, http.ErrServerClosed) && !strings.Contains(err.Error(), "use of closed network connection") { - a.logger.Critical(ctx, "serve statistics HTTP server", slog.Error(err)) + a.logger.Critical(ctx, "serve HTTP API server", slog.Error(err)) } }); err != nil { return nil, err diff --git a/agent/statsendpoint.go b/agent/api.go similarity index 95% rename from agent/statsendpoint.go rename to agent/api.go index 65fece063e..415b20c95a 100644 --- a/agent/statsendpoint.go +++ b/agent/api.go @@ -11,7 +11,7 @@ import ( "github.com/coder/coder/codersdk" ) -func (*agent) statisticsHandler() http.Handler { +func (*agent) apiHandler() http.Handler { r := chi.NewRouter() r.Get("/", func(rw http.ResponseWriter, r *http.Request) { httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.Response{ diff --git a/codersdk/workspaceagentconn.go b/codersdk/workspaceagentconn.go index 438c4032fa..d6a4feffa3 100644 --- a/codersdk/workspaceagentconn.go +++ b/codersdk/workspaceagentconn.go @@ -23,20 +23,18 @@ import ( "github.com/coder/coder/tailnet" ) -var ( - // WorkspaceAgentIP is a static IPv6 address with the Tailscale prefix that is used to route - // connections from clients to this node. A dynamic address is not required because a Tailnet - // client only dials a single agent at a time. - WorkspaceAgentIP = netip.MustParseAddr("fd7a:115c:a1e0:49d6:b259:b7ac:b1b2:48f4") -) +// WorkspaceAgentIP is a static IPv6 address with the Tailscale prefix that is used to route +// connections from clients to this node. A dynamic address is not required because a Tailnet +// client only dials a single agent at a time. +var WorkspaceAgentIP = netip.MustParseAddr("fd7a:115c:a1e0:49d6:b259:b7ac:b1b2:48f4") const ( WorkspaceAgentSSHPort = 1 WorkspaceAgentReconnectingPTYPort = 2 WorkspaceAgentSpeedtestPort = 3 - // WorkspaceAgentStatisticsPort serves a HTTP server with endpoints for gathering - // agent statistics. - WorkspaceAgentStatisticsPort = 4 + // WorkspaceAgentHTTPAPIServerPort serves a HTTP server with endpoints for e.g. + // gathering agent statistics. + WorkspaceAgentHTTPAPIServerPort = 4 // WorkspaceAgentMinimumListeningPort is the minimum port that the listening-ports // endpoint will return to the client, and the minimum port that is accepted @@ -282,7 +280,7 @@ type WorkspaceAgentListeningPort struct { func (c *WorkspaceAgentConn) ListeningPorts(ctx context.Context) (WorkspaceAgentListeningPortsResponse, error) { ctx, span := tracing.StartSpan(ctx) defer span.End() - res, err := c.requestStatisticsServer(ctx, http.MethodGet, "/api/v0/listening-ports", nil) + res, err := c.apiRequest(ctx, http.MethodGet, "/api/v0/listening-ports", nil) if err != nil { return WorkspaceAgentListeningPortsResponse{}, xerrors.Errorf("do request: %w", err) } @@ -295,30 +293,30 @@ func (c *WorkspaceAgentConn) ListeningPorts(ctx context.Context) (WorkspaceAgent return resp, json.NewDecoder(res.Body).Decode(&resp) } -// requestStatisticsServer makes a request to the workspace agent's statistics server. -func (c *WorkspaceAgentConn) requestStatisticsServer(ctx context.Context, method, path string, body io.Reader) (*http.Response, error) { +// apiRequest makes a request to the workspace agent's HTTP API server. +func (c *WorkspaceAgentConn) apiRequest(ctx context.Context, method, path string, body io.Reader) (*http.Response, error) { ctx, span := tracing.StartSpan(ctx) defer span.End() - host := net.JoinHostPort(WorkspaceAgentIP.String(), strconv.Itoa(WorkspaceAgentStatisticsPort)) + host := net.JoinHostPort(WorkspaceAgentIP.String(), strconv.Itoa(WorkspaceAgentHTTPAPIServerPort)) url := fmt.Sprintf("http://%s%s", host, path) req, err := http.NewRequestWithContext(ctx, method, url, body) if err != nil { - return nil, xerrors.Errorf("new statistics server request to %q: %w", url, err) + return nil, xerrors.Errorf("new http api request to %q: %w", url, err) } - return c.statisticsServerClient().Do(req) + return c.apiClient().Do(req) } -// statisticsServerClient returns an HTTP client that can be used to make -// requests to the workspace agent's statistics server. -func (c *WorkspaceAgentConn) statisticsServerClient() *http.Client { +// apiClient returns an HTTP client that can be used to make +// requests to the workspace agent's HTTP API server. +func (c *WorkspaceAgentConn) apiClient() *http.Client { return &http.Client{ Transport: &http.Transport{ // Disable keep alives as we're usually only making a single // request, and this triggers goleak in tests DisableKeepAlives: true, - DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + DialContext: func(_ context.Context, network, addr string) (net.Conn, error) { if network != "tcp" { return nil, xerrors.Errorf("network must be tcp") } @@ -328,13 +326,13 @@ func (c *WorkspaceAgentConn) statisticsServerClient() *http.Client { } // Verify that host is TailnetIP and port is // TailnetStatisticsPort. - if host != WorkspaceAgentIP.String() || port != strconv.Itoa(WorkspaceAgentStatisticsPort) { - return nil, xerrors.Errorf("request %q does not appear to be for statistics server", addr) + if host != WorkspaceAgentIP.String() || port != strconv.Itoa(WorkspaceAgentHTTPAPIServerPort) { + return nil, xerrors.Errorf("request %q does not appear to be for http api", addr) } - conn, err := c.DialContextTCP(context.Background(), netip.AddrPortFrom(WorkspaceAgentIP, WorkspaceAgentStatisticsPort)) + conn, err := c.DialContextTCP(context.Background(), netip.AddrPortFrom(WorkspaceAgentIP, WorkspaceAgentHTTPAPIServerPort)) if err != nil { - return nil, xerrors.Errorf("dial statistics: %w", err) + return nil, xerrors.Errorf("dial http api: %w", err) } return conn, nil diff --git a/scaletest/agentconn/run.go b/scaletest/agentconn/run.go index 2229e6706c..5b5c4a5f91 100644 --- a/scaletest/agentconn/run.go +++ b/scaletest/agentconn/run.go @@ -218,7 +218,7 @@ func verifyConnection(ctx context.Context, logs io.Writer, conn *codersdk.Worksp u := &url.URL{ Scheme: "http", - Host: net.JoinHostPort("localhost", strconv.Itoa(codersdk.WorkspaceAgentStatisticsPort)), + Host: net.JoinHostPort("localhost", strconv.Itoa(codersdk.WorkspaceAgentHTTPAPIServerPort)), Path: "/", } req, err := http.NewRequestWithContext(verifyCtx, http.MethodGet, u.String(), nil)