mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add agent metadata (#6614)
This commit is contained in:
@@ -65,37 +65,56 @@ func (c *Client) GitSSHKey(ctx context.Context) (GitSSHKey, error) {
|
||||
return gitSSHKey, json.NewDecoder(res.Body).Decode(&gitSSHKey)
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
// In the future, we may want to support sending back multiple values for
|
||||
// performance.
|
||||
type PostMetadataRequest = codersdk.WorkspaceAgentMetadataResult
|
||||
|
||||
func (c *Client) PostMetadata(ctx context.Context, key string, req PostMetadataRequest) error {
|
||||
res, err := c.SDK.Request(ctx, http.MethodPost, "/api/v2/workspaceagents/me/metadata/"+key, req)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("execute request: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusNoContent {
|
||||
return codersdk.ReadBodyAsError(res)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Manifest struct {
|
||||
// GitAuthConfigs stores the number of Git configurations
|
||||
// the Coder deployment has. If this number is >0, we
|
||||
// set up special configuration in the workspace.
|
||||
GitAuthConfigs int `json:"git_auth_configs"`
|
||||
VSCodePortProxyURI string `json:"vscode_port_proxy_uri"`
|
||||
Apps []codersdk.WorkspaceApp `json:"apps"`
|
||||
DERPMap *tailcfg.DERPMap `json:"derpmap"`
|
||||
EnvironmentVariables map[string]string `json:"environment_variables"`
|
||||
StartupScript string `json:"startup_script"`
|
||||
StartupScriptTimeout time.Duration `json:"startup_script_timeout"`
|
||||
Directory string `json:"directory"`
|
||||
MOTDFile string `json:"motd_file"`
|
||||
ShutdownScript string `json:"shutdown_script"`
|
||||
ShutdownScriptTimeout time.Duration `json:"shutdown_script_timeout"`
|
||||
GitAuthConfigs int `json:"git_auth_configs"`
|
||||
VSCodePortProxyURI string `json:"vscode_port_proxy_uri"`
|
||||
Apps []codersdk.WorkspaceApp `json:"apps"`
|
||||
DERPMap *tailcfg.DERPMap `json:"derpmap"`
|
||||
EnvironmentVariables map[string]string `json:"environment_variables"`
|
||||
StartupScript string `json:"startup_script"`
|
||||
StartupScriptTimeout time.Duration `json:"startup_script_timeout"`
|
||||
Directory string `json:"directory"`
|
||||
MOTDFile string `json:"motd_file"`
|
||||
ShutdownScript string `json:"shutdown_script"`
|
||||
ShutdownScriptTimeout time.Duration `json:"shutdown_script_timeout"`
|
||||
Metadata []codersdk.WorkspaceAgentMetadataDescription `json:"metadata"`
|
||||
}
|
||||
|
||||
// Metadata fetches metadata for the currently authenticated workspace agent.
|
||||
func (c *Client) Metadata(ctx context.Context) (Metadata, error) {
|
||||
res, err := c.SDK.Request(ctx, http.MethodGet, "/api/v2/workspaceagents/me/metadata", nil)
|
||||
// Manifest fetches manifest for the currently authenticated workspace agent.
|
||||
func (c *Client) Manifest(ctx context.Context) (Manifest, error) {
|
||||
res, err := c.SDK.Request(ctx, http.MethodGet, "/api/v2/workspaceagents/me/manifest", nil)
|
||||
if err != nil {
|
||||
return Metadata{}, err
|
||||
return Manifest{}, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return Metadata{}, codersdk.ReadBodyAsError(res)
|
||||
return Manifest{}, codersdk.ReadBodyAsError(res)
|
||||
}
|
||||
var agentMeta Metadata
|
||||
var agentMeta Manifest
|
||||
err = json.NewDecoder(res.Body).Decode(&agentMeta)
|
||||
if err != nil {
|
||||
return Metadata{}, err
|
||||
return Manifest{}, err
|
||||
}
|
||||
accessingPort := c.SDK.URL.Port()
|
||||
if accessingPort == "" {
|
||||
@@ -106,14 +125,14 @@ func (c *Client) Metadata(ctx context.Context) (Metadata, error) {
|
||||
}
|
||||
accessPort, err := strconv.Atoi(accessingPort)
|
||||
if err != nil {
|
||||
return Metadata{}, xerrors.Errorf("convert accessing port %q: %w", accessingPort, err)
|
||||
return Manifest{}, xerrors.Errorf("convert accessing port %q: %w", accessingPort, err)
|
||||
}
|
||||
// Agents can provide an arbitrary access URL that may be different
|
||||
// that the globally configured one. This breaks the built-in DERP,
|
||||
// which would continue to reference the global access URL.
|
||||
//
|
||||
// This converts all built-in DERPs to use the access URL that the
|
||||
// metadata request was performed with.
|
||||
// manifest request was performed with.
|
||||
for _, region := range agentMeta.DERPMap.Regions {
|
||||
if !region.EmbeddedRelay {
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user