Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad9ab2f

Browse files
committed
feat: add ConnectRPC variants for older Agent API versions
1 parent a110d18 commit ad9ab2f

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

agent/proto/agent_drpc_old.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package proto
2+
3+
import (
4+
"context"
5+
6+
"storj.io/drpc"
7+
)
8+
9+
// DRPCAgentClient20 is the Agent API at v2.0. Notably, it is missing GetAnnouncementBanners, but
10+
// is useful when you want to be maximally compatible with Coderd Release Versions from 2.9+
11+
type DRPCAgentClient20 interface {
12+
DRPCConn() drpc.Conn
13+
14+
GetManifest(ctx context.Context, in *GetManifestRequest) (*Manifest, error)
15+
GetServiceBanner(ctx context.Context, in *GetServiceBannerRequest) (*ServiceBanner, error)
16+
UpdateStats(ctx context.Context, in *UpdateStatsRequest) (*UpdateStatsResponse, error)
17+
UpdateLifecycle(ctx context.Context, in *UpdateLifecycleRequest) (*Lifecycle, error)
18+
BatchUpdateAppHealths(ctx context.Context, in *BatchUpdateAppHealthRequest) (*BatchUpdateAppHealthResponse, error)
19+
UpdateStartup(ctx context.Context, in *UpdateStartupRequest) (*Startup, error)
20+
BatchUpdateMetadata(ctx context.Context, in *BatchUpdateMetadataRequest) (*BatchUpdateMetadataResponse, error)
21+
BatchCreateLogs(ctx context.Context, in *BatchCreateLogsRequest) (*BatchCreateLogsResponse, error)
22+
}
23+
24+
// DRPCAgentClient21 is the Agent API at v2.1. It is useful if you want to be maximally compatible
25+
// with Coderd Release Versions from 2.12+
26+
type DRPCAgentClient21 interface {
27+
DRPCConn() drpc.Conn
28+
29+
GetManifest(ctx context.Context, in *GetManifestRequest) (*Manifest, error)
30+
GetServiceBanner(ctx context.Context, in *GetServiceBannerRequest) (*ServiceBanner, error)
31+
UpdateStats(ctx context.Context, in *UpdateStatsRequest) (*UpdateStatsResponse, error)
32+
UpdateLifecycle(ctx context.Context, in *UpdateLifecycleRequest) (*Lifecycle, error)
33+
BatchUpdateAppHealths(ctx context.Context, in *BatchUpdateAppHealthRequest) (*BatchUpdateAppHealthResponse, error)
34+
UpdateStartup(ctx context.Context, in *UpdateStartupRequest) (*Startup, error)
35+
BatchUpdateMetadata(ctx context.Context, in *BatchUpdateMetadataRequest) (*BatchUpdateMetadataResponse, error)
36+
BatchCreateLogs(ctx context.Context, in *BatchCreateLogsRequest) (*BatchCreateLogsResponse, error)
37+
GetAnnouncementBanners(ctx context.Context, in *GetAnnouncementBannersRequest) (*GetAnnouncementBannersResponse, error)
38+
}

apiversion/apiversion.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ func (v *APIVersion) Validate(version string) error {
7070
return xerrors.Errorf("version %s is no longer supported", version)
7171
}
7272

73+
func (v *APIVersion) Newer(other *APIVersion) bool {
74+
if v.supportedMajor > other.supportedMajor {
75+
return true
76+
}
77+
if v.supportedMajor == other.supportedMajor {
78+
if v.supportedMinor > other.supportedMinor {
79+
return true
80+
}
81+
return false
82+
}
83+
return false
84+
}
85+
7386
// Parse parses a valid major.minor version string into (major, minor).
7487
// Both major and minor must be valid integers separated by a period '.'.
7588
func Parse(version string) (major int, minor int, err error) {

apiversion/apiversion_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,47 @@ func TestAPIVersionValidate(t *testing.T) {
8888
})
8989
}
9090
}
91+
92+
func TestAPIVersion_Newer(t *testing.T) {
93+
t.Parallel()
94+
95+
v := apiversion.New(2, 1)
96+
97+
for _, tc := range []struct {
98+
name string
99+
version *apiversion.APIVersion
100+
newer bool
101+
}{
102+
{
103+
name: "Same",
104+
version: apiversion.New(2, 1),
105+
newer: false,
106+
},
107+
{
108+
name: "MinorNewer",
109+
version: apiversion.New(2, 0),
110+
newer: true,
111+
},
112+
{
113+
name: "MinorOlder",
114+
version: apiversion.New(2, 2),
115+
newer: false,
116+
},
117+
{
118+
name: "MajorNewer",
119+
version: apiversion.New(1, 3),
120+
newer: true,
121+
},
122+
{
123+
name: "MajorOlder",
124+
version: apiversion.New(3, 0),
125+
newer: false,
126+
},
127+
} {
128+
tc := tc
129+
t.Run(tc.name, func(t *testing.T) {
130+
t.Parallel()
131+
require.Equal(t, tc.newer, v.Newer(tc.version))
132+
})
133+
}
134+
}

codersdk/agentsdk/agentsdk.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"cdr.dev/slog"
2323
"github.com/coder/coder/v2/agent/proto"
24+
"github.com/coder/coder/v2/apiversion"
2425
"github.com/coder/coder/v2/codersdk"
2526
drpcsdk "github.com/coder/coder/v2/codersdk/drpc"
2627
)
@@ -155,14 +156,39 @@ func (c *Client) RewriteDERPMap(derpMap *tailcfg.DERPMap) {
155156
}
156157
}
157158

159+
// ConnectRPC20 returns a dRPC client to the Agent API v2.0. Notably, it is missing
160+
// GetAnnouncementBanners, but is useful when you want to be maximally compatible with Coderd
161+
// Release Versions from 2.9+
162+
func (c *Client) ConnectRPC20(ctx context.Context) (proto.DRPCAgentClient20, error) {
163+
conn, err := c.connectRPCVersion(ctx, apiversion.New(2, 0))
164+
if err != nil {
165+
return nil, err
166+
}
167+
return proto.NewDRPCAgentClient(conn), nil
168+
}
169+
170+
// ConnectRPC21 returns a dRPC client to the Agent API v2.1. It is useful when you want to be
171+
// maximally compatible with Coderd Release Versions from 2.12+
172+
func (c *Client) ConnectRPC21(ctx context.Context) (proto.DRPCAgentClient21, error) {
173+
conn, err := c.connectRPCVersion(ctx, apiversion.New(2, 1))
174+
if err != nil {
175+
return nil, err
176+
}
177+
return proto.NewDRPCAgentClient(conn), nil
178+
}
179+
158180
// ConnectRPC connects to the workspace agent API and tailnet API
159181
func (c *Client) ConnectRPC(ctx context.Context) (drpc.Conn, error) {
182+
return c.connectRPCVersion(ctx, proto.CurrentVersion)
183+
}
184+
185+
func (c *Client) connectRPCVersion(ctx context.Context, version *apiversion.APIVersion) (drpc.Conn, error) {
160186
rpcURL, err := c.SDK.URL.Parse("/api/v2/workspaceagents/me/rpc")
161187
if err != nil {
162188
return nil, xerrors.Errorf("parse url: %w", err)
163189
}
164190
q := rpcURL.Query()
165-
q.Add("version", proto.CurrentVersion.String())
191+
q.Add("version", version.String())
166192
rpcURL.RawQuery = q.Encode()
167193

168194
jar, err := cookiejar.New(nil)

0 commit comments

Comments
 (0)