Skip to content

Commit

Permalink
fix: update cloud env vars to pro (#4994)
Browse files Browse the repository at this point in the history
* fix: cloud env vars rename to pro

* fix: update cloud env vars to pro
  • Loading branch information
vLia authored Feb 12, 2024
1 parent d0c837b commit 4901f3f
Show file tree
Hide file tree
Showing 20 changed files with 289 additions and 110 deletions.
18 changes: 15 additions & 3 deletions cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,19 +502,31 @@ func main() {

if mode == common.ModeAgent {
log.DefaultLogger.Info("starting agent service")
proContext := config.ProContext{
APIKey: cfg.TestkubeProAPIKey,
URL: cfg.TestkubeProURL,
LogsPath: cfg.TestkubeProLogsPath,
TLSInsecure: cfg.TestkubeProTLSInsecure,
WorkerCount: cfg.TestkubeProWorkerCount,
LogStreamWorkerCount: cfg.TestkubeProLogStreamWorkerCount,
SkipVerify: cfg.TestkubeProSkipVerify,
EnvID: cfg.TestkubeProEnvID,
OrgID: cfg.TestkubeProOrgID,
Migrate: cfg.TestkubeProMigrate,
}

api.WithProContext(&proContext)

agentHandle, err := agent.NewAgent(
log.DefaultLogger,
api.Mux.Handler(),
cfg.TestkubeProAPIKey,
grpcClient,
cfg.TestkubeProWorkerCount,
cfg.TestkubeProLogStreamWorkerCount,
api.GetLogsStream,
clusterId,
cfg.TestkubeClusterName,
envs,
features,
proContext,
)
if err != nil {
ui.ExitOnError("Starting agent", err)
Expand Down
4 changes: 2 additions & 2 deletions contrib/executor/init/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ func (r *InitRunner) Run(ctx context.Context, execution testkube.Execution) (res
}

// TODO: write a proper cloud implementation
if r.Params.Endpoint != "" && !r.Params.CloudMode {
if r.Params.Endpoint != "" && !r.Params.ProMode {
output.PrintLogf("%s Fetching uploads from object store %s...", ui.IconFile, r.Params.Endpoint)
opts := minio.GetTLSOptions(r.Params.Ssl, r.Params.SkipVerify, r.Params.CertFile, r.Params.KeyFile, r.Params.CAFile)
minioClient := minio.NewClient(r.Params.Endpoint, r.Params.AccessKeyID, r.Params.SecretAccessKey, r.Params.Region, r.Params.Token, r.Params.Bucket, opts...)
fp := content.NewCopyFilesPlacer(minioClient)
fp.PlaceFiles(ctx, execution.TestName, execution.BucketName)
} else if r.Params.CloudMode {
} else if r.Params.ProMode {
output.PrintLogf("%s Copy files functionality is currently not supported in cloud mode", ui.IconWarning)
}

Expand Down
60 changes: 34 additions & 26 deletions docs/docs/articles/running-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,32 +212,40 @@ By default, there is a 10 second timeout limit on all requests on the client sid
The following environment variables are automatically injected into each executed test pod:
DEBUG: if debug mode is on
RUNNER_ENDPOINT: minio endpoint
RUNNER_ACCESSKEYID: minio access key id
RUNNER_SECRETACCESSKEY: minio secret access key
RUNNER_REGION: minio region
RUNNER_TOKEN: mnio token
RUNNER_SSL: if minio ssl is on
RUNNER_SCRAPPERENABLED: if scraping is on
RUNNER_DATADIR: data directory
RUNNER_CDEVENTS_TARGET: cd events target endpoint
RUNNER_COMPRESSARTIFACTS: if artfifacts should be compressed
RUNNER_CLOUD_MODE: cloud mode
RUNNER_CLOUD_API_KEY: cloud api key
RUNNER_CLOUD_API_TLS_INSECURE: if cloud connection is insecure
RUNNER_CLOUD_API_URL: cloud api url
RUNNER_DASHBOARD_URI: dashboard uri
CI: ci flag
RUNNER_CLUSTERID: cluster id
RUNNER_BUCKET: minio bucket
RUNNER_WORKINGDIR: working directory
RUNNER_EXECUTIONID: test execution id
RUNNER_TESTNAME: test name
RUNNER_EXECUTIONNUMBER: test execution number
RUNNER_CONTEXTTYPE: running context type
RUNNER_CONTEXTDATA: running context data
RUNNER_APIURI: api uri
DEBUG: if debug mode is on
RUNNER_ENDPOINT: minio endpoint
RUNNER_ACCESSKEYID: minio access key id
RUNNER_SECRETACCESSKEY: minio secret access key
RUNNER_REGION: minio region
RUNNER_TOKEN: minio token
RUNNER_SSL: if minio ssl is on
RUNNER_SCRAPPERENABLED: if scraping is on
RUNNER_DATADIR: data directory
RUNNER_CDEVENTS_TARGET: cd events target endpoint
RUNNER_COMPRESSARTIFACTS: if artfifacts should be compressed
RUNNER_PRO_MODE: pro mode
RUNNER_PRO_API_KEY: pro api key
RUNNER_PRO_API_TLS_INSECURE: if pro connection is insecure
RUNNER_PRO_API_URL: pro api url
RUNNER_PRO_CONNECTION_TIMEOUT: pro connection timeout limit
RUNNER_PRO_API_SKIP_VERIFY: if pro connection tls verification is off
RUNNER_CLOUD_MODE: DEPRECATED: please use RUNNER_PRO_MODE instead
RUNNER_CLOUD_API_KEY: DEPRECATED: please use RUNNER_PRO_API_KEY instead
RUNNER_CLOUD_API_TLS_INSECURE: DEPRECATED: please use RUNNER_PRO_API_TLS_INSECURE instead
RUNNER_CLOUD_API_URL: DEPRECATED: please use RUNNER_PRO_API_URL instead
RUNNER_CLOUD_CONNECTION_TIMEOUT: DEPRECATED: please use RUNNER_PRO_CONNECTION_TIMEOUT instead
RUNNER_CLOUD_API_SKIP_VERIFY: DEPRECATED: please use RUNNER_PRO_API_SKIP_VERITY instead
RUNNER_DASHBOARD_URI: dashboard uri
CI: ci flag
RUNNER_CLUSTERID: cluster id
RUNNER_BUCKET: minio bucket
RUNNER_WORKINGDIR: working directory
RUNNER_EXECUTIONID: test execution id
RUNNER_TESTNAME: test name
RUNNER_EXECUTIONNUMBER: test execution number
RUNNER_CONTEXTTYPE: running context type
RUNNER_CONTEXTDATA: running context data
RUNNER_APIURI: api uri
## Summary
Expand Down
36 changes: 28 additions & 8 deletions docs/docs/articles/webhooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ You can create a webhook from the dashboard, use the CLI, or create it as a cust

- Monitoring and Observability: Webhooks can also be used to send alerts and notifications to your monitoring and observability tools like Prometheus and Grafana. This provides visibility into your tests, alerts you, and ensures that timely corrective actions can be taken.


## Creating a Webhook

The webhook can be created using the Dashboard, CLI, or a Custom Resource.

<Tabs>
Expand Down Expand Up @@ -56,6 +56,7 @@ Webhooks can be created with Testkube CLI using the `create webhook` command.
```sh
testkube create webhook --name example-webhook --events start-test --events end-test-success --events end-test-failed --uri <YOUR_ENDPOINT_URL>
```

`--name` - Your webhook name (in this case `example-webhook`).
`--events` - Event that will trigger a webhook. Multiple `--events` can be defined (in this case `--events start-test --events end-test-success --events end-test-failed`). All available trigger events can be found in the [Supported Event types](#supported-event-types) section.
`--uri` - The HTTPS endpoint where you want to receive the webhook events.
Expand All @@ -78,6 +79,7 @@ spec:
- end-test-failed
selector: ""
```
Where <YOUR_ENDPOINT_URL> should be replaced with the HTTPS endpoint URL where you want to receive the webhook events.
And then apply with:
Expand All @@ -91,6 +93,7 @@ kubectl apply -f webhook.yaml
</Tabs>

### Resource Selector (labels)

In order to limit webhook triggers to a specific resource, or resources, the Resource Selector can be used. It allows you to select the specific resource by label, or labels.

<Tabs>
Expand Down Expand Up @@ -134,10 +137,10 @@ spec:
</TabItem>
</Tabs>
### Webhook Payload
Webhook payload can be configured - in this example, `event id`:

```
{"text": "event id {{ .Id }}"}
```
Expand Down Expand Up @@ -166,8 +169,8 @@ And set it with `--payload-template template.json`.
```sh
testkube create webhook --name example-webhook --events start-test --events end-test-passed --events end-test-failed --payload-template template.json --uri <YOUR_ENDPOINT_URL>
```
Where <YOUR_ENDPOINT_URL> should be replaced with the HTTPS endpoint URL where you want to receive the webhook events.

Where <YOUR_ENDPOINT_URL> should be replaced with the HTTPS endpoint URL where you want to receive the webhook events.

```sh title="Expected output:"
Webhook created example-webhook 🥇
Expand All @@ -178,12 +181,14 @@ Webhook created example-webhook 🥇
<TabItem value="crd" label="Custom Resource">

Payload template can be configured with `spec.payloadTemplate`.

```
payloadTemplate: |
{"text": "event id {{ .Id }}"}
```

Example:

```
apiVersion: executor.testkube.io/v1
kind: Webhook
Expand All @@ -207,35 +212,43 @@ spec:
</Tabs>

### Webhook Payload Variables
Webhook payload can contain **event-specific** variables - they will be replaced with actual data when the events occurs. In the above examples, only the event `Id` is being sent.

Webhook payload can contain **event-specific** variables - they will be replaced with actual data when the events occurs. In the above examples, only the event `Id` is being sent.
However, any of these [supported Event Variables](#supported-event-variables) can be used.

For example, the following payload:

```
{"text": "Event {{ .Type_ }} - Test '{{ .TestExecution.TestName }}' execution ({{ .TestExecution.Number }}) finished with '{{ .TestExecution.ExecutionResult.Status }}' status"}
```

will result in:

```
{"text": "Event end-test-success - Test 'postman-executor-smoke' execution (948) finished with 'passed' status"}
```

#### testkube-api-server ENV variables

In addition to event-specific variables, it's also possible to pass testkube-api-server ENV variables:

```sh title="template.txt"
TESTKUBE_CLOUD_URL: {{ index .Envs "TESTKUBE_CLOUD_URL" }}
TESTKUBE_PRO_URL: {{ index .Envs "TESTKUBE_PRO_URL" }}
```

### URI and HTTP Headers

You can add additional HTTP headers like `Authorization` or `x-api-key` to have a secret token.
It's possible to use golang based template string as header or uri value.

### Helper methods

We also provide special helper methods to use in the webhook template:
`executionstatustostring` is the method to convert a pointer to a execution status to a string type.
`testsuiteexecutionstatustostring` is the method to convert a pointer to a test suite execution status to a string type.

Usage example:

```yaml
- name: TEXT_COLOUR
value: {{ if eq (.TestSuiteExecution.Status | testsuiteexecutionstatustostring ) "passed" }}"00FF00"{{ else }}"FF0000"{{ end }}
Expand Down Expand Up @@ -288,7 +301,9 @@ spec:
</Tabs>

## Supported Event types

Webhooks can be triggered on any of the following events:

- start-test
- end-test-success
- end-test-failed
Expand All @@ -304,6 +319,7 @@ Webhooks can be triggered on any of the following events:
- deleted

They can be triggered by the following resources:

- test
- testsuite
- executor
Expand All @@ -315,6 +331,7 @@ They can be triggered by the following resources:
## Supported Event Variables

### Event-specific variables:

- `Id` - event ID (for example, `2a20c7da-3b77-4ea9-a33d-403187d3e9e6`)
- `Resource`
- `ResourceId`
Expand All @@ -327,16 +344,17 @@ They can be triggered by the following resources:
The full Event Data Model can be found [here](https://github.com/kubeshop/testkube/blob/main/pkg/api/v1/testkube/model_event.go).

### TestExecution (Execution):

- `Id` - Execution ID (for example, `64f8cf3c712890925aea51ce`)
- `TestName` - Test Name (for example, `postman-executor-smoke`)
- `TestSuiteName` - Test Suite name (if run as a part of a Test Suite)
- `TestNamespace` - Execution namespace, where testkube is installed (for example, `testkube`)
- `TestType` - Test type (for example, `postman/collection`)
- `Name` - Execution name (for example, `postman-executor-smoke-937)
- `Name` - Execution name (for example, `postman-executor-smoke-937)
- `Number` - Execution number (for example, `937`)
- `Envs` - List of ENV variables for specific Test (if defined)
- `Envs` - List of ENV variables for specific Test (if defined)
- `Command` - Command executed inside the Pod (for example, `newman`)
- `Args` - Command arguments (for example, `run <runPath> -e <envFile> --reporters cli,json --reporter-json-export <reportFile>`)
- `Args` - Command arguments (for example, `run <runPath> -e <envFile> --reporters cli,json --reporter-json-export <reportFile>`)
- `Variables` - List of variables
- `Content` - Test content
- `StartTime` - Test start time (for example, `2023-09-06 19:23:34.543433547 +0000 UTC`)
Expand Down Expand Up @@ -369,10 +387,12 @@ The full TestSuiteExecution data model can be found [here](https://github.com/ku
## Additional Examples

### Microsoft Teams

Webhooks can also be used to send messages to Microsoft Teams channels.
First, you need to create an incoming webhook in Teams for a specific channel. You can see how to do it in the Teams Docs [here](https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook?tabs=dotnet#create-incoming-webhooks-1). After your Teams incoming webhook is created, you can use it with Testkube webhooks - just use the URL provided (it will probably look like this: `https://xxxxx.webhook.office.com/xxxxxxxxx`).

In order to send the message when test execution finishes, the following Webhook can be used:

```
apiVersion: executor.testkube.io/v1
kind: Webhook
Expand Down
17 changes: 8 additions & 9 deletions internal/app/api/v1/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package v1
import (
"fmt"
"net/http"
"os"
"strings"

"github.com/gofiber/fiber/v2"
Expand All @@ -22,11 +21,6 @@ const (
// mediaTypeYAML is yaml media type
mediaTypeYAML = "text/yaml"

// env names for cloud context
cloudApiKeyEnvName = "TESTKUBE_CLOUD_API_KEY"
cloudEnvIdEnvName = "TESTKUBE_CLOUD_ENV_ID"
cloudOrgIdEnvName = "TESTKUBE_CLOUD_ORG_ID"

// contextCloud is cloud context
contextCloud = "cloud"
// contextOSS is oss context
Expand Down Expand Up @@ -57,17 +51,22 @@ func (s *TestkubeAPI) AuthHandler() fiber.Handler {
// InfoHandler is a handler to get info
func (s *TestkubeAPI) InfoHandler() fiber.Handler {
apiContext := contextOSS
if os.Getenv(cloudApiKeyEnvName) != "" {
if s.proContext != nil && s.proContext.APIKey != "" {
apiContext = contextCloud
}
var envID, orgID string
if s.proContext != nil {
envID = s.proContext.EnvID
orgID = s.proContext.OrgID
}
return func(c *fiber.Ctx) error {
return c.JSON(testkube.ServerInfo{
Commit: version.Commit,
Version: version.Version,
Namespace: s.Namespace,
Context: apiContext,
EnvId: os.Getenv(cloudEnvIdEnvName),
OrgId: os.Getenv(cloudOrgIdEnvName),
EnvId: envID,
OrgId: orgID,
HelmchartVersion: s.helmchartVersion,
DashboardUri: s.dashboardURI,
Features: &testkube.Features{
Expand Down
13 changes: 10 additions & 3 deletions internal/app/api/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (

"github.com/pkg/errors"

"github.com/kubeshop/testkube/internal/config"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/repository/config"
repoConfig "github.com/kubeshop/testkube/pkg/repository/config"

"github.com/kubeshop/testkube/pkg/version"

Expand Down Expand Up @@ -71,7 +72,7 @@ func NewTestkubeAPI(
clientset kubernetes.Interface,
testkubeClientset testkubeclientset.Interface,
testsourcesClient *testsourcesclientv1.TestSourcesClient,
configMap config.Repository,
configMap repoConfig.Repository,
clusterId string,
eventsEmitter *event.Emitter,
executor client.Executor,
Expand Down Expand Up @@ -183,7 +184,7 @@ type TestkubeAPI struct {
oauthParams oauthParams
WebsocketLoader *ws.WebsocketLoader
Events *event.Emitter
ConfigMap config.Repository
ConfigMap repoConfig.Repository
scheduler *scheduler.Scheduler
Clientset kubernetes.Interface
slackLoader *slack.SlackLoader
Expand All @@ -198,6 +199,7 @@ type TestkubeAPI struct {
featureFlags featureflags.FeatureFlags
logsStream logsclient.Stream
logGrpcClient logsclient.StreamGetter
proContext *config.ProContext
}

type storageParams struct {
Expand Down Expand Up @@ -588,3 +590,8 @@ func getFilterFromRequest(c *fiber.Ctx) result.Filter {

return filter
}

func (s *TestkubeAPI) WithProContext(proContext *config.ProContext) *TestkubeAPI {
s.proContext = proContext
return s
}
Loading

0 comments on commit 4901f3f

Please sign in to comment.