Skip to content

Commit c023379

Browse files
authored
Merge pull request #2130 from alexandear/enable-noctx
Enable noctx linter and fix up issues
2 parents e944541 + 04c7533 commit c023379

File tree

19 files changed

+78
-71
lines changed

19 files changed

+78
-71
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ linters:
6464
# - maligned
6565
# - nestif
6666
# - nlreturn
67-
# - noctx
67+
- noctx
6868
- nolintlint
6969
# - rowserrcheck
7070
# - scopelint

cmd/limactl/start.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
146146
}
147147
}
148148
logrus.Debugf("interpreting argument %q as a http url for instance %q", arg, st.instName)
149-
resp, err := http.Get(arg)
149+
req, err := http.NewRequestWithContext(cmd.Context(), http.MethodGet, arg, http.NoBody)
150+
if err != nil {
151+
return nil, err
152+
}
153+
resp, err := http.DefaultClient.Do(req)
150154
if err != nil {
151155
return nil, err
152156
}

pkg/downloader/downloader.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package downloader
22

33
import (
44
"bytes"
5+
"context"
56
"crypto/sha256"
67
"errors"
78
"fmt"
@@ -15,6 +16,7 @@ import (
1516

1617
"github.com/cheggaaa/pb/v3"
1718
"github.com/containerd/continuity/fs"
19+
"github.com/lima-vm/lima/pkg/httpclientutil"
1820
"github.com/lima-vm/lima/pkg/localpathutil"
1921
"github.com/lima-vm/lima/pkg/progressbar"
2022
"github.com/opencontainers/go-digest"
@@ -125,7 +127,7 @@ func WithExpectedDigest(expectedDigest digest.Digest) Opt {
125127
// (So, the local path cannot be set to /dev/null for "caching only" mode.)
126128
//
127129
// The local path can be an empty string for "caching only" mode.
128-
func Download(local, remote string, opts ...Opt) (*Result, error) {
130+
func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result, error) {
129131
var o options
130132
for _, f := range opts {
131133
if err := f(&o); err != nil {
@@ -173,7 +175,7 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
173175
}
174176

175177
if o.cacheDir == "" {
176-
if err := downloadHTTP(localPath, remote, o.description, o.expectedDigest); err != nil {
178+
if err := downloadHTTP(ctx, localPath, remote, o.description, o.expectedDigest); err != nil {
177179
return nil, err
178180
}
179181
res := &Result{
@@ -222,7 +224,7 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
222224
if err := os.WriteFile(shadURL, []byte(remote), 0o644); err != nil {
223225
return nil, err
224226
}
225-
if err := downloadHTTP(shadData, remote, o.description, o.expectedDigest); err != nil {
227+
if err := downloadHTTP(ctx, shadData, remote, o.description, o.expectedDigest); err != nil {
226228
return nil, err
227229
}
228230
// no need to pass the digest to copyLocal(), as we already verified the digest
@@ -472,7 +474,7 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err
472474
return nil
473475
}
474476

475-
func downloadHTTP(localPath, url, description string, expectedDigest digest.Digest) error {
477+
func downloadHTTP(ctx context.Context, localPath, url, description string, expectedDigest digest.Digest) error {
476478
if localPath == "" {
477479
return fmt.Errorf("downloadHTTP: got empty localPath")
478480
}
@@ -487,14 +489,11 @@ func downloadHTTP(localPath, url, description string, expectedDigest digest.Dige
487489
}
488490
defer fileWriter.Close()
489491

490-
resp, err := http.Get(url)
492+
resp, err := httpclientutil.Get(ctx, http.DefaultClient, url)
491493
if err != nil {
492494
return err
493495
}
494496
defer resp.Body.Close()
495-
if resp.StatusCode != http.StatusOK {
496-
return fmt.Errorf("expected HTTP status %d, got %s", http.StatusOK, resp.Status)
497-
}
498497
bar, err := progressbar.New(resp.ContentLength)
499498
if err != nil {
500499
return err

pkg/downloader/downloader_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package downloader
22

33
import (
4+
"context"
45
"net/http"
56
"net/http/httptest"
67
"os"
@@ -28,61 +29,61 @@ func TestDownloadRemote(t *testing.T) {
2829
t.Run("without cache", func(t *testing.T) {
2930
t.Run("without digest", func(t *testing.T) {
3031
localPath := filepath.Join(t.TempDir(), t.Name())
31-
r, err := Download(localPath, dummyRemoteFileURL)
32+
r, err := Download(context.Background(), localPath, dummyRemoteFileURL)
3233
assert.NilError(t, err)
3334
assert.Equal(t, StatusDownloaded, r.Status)
3435

3536
// download again, make sure StatusSkippedIsReturned
36-
r, err = Download(localPath, dummyRemoteFileURL)
37+
r, err = Download(context.Background(), localPath, dummyRemoteFileURL)
3738
assert.NilError(t, err)
3839
assert.Equal(t, StatusSkipped, r.Status)
3940
})
4041
t.Run("with digest", func(t *testing.T) {
4142
wrongDigest := digest.Digest("sha256:8313944efb4f38570c689813f288058b674ea6c487017a5a4738dc674b65f9d9")
4243
localPath := filepath.Join(t.TempDir(), t.Name())
43-
_, err := Download(localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest))
44+
_, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest))
4445
assert.ErrorContains(t, err, "expected digest")
4546

46-
r, err := Download(localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
47+
r, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
4748
assert.NilError(t, err)
4849
assert.Equal(t, StatusDownloaded, r.Status)
4950

50-
r, err = Download(localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
51+
r, err = Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
5152
assert.NilError(t, err)
5253
assert.Equal(t, StatusSkipped, r.Status)
5354
})
5455
})
5556
t.Run("with cache", func(t *testing.T) {
5657
cacheDir := filepath.Join(t.TempDir(), "cache")
5758
localPath := filepath.Join(t.TempDir(), t.Name())
58-
r, err := Download(localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
59+
r, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
5960
assert.NilError(t, err)
6061
assert.Equal(t, StatusDownloaded, r.Status)
6162

62-
r, err = Download(localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
63+
r, err = Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
6364
assert.NilError(t, err)
6465
assert.Equal(t, StatusSkipped, r.Status)
6566

6667
localPath2 := localPath + "-2"
67-
r, err = Download(localPath2, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
68+
r, err = Download(context.Background(), localPath2, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
6869
assert.NilError(t, err)
6970
assert.Equal(t, StatusUsedCache, r.Status)
7071
})
7172
t.Run("caching-only mode", func(t *testing.T) {
72-
_, err := Download("", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
73+
_, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
7374
assert.ErrorContains(t, err, "cache directory to be specified")
7475

7576
cacheDir := filepath.Join(t.TempDir(), "cache")
76-
r, err := Download("", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
77+
r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
7778
assert.NilError(t, err)
7879
assert.Equal(t, StatusDownloaded, r.Status)
7980

80-
r, err = Download("", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
81+
r, err = Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
8182
assert.NilError(t, err)
8283
assert.Equal(t, StatusUsedCache, r.Status)
8384

8485
localPath := filepath.Join(t.TempDir(), t.Name())
85-
r, err = Download(localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
86+
r, err = Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
8687
assert.NilError(t, err)
8788
assert.Equal(t, StatusUsedCache, r.Status)
8889
})
@@ -91,7 +92,7 @@ func TestDownloadRemote(t *testing.T) {
9192
assert.ErrorContains(t, err, "cache directory to be specified")
9293

9394
cacheDir := filepath.Join(t.TempDir(), "cache")
94-
r, err := Download("", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
95+
r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
9596
assert.NilError(t, err)
9697
assert.Equal(t, StatusDownloaded, r.Status)
9798

@@ -118,7 +119,7 @@ func TestDownloadLocal(t *testing.T) {
118119
t.Cleanup(func() { _ = f.Close() })
119120
testLocalFileURL := "file://" + localFile
120121

121-
r, err := Download(localPath, testLocalFileURL)
122+
r, err := Download(context.Background(), localPath, testLocalFileURL)
122123
assert.NilError(t, err)
123124
assert.Equal(t, StatusDownloaded, r.Status)
124125
})
@@ -132,10 +133,10 @@ func TestDownloadLocal(t *testing.T) {
132133
testLocalFileURL := "file://" + localTestFile
133134
wrongDigest := digest.Digest(emptyFileDigest)
134135

135-
_, err := Download(localPath, testLocalFileURL, WithExpectedDigest(wrongDigest))
136+
_, err := Download(context.Background(), localPath, testLocalFileURL, WithExpectedDigest(wrongDigest))
136137
assert.ErrorContains(t, err, "expected digest")
137138

138-
r, err := Download(localPath, testLocalFileURL, WithExpectedDigest(testDownloadLocalDigest))
139+
r, err := Download(context.Background(), localPath, testLocalFileURL, WithExpectedDigest(testDownloadLocalDigest))
139140
assert.NilError(t, err)
140141
assert.Equal(t, StatusDownloaded, r.Status)
141142

@@ -170,7 +171,7 @@ func TestDownloadCompressed(t *testing.T) {
170171
localFile += ".gz"
171172
testLocalFileURL := "file://" + localFile
172173

173-
r, err := Download(localPath, testLocalFileURL, WithDecompress(true))
174+
r, err := Download(context.Background(), localPath, testLocalFileURL, WithDecompress(true))
174175
assert.NilError(t, err)
175176
assert.Equal(t, StatusDownloaded, r.Status)
176177

pkg/driver/driver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type Driver interface {
2626
Initialize(_ context.Context) error
2727

2828
// CreateDisk returns error if the current driver fails in creating disk
29-
CreateDisk() error
29+
CreateDisk(_ context.Context) error
3030

3131
// Start is used for booting the vm using driver instance
3232
// It returns a chan error on successful boot
@@ -90,7 +90,7 @@ func (d *BaseDriver) Initialize(_ context.Context) error {
9090
return nil
9191
}
9292

93-
func (d *BaseDriver) CreateDisk() error {
93+
func (d *BaseDriver) CreateDisk(_ context.Context) error {
9494
return nil
9595
}
9696

pkg/fileutils/download.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fileutils
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"path"
@@ -14,13 +15,13 @@ import (
1415
var ErrSkipped = errors.New("skipped to download")
1516

1617
// DownloadFile downloads a file to the cache, optionally copying it to the destination. Returns path in cache.
17-
func DownloadFile(dest string, f limayaml.File, decompress bool, description string, expectedArch limayaml.Arch) (string, error) {
18+
func DownloadFile(ctx context.Context, dest string, f limayaml.File, decompress bool, description string, expectedArch limayaml.Arch) (string, error) {
1819
if f.Arch != expectedArch {
1920
return "", fmt.Errorf("%w: %q: unsupported arch: %q", ErrSkipped, f.Location, f.Arch)
2021
}
2122
fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest}
2223
logrus.WithFields(fields).Infof("Attempting to download %s", description)
23-
res, err := downloader.Download(dest, f.Location,
24+
res, err := downloader.Download(ctx, dest, f.Location,
2425
downloader.WithCache(),
2526
downloader.WithDecompress(decompress),
2627
downloader.WithDescription(fmt.Sprintf("%s (%s)", description, path.Base(f.Location))),

pkg/networks/reconcile/reconcile.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Reconcile(ctx context.Context, newInst string) error {
5454
if activeNetwork[name] {
5555
err = startNetwork(ctx, &config, name)
5656
} else {
57-
err = stopNetwork(&config, name)
57+
err = stopNetwork(ctx, &config, name)
5858
}
5959
if err != nil {
6060
return err
@@ -218,15 +218,15 @@ func startNetwork(ctx context.Context, config *networks.YAML, name string) error
218218
return nil
219219
}
220220

221-
func stopNetwork(config *networks.YAML, name string) error {
221+
func stopNetwork(ctx context.Context, config *networks.YAML, name string) error {
222222
logrus.Debugf("Make sure %q network is stopped", name)
223223
// Handle usernet first without sudo requirements
224224
isUsernet, err := config.Usernet(name)
225225
if err != nil {
226226
return err
227227
}
228228
if isUsernet {
229-
if err := usernet.Stop(name); err != nil {
229+
if err := usernet.Stop(ctx, name); err != nil {
230230
return fmt.Errorf("failed to stop usernet %q: %w", name, err)
231231
}
232232
return nil

pkg/networks/usernet/client.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
gvproxyclient "github.com/containers/gvisor-tap-vsock/pkg/client"
1313
"github.com/containers/gvisor-tap-vsock/pkg/types"
1414
"github.com/lima-vm/lima/pkg/driver"
15+
"github.com/lima-vm/lima/pkg/httpclientutil"
1516
"github.com/lima-vm/lima/pkg/limayaml"
1617
"github.com/lima-vm/lima/pkg/networks/usernet/dnshosts"
1718
)
@@ -25,9 +26,9 @@ type Client struct {
2526
subnet net.IP
2627
}
2728

28-
func (c *Client) ConfigureDriver(driver *driver.BaseDriver) error {
29+
func (c *Client) ConfigureDriver(ctx context.Context, driver *driver.BaseDriver) error {
2930
macAddress := limayaml.MACAddress(driver.Instance.Dir)
30-
ipAddress, err := c.ResolveIPAddress(macAddress)
31+
ipAddress, err := c.ResolveIPAddress(ctx, macAddress)
3132
if err != nil {
3233
return err
3334
}
@@ -72,15 +73,15 @@ func (c *Client) ResolveAndForwardSSH(ipAddr string, sshPort int) error {
7273
return nil
7374
}
7475

75-
func (c *Client) ResolveIPAddress(vmMacAddr string) (string, error) {
76+
func (c *Client) ResolveIPAddress(ctx context.Context, vmMacAddr string) (string, error) {
7677
timeout := time.After(2 * time.Minute)
7778
ticker := time.NewTicker(500 * time.Millisecond)
7879
for {
7980
select {
8081
case <-timeout:
8182
return "", errors.New("usernet unable to resolve IP for SSH forwarding")
8283
case <-ticker.C:
83-
leases, err := c.Leases()
84+
leases, err := c.Leases(ctx)
8485
if err != nil {
8586
return "", err
8687
}
@@ -94,15 +95,13 @@ func (c *Client) ResolveIPAddress(vmMacAddr string) (string, error) {
9495
}
9596
}
9697

97-
func (c *Client) Leases() (map[string]string, error) {
98-
res, err := c.client.Get(fmt.Sprintf("%s%s", c.base, "/services/dhcp/leases"))
98+
func (c *Client) Leases(ctx context.Context) (map[string]string, error) {
99+
u := fmt.Sprintf("%s%s", c.base, "/services/dhcp/leases")
100+
res, err := httpclientutil.Get(ctx, c.client, u)
99101
if err != nil {
100102
return nil, err
101103
}
102104
defer res.Body.Close()
103-
if res.StatusCode != http.StatusOK {
104-
return nil, fmt.Errorf("unexpected status: %d", res.StatusCode)
105-
}
106105
dec := json.NewDecoder(res.Body)
107106
var leases map[string]string
108107
if err := dec.Decode(&leases); err != nil {

pkg/networks/usernet/recoincile.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func Start(ctx context.Context, name string) error {
121121

122122
// Stop stops running instance a usernet network with the given name.
123123
// The name parameter must point to a valid network configuration name under <LIMA_HOME>/_config/networks.yaml with `mode: user-v2`
124-
func Stop(name string) error {
124+
func Stop(ctx context.Context, name string) error {
125125
logrus.Debugf("Make sure usernet network is stopped")
126126
pidFile, err := PIDFile(name)
127127
if err != nil {
@@ -132,7 +132,7 @@ func Stop(name string) error {
132132
if pid != 0 {
133133
logrus.Debugf("Stopping usernet daemon")
134134

135-
err = writeLeases(name)
135+
err = writeLeases(ctx, name)
136136
if err != nil {
137137
return err
138138
}
@@ -190,9 +190,9 @@ func readLeases(name string) (map[string]string, error) {
190190
return leases, err
191191
}
192192

193-
func writeLeases(nwName string) error {
193+
func writeLeases(ctx context.Context, nwName string) error {
194194
client := NewClientByName(nwName)
195-
leases, err := client.Leases()
195+
leases, err := client.Leases(ctx)
196196
if err != nil {
197197
return err
198198
}

0 commit comments

Comments
 (0)