mirror of https://github.com/coder/coder.git
chore: Rename agent statistics server to http api server (#5961)
This commit is contained in:
parent
f9ae105a26
commit
e6f5623627
|
@ -526,23 +526,23 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (_
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
statisticsListener, err := network.Listen("tcp", ":"+strconv.Itoa(codersdk.WorkspaceAgentStatisticsPort))
|
apiListener, err := network.Listen("tcp", ":"+strconv.Itoa(codersdk.WorkspaceAgentHTTPAPIServerPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("listen for statistics: %w", err)
|
return nil, xerrors.Errorf("api listener: %w", err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = statisticsListener.Close()
|
_ = apiListener.Close()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if err = a.trackConnGoroutine(func() {
|
if err = a.trackConnGoroutine(func() {
|
||||||
defer statisticsListener.Close()
|
defer apiListener.Close()
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Handler: a.statisticsHandler(),
|
Handler: a.apiHandler(),
|
||||||
ReadTimeout: 20 * time.Second,
|
ReadTimeout: 20 * time.Second,
|
||||||
ReadHeaderTimeout: 20 * time.Second,
|
ReadHeaderTimeout: 20 * time.Second,
|
||||||
WriteTimeout: 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() {
|
go func() {
|
||||||
select {
|
select {
|
||||||
|
@ -552,9 +552,9 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (_
|
||||||
_ = server.Close()
|
_ = 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") {
|
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 {
|
}); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
"github.com/coder/coder/codersdk"
|
"github.com/coder/coder/codersdk"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (*agent) statisticsHandler() http.Handler {
|
func (*agent) apiHandler() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Get("/", func(rw http.ResponseWriter, r *http.Request) {
|
r.Get("/", func(rw http.ResponseWriter, r *http.Request) {
|
||||||
httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.Response{
|
httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.Response{
|
|
@ -23,20 +23,18 @@ import (
|
||||||
"github.com/coder/coder/tailnet"
|
"github.com/coder/coder/tailnet"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// WorkspaceAgentIP is a static IPv6 address with the Tailscale prefix that is used to route
|
||||||
// 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
|
||||||
// connections from clients to this node. A dynamic address is not required because a Tailnet
|
// client only dials a single agent at a time.
|
||||||
// client only dials a single agent at a time.
|
var WorkspaceAgentIP = netip.MustParseAddr("fd7a:115c:a1e0:49d6:b259:b7ac:b1b2:48f4")
|
||||||
WorkspaceAgentIP = netip.MustParseAddr("fd7a:115c:a1e0:49d6:b259:b7ac:b1b2:48f4")
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
WorkspaceAgentSSHPort = 1
|
WorkspaceAgentSSHPort = 1
|
||||||
WorkspaceAgentReconnectingPTYPort = 2
|
WorkspaceAgentReconnectingPTYPort = 2
|
||||||
WorkspaceAgentSpeedtestPort = 3
|
WorkspaceAgentSpeedtestPort = 3
|
||||||
// WorkspaceAgentStatisticsPort serves a HTTP server with endpoints for gathering
|
// WorkspaceAgentHTTPAPIServerPort serves a HTTP server with endpoints for e.g.
|
||||||
// agent statistics.
|
// gathering agent statistics.
|
||||||
WorkspaceAgentStatisticsPort = 4
|
WorkspaceAgentHTTPAPIServerPort = 4
|
||||||
|
|
||||||
// WorkspaceAgentMinimumListeningPort is the minimum port that the listening-ports
|
// WorkspaceAgentMinimumListeningPort is the minimum port that the listening-ports
|
||||||
// endpoint will return to the client, and the minimum port that is accepted
|
// 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) {
|
func (c *WorkspaceAgentConn) ListeningPorts(ctx context.Context) (WorkspaceAgentListeningPortsResponse, error) {
|
||||||
ctx, span := tracing.StartSpan(ctx)
|
ctx, span := tracing.StartSpan(ctx)
|
||||||
defer span.End()
|
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 {
|
if err != nil {
|
||||||
return WorkspaceAgentListeningPortsResponse{}, xerrors.Errorf("do request: %w", err)
|
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)
|
return resp, json.NewDecoder(res.Body).Decode(&resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// requestStatisticsServer makes a request to the workspace agent's statistics server.
|
// apiRequest makes a request to the workspace agent's HTTP API server.
|
||||||
func (c *WorkspaceAgentConn) requestStatisticsServer(ctx context.Context, method, path string, body io.Reader) (*http.Response, error) {
|
func (c *WorkspaceAgentConn) apiRequest(ctx context.Context, method, path string, body io.Reader) (*http.Response, error) {
|
||||||
ctx, span := tracing.StartSpan(ctx)
|
ctx, span := tracing.StartSpan(ctx)
|
||||||
defer span.End()
|
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)
|
url := fmt.Sprintf("http://%s%s", host, path)
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, method, url, body)
|
req, err := http.NewRequestWithContext(ctx, method, url, body)
|
||||||
if err != nil {
|
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
|
// apiClient returns an HTTP client that can be used to make
|
||||||
// requests to the workspace agent's statistics server.
|
// requests to the workspace agent's HTTP API server.
|
||||||
func (c *WorkspaceAgentConn) statisticsServerClient() *http.Client {
|
func (c *WorkspaceAgentConn) apiClient() *http.Client {
|
||||||
return &http.Client{
|
return &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
// Disable keep alives as we're usually only making a single
|
// Disable keep alives as we're usually only making a single
|
||||||
// request, and this triggers goleak in tests
|
// request, and this triggers goleak in tests
|
||||||
DisableKeepAlives: true,
|
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" {
|
if network != "tcp" {
|
||||||
return nil, xerrors.Errorf("network must be 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
|
// Verify that host is TailnetIP and port is
|
||||||
// TailnetStatisticsPort.
|
// TailnetStatisticsPort.
|
||||||
if host != WorkspaceAgentIP.String() || port != strconv.Itoa(WorkspaceAgentStatisticsPort) {
|
if host != WorkspaceAgentIP.String() || port != strconv.Itoa(WorkspaceAgentHTTPAPIServerPort) {
|
||||||
return nil, xerrors.Errorf("request %q does not appear to be for statistics server", addr)
|
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 {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("dial statistics: %w", err)
|
return nil, xerrors.Errorf("dial http api: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return conn, nil
|
return conn, nil
|
||||||
|
|
|
@ -218,7 +218,7 @@ func verifyConnection(ctx context.Context, logs io.Writer, conn *codersdk.Worksp
|
||||||
|
|
||||||
u := &url.URL{
|
u := &url.URL{
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
Host: net.JoinHostPort("localhost", strconv.Itoa(codersdk.WorkspaceAgentStatisticsPort)),
|
Host: net.JoinHostPort("localhost", strconv.Itoa(codersdk.WorkspaceAgentHTTPAPIServerPort)),
|
||||||
Path: "/",
|
Path: "/",
|
||||||
}
|
}
|
||||||
req, err := http.NewRequestWithContext(verifyCtx, http.MethodGet, u.String(), nil)
|
req, err := http.NewRequestWithContext(verifyCtx, http.MethodGet, u.String(), nil)
|
||||||
|
|
Loading…
Reference in New Issue