From 6c901d6347ce0f8fff1f662f54b14dbae75b3112 Mon Sep 17 00:00:00 2001 From: Jacek Wysocki Date: Fri, 30 Jun 2023 10:19:31 +0200 Subject: [PATCH] fix: add timeout to grpc conn (#4107) * fix: added timout to grpc conn * fix: log in valid place * fix: allow to configure timeout with env variable * fix: print timout * fix: print timout * fix: timeout as seconds --- pkg/envs/variables.go | 38 +++++++++++++------------ pkg/executor/scraper/factory/factory.go | 9 ++++-- pkg/scheduler/test_scheduler_test.go | 2 +- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/pkg/envs/variables.go b/pkg/envs/variables.go index aeaf88c500d..bab2757f0cb 100644 --- a/pkg/envs/variables.go +++ b/pkg/envs/variables.go @@ -11,24 +11,25 @@ import ( // Params are the environment variables provided by the Testkube api-server type Params struct { - Endpoint string // RUNNER_ENDPOINT - AccessKeyID string // RUNNER_ACCESSKEYID - SecretAccessKey string // RUNNER_SECRETACCESSKEY - Region string // RUNNER_REGION - Token string // RUNNER_TOKEN - Bucket string // RUNNER_BUCKET - Ssl bool // RUNNER_SSL - ScrapperEnabled bool // RUNNER_SCRAPPERENABLED - DataDir string // RUNNER_DATADIR - GitUsername string // RUNNER_GITUSERNAME - GitToken string // RUNNER_GITTOKEN - ClusterID string `envconfig:"RUNNER_CLUSTERID"` // RUNNER_CLUSTERID - CDEventsTarget string `envconfig:"RUNNER_CDEVENTS_TARGET"` // RUNNER_CDEVENTS_TARGET - DashboardURI string `envconfig:"RUNNER_DASHBOARD_URI"` // RUNNER_DASHBOARD_URI - CloudMode bool `envconfig:"RUNNER_CLOUD_MODE"` // RUNNER_CLOUD_MODE - CloudAPIKey string `envconfig:"RUNNER_CLOUD_API_KEY"` // RUNNER_CLOUD_API_KEY - CloudAPITLSInsecure bool `envconfig:"RUNNER_CLOUD_API_TLS_INSECURE"` // RUNNER_CLOUD_API_TLS_INSECURE - CloudAPIURL string `envconfig:"RUNNER_CLOUD_API_URL"` // RUNNER_CLOUD_API_URL + Endpoint string // RUNNER_ENDPOINT + AccessKeyID string // RUNNER_ACCESSKEYID + SecretAccessKey string // RUNNER_SECRETACCESSKEY + Region string // RUNNER_REGION + Token string // RUNNER_TOKEN + Bucket string // RUNNER_BUCKET + Ssl bool // RUNNER_SSL + ScrapperEnabled bool // RUNNER_SCRAPPERENABLED + DataDir string // RUNNER_DATADIR + GitUsername string // RUNNER_GITUSERNAME + GitToken string // RUNNER_GITTOKEN + ClusterID string `envconfig:"RUNNER_CLUSTERID"` // RUNNER_CLUSTERID + CDEventsTarget string `envconfig:"RUNNER_CDEVENTS_TARGET"` // RUNNER_CDEVENTS_TARGET + DashboardURI string `envconfig:"RUNNER_DASHBOARD_URI"` // RUNNER_DASHBOARD_URI + CloudMode bool `envconfig:"RUNNER_CLOUD_MODE"` // RUNNER_CLOUD_MODE + CloudAPIKey string `envconfig:"RUNNER_CLOUD_API_KEY"` // RUNNER_CLOUD_API_KEY + CloudAPITLSInsecure bool `envconfig:"RUNNER_CLOUD_API_TLS_INSECURE"` // RUNNER_CLOUD_API_TLS_INSECURE + CloudAPIURL string `envconfig:"RUNNER_CLOUD_API_URL"` // RUNNER_CLOUD_API_URL + CloudConnectionTimeoutSec int `envconfig:"RUNNER_CLOUD_CONNECTION_TIMEOUT" default:"10"` // RUNNER_CLOUD_CONNECTION_TIMEOUT } // LoadTestkubeVariables loads the parameters provided as environment variables in the Test CRD @@ -66,6 +67,7 @@ func printParams(params Params) { output.PrintLogf("RUNNER_CLOUD_API_TLS_INSECURE=\"%t\"", params.CloudAPITLSInsecure) output.PrintLogf("RUNNER_CLOUD_API_URL=\"%s\"", params.CloudAPIURL) printSensitiveParam("RUNNER_CLOUD_API_KEY", params.CloudAPIKey) + output.PrintLogf("RUNNER_CLOUD_CONNECTION_TIMEOUT=%d", params.CloudConnectionTimeoutSec) } // printSensitiveParam shows in logs if a parameter is set or not diff --git a/pkg/executor/scraper/factory/factory.go b/pkg/executor/scraper/factory/factory.go index 1c0547c910a..47c0825e1ac 100644 --- a/pkg/executor/scraper/factory/factory.go +++ b/pkg/executor/scraper/factory/factory.go @@ -3,6 +3,7 @@ package factory import ( "context" "fmt" + "time" cloudevents "github.com/cloudevents/sdk-go/v2" "github.com/pkg/errors" @@ -89,12 +90,16 @@ func GetScraper(ctx context.Context, params envs.Params, extractorType Extractor } func getCloudLoader(ctx context.Context, params envs.Params) (uploader *cloudscraper.CloudUploader, err error) { - output.PrintLog(fmt.Sprintf("%s Uploading artifacts using Cloud Uploader", ui.IconCheckMark)) + // timeout blocking connection to cloud + ctxTimeout, cancel := context.WithTimeout(ctx, time.Duration(params.CloudConnectionTimeoutSec)*time.Second) + defer cancel() - grpcConn, err := agent.NewGRPCConnection(ctx, params.CloudAPITLSInsecure, params.CloudAPIURL, log.DefaultLogger) + output.PrintLogf("%s Uploading artifacts using Cloud Uploader (timeout:%ds)", ui.IconCheckMark, params.CloudConnectionTimeoutSec) + grpcConn, err := agent.NewGRPCConnection(ctxTimeout, params.CloudAPITLSInsecure, params.CloudAPIURL, log.DefaultLogger) if err != nil { return nil, err } + output.PrintLogf("%s Connected to Testkube Cloud", ui.IconCheckMark) grpcClient := cloud.NewTestKubeCloudAPIClient(grpcConn) cloudExecutor := cloudexecutor.NewCloudGRPCExecutor(grpcClient, grpcConn, params.CloudAPIKey) diff --git a/pkg/scheduler/test_scheduler_test.go b/pkg/scheduler/test_scheduler_test.go index a40f93bff2b..c038a0f75f4 100644 --- a/pkg/scheduler/test_scheduler_test.go +++ b/pkg/scheduler/test_scheduler_test.go @@ -112,7 +112,7 @@ func TestGetExecuteOptions(t *testing.T) { ExecutionLabels: map[string]string{"label": "value"}, Namespace: "namespace", VariablesFile: "", - Variables: map[string]testkube.Variable{"var": testkube.Variable{Name: "one"}}, + Variables: map[string]testkube.Variable{"var": {Name: "one"}}, Command: []string{"run"}, Args: []string{}, ArgsMode: "",