- Notifications
You must be signed in to change notification settings - Fork1.1k
feat: implement agent socket api, client and cli (#20758)#20976
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
SasSwart wants to merge1 commit intorelease/2.29Choose a base branch fromjjs/release-2.29-scriptordering
base:release/2.29
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+1,313 −276
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletionsagent/agent.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletionsagent/agentsocket/client.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package agentsocket | ||
| import ( | ||
| "context" | ||
| "golang.org/x/xerrors" | ||
| "storj.io/drpc" | ||
| "storj.io/drpc/drpcconn" | ||
| "github.com/coder/coder/v2/agent/agentsocket/proto" | ||
| "github.com/coder/coder/v2/agent/unit" | ||
| ) | ||
| // Option represents a configuration option for NewClient. | ||
| type Option func(*options) | ||
| type options struct { | ||
| path string | ||
| } | ||
| // WithPath sets the socket path. If not provided or empty, the client will | ||
| // auto-discover the default socket path. | ||
| func WithPath(path string) Option { | ||
| return func(opts *options) { | ||
| if path == "" { | ||
| return | ||
| } | ||
| opts.path = path | ||
| } | ||
| } | ||
| // Client provides a client for communicating with the workspace agentsocket API. | ||
| type Client struct { | ||
| client proto.DRPCAgentSocketClient | ||
| conn drpc.Conn | ||
| } | ||
| // NewClient creates a new socket client and opens a connection to the socket. | ||
| // If path is not provided via WithPath or is empty, it will auto-discover the | ||
| // default socket path. | ||
| func NewClient(ctx context.Context, opts ...Option) (*Client, error) { | ||
| options := &options{} | ||
| for _, opt := range opts { | ||
| opt(options) | ||
| } | ||
| conn, err := dialSocket(ctx, options.path) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("connect to socket: %w", err) | ||
| } | ||
| drpcConn := drpcconn.New(conn) | ||
| client := proto.NewDRPCAgentSocketClient(drpcConn) | ||
| return &Client{ | ||
| client: client, | ||
| conn: drpcConn, | ||
| }, nil | ||
| } | ||
| // Close closes the socket connection. | ||
| func (c *Client) Close() error { | ||
| return c.conn.Close() | ||
| } | ||
| // Ping sends a ping request to the agent. | ||
| func (c *Client) Ping(ctx context.Context) error { | ||
| _, err := c.client.Ping(ctx, &proto.PingRequest{}) | ||
| return err | ||
| } | ||
| // SyncStart starts a unit in the dependency graph. | ||
| func (c *Client) SyncStart(ctx context.Context, unitName unit.ID) error { | ||
| _, err := c.client.SyncStart(ctx, &proto.SyncStartRequest{ | ||
| Unit: string(unitName), | ||
| }) | ||
| return err | ||
| } | ||
| // SyncWant declares a dependency between units. | ||
| func (c *Client) SyncWant(ctx context.Context, unitName, dependsOn unit.ID) error { | ||
| _, err := c.client.SyncWant(ctx, &proto.SyncWantRequest{ | ||
| Unit: string(unitName), | ||
| DependsOn: string(dependsOn), | ||
| }) | ||
| return err | ||
| } | ||
| // SyncComplete marks a unit as complete in the dependency graph. | ||
| func (c *Client) SyncComplete(ctx context.Context, unitName unit.ID) error { | ||
| _, err := c.client.SyncComplete(ctx, &proto.SyncCompleteRequest{ | ||
| Unit: string(unitName), | ||
| }) | ||
| return err | ||
| } | ||
| // SyncReady requests whether a unit is ready to be started. That is, all dependencies are satisfied. | ||
| func (c *Client) SyncReady(ctx context.Context, unitName unit.ID) (bool, error) { | ||
| resp, err := c.client.SyncReady(ctx, &proto.SyncReadyRequest{ | ||
| Unit: string(unitName), | ||
| }) | ||
| return resp.Ready, err | ||
| } | ||
| // SyncStatus gets the status of a unit and its dependencies. | ||
| func (c *Client) SyncStatus(ctx context.Context, unitName unit.ID) (SyncStatusResponse, error) { | ||
| resp, err := c.client.SyncStatus(ctx, &proto.SyncStatusRequest{ | ||
| Unit: string(unitName), | ||
| }) | ||
| if err != nil { | ||
| return SyncStatusResponse{}, err | ||
| } | ||
| var dependencies []DependencyInfo | ||
| for _, dep := range resp.Dependencies { | ||
| dependencies = append(dependencies, DependencyInfo{ | ||
| DependsOn: unit.ID(dep.DependsOn), | ||
| RequiredStatus: unit.Status(dep.RequiredStatus), | ||
| CurrentStatus: unit.Status(dep.CurrentStatus), | ||
| IsSatisfied: dep.IsSatisfied, | ||
| }) | ||
| } | ||
| return SyncStatusResponse{ | ||
| UnitName: unitName, | ||
| Status: unit.Status(resp.Status), | ||
| IsReady: resp.IsReady, | ||
| Dependencies: dependencies, | ||
| }, nil | ||
| } | ||
| // SyncStatusResponse contains the status information for a unit. | ||
| type SyncStatusResponse struct { | ||
| UnitName unit.ID `table:"unit,default_sort" json:"unit_name"` | ||
| Status unit.Status `table:"status" json:"status"` | ||
| IsReady bool `table:"ready" json:"is_ready"` | ||
| Dependencies []DependencyInfo `table:"dependencies" json:"dependencies"` | ||
| } | ||
| // DependencyInfo contains information about a unit dependency. | ||
| type DependencyInfo struct { | ||
| DependsOn unit.ID `table:"depends on,default_sort" json:"depends_on"` | ||
| RequiredStatus unit.Status `table:"required status" json:"required_status"` | ||
| CurrentStatus unit.Status `table:"current status" json:"current_status"` | ||
| IsSatisfied bool `table:"satisfied" json:"is_satisfied"` | ||
| } |
69 changes: 11 additions & 58 deletionsagent/agentsocket/server.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.