chore(runner): support update log and task

Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi.Wu
2022-09-25 18:54:00 +08:00
committed by Jason Song
parent 20c3d85ba9
commit 82431d8e11
11 changed files with 489 additions and 375 deletions

View File

@ -1,9 +1,8 @@
package client
import (
"context"
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
"gitea.com/gitea/proto-go/ping/v1/pingv1connect"
"gitea.com/gitea/proto-go/runner/v1/runnerv1connect"
)
type Filter struct {
@ -16,21 +15,6 @@ type Filter struct {
// 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
// Register for new runner.
Register(ctx context.Context, args *runnerv1.RegisterRequest) (*runnerv1.Runner, error)
// Request requests the next available build stage for execution.
Request(ctx context.Context, args *runnerv1.RequestRequest) (*runnerv1.Stage, error)
// Detail fetches build details
Detail(ctx context.Context, args *runnerv1.DetailRequest) (*runnerv1.DetailResponse, error)
// Update updates the build stage.
Update(ctxt context.Context, args *runnerv1.UpdateRequest) error
// UpdateStep updates the build step.
UpdateStep(ctx context.Context, args *runnerv1.UpdateStepRequest) error
pingv1connect.PingServiceClient
runnerv1connect.RunnerServiceClient
}

View File

@ -7,9 +7,7 @@ import (
"net/http"
"time"
pingv1 "gitea.com/gitea/proto-go/ping/v1"
"gitea.com/gitea/proto-go/ping/v1/pingv1connect"
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
"gitea.com/gitea/proto-go/runner/v1/runnerv1connect"
"github.com/bufbuild/connect-go"
@ -17,34 +15,40 @@ import (
)
// New returns a new runner client.
func New(endpoint, secret string, skipverify bool, opts ...Option) *HTTPClient {
client := &HTTPClient{
Endpoint: endpoint,
Secret: secret,
SkipVerify: skipverify,
}
func New(endpoint, secret string, opts ...Option) *HTTPClient {
cfg := &config{}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated
opt.Apply(client)
opt.apply(cfg)
}
client.Client = &http.Client{
Timeout: 1 * time.Minute,
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(netw, addr)
interceptor := connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
req.Header().Set("X-Runner-Token", secret)
return next(ctx, req)
}
})
cfg.opts = append(cfg.opts, connect.WithInterceptors(interceptor))
if cfg.httpClient == nil {
cfg.httpClient = &http.Client{
Timeout: 1 * time.Minute,
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
},
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(netw, addr)
},
},
}
}
if skipverify {
client.Client = &http.Client{
if cfg.skipVerify {
cfg.httpClient = &http.Client{
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
@ -56,119 +60,25 @@ func New(endpoint, secret string, skipverify bool, opts ...Option) *HTTPClient {
},
}
}
return client
return &HTTPClient{
PingServiceClient: pingv1connect.NewPingServiceClient(
cfg.httpClient,
endpoint,
cfg.opts...,
),
RunnerServiceClient: runnerv1connect.NewRunnerServiceClient(
cfg.httpClient,
endpoint,
cfg.opts...,
),
}
}
var _ Client = (*HTTPClient)(nil)
// An HTTPClient manages communication with the runner API.
type HTTPClient struct {
Client *http.Client
Endpoint string
Secret string
SkipVerify bool
opts []connect.ClientOption
}
// Ping sends a ping message to the server to test connectivity.
func (p *HTTPClient) Ping(ctx context.Context, machine string) error {
client := pingv1connect.NewPingServiceClient(
p.Client,
p.Endpoint,
p.opts...,
)
req := connect.NewRequest(&pingv1.PingRequest{
Data: machine,
})
req.Header().Set("X-Runner-Token", p.Secret)
_, err := client.Ping(ctx, req)
return err
}
// Register a new runner.
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(arg)
req.Header().Set("X-Runner-Token", p.Secret)
res, err := client.Register(ctx, req)
if err != nil {
return nil, err
}
return res.Msg.Runner, err
}
// Request requests the next available build stage for execution.
func (p *HTTPClient) Request(ctx context.Context, arg *runnerv1.RequestRequest) (*runnerv1.Stage, error) {
client := runnerv1connect.NewRunnerServiceClient(
p.Client,
p.Endpoint,
p.opts...,
)
req := connect.NewRequest(arg)
req.Header().Set("X-Runner-Token", p.Secret)
res, err := client.Request(ctx, req)
if err != nil {
return nil, err
}
return res.Msg.Stage, err
}
// Update updates the build stage.
func (p *HTTPClient) Update(ctx context.Context, arg *runnerv1.UpdateRequest) error {
client := runnerv1connect.NewRunnerServiceClient(
p.Client,
p.Endpoint,
p.opts...,
)
req := connect.NewRequest(arg)
req.Header().Set("X-Runner-Token", p.Secret)
_, err := client.Update(ctx, req)
return err
}
// UpdateStep updates the build step.
func (p *HTTPClient) UpdateStep(ctx context.Context, arg *runnerv1.UpdateStepRequest) error {
client := runnerv1connect.NewRunnerServiceClient(
p.Client,
p.Endpoint,
p.opts...,
)
req := connect.NewRequest(arg)
req.Header().Set("X-Runner-Token", p.Secret)
_, err := client.UpdateStep(ctx, req)
return err
}
// Detail fetches build details
func (p *HTTPClient) Detail(ctx context.Context, arg *runnerv1.DetailRequest) (*runnerv1.DetailResponse, error) {
client := runnerv1connect.NewRunnerServiceClient(
p.Client,
p.Endpoint,
p.opts...,
)
req := connect.NewRequest(arg)
req.Header().Set("X-Runner-Token", p.Secret)
resp, err := client.Detail(ctx, req)
return &runnerv1.DetailResponse{
Repo: resp.Msg.Repo,
Build: resp.Msg.Build,
Stage: resp.Msg.Stage,
}, err
pingv1connect.PingServiceClient
runnerv1connect.RunnerServiceClient
}

View File

@ -1,36 +1,58 @@
package client
import "github.com/bufbuild/connect-go"
import (
"net/http"
"github.com/bufbuild/connect-go"
)
type config struct {
httpClient *http.Client
skipVerify bool
opts []connect.ClientOption
}
// An Option configures a mutex.
type Option interface {
Apply(*HTTPClient)
apply(*config)
}
// OptionFunc is a function that configure a value.
type OptionFunc func(*HTTPClient)
type OptionFunc func(*config)
// Apply calls f(option)
func (f OptionFunc) Apply(cli *HTTPClient) {
f(cli)
func (f OptionFunc) apply(cfg *config) {
f(cfg)
}
func WithSkipVerify(c bool) Option {
return OptionFunc(func(cfg *config) {
cfg.skipVerify = c
})
}
func WithClientOptions(opts ...connect.ClientOption) Option {
return OptionFunc(func(cfg *config) {
cfg.opts = append(cfg.opts, opts...)
})
}
// WithGRPC configures clients to use the HTTP/2 gRPC protocol.
func WithGRPC(c bool) Option {
return OptionFunc(func(cli *HTTPClient) {
return OptionFunc(func(cfg *config) {
if !c {
return
}
cli.opts = append(cli.opts, connect.WithGRPC())
cfg.opts = append(cfg.opts, connect.WithGRPC())
})
}
// WithGRPCWeb configures clients to use the gRPC-Web protocol.
func WithGRPCWeb(c bool) Option {
return OptionFunc(func(cli *HTTPClient) {
return OptionFunc(func(cfg *config) {
if !c {
return
}
cli.opts = append(cli.opts, connect.WithGRPCWeb())
cfg.opts = append(cfg.opts, connect.WithGRPCWeb())
})
}