Skip to content

Commit

Permalink
Use channels to pass data to the debug tui (#75)
Browse files Browse the repository at this point in the history
* Move data channel out of handler

Signed-off-by: Evan Harris <[email protected]>

* Mark TODO

Signed-off-by: Evan Harris <[email protected]>

* Add debug key bindings

Signed-off-by: Evan Harris <[email protected]>

* Add TUI flags

Signed-off-by: Evan Harris <[email protected]>

* Add TUI flags to debug cli

Signed-off-by: Evan Harris <[email protected]>

* Add debug event

Signed-off-by: Evan Harris <[email protected]>

* Implement debug tui with channel based data flow

Signed-off-by: Evan Harris <[email protected]>

* Open debug tui from home tui

Signed-off-by: Evan Harris <[email protected]>

* Rm uused flags

Signed-off-by: Evan Harris <[email protected]>

* Allow debug --tui invocation with no other flags

Signed-off-by: Evan Harris <[email protected]>

* Support use of debug --tui to render debuggable containers

Signed-off-by: Evan Harris <[email protected]>

* Fix spelling

Signed-off-by: Evan Harris <[email protected]>

* Change name from data -> subscription

Signed-off-by: Evan Harris <[email protected]>

* Improve comment and fix typo

Signed-off-by: Evan Harris <[email protected]>

---------

Signed-off-by: Evan Harris <[email protected]>
  • Loading branch information
eharris128 authored Oct 28, 2024
1 parent 7081b05 commit bcb4e34
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 88 deletions.
50 changes: 38 additions & 12 deletions pkg/app/execontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
)

const (
ofJSON = "json"
ofText = "text"
ofJSON = "json"
ofText = "text"
ofSubscription = "subscription"
)

type ExecutionContext struct {
Expand Down Expand Up @@ -122,20 +123,34 @@ func NewExecutionContext(
}

type Output struct {
CmdName string
Quiet bool
OutputFormat string
DataChannels map[string]chan interface{}
CmdName string
Quiet bool
OutputFormat string
DataChannels map[string]chan interface{}
internalDataCh chan interface{}
}

func NewOutput(cmdName string, quiet bool, outputFormat string, channels map[string]chan interface{}) *Output {
ref := &Output{
CmdName: cmdName,
Quiet: quiet,
OutputFormat: outputFormat,
DataChannels: channels,
CmdName: cmdName,
Quiet: quiet,
OutputFormat: outputFormat,
DataChannels: channels,
internalDataCh: make(chan interface{}),
}

// We want to listen to the internal channel for any data
// And dump it onto the appropriate DataChannels
go func() {
for data := range ref.internalDataCh {
if data != nil {
for _, ch := range ref.DataChannels {
ch <- data
}
}
}
}()

return ref
}

Expand Down Expand Up @@ -272,6 +287,15 @@ func (ref *Output) Message(data string) {

}

func (ref *Output) Data(channelKey string, data interface{}) {
if ch, exists := ref.DataChannels[channelKey]; exists {
ch <- data // Send data to the corresponding channel
fmt.Printf("Data sent to channel '%s': %v\n", channelKey, data)
} else {
fmt.Printf("Channel for channelKey '%s' not found\n", channelKey)
}
}

func (ref *Output) State(state string, params ...OutVars) {
if ref.Quiet {
return
Expand Down Expand Up @@ -344,7 +368,8 @@ var (
)

func (ref *Output) Info(infoType string, params ...OutVars) {
if ref.Quiet {
// TODO - carry this pattern to other Output methods
if ref.Quiet && ref.OutputFormat != ofSubscription {
return
}

Expand Down Expand Up @@ -379,7 +404,8 @@ func (ref *Output) Info(infoType string, params ...OutVars) {
fmt.Println(string(jsonData))
case ofText:
fmt.Printf("cmd=%s info=%s%s%s\n", ref.CmdName, itcolor(infoType), sep, data)

case ofSubscription:
ref.internalDataCh <- msg // Send data to the internal channel
default:
log.Fatalf("Unknown console output flag: %s\n. It should be either 'text' or 'json", ref.OutputFormat)
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/app/master/command/cliflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ const (
FlagContainerDNSSearchUsage = "Add a dns search domain for unqualified hostnames analyzing image at runtime"
FlagMountUsage = "Mount volume analyzing image"
FlagDeleteFatImageUsage = "Delete generated fat image requires --dockerfile flag"

// TUI Related flags
FlagTUI = "tui"
FlagTUIUsage = "Enable terminal user interface mode"
)

// Container runtime command flag names and usage descriptions
Expand Down Expand Up @@ -1026,6 +1030,12 @@ var CommonFlags = map[string]cli.Flag{
Usage: FlagRuntimeUsage,
EnvVars: []string{"DSLIM_CRT_NAME"},
},
// TUI Mode
FlagTUI: &cli.BoolFlag{
Name: FlagTUI,
Usage: FlagTUIUsage,
EnvVars: []string{"DSLIM_TUI"},
},
}

//var CommonFlags
Expand Down
14 changes: 14 additions & 0 deletions pkg/app/master/command/debug/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/mintoolkit/mint/pkg/app"
"github.com/mintoolkit/mint/pkg/app/master/command"
"github.com/mintoolkit/mint/pkg/app/master/tui"
"github.com/mintoolkit/mint/pkg/crt"
)

Expand Down Expand Up @@ -86,6 +87,8 @@ type CommandParams struct {
UseSecurityContextFromTarget bool
/// fallback to using target container user if it's non-root (mostly for kubernetes)
DoFallbackToTargetUser bool
// `debug --tui` use mode`
TUI bool
}

func ParseNameValueList(list []string) []NVPair {
Expand Down Expand Up @@ -166,9 +169,19 @@ var CLI = &cli.Command{
cflag(FlagRunPrivileged),
cflag(FlagSecurityContextFromTarget),
cflag(FlagFallbackToTargetUser),
command.Cflag(command.FlagTUI),
},
Action: func(ctx *cli.Context) error {
gcvalues := command.GlobalFlagValues(ctx)

// If we stick with this approach, the user should be communicated to
// use `--tui` as a standalone flag for `debug`
if ctx.Bool(command.FlagTUI) {
initialTUI := InitialTUI(true, gcvalues)
tui.RunTUI(initialTUI, true)
return nil
}

xc := app.NewExecutionContext(
Name,
gcvalues.QuietCLIMode,
Expand Down Expand Up @@ -209,6 +222,7 @@ var CLI = &cli.Command{
DoRunPrivileged: ctx.Bool(FlagRunPrivileged),
UseSecurityContextFromTarget: ctx.Bool(FlagSecurityContextFromTarget),
DoFallbackToTargetUser: ctx.Bool(FlagFallbackToTargetUser),
TUI: ctx.Bool(command.FlagTUI),
}

if commandParams.ActionListNamespaces &&
Expand Down
1 change: 1 addition & 0 deletions pkg/app/master/command/debug/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func OnCommand(

resolved := command.ResolveAutoRuntime(commandParams.Runtime)
logger.Tracef("runtime.handler: rt=%s resolved=%s", commandParams.Runtime, resolved)

switch resolved {
case crt.DockerRuntime:
client, err := dockerclient.New(gparams.ClientConfig)
Expand Down
Loading

0 comments on commit bcb4e34

Please sign in to comment.