Skip to content

feat: provide backwards compatibility with the buildlog #82

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 74 additions & 9 deletions buildlog/coder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/google/uuid"
"golang.org/x/mod/semver"
"golang.org/x/xerrors"
"storj.io/drpc"

Expand Down Expand Up @@ -35,7 +36,36 @@ type CoderClient interface {
io.Closer
}

type coderClient struct {
type agentClientV1 struct {
ctx context.Context
client *agentsdk.Client
}

func (c *agentClientV1) Send(level codersdk.LogLevel, log string) error {
lines := cutString(log, MaxCoderLogSize)

logs := make([]agentsdk.Log, 0, CoderLoggerMaxLogs)
for _, output := range lines {
logs = append(logs, agentsdk.Log{
CreatedAt: time.Now(),
Output: output,
Level: level,
})
}
err := c.client.PatchLogs(c.ctx, agentsdk.PatchLogs{
Logs: logs,
})
if err != nil {
return xerrors.Errorf("send build log: %w", err)
}
return nil
}

func (*agentClientV1) Close() error {
return nil
}

type agentClientV2 struct {
ctx context.Context
cancel context.CancelFunc
source uuid.UUID
Expand All @@ -44,7 +74,7 @@ type coderClient struct {
log slog.Logger
}

func (c *coderClient) Send(level codersdk.LogLevel, log string) error {
func (c *agentClientV2) Send(level codersdk.LogLevel, log string) error {
err := c.sl.Send(c.ctx, agentsdk.Log{
CreatedAt: time.Now(),
Output: log,
Expand All @@ -56,7 +86,7 @@ func (c *coderClient) Send(level codersdk.LogLevel, log string) error {
return nil
}

func (c *coderClient) Close() error {
func (c *agentClientV2) Close() error {
defer c.cancel()
c.ls.Flush(c.source)
err := c.ls.WaitUntilEmpty(c.ctx)
Expand All @@ -66,10 +96,7 @@ func (c *coderClient) Close() error {
return nil
}

func OpenCoderClient(ctx context.Context, accessURL *url.URL, logger slog.Logger, token string) (CoderClient, error) {
client := agentsdk.New(accessURL)
client.SetSessionToken(token)

func newAgentClientV2(ctx context.Context, logger slog.Logger, client *agentsdk.Client) (CoderClient, error) {
cctx, cancel := context.WithCancel(ctx)
uid := uuid.New()
ls := agentsdk.NewLogSender(logger)
Expand Down Expand Up @@ -98,7 +125,7 @@ func OpenCoderClient(ctx context.Context, accessURL *url.URL, logger slog.Logger
}
}()

return &coderClient{
return &agentClientV2{
ctx: cctx,
cancel: cancel,
source: uid,
Expand All @@ -108,14 +135,34 @@ func OpenCoderClient(ctx context.Context, accessURL *url.URL, logger slog.Logger
}, nil
}

func OpenCoderClient(ctx context.Context, accessURL *url.URL, logger slog.Logger, token string) (CoderClient, error) {
client := agentsdk.New(accessURL)
client.SetSessionToken(token)

resp, err := client.SDK.BuildInfo(ctx)
if err != nil {
return nil, xerrors.Errorf("build info: %w", err)
}

if semver.Compare(semver.MajorMinor(resp.Version), "v2.13") < 0 {
return &agentClientV1{
ctx: ctx,
client: client,
}, nil
}

return newAgentClientV2(ctx, logger, client)
}

type CoderLogger struct {
ctx context.Context
client CoderClient
logger slog.Logger
}

func OpenCoderLogger(client CoderClient, log slog.Logger) Logger {
func OpenCoderLogger(ctx context.Context, client CoderClient, log slog.Logger) Logger {
coder := &CoderLogger{
ctx: ctx,
client: client,
logger: log,
}
Expand Down Expand Up @@ -156,3 +203,21 @@ func (c *CoderLogger) Write(p []byte) (int, error) {
func (c *CoderLogger) Close() error {
return c.client.Close()
}

// cutString cuts a string up into smaller strings that have a len no greater
// than the provided max size.
// If the string is less than the max size the return slice has one
// element with a value of the provided string.
func cutString(s string, maxSize int) []string {
if len(s) <= maxSize {
return []string{s}
}

toks := []string{}
for len(s) > maxSize {
toks = append(toks, s[:maxSize])
s = s[maxSize:]
}

return append(toks, s)
}
2 changes: 1 addition & 1 deletion cli/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func dockerCmd() *cobra.Command {
}

blog = buildlog.MultiLogger(
buildlog.OpenCoderLogger(agent, log),
buildlog.OpenCoderLogger(ctx, agent, log),
blog,
)
}
Expand Down
34 changes: 16 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
module github.com/coder/envbox

go 1.22.3

toolchain go1.22.4
go 1.22.4

// There are a few minor changes we make to Tailscale that we're slowly upstreaming. Compare here:
// https://github.com/tailscale/tailscale/compare/main...coder:tailscale:main
replace tailscale.com => github.com/coder/tailscale v1.1.1-0.20240530071520-1ac63d3a4ee3

require (
cdr.dev/slog v1.6.2-0.20240126064726-20367d4aede6
github.com/coder/coder/v2 v2.12.3
github.com/coder/coder/v2 v2.12.0
github.com/coder/retry v1.5.1
github.com/cpuguy83/dockercfg v0.3.1
github.com/docker/docker v24.0.9+incompatible
Expand All @@ -25,7 +23,8 @@ require (
github.com/stretchr/testify v1.9.0
github.com/vishvananda/netlink v1.2.1-beta.2
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
golang.org/x/sys v0.20.0
golang.org/x/mod v0.18.0
golang.org/x/sys v0.21.0
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028
k8s.io/mount-utils v0.26.2
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
Expand All @@ -50,7 +49,7 @@ require (
github.com/akutz/memconn v0.1.0 // indirect
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.27.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.30.0 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.7 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.7 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.3 // indirect
Expand All @@ -73,7 +72,7 @@ require (
github.com/charmbracelet/lipgloss v0.8.0 // indirect
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 // indirect
github.com/coder/serpent v0.7.0 // indirect
github.com/coder/terraform-provider-coder v0.22.0 // indirect
github.com/coder/terraform-provider-coder v0.23.0 // indirect
github.com/containerd/continuity v0.4.2 // indirect
github.com/coreos/go-iptables v0.6.0 // indirect
github.com/coreos/go-oidc/v3 v3.10.0 // indirect
Expand Down Expand Up @@ -109,7 +108,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/hashicorp/hcl/v2 v2.20.0 // indirect
github.com/hashicorp/hcl/v2 v2.21.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.12.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
Expand All @@ -123,7 +122,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/native v1.1.1-0.20230202152459-5c7d0dd6ab86 // indirect
github.com/jsimonetti/rtnetlink v1.3.5 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kortschak/wol v0.0.0-20200729010619-da482cc4850a // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down Expand Up @@ -171,7 +170,7 @@ require (
github.com/tcnksm/go-httpstat v0.2.0 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/u-root/uio v0.0.0-20240209044354-b3d14b93376a // indirect
github.com/valyala/fasthttp v1.54.0 // indirect
github.com/valyala/fasthttp v1.55.0 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
Expand All @@ -193,20 +192,19 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go4.org/mem v0.0.0-20220726221520-4f986261bf13 // indirect
go4.org/netipx v0.0.0-20230728180743-ad4cb58a6516 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.0 // indirect
golang.org/x/tools v0.22.0 // indirect
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect
golang.zx2c4.com/wireguard/windows v0.5.3 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240506185236-b8a5c65736ae // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/DataDog/dd-trace-go.v1 v1.64.0 // indirect
Expand Down
Loading
Loading