Skip to content

Commit

Permalink
chore: make using action tracker easier
Browse files Browse the repository at this point in the history
Refactor so that action tracker accepts an interface.

Signed-off-by: Noel Georgi <git@frezbo.dev>
  • Loading branch information
frezbo committed Jul 11, 2024
1 parent 0aebeff commit a727a1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
26 changes: 19 additions & 7 deletions cmd/talosctl/pkg/talos/action/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"google.golang.org/grpc/status"

"github.com/siderolabs/talos/cmd/talosctl/cmd/common"
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/global"
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers"
machineapi "github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/client"
Expand Down Expand Up @@ -92,7 +91,7 @@ type Tracker struct {
timeout time.Duration
isTerminal bool
debug bool
cliContext *global.Args
clientExecutor ClientExecutor
}

// TrackerOption is the functional option for the Tracker.
Expand All @@ -119,21 +118,28 @@ func WithDebug(debug bool) TrackerOption {
}
}

// WithTerminalOverride sets the terminal override.
func WithTerminalOverride(isTerminal bool) TrackerOption {
return func(t *Tracker) {
t.isTerminal = isTerminal
}
}

// NewTracker creates a new Tracker.
func NewTracker(
cliContext *global.Args,
clientExecutor ClientExecutor,
expectedEventFn func(event client.EventResult) bool,
actionFn func(ctx context.Context, c *client.Client) (string, error),
opts ...TrackerOption,
) *Tracker {
tracker := Tracker{
expectedEventFn: expectedEventFn,
actionFn: actionFn,
nodeToLatestStatusUpdate: make(map[string]reporter.Update, len(cliContext.Nodes)),
nodeToLatestStatusUpdate: make(map[string]reporter.Update, len(clientExecutor.NodeList())),
reporter: reporter.New(),
reportCh: make(chan nodeUpdate),
isTerminal: isatty.IsTerminal(os.Stderr.Fd()),
cliContext: cliContext,
clientExecutor: clientExecutor,
}

for _, option := range opts {
Expand All @@ -143,6 +149,12 @@ func NewTracker(
return &tracker
}

// ClientExecutor is the interface for the client executor.
type ClientExecutor interface {
WithClient(action func(context.Context, *client.Client) error, dialOptions ...grpc.DialOption) error
NodeList() []string
}

// Run executes the action on nodes and tracks its progress by watching events with retries.
// After receiving the expected event, if provided, it tracks the progress by running the post check with retries.
//
Expand All @@ -152,7 +164,7 @@ func (a *Tracker) Run() error {

var eg errgroup.Group

err := a.cliContext.WithClient(func(ctx context.Context, c *client.Client) error {
err := a.clientExecutor.WithClient(func(ctx context.Context, c *client.Client) error {
ctx, cancel := context.WithTimeout(ctx, a.timeout)
defer cancel()

Expand All @@ -170,7 +182,7 @@ func (a *Tracker) Run() error {

var trackEg errgroup.Group

for _, node := range a.cliContext.Nodes {
for _, node := range a.clientExecutor.NodeList() {
var (
dmesg *circular.Buffer
err error
Expand Down
5 changes: 5 additions & 0 deletions cmd/talosctl/pkg/talos/global/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type Args struct {
Endpoints []string
}

// NodeList returns the list of nodes to run the command against.
func (c *Args) NodeList() []string {
return c.Nodes
}

// WithClientNoNodes wraps common code to initialize Talos client and provide cancellable context.
//
// WithClientNoNodes doesn't set any node information on the request context.
Expand Down

0 comments on commit a727a1d

Please sign in to comment.