diff --git a/.golangci.yml b/.golangci.yml index ade7260bf6fa..597722eaa67e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -25,6 +25,7 @@ linters: - misspell - nakedret - nilerr + - noctx - nolintlint - prealloc - predeclared @@ -37,6 +38,7 @@ linters: - typecheck - unconvert - unparam + - unused - varcheck - whitespace @@ -149,6 +151,8 @@ linters-settings: - unnecessaryDefer - whyNoLint - wrapperFunc + unused: + go: "1.17" issues: max-same-issues: 0 max-issues-per-linter: 0 diff --git a/cmd/clusterctl/client/config/reader_viper.go b/cmd/clusterctl/client/config/reader_viper.go index 135ec7dca64a..939f8720afaf 100644 --- a/cmd/clusterctl/client/config/reader_viper.go +++ b/cmd/clusterctl/client/config/reader_viper.go @@ -17,6 +17,7 @@ limitations under the License. package config import ( + "context" "fmt" "io" "net/http" @@ -133,6 +134,8 @@ func (v *viperReader) Init(path string) error { } func downloadFile(url string, filepath string) error { + ctx := context.TODO() + // Create the file out, err := os.Create(filepath) if err != nil { @@ -144,7 +147,12 @@ func downloadFile(url string, filepath string) error { Timeout: 30 * time.Second, } // Get the data - resp, err := client.Get(url) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) + if err != nil { + return errors.Wrapf(err, "failed to download the clusterctl config file from %s: failed to create request", url) + } + + resp, err := client.Do(req) if err != nil { return errors.Wrapf(err, "failed to download the clusterctl config file from %s", url) } diff --git a/cmd/clusterctl/client/repository/repository_github.go b/cmd/clusterctl/client/repository/repository_github.go index b710cfb91da9..8e298c187e91 100644 --- a/cmd/clusterctl/client/repository/repository_github.go +++ b/cmd/clusterctl/client/repository/repository_github.go @@ -262,12 +262,14 @@ func (g *gitHubRepository) getReleaseByTag(tag string) (*github.RepositoryReleas // downloadFilesFromRelease download a file from release. func (g *gitHubRepository) downloadFilesFromRelease(release *github.RepositoryRelease, fileName string) ([]byte, error) { + ctx := context.TODO() + cacheID := fmt.Sprintf("%s/%s:%s:%s", g.owner, g.repository, *release.TagName, fileName) if content, ok := cacheFiles[cacheID]; ok { return content, nil } - client := g.getClient() + c := g.getClient() absoluteFileName := filepath.Join(g.rootPath, fileName) // search for the file into the release assets, retrieving the asset id @@ -282,12 +284,17 @@ func (g *gitHubRepository) downloadFilesFromRelease(release *github.RepositoryRe return nil, errors.Errorf("failed to get file %q from %q release", fileName, *release.TagName) } - reader, redirect, err := client.Repositories.DownloadReleaseAsset(context.TODO(), g.owner, g.repository, *assetID, http.DefaultClient) + reader, redirect, err := c.Repositories.DownloadReleaseAsset(context.TODO(), g.owner, g.repository, *assetID, http.DefaultClient) if err != nil { return nil, g.handleGithubErr(err, "failed to download file %q from %q release", *release.TagName, fileName) } if redirect != "" { - response, err := http.Get(redirect) //nolint:bodyclose,gosec // (NB: The reader is actually closed in a defer) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, redirect, http.NoBody) + if err != nil { + 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) + } + + response, err := http.DefaultClient.Do(req) //nolint:bodyclose // (NB: The reader is actually closed in a defer) if err != nil { return nil, errors.Wrapf(err, "failed to download file %q from %q release via redirect location %q", *release.TagName, fileName, redirect) }