Skip to content

Commit a727a1d

Browse files
committed
chore: make using action tracker easier
Refactor so that action tracker accepts an interface. Signed-off-by: Noel Georgi <[email protected]>
1 parent 0aebeff commit a727a1d

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

cmd/talosctl/pkg/talos/action/tracker.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"google.golang.org/grpc/status"
2727

2828
"github.com/siderolabs/talos/cmd/talosctl/cmd/common"
29-
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/global"
3029
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers"
3130
machineapi "github.com/siderolabs/talos/pkg/machinery/api/machine"
3231
"github.com/siderolabs/talos/pkg/machinery/client"
@@ -92,7 +91,7 @@ type Tracker struct {
9291
timeout time.Duration
9392
isTerminal bool
9493
debug bool
95-
cliContext *global.Args
94+
clientExecutor ClientExecutor
9695
}
9796

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

121+
// WithTerminalOverride sets the terminal override.
122+
func WithTerminalOverride(isTerminal bool) TrackerOption {
123+
return func(t *Tracker) {
124+
t.isTerminal = isTerminal
125+
}
126+
}
127+
122128
// NewTracker creates a new Tracker.
123129
func NewTracker(
124-
cliContext *global.Args,
130+
clientExecutor ClientExecutor,
125131
expectedEventFn func(event client.EventResult) bool,
126132
actionFn func(ctx context.Context, c *client.Client) (string, error),
127133
opts ...TrackerOption,
128134
) *Tracker {
129135
tracker := Tracker{
130136
expectedEventFn: expectedEventFn,
131137
actionFn: actionFn,
132-
nodeToLatestStatusUpdate: make(map[string]reporter.Update, len(cliContext.Nodes)),
138+
nodeToLatestStatusUpdate: make(map[string]reporter.Update, len(clientExecutor.NodeList())),
133139
reporter: reporter.New(),
134140
reportCh: make(chan nodeUpdate),
135141
isTerminal: isatty.IsTerminal(os.Stderr.Fd()),
136-
cliContext: cliContext,
142+
clientExecutor: clientExecutor,
137143
}
138144

139145
for _, option := range opts {
@@ -143,6 +149,12 @@ func NewTracker(
143149
return &tracker
144150
}
145151

152+
// ClientExecutor is the interface for the client executor.
153+
type ClientExecutor interface {
154+
WithClient(action func(context.Context, *client.Client) error, dialOptions ...grpc.DialOption) error
155+
NodeList() []string
156+
}
157+
146158
// Run executes the action on nodes and tracks its progress by watching events with retries.
147159
// After receiving the expected event, if provided, it tracks the progress by running the post check with retries.
148160
//
@@ -152,7 +164,7 @@ func (a *Tracker) Run() error {
152164

153165
var eg errgroup.Group
154166

155-
err := a.cliContext.WithClient(func(ctx context.Context, c *client.Client) error {
167+
err := a.clientExecutor.WithClient(func(ctx context.Context, c *client.Client) error {
156168
ctx, cancel := context.WithTimeout(ctx, a.timeout)
157169
defer cancel()
158170

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

171183
var trackEg errgroup.Group
172184

173-
for _, node := range a.cliContext.Nodes {
185+
for _, node := range a.clientExecutor.NodeList() {
174186
var (
175187
dmesg *circular.Buffer
176188
err error

cmd/talosctl/pkg/talos/global/client.go

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ type Args struct {
2828
Endpoints []string
2929
}
3030

31+
// NodeList returns the list of nodes to run the command against.
32+
func (c *Args) NodeList() []string {
33+
return c.Nodes
34+
}
35+
3136
// WithClientNoNodes wraps common code to initialize Talos client and provide cancellable context.
3237
//
3338
// WithClientNoNodes doesn't set any node information on the request context.

0 commit comments

Comments
 (0)