Skip to content

chore: Implement workspace proxy going away (graceful shutdown) #7459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 55 additions & 2 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions codersdk/workspaceproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
type ProxyHealthStatus string

const (
// ProxyReachable means the proxy access url is reachable and returns a healthy
// ProxyHealthy means the proxy access url is reachable and returns a healthy
// status code.
ProxyReachable ProxyHealthStatus = "reachable"
ProxyHealthy ProxyHealthStatus = "ok"
// ProxyUnreachable means the proxy access url is not responding.
ProxyUnreachable ProxyHealthStatus = "unreachable"
// ProxyUnhealthy means the proxy access url is responding, but there is some
Expand Down Expand Up @@ -110,6 +110,24 @@ func (c *Client) WorkspaceProxies(ctx context.Context) ([]WorkspaceProxy, error)
return proxies, json.NewDecoder(res.Body).Decode(&proxies)
}

func (c *Client) WorkspaceProxyByName(ctx context.Context, name string) (WorkspaceProxy, error) {
res, err := c.Request(ctx, http.MethodGet,
fmt.Sprintf("/api/v2/workspaceproxies/%s", name),
nil,
)
if err != nil {
return WorkspaceProxy{}, xerrors.Errorf("make request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return WorkspaceProxy{}, ReadBodyAsError(res)
}

var proxy WorkspaceProxy
return proxy, json.NewDecoder(res.Body).Decode(&proxy)
}

func (c *Client) DeleteWorkspaceProxyByName(ctx context.Context, name string) error {
res, err := c.Request(ctx, http.MethodDelete,
fmt.Sprintf("/api/v2/workspaceproxies/%s", name),
Expand Down
58 changes: 55 additions & 3 deletions docs/api/enterprise.md
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ curl -X GET http://coder-server:8080/api/v2/workspaceproxies \
"errors": ["string"],
"warnings": ["string"]
},
"status": "reachable"
"status": "ok"
},
"updated_at": "2019-08-24T14:15:22Z",
"url": "string",
Expand Down Expand Up @@ -1232,7 +1232,7 @@ Status Code **200**

| Property | Value |
| -------- | -------------- |
| `status` | `reachable` |
| `status` | `ok` |
| `status` | `unreachable` |
| `status` | `unhealthy` |
| `status` | `unregistered` |
Expand Down Expand Up @@ -1286,7 +1286,7 @@ curl -X POST http://coder-server:8080/api/v2/workspaceproxies \
"errors": ["string"],
"warnings": ["string"]
},
"status": "reachable"
"status": "ok"
},
"updated_at": "2019-08-24T14:15:22Z",
"url": "string",
Expand All @@ -1302,6 +1302,58 @@ curl -X POST http://coder-server:8080/api/v2/workspaceproxies \

To perform this operation, you must be authenticated. [Learn more](authentication.md).

## Get workspace proxy

### Code samples

```shell
# Example request using curl
curl -X GET http://coder-server:8080/api/v2/workspaceproxies/{workspaceproxy} \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```

`GET /workspaceproxies/{workspaceproxy}`

### Parameters

| Name | In | Type | Required | Description |
| ---------------- | ---- | ------------ | -------- | ---------------- |
| `workspaceproxy` | path | string(uuid) | true | Proxy ID or name |

### Example responses

> 200 Response

```json
{
"created_at": "2019-08-24T14:15:22Z",
"deleted": true,
"icon": "string",
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"name": "string",
"status": {
"checked_at": "2019-08-24T14:15:22Z",
"report": {
"errors": ["string"],
"warnings": ["string"]
},
"status": "ok"
},
"updated_at": "2019-08-24T14:15:22Z",
"url": "string",
"wildcard_hostname": "string"
}
```

### Responses

| Status | Meaning | Description | Schema |
| ------ | ------------------------------------------------------- | ----------- | ------------------------------------------------------------ |
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.WorkspaceProxy](schemas.md#codersdkworkspaceproxy) |

To perform this operation, you must be authenticated. [Learn more](authentication.md).

## Delete workspace proxy

### Code samples
Expand Down
8 changes: 4 additions & 4 deletions docs/api/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -3454,7 +3454,7 @@ Parameter represents a set value for the scope.
## codersdk.ProxyHealthStatus

```json
"reachable"
"ok"
```

### Properties
Expand All @@ -3463,7 +3463,7 @@ Parameter represents a set value for the scope.

| Value |
| -------------- |
| `reachable` |
| `ok` |
| `unreachable` |
| `unhealthy` |
| `unregistered` |
Expand Down Expand Up @@ -5338,7 +5338,7 @@ Parameter represents a set value for the scope.
"errors": ["string"],
"warnings": ["string"]
},
"status": "reachable"
"status": "ok"
},
"updated_at": "2019-08-24T14:15:22Z",
"url": "string",
Expand Down Expand Up @@ -5369,7 +5369,7 @@ Parameter represents a set value for the scope.
"errors": ["string"],
"warnings": ["string"]
},
"status": "reachable"
"status": "ok"
}
```

Expand Down
1 change: 1 addition & 0 deletions enterprise/cli/proxyserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ func (*RootCmd) proxyServer() *clibase.Cmd {
if err != nil {
return xerrors.Errorf("create workspace proxy: %w", err)
}
closers.Add(func() { _ = proxy.Close() })

shutdownConnsCtx, shutdownConns := context.WithCancel(ctx)
defer shutdownConns()
Expand Down
4 changes: 3 additions & 1 deletion enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,15 @@ func New(ctx context.Context, options *Options) (*API, error) {
)
r.Post("/issue-signed-app-token", api.workspaceProxyIssueSignedAppToken)
r.Post("/register", api.workspaceProxyRegister)
r.Post("/goingaway", api.workspaceProxyGoingAway)
})
r.Route("/{workspaceproxy}", func(r chi.Router) {
r.Use(
apiKeyMiddleware,
httpmw.ExtractWorkspaceProxyParam(api.Database),
)

r.Get("/", api.fetchWorkspaceProxy)
r.Delete("/", api.deleteWorkspaceProxy)
})
})
Expand Down Expand Up @@ -237,7 +239,7 @@ func New(ctx context.Context, options *Options) (*API, error) {
if api.AGPL.Experiments.Enabled(codersdk.ExperimentMoons) {
// Proxy health is a moon feature.
api.ProxyHealth, err = proxyhealth.New(&proxyhealth.Options{
Interval: time.Minute * 1,
Interval: options.ProxyHealthInterval,
DB: api.Database,
Logger: options.Logger.Named("proxyhealth"),
Client: api.HTTPClient,
Expand Down
2 changes: 2 additions & 0 deletions enterprise/coderd/coderdenttest/coderdenttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Options struct {
EntitlementsUpdateInterval time.Duration
SCIMAPIKey []byte
UserWorkspaceQuota int
ProxyHealthInterval time.Duration
}

// New constructs a codersdk client connected to an in-memory Enterprise API instance.
Expand All @@ -74,6 +75,7 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, io.Closer, *c
Options: oop,
EntitlementsUpdateInterval: options.EntitlementsUpdateInterval,
Keys: Keys,
ProxyHealthInterval: options.ProxyHealthInterval,
})
assert.NoError(t, err)
setHandler(coderAPI.AGPL.RootHandler)
Expand Down
Loading