chore(gRPC): register new runner

Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi.Wu
2022-08-28 14:05:56 +08:00
committed by Jason Song
parent a3e9bbed25
commit 449388f3ab
7 changed files with 70 additions and 46 deletions

View File

@ -6,11 +6,19 @@ import (
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
)
type Filter struct {
Kind string `json:"kind"`
Type string `json:"type"`
OS string `json:"os"`
Arch string `json:"arch"`
Capacity int `json:"capacity"`
}
// A Client manages communication with the runner.
type Client interface {
// Ping sends a ping message to the server to test connectivity.
Ping(ctx context.Context, machine string) error
// Request requests the next available build stage for execution.
Request(ctx context.Context) (*runnerv1.Stage, error)
// Register for new runner.
Register(ctx context.Context, args *runnerv1.RegisterRequest) (*runnerv1.Runner, error)
}

View File

@ -59,6 +59,8 @@ func New(endpoint, secret string, skipverify bool, opts ...Option) *HTTPClient {
return client
}
var _ Client = (*HTTPClient)(nil)
// An HTTPClient manages communication with the runner API.
type HTTPClient struct {
Client *http.Client
@ -80,27 +82,26 @@ func (p *HTTPClient) Ping(ctx context.Context, machine string) error {
Data: machine,
})
req.Header().Set("X-Gitea-Token", p.Secret)
req.Header().Set("X-Runner-Token", p.Secret)
_, err := client.Ping(ctx, req)
return err
}
// Ping sends a ping message to the server to test connectivity.
func (p *HTTPClient) Request(ctx context.Context) (*runnerv1.Stage, error) {
func (p *HTTPClient) Register(ctx context.Context, arg *runnerv1.RegisterRequest) (*runnerv1.Runner, error) {
client := runnerv1connect.NewRunnerServiceClient(
p.Client,
p.Endpoint,
p.opts...,
)
req := connect.NewRequest(&runnerv1.ConnectRequest{})
req := connect.NewRequest(arg)
req.Header().Set("X-Runner-Token", p.Secret)
req.Header().Set("X-Gitea-Token", p.Secret)
res, err := client.Connect(ctx, req)
res, err := client.Register(ctx, req)
if err != nil {
return nil, err
}
return res.Msg.Stage, err
return res.Msg.Runner, err
}