You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2021/11/25 07:57:09 UTC

[camel-k] 04/06: fix(lint): context should be propagated (contextcheck)

This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 194ae8a60057a625af99b077a20ac5800570bef1
Author: Luca Burgazzoli <lb...@gmail.com>
AuthorDate: Wed Nov 24 15:30:33 2021 +0100

    fix(lint): context should be propagated (contextcheck)
---
 .golangci.yml                           |  1 -
 pkg/builder/s2i.go                      |  1 +
 pkg/cmd/run.go                          |  6 +++---
 pkg/cmd/run_help.go                     |  2 +-
 pkg/cmd/util_content.go                 | 20 ++++++++++----------
 pkg/cmd/util_content_test.go            | 15 ++++++++-------
 pkg/cmd/util_dependencies.go            |  6 +++---
 pkg/cmd/util_sources.go                 |  6 +++---
 pkg/controller/build/monitor_routine.go |  2 ++
 pkg/util/camel/catalog.go               |  2 +-
 10 files changed, 32 insertions(+), 29 deletions(-)

diff --git a/.golangci.yml b/.golangci.yml
index c9af8be..e7eb670 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -55,6 +55,5 @@ linters:
     - nilnil
     - exhaustive
     - nakedret
-    - contextcheck
     - maligned
 
diff --git a/pkg/builder/s2i.go b/pkg/builder/s2i.go
index 2f7f488..153d0a3 100644
--- a/pkg/builder/s2i.go
+++ b/pkg/builder/s2i.go
@@ -209,6 +209,7 @@ func (t *s2iTask) Do(ctx context.Context) v1.BuildStatus {
 	err = t.waitForS2iBuildCompletion(ctx, t.c, &s2iBuild)
 	if err != nil {
 		if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
+			// nolint: contextcheck
 			if err := t.cancelBuild(context.Background(), &s2iBuild); err != nil {
 				log.Errorf(err, "cannot cancel s2i Build: %s/%s", s2iBuild.Namespace, s2iBuild.Name)
 			}
diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index 261c530..ba01178 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -579,7 +579,7 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.C
 	}
 
 	for _, resource := range o.OpenAPIs {
-		if err = addResource(resource, &integration.Spec, o.Compression, v1.ResourceTypeOpenAPI); err != nil {
+		if err = addResource(o.Context, resource, &integration.Spec, o.Compression, v1.ResourceTypeOpenAPI); err != nil {
 			return nil, err
 		}
 	}
@@ -686,8 +686,8 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.C
 	return integration, nil
 }
 
-func addResource(resourceLocation string, integrationSpec *v1.IntegrationSpec, enableCompression bool, resourceType v1.ResourceType) error {
-	if data, _, compressed, err := loadTextContent(resourceLocation, enableCompression); err == nil {
+func addResource(ctx context.Context, resourceLocation string, integrationSpec *v1.IntegrationSpec, enableCompression bool, resourceType v1.ResourceType) error {
+	if data, _, compressed, err := loadTextContent(ctx, resourceLocation, enableCompression); err == nil {
 		integrationSpec.AddResources(v1.ResourceSpec{
 			DataSpec: v1.DataSpec{
 				Name:        path.Base(resourceLocation),
diff --git a/pkg/cmd/run_help.go b/pkg/cmd/run_help.go
index 645efef..38422de 100644
--- a/pkg/cmd/run_help.go
+++ b/pkg/cmd/run_help.go
@@ -210,7 +210,7 @@ func applyOption(config *RunConfigOption, integrationSpec *v1.IntegrationSpec,
 			return fmt.Errorf("you cannot provide a file larger than 1 MB (it was %s MB), check configmap option or --volume instead", printSize)
 		}
 		// Don't allow a binary non compressed resource
-		rawData, contentType, err := loadRawContent(config.Name())
+		rawData, contentType, err := loadRawContent(context.Background(), config.Name())
 		if err != nil {
 			return err
 		}
diff --git a/pkg/cmd/util_content.go b/pkg/cmd/util_content.go
index 0ec57a4..8b52a34 100644
--- a/pkg/cmd/util_content.go
+++ b/pkg/cmd/util_content.go
@@ -43,7 +43,7 @@ func fileSize(source string) (int64, error) {
 	return fi.Size(), nil
 }
 
-func loadRawContent(source string) ([]byte, string, error) {
+func loadRawContent(ctx context.Context, source string) ([]byte, string, error) {
 	var content []byte
 	var err error
 
@@ -63,11 +63,11 @@ func loadRawContent(source string) ([]byte, string, error) {
 
 		switch u.Scheme {
 		case "github":
-			content, err = loadContentGitHub(u)
+			content, err = loadContentGitHub(ctx, u)
 		case "http":
-			content, err = loadContentHTTP(u)
+			content, err = loadContentHTTP(ctx, u)
 		case "https":
-			content, err = loadContentHTTP(u)
+			content, err = loadContentHTTP(ctx, u)
 		default:
 			return nil, "", fmt.Errorf("missing file or unsupported scheme %s", u.Scheme)
 		}
@@ -87,8 +87,8 @@ func isBinary(contentType string) bool {
 	return !strings.HasPrefix(contentType, "text")
 }
 
-func loadTextContent(source string, base64Compression bool) (string, string, bool, error) {
-	content, contentType, err := loadRawContent(source)
+func loadTextContent(ctx context.Context, source string, base64Compression bool) (string, string, bool, error) {
+	content, contentType, err := loadRawContent(ctx, source)
 	if err != nil {
 		return "", "", false, err
 	}
@@ -101,8 +101,8 @@ func loadTextContent(source string, base64Compression bool) (string, string, boo
 	return string(content), contentType, false, nil
 }
 
-func loadContentHTTP(u fmt.Stringer) ([]byte, error) {
-	req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, u.String(), nil)
+func loadContentHTTP(ctx context.Context, u fmt.Stringer) ([]byte, error) {
+	req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
 	if err != nil {
 		return nil, err
 	}
@@ -124,7 +124,7 @@ func loadContentHTTP(u fmt.Stringer) ([]byte, error) {
 	return io.ReadAll(resp.Body)
 }
 
-func loadContentGitHub(u *url.URL) ([]byte, error) {
+func loadContentGitHub(ctx context.Context, u *url.URL) ([]byte, error) {
 	src := u.Scheme + ":" + u.Opaque
 	re := regexp.MustCompile(`^github:([^/]+)/([^/]+)/(.+)$`)
 
@@ -144,5 +144,5 @@ func loadContentGitHub(u *url.URL) ([]byte, error) {
 		return []byte{}, err
 	}
 
-	return loadContentHTTP(rawURL)
+	return loadContentHTTP(ctx, rawURL)
 }
diff --git a/pkg/cmd/util_content_test.go b/pkg/cmd/util_content_test.go
index 5684ac8..388c867 100644
--- a/pkg/cmd/util_content_test.go
+++ b/pkg/cmd/util_content_test.go
@@ -18,6 +18,7 @@ limitations under the License.
 package cmd
 
 import (
+	"context"
 	"fmt"
 	"io/ioutil"
 	"net/http"
@@ -30,7 +31,7 @@ import (
 )
 
 func TestRawContentFileMissing(t *testing.T) {
-	_, _, err := loadRawContent("dsadas")
+	_, _, err := loadRawContent(context.TODO(), "dsadas")
 	assert.NotNil(t, err)
 }
 
@@ -43,7 +44,7 @@ func TestRawBinaryContentType(t *testing.T) {
 	assert.Nil(t, tmpFile.Close())
 	assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), []byte{1, 2, 3, 4, 5, 6}, 0o400))
 
-	data, contentType, err := loadRawContent(tmpFile.Name())
+	data, contentType, err := loadRawContent(context.TODO(), tmpFile.Name())
 	assert.Nil(t, err)
 	assert.Equal(t, []byte{1, 2, 3, 4, 5, 6}, data)
 	assert.True(t, isBinary(contentType))
@@ -58,7 +59,7 @@ func TestRawApplicationContentType(t *testing.T) {
 	assert.Nil(t, tmpFile.Close())
 	assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), []byte(`{"hello":"world"}`), 0o400))
 
-	data, contentType, err := loadRawContent(tmpFile.Name())
+	data, contentType, err := loadRawContent(context.TODO(), tmpFile.Name())
 	assert.Nil(t, err)
 	assert.Equal(t, `{"hello":"world"}`, string(data))
 	assert.False(t, isBinary(contentType))
@@ -73,7 +74,7 @@ func TestTextContentType(t *testing.T) {
 	assert.Nil(t, tmpFile.Close())
 	assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), []byte(`{"hello":"world"}`), 0o400))
 
-	data, contentType, compressed, err := loadTextContent(tmpFile.Name(), false)
+	data, contentType, compressed, err := loadTextContent(context.TODO(), tmpFile.Name(), false)
 	assert.Nil(t, err)
 	assert.Equal(t, `{"hello":"world"}`, data)
 	assert.False(t, isBinary(contentType))
@@ -89,7 +90,7 @@ func TestTextCompressed(t *testing.T) {
 	assert.Nil(t, tmpFile.Close())
 	assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), []byte(`{"hello":"world"}`), 0o400))
 
-	data, contentType, compressed, err := loadTextContent(tmpFile.Name(), true)
+	data, contentType, compressed, err := loadTextContent(context.TODO(), tmpFile.Name(), true)
 	assert.Nil(t, err)
 	assert.NotEqual(t, `{"hello":"world"}`, data)
 	assert.False(t, isBinary(contentType))
@@ -105,14 +106,14 @@ func TestIsBinary(t *testing.T) {
 func TestContentHttp(t *testing.T) {
 	expected := "the content"
 	svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-		_, _ = fmt.Fprintf(w, expected)
+		_, _ = fmt.Fprint(w, expected)
 	}))
 	defer svr.Close()
 
 	u, err := url.Parse(svr.URL)
 	assert.Nil(t, err)
 
-	data, err := loadContentHTTP(u)
+	data, err := loadContentHTTP(context.TODO(), u)
 	assert.Nil(t, err)
 	assert.NotEmpty(t, data)
 	assert.Equal(t, expected, string(data))
diff --git a/pkg/cmd/util_dependencies.go b/pkg/cmd/util_dependencies.go
index 351091f..5f0a758 100644
--- a/pkg/cmd/util_dependencies.go
+++ b/pkg/cmd/util_dependencies.go
@@ -51,7 +51,7 @@ func getDependencies(ctx context.Context, args []string, additionalDependencies
 	}
 
 	// Get top-level dependencies
-	dependencies, err := getTopLevelDependencies(catalog, args)
+	dependencies, err := getTopLevelDependencies(ctx, catalog, args)
 	if err != nil {
 		return nil, err
 	}
@@ -75,13 +75,13 @@ func getDependencies(ctx context.Context, args []string, additionalDependencies
 	return dependencies, nil
 }
 
-func getTopLevelDependencies(catalog *camel.RuntimeCatalog, args []string) ([]string, error) {
+func getTopLevelDependencies(ctx context.Context, catalog *camel.RuntimeCatalog, args []string) ([]string, error) {
 	// List of top-level dependencies
 	dependencies := strset.New()
 
 	// Invoke the dependency inspector code for each source file
 	for _, source := range args {
-		data, _, _, err := loadTextContent(source, false)
+		data, _, _, err := loadTextContent(ctx, source, false)
 		if err != nil {
 			return []string{}, err
 		}
diff --git a/pkg/cmd/util_sources.go b/pkg/cmd/util_sources.go
index fc9e624..4765535 100644
--- a/pkg/cmd/util_sources.go
+++ b/pkg/cmd/util_sources.go
@@ -140,7 +140,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
 					Compress: compress,
 				}
 
-				content, err := loadContentGitHub(u)
+				content, err := loadContentGitHub(ctx, u)
 				if err != nil {
 					return sources, err
 				}
@@ -156,7 +156,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
 					Compress: compress,
 				}
 
-				content, err := loadContentHTTP(u)
+				content, err := loadContentHTTP(ctx, u)
 				if err != nil {
 					return sources, err
 				}
@@ -172,7 +172,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
 					Compress: compress,
 				}
 
-				content, err := loadContentHTTP(u)
+				content, err := loadContentHTTP(ctx, u)
 				if err != nil {
 					return sources, err
 				}
diff --git a/pkg/controller/build/monitor_routine.go b/pkg/controller/build/monitor_routine.go
index aec80b8..cea4d0b 100644
--- a/pkg/controller/build/monitor_routine.go
+++ b/pkg/controller/build/monitor_routine.go
@@ -75,6 +75,8 @@ func (action *monitorRoutineAction) Handle(ctx context.Context, build *v1.Build)
 		}
 		// Start the build asynchronously to avoid blocking the reconciliation loop
 		routines.Store(build.Name, true)
+
+		// nolint: contextcheck
 		go action.runBuild(build)
 
 	case v1.BuildPhaseRunning:
diff --git a/pkg/util/camel/catalog.go b/pkg/util/camel/catalog.go
index 531e791..f47cf61 100644
--- a/pkg/util/camel/catalog.go
+++ b/pkg/util/camel/catalog.go
@@ -124,7 +124,7 @@ func GenerateCatalogCommon(
 	if caCert != nil {
 		trustStoreName := "trust.jks"
 		trustStorePass := jvm.NewKeystorePassword()
-		err := jvm.GenerateKeystore(context.Background(), tmpDir, trustStoreName, trustStorePass, caCert)
+		err := jvm.GenerateKeystore(ctx, tmpDir, trustStoreName, trustStorePass, caCert)
 		if err != nil {
 			return nil, err
 		}