Skip to content

Commit 5426244

Browse files
committed
refactor: reduce context.Background()
Signed-off-by: ashwat287 <[email protected]>
1 parent cd685e5 commit 5426244

33 files changed

+175
-145
lines changed

cmd/limactl/clone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func cloneAction(cmd *cobra.Command, args []string) error {
7171
if err != nil {
7272
return err
7373
}
74-
yBytes, err := yqutil.EvaluateExpression(yq, yContent)
74+
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
7575
if err != nil {
7676
return err
7777
}

cmd/limactl/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func deleteAction(cmd *cobra.Command, args []string) error {
4747
}
4848
return err
4949
}
50-
if err := instance.Delete(cmd.Context(), inst, force); err != nil {
50+
if err := instance.Delete(ctx, inst, force); err != nil {
5151
return fmt.Errorf("failed to delete instance %q: %w", instName, err)
5252
}
5353
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
@@ -60,7 +60,7 @@ func deleteAction(cmd *cobra.Command, args []string) error {
6060
}
6161
logrus.Infof("Deleted %q (%q)", instName, inst.Dir)
6262
}
63-
return networks.Reconcile(cmd.Context(), "")
63+
return networks.Reconcile(ctx, "")
6464
}
6565

6666
func deleteBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

cmd/limactl/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func editAction(cmd *cobra.Command, args []string) error {
8787
var yBytes []byte
8888
if len(yqExprs) > 0 {
8989
yq := yqutil.Join(yqExprs)
90-
yBytes, err = yqutil.EvaluateExpression(yq, yContent)
90+
yBytes, err = yqutil.EvaluateExpression(ctx, yq, yContent)
9191
if err != nil {
9292
return err
9393
}

cmd/limactl/hostagent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
131131
logrus.WithError(serveErr).Warn("hostagent API server exited with an error")
132132
}
133133
}()
134-
return ha.Run(cmd.Context())
134+
return ha.Run(ctx)
135135
}
136136

137137
// syncer is implemented by *os.File.

cmd/limactl/network.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package main
55

66
import (
7+
"context"
78
"encoding/json"
89
"errors"
910
"fmt"
@@ -174,6 +175,8 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
174175
return err
175176
}
176177

178+
ctx := cmd.Context()
179+
177180
switch mode {
178181
case networks.ModeBridged:
179182
if gateway != "" {
@@ -183,7 +186,7 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
183186
return fmt.Errorf("network mode %q requires specifying interface", mode)
184187
}
185188
yq := fmt.Sprintf(`.networks.%q = {"mode":%q,"interface":%q}`, name, mode, intf)
186-
return networkApplyYQ(yq)
189+
return networkApplyYQ(ctx, yq)
187190
default:
188191
if gateway == "" {
189192
return fmt.Errorf("network mode %q requires specifying gateway", mode)
@@ -208,11 +211,11 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
208211
// TODO: check IP range collision
209212

210213
yq := fmt.Sprintf(`.networks.%q = {"mode":%q,"gateway":%q,"netmask":%q,"interface":%q}`, name, mode, gwIP.String(), gwMaskStr, intf)
211-
return networkApplyYQ(yq)
214+
return networkApplyYQ(ctx, yq)
212215
}
213216
}
214217

215-
func networkApplyYQ(yq string) error {
218+
func networkApplyYQ(ctx context.Context, yq string) error {
216219
filePath, err := networks.ConfigFile()
217220
if err != nil {
218221
return err
@@ -221,7 +224,7 @@ func networkApplyYQ(yq string) error {
221224
if err != nil {
222225
return err
223226
}
224-
yBytes, err := yqutil.EvaluateExpression(yq, yContent)
227+
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
225228
if err != nil {
226229
return err
227230
}
@@ -263,7 +266,7 @@ func networkDeleteAction(cmd *cobra.Command, args []string) error {
263266
yq += " | "
264267
}
265268
}
266-
return networkApplyYQ(yq)
269+
return networkApplyYQ(cmd.Context(), yq)
267270
}
268271

269272
func networkBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

cmd/limactl/start.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
304304
return nil, err
305305
}
306306
} else {
307-
tmpl, err = limatmpl.Read(cmd.Context(), name, arg)
307+
tmpl, err = limatmpl.Read(ctx, name, arg)
308308
if err != nil {
309309
return nil, err
310310
}
@@ -318,7 +318,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
318318
}
319319
}
320320

321-
if err := tmpl.Embed(cmd.Context(), true, true); err != nil {
321+
if err := tmpl.Embed(ctx, true, true); err != nil {
322322
return nil, err
323323
}
324324
yqExprs, err := editflags.YQExpressions(flags, true)
@@ -328,18 +328,18 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
328328
yq := yqutil.Join(yqExprs)
329329
if tty {
330330
var err error
331-
tmpl, err = chooseNextCreatorState(cmd.Context(), tmpl, yq)
331+
tmpl, err = chooseNextCreatorState(ctx, tmpl, yq)
332332
if err != nil {
333333
return nil, err
334334
}
335335
} else {
336336
logrus.Info("Terminal is not available, proceeding without opening an editor")
337-
if err := modifyInPlace(tmpl, yq); err != nil {
337+
if err := modifyInPlace(ctx, tmpl, yq); err != nil {
338338
return nil, err
339339
}
340340
}
341341
saveBrokenYAML := tty
342-
return instance.Create(cmd.Context(), tmpl.Name, tmpl.Bytes, saveBrokenYAML)
342+
return instance.Create(ctx, tmpl.Name, tmpl.Bytes, saveBrokenYAML)
343343
}
344344

345345
func applyYQExpressionToExistingInstance(ctx context.Context, inst *store.Instance, yq string) (*store.Instance, error) {
@@ -352,7 +352,7 @@ func applyYQExpressionToExistingInstance(ctx context.Context, inst *store.Instan
352352
return nil, err
353353
}
354354
logrus.Debugf("Applying yq expression %q to an existing instance %q", yq, inst.Name)
355-
yBytes, err := yqutil.EvaluateExpression(yq, yContent)
355+
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
356356
if err != nil {
357357
return nil, err
358358
}
@@ -375,8 +375,8 @@ func applyYQExpressionToExistingInstance(ctx context.Context, inst *store.Instan
375375
return store.Inspect(ctx, inst.Name)
376376
}
377377

378-
func modifyInPlace(st *limatmpl.Template, yq string) error {
379-
out, err := yqutil.EvaluateExpression(yq, st.Bytes)
378+
func modifyInPlace(ctx context.Context, st *limatmpl.Template, yq string) error {
379+
out, err := yqutil.EvaluateExpression(ctx, yq, st.Bytes)
380380
if err != nil {
381381
return err
382382
}
@@ -401,7 +401,7 @@ func (exitSuccessError) ExitCode() int {
401401

402402
func chooseNextCreatorState(ctx context.Context, tmpl *limatmpl.Template, yq string) (*limatmpl.Template, error) {
403403
for {
404-
if err := modifyInPlace(tmpl, yq); err != nil {
404+
if err := modifyInPlace(ctx, tmpl, yq); err != nil {
405405
logrus.WithError(err).Warn("Failed to evaluate yq expression")
406406
return tmpl, err
407407
}

cmd/limactl/template.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func templateCopyAction(cmd *cobra.Command, args []string) error {
119119
if embed && verbatim {
120120
return errors.New("--verbatim cannot be used with any of --embed, --embed-all, or --fill")
121121
}
122-
tmpl, err := limatmpl.Read(cmd.Context(), "", source)
122+
tmpl, err := limatmpl.Read(ctx, "", source)
123123
if err != nil {
124124
return err
125125
}
@@ -129,11 +129,11 @@ func templateCopyAction(cmd *cobra.Command, args []string) error {
129129
if !verbatim {
130130
if embed {
131131
// Embed default base.yaml only when fill is true.
132-
if err := tmpl.Embed(cmd.Context(), embedAll, fill); err != nil {
132+
if err := tmpl.Embed(ctx, embedAll, fill); err != nil {
133133
return err
134134
}
135135
} else {
136-
if err := tmpl.UseAbsLocators(); err != nil {
136+
if err := tmpl.UseAbsLocators(ctx); err != nil {
137137
return err
138138
}
139139
}
@@ -182,14 +182,14 @@ func templateYQAction(cmd *cobra.Command, args []string) error {
182182
ctx := cmd.Context()
183183
locator := args[0]
184184
expr := args[1]
185-
tmpl, err := limatmpl.Read(cmd.Context(), "", locator)
185+
tmpl, err := limatmpl.Read(ctx, "", locator)
186186
if err != nil {
187187
return err
188188
}
189189
if len(tmpl.Bytes) == 0 {
190190
return fmt.Errorf("don't know how to interpret %q as a template locator", locator)
191191
}
192-
if err := tmpl.Embed(cmd.Context(), true, true); err != nil {
192+
if err := tmpl.Embed(ctx, true, true); err != nil {
193193
return err
194194
}
195195
if err := fillDefaults(ctx, tmpl); err != nil {
@@ -225,7 +225,7 @@ func templateValidateAction(cmd *cobra.Command, args []string) error {
225225
}
226226

227227
for _, arg := range args {
228-
tmpl, err := limatmpl.Read(cmd.Context(), "", arg)
228+
tmpl, err := limatmpl.Read(ctx, "", arg)
229229
if err != nil {
230230
return err
231231
}
@@ -236,7 +236,7 @@ func templateValidateAction(cmd *cobra.Command, args []string) error {
236236
return fmt.Errorf("can't determine instance name from template locator %q", arg)
237237
}
238238
// Embed default base.yaml only when fill is true.
239-
if err := tmpl.Embed(cmd.Context(), true, fill); err != nil {
239+
if err := tmpl.Embed(ctx, true, fill); err != nil {
240240
return err
241241
}
242242
// Load() will merge the template with override.yaml and default.yaml via FillDefaults().

pkg/driver/driver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type Lifecycle interface {
4040
type GUI interface {
4141
// RunGUI is for starting GUI synchronously by hostagent. This method should be wait and return only after vm terminates
4242
// It returns error if there are any failures
43-
RunGUI() error
43+
RunGUI(ctx context.Context) error
4444

4545
ChangeDisplayPassword(ctx context.Context, password string) error
4646
DisplayConnection(ctx context.Context) (string, error)
@@ -63,7 +63,7 @@ type Registration interface {
6363
// GuestAgent defines operations for the guest agent.
6464
type GuestAgent interface {
6565
// ForwardGuestAgent returns if the guest agent sock needs forwarding by host agent.
66-
ForwardGuestAgent() bool
66+
ForwardGuestAgent(_ context.Context) bool
6767

6868
// GuestAgentConn returns the guest agent connection, or nil (if forwarded by ssh).
6969
GuestAgentConn(_ context.Context) (net.Conn, string, error)
@@ -77,10 +77,10 @@ type Driver interface {
7777
Registration
7878
GuestAgent
7979

80-
Info() Info
80+
Info(_ context.Context) Info
8181

8282
// SetConfig sets the configuration for the instance.
83-
Configure(inst *store.Instance) *ConfiguredDriver
83+
Configure(_ context.Context, inst *store.Instance) *ConfiguredDriver
8484
}
8585

8686
type ConfiguredDriver struct {

pkg/driver/external/client/methods.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ func (d *DriverClient) Stop(ctx context.Context) error {
102102
return nil
103103
}
104104

105-
func (d *DriverClient) RunGUI() error {
105+
func (d *DriverClient) RunGUI(ctx context.Context) error {
106106
d.logger.Debug("Running GUI for the driver instance")
107107

108-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
108+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
109109
defer cancel()
110110

111111
_, err := d.DriverSvc.RunGUI(ctx, &emptypb.Empty{})
@@ -230,10 +230,10 @@ func (d *DriverClient) Unregister(ctx context.Context) error {
230230
return nil
231231
}
232232

233-
func (d *DriverClient) ForwardGuestAgent() bool {
233+
func (d *DriverClient) ForwardGuestAgent(ctx context.Context) bool {
234234
d.logger.Debug("Checking if guest agent needs to be forwarded")
235235

236-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
236+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
237237
defer cancel()
238238

239239
resp, err := d.DriverSvc.ForwardGuestAgent(ctx, &emptypb.Empty{})
@@ -256,10 +256,10 @@ func (d *DriverClient) GuestAgentConn(ctx context.Context) (net.Conn, string, er
256256
return nil, "", nil
257257
}
258258

259-
func (d *DriverClient) Info() driver.Info {
259+
func (d *DriverClient) Info(ctx context.Context) driver.Info {
260260
d.logger.Debug("Getting driver info")
261261

262-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
262+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
263263
defer cancel()
264264

265265
resp, err := d.DriverSvc.GetInfo(ctx, &emptypb.Empty{})
@@ -278,7 +278,7 @@ func (d *DriverClient) Info() driver.Info {
278278
return info
279279
}
280280

281-
func (d *DriverClient) Configure(inst *store.Instance) *driver.ConfiguredDriver {
281+
func (d *DriverClient) Configure(ctx context.Context, inst *store.Instance) *driver.ConfiguredDriver {
282282
d.logger.Debugf("Setting config for instance %s with SSH local port %d", inst.Name, inst.SSHLocalPort)
283283

284284
instJSON, err := inst.MarshalJSON()
@@ -287,7 +287,7 @@ func (d *DriverClient) Configure(inst *store.Instance) *driver.ConfiguredDriver
287287
return nil
288288
}
289289

290-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
290+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
291291
defer cancel()
292292

293293
_, err = d.DriverSvc.SetConfig(ctx, &pb.SetConfigRequest{

pkg/driver/external/server/methods.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (s *DriverServer) Start(_ *emptypb.Empty, stream pb.Driver_StartServer) err
5353
}
5454
}
5555

56-
func (s *DriverServer) SetConfig(_ context.Context, req *pb.SetConfigRequest) (*emptypb.Empty, error) {
56+
func (s *DriverServer) SetConfig(ctx context.Context, req *pb.SetConfigRequest) (*emptypb.Empty, error) {
5757
s.logger.Debugf("Received SetConfig request")
5858
var inst store.Instance
5959

@@ -62,7 +62,7 @@ func (s *DriverServer) SetConfig(_ context.Context, req *pb.SetConfigRequest) (*
6262
return &emptypb.Empty{}, err
6363
}
6464

65-
_ = s.driver.Configure(&inst)
65+
_ = s.driver.Configure(ctx, &inst)
6666

6767
return &emptypb.Empty{}, nil
6868
}
@@ -76,7 +76,7 @@ func (s *DriverServer) GuestAgentConn(ctx context.Context, _ *emptypb.Empty) (*e
7676
}
7777

7878
if connType != "unix" {
79-
proxySocketPath := filepath.Join(s.driver.Info().InstanceDir, filenames.GuestAgentSock)
79+
proxySocketPath := filepath.Join(s.driver.Info(ctx).InstanceDir, filenames.GuestAgentSock)
8080

8181
var lc net.ListenConfig
8282
listener, err := lc.Listen(ctx, "unix", proxySocketPath)
@@ -102,9 +102,9 @@ func (s *DriverServer) GuestAgentConn(ctx context.Context, _ *emptypb.Empty) (*e
102102
return &emptypb.Empty{}, nil
103103
}
104104

105-
func (s *DriverServer) GetInfo(_ context.Context, _ *emptypb.Empty) (*pb.InfoResponse, error) {
105+
func (s *DriverServer) GetInfo(ctx context.Context, _ *emptypb.Empty) (*pb.InfoResponse, error) {
106106
s.logger.Debug("Received GetInfo request")
107-
info := s.driver.Info()
107+
info := s.driver.Info(ctx)
108108

109109
infoJSON, err := json.Marshal(info)
110110
if err != nil {
@@ -161,9 +161,9 @@ func (s *DriverServer) Stop(ctx context.Context, empty *emptypb.Empty) (*emptypb
161161
return empty, nil
162162
}
163163

164-
func (s *DriverServer) RunGUI(_ context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) {
164+
func (s *DriverServer) RunGUI(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) {
165165
s.logger.Debug("Received RunGUI request")
166-
err := s.driver.RunGUI()
166+
err := s.driver.RunGUI(ctx)
167167
if err != nil {
168168
s.logger.Errorf("RunGUI failed: %v", err)
169169
return empty, err
@@ -260,7 +260,7 @@ func (s *DriverServer) Unregister(ctx context.Context, empty *emptypb.Empty) (*e
260260
return empty, nil
261261
}
262262

263-
func (s *DriverServer) ForwardGuestAgent(_ context.Context, _ *emptypb.Empty) (*pb.ForwardGuestAgentResponse, error) {
263+
func (s *DriverServer) ForwardGuestAgent(ctx context.Context, _ *emptypb.Empty) (*pb.ForwardGuestAgentResponse, error) {
264264
s.logger.Debug("Received ForwardGuestAgent request")
265-
return &pb.ForwardGuestAgentResponse{ShouldForward: s.driver.ForwardGuestAgent()}, nil
265+
return &pb.ForwardGuestAgentResponse{ShouldForward: s.driver.ForwardGuestAgent(ctx)}, nil
266266
}

0 commit comments

Comments
 (0)