Skip to content

Commit a43837d

Browse files
jedevcktock
authored andcommitted
monitor: extend monitor interface from controller
Signed-off-by: Justin Chadwell <[email protected]>
1 parent 9f884ed commit a43837d

File tree

6 files changed

+20
-114
lines changed

6 files changed

+20
-114
lines changed

monitor/commands/attach.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (cm *AttachCmd) Exec(ctx context.Context, args []string) error {
5858
}
5959

6060
func isProcessID(ctx context.Context, c types.Monitor, ref string) (bool, error) {
61-
infos, err := c.ListProcesses(ctx)
61+
infos, err := c.ListProcesses(ctx, c.AttachedSessionID())
6262
if err != nil {
6363
return false, err
6464
}

monitor/commands/disconnect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (cm *DisconnectCmd) Exec(ctx context.Context, args []string) error {
2626
}
2727
isProcess, err := isProcessID(ctx, cm.m, target)
2828
if err == nil && isProcess {
29-
if err := cm.m.DisconnectProcess(ctx, target); err != nil {
29+
if err := cm.m.DisconnectProcess(ctx, cm.m.AttachedSessionID(), target); err != nil {
3030
return errors.Errorf("disconnecting from process failed %v", target)
3131
}
3232
return nil

monitor/commands/ps.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func (cm *PsCmd) Info() types.CommandInfo {
2323
}
2424

2525
func (cm *PsCmd) Exec(ctx context.Context, args []string) error {
26-
plist, err := cm.m.ListProcesses(ctx)
26+
ref := cm.m.AttachedSessionID()
27+
plist, err := cm.m.ListProcesses(ctx, ref)
2728
if err != nil {
2829
return err
2930
}

monitor/commands/reload.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ func (cm *ReloadCmd) Info() types.CommandInfo {
3232

3333
func (cm *ReloadCmd) Exec(ctx context.Context, args []string) error {
3434
var bo *controllerapi.BuildOptions
35-
if cm.m.AttachedSessionID() != "" {
35+
if ref := cm.m.AttachedSessionID(); ref != "" {
3636
// Rebuilding an existing session; Restore the build option used for building this session.
37-
res, err := cm.m.Inspect(ctx)
37+
res, err := cm.m.Inspect(ctx, ref)
3838
if err != nil {
3939
fmt.Printf("failed to inspect the current build session: %v\n", err)
4040
} else {
@@ -46,8 +46,8 @@ func (cm *ReloadCmd) Exec(ctx context.Context, args []string) error {
4646
if bo == nil {
4747
return errors.Errorf("no build option is provided")
4848
}
49-
if cm.m.AttachedSessionID() != "" {
50-
if err := cm.m.Disconnect(ctx); err != nil {
49+
if ref := cm.m.AttachedSessionID(); ref != "" {
50+
if err := cm.m.Disconnect(ctx, ref); err != nil {
5151
fmt.Println("disconnect error", err)
5252
}
5353
}

monitor/monitor.go

Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/docker/buildx/util/ioset"
1818
"github.com/docker/buildx/util/progress"
1919
"github.com/google/shlex"
20-
"github.com/moby/buildkit/client"
2120
"github.com/moby/buildkit/identity"
2221
"github.com/pkg/errors"
2322
"github.com/sirupsen/logrus"
@@ -63,8 +62,8 @@ func RunMonitor(ctx context.Context, curRef string, options *controllerapi.Build
6362
invokeForwarder := ioset.NewForwarder()
6463
invokeForwarder.SetIn(&containerIn)
6564
m := &monitor{
66-
controllerRef: newControllerRef(c, curRef),
67-
invokeIO: invokeForwarder,
65+
BuildxController: c,
66+
invokeIO: invokeForwarder,
6867
muxIO: ioset.NewMuxIO(ioset.In{
6968
Stdin: io.NopCloser(stdin),
7069
Stdout: nopCloser{stdout},
@@ -77,6 +76,7 @@ func RunMonitor(ctx context.Context, curRef string, options *controllerapi.Build
7776
return "Switched IO\n"
7877
}),
7978
}
79+
m.ref.Store(curRef)
8080

8181
// Start container automatically
8282
fmt.Fprintf(stdout, "Launching interactive container. Press Ctrl-a-c to switch to monitor console\n")
@@ -203,7 +203,8 @@ type readWriter struct {
203203
}
204204

205205
type monitor struct {
206-
*controllerRef
206+
control.BuildxController
207+
ref atomic.Value
207208

208209
muxIO *ioset.MuxIO
209210
invokeIO *ioset.Forwarder
@@ -212,15 +213,15 @@ type monitor struct {
212213
}
213214

214215
func (m *monitor) DisconnectSession(ctx context.Context, targetID string) error {
215-
return m.controllerRef.raw().Disconnect(ctx, targetID)
216+
return m.Disconnect(ctx, targetID)
216217
}
217218

218219
func (m *monitor) AttachSession(ref string) {
219-
m.controllerRef.ref.Store(ref)
220+
m.ref.Store(ref)
220221
}
221222

222223
func (m *monitor) AttachedSessionID() string {
223-
return m.controllerRef.ref.Load().(string)
224+
return m.ref.Load().(string)
224225
}
225226

226227
func (m *monitor) Rollback(ctx context.Context, cfg controllerapi.InvokeConfig) string {
@@ -298,87 +299,12 @@ func (m *monitor) invoke(ctx context.Context, pid string, cfg controllerapi.Invo
298299
defer invokeCancelAndDetachFn()
299300
m.invokeCancel = invokeCancelAndDetachFn
300301

301-
err := m.controllerRef.invoke(invokeCtx, pid, cfg, containerIn.Stdin, containerIn.Stdout, containerIn.Stderr)
302+
err := m.Invoke(invokeCtx, m.AttachedSessionID(), pid, cfg, containerIn.Stdin, containerIn.Stdout, containerIn.Stderr)
302303
close(waitInvokeDoneCh)
303304

304305
return err
305306
}
306307

307-
func newControllerRef(c control.BuildxController, ref string) *controllerRef {
308-
cr := &controllerRef{c: c}
309-
cr.ref.Store(ref)
310-
return cr
311-
}
312-
313-
type controllerRef struct {
314-
c control.BuildxController
315-
ref atomic.Value
316-
}
317-
318-
func (c *controllerRef) raw() control.BuildxController {
319-
return c.c
320-
}
321-
322-
func (c *controllerRef) getRef() (string, error) {
323-
ref := c.ref.Load()
324-
if ref == "" {
325-
return "", errors.Errorf("client is not attached to a session")
326-
}
327-
return ref.(string), nil
328-
}
329-
330-
func (c *controllerRef) Build(ctx context.Context, options controllerapi.BuildOptions, in io.ReadCloser, progress progress.Writer) (ref string, resp *client.SolveResponse, err error) {
331-
return c.c.Build(ctx, options, in, progress)
332-
}
333-
334-
func (c *controllerRef) invoke(ctx context.Context, pid string, options controllerapi.InvokeConfig, ioIn io.ReadCloser, ioOut io.WriteCloser, ioErr io.WriteCloser) error {
335-
ref, err := c.getRef()
336-
if err != nil {
337-
return err
338-
}
339-
return c.c.Invoke(ctx, ref, pid, options, ioIn, ioOut, ioErr)
340-
}
341-
342-
func (c *controllerRef) Kill(ctx context.Context) error {
343-
return c.c.Kill(ctx)
344-
}
345-
346-
func (c *controllerRef) List(ctx context.Context) (refs []string, _ error) {
347-
return c.c.List(ctx)
348-
}
349-
350-
func (c *controllerRef) ListProcesses(ctx context.Context) (infos []*controllerapi.ProcessInfo, retErr error) {
351-
ref, err := c.getRef()
352-
if err != nil {
353-
return nil, err
354-
}
355-
return c.c.ListProcesses(ctx, ref)
356-
}
357-
358-
func (c *controllerRef) DisconnectProcess(ctx context.Context, pid string) error {
359-
ref, err := c.getRef()
360-
if err != nil {
361-
return err
362-
}
363-
return c.c.DisconnectProcess(ctx, ref, pid)
364-
}
365-
366-
func (c *controllerRef) Inspect(ctx context.Context) (*controllerapi.InspectResponse, error) {
367-
ref, err := c.getRef()
368-
if err != nil {
369-
return nil, err
370-
}
371-
return c.c.Inspect(ctx, ref)
372-
}
373-
374-
func (c *controllerRef) Disconnect(ctx context.Context) error {
375-
ref, err := c.getRef()
376-
if err != nil {
377-
return err
378-
}
379-
return c.c.Disconnect(ctx, ref)
380-
}
381-
382308
type nopCloser struct {
383309
io.Writer
384310
}

monitor/types/types.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package types
22

33
import (
44
"context"
5-
"io"
65

6+
"github.com/docker/buildx/controller/control"
77
controllerapi "github.com/docker/buildx/controller/pb"
8-
"github.com/docker/buildx/util/progress"
9-
"github.com/moby/buildkit/client"
108
)
119

1210
// Monitor provides APIs for attaching and controlling the buildx server.
1311
type Monitor interface {
12+
control.BuildxController
13+
1414
// Rollback re-runs the interactive container with initial rootfs contents.
1515
Rollback(ctx context.Context, cfg controllerapi.InvokeConfig) string
1616

@@ -34,27 +34,6 @@ type Monitor interface {
3434

3535
// AttachedSessionID returns the ID of the attached session.
3636
AttachedSessionID() string
37-
38-
// Build executes the specified build and returns an ID of the session for debugging that build.
39-
Build(ctx context.Context, options controllerapi.BuildOptions, in io.ReadCloser, progress progress.Writer) (ref string, resp *client.SolveResponse, err error)
40-
41-
// Kill kills the buildx server.
42-
Kill(ctx context.Context) error
43-
44-
// List lists sessions.
45-
List(ctx context.Context) (refs []string, _ error)
46-
47-
// ListPrcesses lists processes in the attached session.
48-
ListProcesses(ctx context.Context) (infos []*controllerapi.ProcessInfo, retErr error)
49-
50-
// DisconnectProcess finishes the specified process.
51-
DisconnectProcess(ctx context.Context, pid string) error
52-
53-
// Inspect returns information about the attached build.
54-
Inspect(ctx context.Context) (*controllerapi.InspectResponse, error)
55-
56-
// Disconnect finishes the attached session.
57-
Disconnect(ctx context.Context) error
5837
}
5938

6039
// CommandInfo is information about a command.

0 commit comments

Comments
 (0)