Skip to content

Commit abff417

Browse files
authored
Merge pull request #5959 from sbueringer/pr-enable-linters
🌱 linter: enable noctx and unused
2 parents 0d0d0c9 + 3d2876c commit abff417

File tree

9 files changed

+62
-40
lines changed

9 files changed

+62
-40
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ linters:
2525
- misspell
2626
- nakedret
2727
- nilerr
28+
- noctx
2829
- nolintlint
2930
- prealloc
3031
- predeclared
@@ -37,6 +38,7 @@ linters:
3738
- typecheck
3839
- unconvert
3940
- unparam
41+
- unused
4042
- varcheck
4143
- whitespace
4244

@@ -149,6 +151,8 @@ linters-settings:
149151
- unnecessaryDefer
150152
- whyNoLint
151153
- wrapperFunc
154+
unused:
155+
go: "1.17"
152156
issues:
153157
max-same-issues: 0
154158
max-issues-per-linter: 0

cmd/clusterctl/client/config/reader_viper.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package config
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"io"
2223
"net/http"
@@ -133,6 +134,8 @@ func (v *viperReader) Init(path string) error {
133134
}
134135

135136
func downloadFile(url string, filepath string) error {
137+
ctx := context.TODO()
138+
136139
// Create the file
137140
out, err := os.Create(filepath)
138141
if err != nil {
@@ -144,7 +147,12 @@ func downloadFile(url string, filepath string) error {
144147
Timeout: 30 * time.Second,
145148
}
146149
// Get the data
147-
resp, err := client.Get(url)
150+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
151+
if err != nil {
152+
return errors.Wrapf(err, "failed to download the clusterctl config file from %s: failed to create request", url)
153+
}
154+
155+
resp, err := client.Do(req)
148156
if err != nil {
149157
return errors.Wrapf(err, "failed to download the clusterctl config file from %s", url)
150158
}

cmd/clusterctl/client/repository/repository_github.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ func (g *gitHubRepository) getReleaseByTag(tag string) (*github.RepositoryReleas
262262

263263
// downloadFilesFromRelease download a file from release.
264264
func (g *gitHubRepository) downloadFilesFromRelease(release *github.RepositoryRelease, fileName string) ([]byte, error) {
265+
ctx := context.TODO()
266+
265267
cacheID := fmt.Sprintf("%s/%s:%s:%s", g.owner, g.repository, *release.TagName, fileName)
266268
if content, ok := cacheFiles[cacheID]; ok {
267269
return content, nil
@@ -287,7 +289,12 @@ func (g *gitHubRepository) downloadFilesFromRelease(release *github.RepositoryRe
287289
return nil, g.handleGithubErr(err, "failed to download file %q from %q release", *release.TagName, fileName)
288290
}
289291
if redirect != "" {
290-
response, err := http.Get(redirect) //nolint:bodyclose,gosec // (NB: The reader is actually closed in a defer)
292+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, redirect, http.NoBody)
293+
if err != nil {
294+
return nil, errors.Wrapf(err, "failed to download file %q from %q release via redirect location %q: failed to create request", *release.TagName, fileName, redirect)
295+
}
296+
297+
response, err := http.DefaultClient.Do(req) //nolint:bodyclose // (NB: The reader is actually closed in a defer)
291298
if err != nil {
292299
return nil, errors.Wrapf(err, "failed to download file %q from %q release via redirect location %q", *release.TagName, fileName, redirect)
293300
}

hack/tools/mdbook/embed/embed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (l Embed) Process(input *plugin.Input) error {
5656
Path: path.Join("/", repository, branch, filePath),
5757
}
5858

59-
resp, err := http.Get(rawURL.String())
59+
resp, err := http.Get(rawURL.String()) //nolint:noctx // NB: as we're just implementing an external interface we won't be able to get a context here.
6060
if err != nil {
6161
return "", err
6262
}

hack/tools/mdbook/releaselink/releaselink.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (l ReleaseLink) Process(input *plugin.Input) error {
6868
Path: path.Join(gomodule, "@v", "/list"),
6969
}
7070

71-
resp, err := http.Get(rawURL.String())
71+
resp, err := http.Get(rawURL.String()) //nolint:noctx // NB: as we're just implementing an external interface we won't be able to get a context here.
7272
if err != nil {
7373
return "", err
7474
}

test/e2e/clusterctl_upgrade.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
189189
// Download the older clusterctl version to be used for setting up the management cluster to be upgraded
190190

191191
log.Logf("Downloading clusterctl binary from %s", clusterctlBinaryURL)
192-
clusterctlBinaryPath := downloadToTmpFile(clusterctlBinaryURL)
192+
clusterctlBinaryPath := downloadToTmpFile(ctx, clusterctlBinaryURL)
193193
defer os.Remove(clusterctlBinaryPath) // clean up
194194

195195
err := os.Chmod(clusterctlBinaryPath, 0744) //nolint:gosec
@@ -391,13 +391,16 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
391391
})
392392
}
393393

394-
func downloadToTmpFile(url string) string {
394+
func downloadToTmpFile(ctx context.Context, url string) string {
395395
tmpFile, err := os.CreateTemp("", "clusterctl")
396396
Expect(err).ToNot(HaveOccurred(), "failed to get temporary file")
397397
defer tmpFile.Close()
398398

399399
// Get the data
400-
resp, err := http.Get(url) //nolint:gosec
400+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
401+
Expect(err).ToNot(HaveOccurred(), "failed to get clusterctl: failed to create request")
402+
403+
resp, err := http.DefaultClient.Do(req)
401404
Expect(err).ToNot(HaveOccurred(), "failed to get clusterctl")
402405
defer resp.Body.Close()
403406

test/framework/clusterctl/repository.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func YAMLForComponentSource(ctx context.Context, source ProviderVersionSource) (
143143

144144
switch source.Type {
145145
case URLSource:
146-
buf, err := getComponentSourceFromURL(source)
146+
buf, err := getComponentSourceFromURL(ctx, source)
147147
if err != nil {
148148
return nil, errors.Wrap(err, "failed to get component source YAML from URL")
149149
}
@@ -173,7 +173,7 @@ func YAMLForComponentSource(ctx context.Context, source ProviderVersionSource) (
173173
}
174174

175175
// getComponentSourceFromURL fetches contents of component source YAML file from provided URL source.
176-
func getComponentSourceFromURL(source ProviderVersionSource) ([]byte, error) {
176+
func getComponentSourceFromURL(ctx context.Context, source ProviderVersionSource) ([]byte, error) {
177177
var buf []byte
178178

179179
u, err := url.Parse(source.Value)
@@ -189,14 +189,18 @@ func getComponentSourceFromURL(source ProviderVersionSource) ([]byte, error) {
189189
return nil, errors.Wrap(err, "failed to read file")
190190
}
191191
case httpURIScheme, httpsURIScheme:
192-
resp, err := http.Get(source.Value)
192+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, source.Value, http.NoBody)
193193
if err != nil {
194-
return nil, err
194+
return nil, errors.Wrapf(err, "failed to get %s: failed to create request", source.Value)
195+
}
196+
resp, err := http.DefaultClient.Do(req)
197+
if err != nil {
198+
return nil, errors.Wrapf(err, "failed to get %s", source.Value)
195199
}
196200
defer resp.Body.Close()
197201
buf, err = io.ReadAll(resp.Body)
198202
if err != nil {
199-
return nil, err
203+
return nil, errors.Wrapf(err, "failed to get %s: failed to read body", source.Value)
200204
}
201205
default:
202206
return nil, errors.Errorf("unknown scheme for component source %q: allowed values are file, http, https", u.Scheme)

test/framework/kubernetesversions/versions.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ limitations under the License.
1717
package kubernetesversions
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"io"
2223
"net/http"
2324
"strings"
2425

2526
"github.com/blang/semver"
27+
"github.com/pkg/errors"
2628
)
2729

2830
const (
@@ -33,33 +35,49 @@ const (
3335

3436
// LatestCIRelease fetches the latest main branch Kubernetes version.
3537
func LatestCIRelease() (string, error) {
36-
resp, err := http.Get(ciVersionURL)
38+
ctx := context.TODO()
39+
40+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ciVersionURL, http.NoBody)
3741
if err != nil {
38-
return "", err
42+
return "", errors.Wrapf(err, "failed to get %s: failed to create request", ciVersionURL)
43+
}
44+
45+
resp, err := http.DefaultClient.Do(req)
46+
if err != nil {
47+
return "", errors.Wrapf(err, "failed to get %s", ciVersionURL)
3948
}
4049
defer resp.Body.Close()
4150
b, err := io.ReadAll(resp.Body)
4251
if err != nil {
43-
return "", err
52+
return "", errors.Wrapf(err, "failed to get %s: failed to read body", ciVersionURL)
4453
}
4554

4655
return strings.TrimSpace(string(b)), nil
4756
}
4857

4958
// LatestPatchRelease returns the latest patch release matching.
5059
func LatestPatchRelease(searchVersion string) (string, error) {
60+
ctx := context.TODO()
61+
5162
searchSemVer, err := semver.Make(strings.TrimPrefix(searchVersion, tagPrefix))
5263
if err != nil {
5364
return "", err
5465
}
55-
resp, err := http.Get(fmt.Sprintf(stableVersionURL, searchSemVer.Major, searchSemVer.Minor))
66+
67+
url := fmt.Sprintf(stableVersionURL, searchSemVer.Major, searchSemVer.Minor)
68+
69+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
5670
if err != nil {
57-
return "", err
71+
return "", errors.Wrapf(err, "failed to get %s: failed to create request", url)
72+
}
73+
resp, err := http.DefaultClient.Do(req)
74+
if err != nil {
75+
return "", errors.Wrapf(err, "failed to get %s", url)
5876
}
5977
defer resp.Body.Close()
6078
b, err := io.ReadAll(resp.Body)
6179
if err != nil {
62-
return "", err
80+
return "", errors.Wrapf(err, "failed to get %s: failed to read body", url)
6381
}
6482

6583
return strings.TrimSpace(string(b)), nil

test/infrastructure/container/docker.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -341,28 +341,6 @@ func dockerContainerToContainer(container *types.Container) Container {
341341
}
342342
}
343343

344-
// ownerAndGroup gets the user configuration for the container (user:group).
345-
func (crc *RunContainerInput) ownerAndGroup() string {
346-
if crc.User != "" {
347-
if crc.Group != "" {
348-
return fmt.Sprintf("%s:%s", crc.User, crc.Group)
349-
}
350-
351-
return crc.User
352-
}
353-
354-
return ""
355-
}
356-
357-
// environmentVariables gets the collection of environment variables for the container.
358-
func (crc *RunContainerInput) environmentVariables() []string {
359-
envVars := []string{}
360-
for key, val := range crc.EnvironmentVars {
361-
envVars = append(envVars, fmt.Sprintf("%s=%s", key, val))
362-
}
363-
return envVars
364-
}
365-
366344
// RunContainer will run a docker container with the given settings and arguments, returning any errors.
367345
func (d *dockerRuntime) RunContainer(ctx context.Context, runConfig *RunContainerInput, output io.Writer) error {
368346
containerConfig := dockercontainer.Config{

0 commit comments

Comments
 (0)