Skip to content

Commit

Permalink
linter: enable noctx and unused
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer buringerst@vmware.com
  • Loading branch information
sbueringer committed Jan 20, 2022
1 parent 07e31b7 commit 3d2876c
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 40 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ linters:
- misspell
- nakedret
- nilerr
- noctx
- nolintlint
- prealloc
- predeclared
Expand All @@ -37,6 +38,7 @@ linters:
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace

Expand Down Expand Up @@ -149,6 +151,8 @@ linters-settings:
- unnecessaryDefer
- whyNoLint
- wrapperFunc
unused:
go: "1.17"
issues:
max-same-issues: 0
max-issues-per-linter: 0
Expand Down
10 changes: 9 additions & 1 deletion cmd/clusterctl/client/config/reader_viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package config

import (
"context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/clusterctl/client/repository/repository_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ 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
Expand All @@ -287,7 +289,12 @@ func (g *gitHubRepository) downloadFilesFromRelease(release *github.RepositoryRe
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)
}
Expand Down
2 changes: 1 addition & 1 deletion hack/tools/mdbook/embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (l Embed) Process(input *plugin.Input) error {
Path: path.Join("/", repository, branch, filePath),
}

resp, err := http.Get(rawURL.String())
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.
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion hack/tools/mdbook/releaselink/releaselink.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (l ReleaseLink) Process(input *plugin.Input) error {
Path: path.Join(gomodule, "@v", "/list"),
}

resp, err := http.Get(rawURL.String())
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.
if err != nil {
return "", err
}
Expand Down
9 changes: 6 additions & 3 deletions test/e2e/clusterctl_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
// Download the older clusterctl version to be used for setting up the management cluster to be upgraded

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

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

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

// Get the data
resp, err := http.Get(url) //nolint:gosec
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
Expect(err).ToNot(HaveOccurred(), "failed to get clusterctl: failed to create request")

resp, err := http.DefaultClient.Do(req)
Expect(err).ToNot(HaveOccurred(), "failed to get clusterctl")
defer resp.Body.Close()

Expand Down
14 changes: 9 additions & 5 deletions test/framework/clusterctl/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func YAMLForComponentSource(ctx context.Context, source ProviderVersionSource) (

switch source.Type {
case URLSource:
buf, err := getComponentSourceFromURL(source)
buf, err := getComponentSourceFromURL(ctx, source)
if err != nil {
return nil, errors.Wrap(err, "failed to get component source YAML from URL")
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func YAMLForComponentSource(ctx context.Context, source ProviderVersionSource) (
}

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

u, err := url.Parse(source.Value)
Expand All @@ -189,14 +189,18 @@ func getComponentSourceFromURL(source ProviderVersionSource) ([]byte, error) {
return nil, errors.Wrap(err, "failed to read file")
}
case httpURIScheme, httpsURIScheme:
resp, err := http.Get(source.Value)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, source.Value, http.NoBody)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to get %s: failed to create request", source.Value)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrapf(err, "failed to get %s", source.Value)
}
defer resp.Body.Close()
buf, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to get %s: failed to read body", source.Value)
}
default:
return nil, errors.Errorf("unknown scheme for component source %q: allowed values are file, http, https", u.Scheme)
Expand Down
30 changes: 24 additions & 6 deletions test/framework/kubernetesversions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package kubernetesversions

import (
"context"
"fmt"
"io"
"net/http"
"strings"

"github.com/blang/semver"
"github.com/pkg/errors"
)

const (
Expand All @@ -33,33 +35,49 @@ const (

// LatestCIRelease fetches the latest main branch Kubernetes version.
func LatestCIRelease() (string, error) {
resp, err := http.Get(ciVersionURL)
ctx := context.TODO()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, ciVersionURL, http.NoBody)
if err != nil {
return "", err
return "", errors.Wrapf(err, "failed to get %s: failed to create request", ciVersionURL)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", errors.Wrapf(err, "failed to get %s", ciVersionURL)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
return "", errors.Wrapf(err, "failed to get %s: failed to read body", ciVersionURL)
}

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

// LatestPatchRelease returns the latest patch release matching.
func LatestPatchRelease(searchVersion string) (string, error) {
ctx := context.TODO()

searchSemVer, err := semver.Make(strings.TrimPrefix(searchVersion, tagPrefix))
if err != nil {
return "", err
}
resp, err := http.Get(fmt.Sprintf(stableVersionURL, searchSemVer.Major, searchSemVer.Minor))

url := fmt.Sprintf(stableVersionURL, searchSemVer.Major, searchSemVer.Minor)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return "", err
return "", errors.Wrapf(err, "failed to get %s: failed to create request", url)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", errors.Wrapf(err, "failed to get %s", url)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
return "", errors.Wrapf(err, "failed to get %s: failed to read body", url)
}

return strings.TrimSpace(string(b)), nil
Expand Down
22 changes: 0 additions & 22 deletions test/infrastructure/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,28 +341,6 @@ func dockerContainerToContainer(container *types.Container) Container {
}
}

// ownerAndGroup gets the user configuration for the container (user:group).
func (crc *RunContainerInput) ownerAndGroup() string {
if crc.User != "" {
if crc.Group != "" {
return fmt.Sprintf("%s:%s", crc.User, crc.Group)
}

return crc.User
}

return ""
}

// environmentVariables gets the collection of environment variables for the container.
func (crc *RunContainerInput) environmentVariables() []string {
envVars := []string{}
for key, val := range crc.EnvironmentVars {
envVars = append(envVars, fmt.Sprintf("%s=%s", key, val))
}
return envVars
}

// RunContainer will run a docker container with the given settings and arguments, returning any errors.
func (d *dockerRuntime) RunContainer(ctx context.Context, runConfig *RunContainerInput, output io.Writer) error {
containerConfig := dockercontainer.Config{
Expand Down

0 comments on commit 3d2876c

Please sign in to comment.