Skip to content

Commit

Permalink
feat: webhook template and headers (#3978)
Browse files Browse the repository at this point in the history
* feat: webhook template and headers

* fix: escape text

* docs: cli

* feat: add unit test

* fix: unit test

* fix: remove reset

* fix: template

* fix: lint

* fi

* docs: webhook fields description

* fix: dep

* Update api/v1/testkube.yaml

Co-authored-by: Lilla Vass <[email protected]>

* Update pkg/api/v1/testkube/model_webhook.go

Co-authored-by: Lilla Vass <[email protected]>

* Update pkg/api/v1/testkube/model_webhook_create_request.go

Co-authored-by: Lilla Vass <[email protected]>

---------

Co-authored-by: Lilla Vass <[email protected]>
  • Loading branch information
vsukhin and vLia authored Jun 7, 2023
1 parent 56a8efd commit 5e1eb25
Show file tree
Hide file tree
Showing 26 changed files with 336 additions and 58 deletions.
11 changes: 11 additions & 0 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4533,6 +4533,17 @@ components:
payloadObjectField:
type: string
description: will load the generated payload for notification inside the object
payloadTemplate:
type: string
description: golang based template for notification payload
headers:
type: object
description: "webhook headers"
additionalProperties:
type: string
example:
env: "Content-Type"
app: "application/xml"
labels:
type: object
description: "webhook labels"
Expand Down
20 changes: 20 additions & 0 deletions cmd/kubectl-testkube/commands/webhooks/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package webhooks

import (
"fmt"
"os"
"strconv"

"github.com/spf13/cobra"
Expand All @@ -19,6 +21,8 @@ func NewCreateWebhookCmd() *cobra.Command {
selector string
labels map[string]string
payloadObjectField string
payloadTemplate string
headers map[string]string
)

cmd := &cobra.Command{
Expand All @@ -35,6 +39,14 @@ func NewCreateWebhookCmd() *cobra.Command {
}

namespace := cmd.Flag("namespace").Value.String()
payloadTemplate = cmd.Flag("payload-template").Value.String()
payloadTemplateContent := ""
if payloadTemplate != "" {
b, err := os.ReadFile(payloadTemplate)
ui.ExitOnError("reading job template", err)
payloadTemplateContent = string(b)
}

var client apiv1.Client
if !crdOnly {
client, namespace = common.GetClient(cmd)
Expand All @@ -53,6 +65,8 @@ func NewCreateWebhookCmd() *cobra.Command {
Selector: selector,
Labels: labels,
PayloadObjectField: payloadObjectField,
PayloadTemplate: payloadTemplateContent,
Headers: headers,
}

if !crdOnly {
Expand All @@ -61,6 +75,10 @@ func NewCreateWebhookCmd() *cobra.Command {

ui.Success("Webhook created", name)
} else {
if options.PayloadTemplate != "" {
options.PayloadTemplate = fmt.Sprintf("%q", options.PayloadTemplate)
}

data, err := crd.ExecuteTemplate(crd.TemplateWebhook, options)
ui.ExitOnError("executing crd template", err)

Expand All @@ -75,6 +93,8 @@ func NewCreateWebhookCmd() *cobra.Command {
cmd.Flags().StringVarP(&selector, "selector", "", "", "expression to select tests and test suites for webhook events: --selector app=backend")
cmd.Flags().StringToStringVarP(&labels, "label", "l", nil, "label key value pair: --label key1=value1")
cmd.Flags().StringVarP(&payloadObjectField, "payload-field", "", "", "field to use for notification object payload")
cmd.Flags().StringVarP(&payloadTemplate, "payload-template", "", "", "if webhook needs to send a custom notification, then a path to template file should be provided")
cmd.Flags().StringToStringVarP(&headers, "header", "", nil, "webhook header value pair: --header Content-Type=application/xml")

return cmd
}
9 changes: 9 additions & 0 deletions cmd/kubectl-testkube/commands/webhooks/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package webhooks

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -32,6 +33,10 @@ func NewGetWebhookCmd() *cobra.Command {
ui.ExitOnError("getting webhook: "+name, err)

if crdOnly {
if webhook.PayloadTemplate != "" {
webhook.PayloadTemplate = fmt.Sprintf("%q", webhook.PayloadTemplate)
}

common.UIPrintCRD(crd.TemplateWebhook, webhook, &firstEntry)
return
}
Expand All @@ -44,6 +49,10 @@ func NewGetWebhookCmd() *cobra.Command {

if crdOnly {
for _, webhook := range webhooks {
if webhook.PayloadTemplate != "" {
webhook.PayloadTemplate = fmt.Sprintf("%q", webhook.PayloadTemplate)
}

common.UIPrintCRD(crd.TemplateWebhook, webhook, &firstEntry)
}

Expand Down
9 changes: 8 additions & 1 deletion docs/docs/articles/webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ spec:
- end-test
- end-test-success
- end-test-failed
selector: ""
payloadobjectfield: ""
payloadtemplate: |
{"text": "event id {{ .Id }}"}
headers:
X-Token: "12345"
```
And then apply with:
Expand All @@ -25,4 +31,5 @@ And then apply with:
kubectl apply -f webhook.yaml
```

Here you'll be able to pass events depending on which webhooks you want to be triggered. Testkube will pass `Event` which can have `type` and `testExecution` or `testsuiteExecution` fields.
Here you'll be able to pass events depending on which webhooks you want to be triggered. Testkube will pass `Event` which can have `type` and `testExecution` or `testsuiteExecution` fields. You can run webhook for any test/test suite or only matched with selector expression. It's possible to provide additional headers as well as define your golang based template to create your own payload body based on `Event` object.

1 change: 1 addition & 0 deletions docs/docs/cli/testkube.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ testkube [flags]

* [testkube abort](testkube_abort.md) - Abort tests or test suites
* [testkube agent](testkube_agent.md) - Testkube Cloud Agent related commands
* [testkube cloud](testkube_cloud.md) - Testkube Cloud commands
* [testkube completion](testkube_completion.md) - Generate the autocompletion script for the specified shell
* [testkube config](testkube_config.md) - Set feature configuration value
* [testkube create](testkube_create.md) - Create resource
Expand Down
30 changes: 30 additions & 0 deletions docs/docs/cli/testkube_cloud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## testkube cloud

Testkube Cloud commands

```
testkube cloud [flags]
```

### Options

```
-h, --help help for cloud
```

### Options inherited from parent commands

```
-a, --api-uri string api uri, default value read from config if set (default "https://demo.testkube.io/results/v1")
-c, --client string client used for connecting to Testkube API one of proxy|direct (default "proxy")
--namespace string Kubernetes namespace, default value read from config if set (default "testkube")
--oauth-enabled enable oauth
--verbose show additional debug messages
```

### SEE ALSO

* [testkube](testkube.md) - Testkube entrypoint for kubectl plugin
* [testkube cloud connect](testkube_cloud_connect.md) - Testkube Cloud connect
* [testkube cloud disconnect](testkube_cloud_disconnect.md) - Switch back to Testkube OSS mode, based on active .kube/config file

43 changes: 43 additions & 0 deletions docs/docs/cli/testkube_cloud_connect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## testkube cloud connect

Testkube Cloud connect

```
testkube cloud connect [flags]
```

### Options

```
--agent-token string Testkube Cloud agent key [required for cloud mode]
--chart string chart name (usually you don't need to change it) (default "kubeshop/testkube")
--cloud-root-domain string defaults to testkube.io, usually don't need to be changed [required for cloud mode] (default "testkube.io")
--dashboard-replicas int Dashboard replicas
--dry-run dry run mode - only print commands that would be executed
--env-id string Testkube Cloud environment id [required for cloud mode]
-h, --help help for connect
--minio-replicas int MinIO replicas
--mongo-replicas int MongoDB replicas
--name string installation name (usually you don't need to change it) (default "testkube")
--namespace string namespace where to install (default "testkube")
--no-confirm don't ask for confirmation - unatended installation mode
--no-dashboard don't install dashboard
--no-minio don't install MinIO
--no-mongo don't install MongoDB
--org-id string Testkube Cloud organization id [required for cloud mode]
--values string path to Helm values file
```

### Options inherited from parent commands

```
-a, --api-uri string api uri, default value read from config if set (default "https://demo.testkube.io/results/v1")
-c, --client string client used for connecting to Testkube API one of proxy|direct (default "proxy")
--oauth-enabled enable oauth
--verbose show additional debug messages
```

### SEE ALSO

* [testkube cloud](testkube_cloud.md) - Testkube Cloud commands

39 changes: 39 additions & 0 deletions docs/docs/cli/testkube_cloud_disconnect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## testkube cloud disconnect

Switch back to Testkube OSS mode, based on active .kube/config file

```
testkube cloud disconnect [flags]
```

### Options

```
--chart string chart name (usually you don't need to change it) (default "kubeshop/testkube")
--dashboard-replicas int Dashboard replicas (default 1)
--dry-run dry run mode - only print commands that would be executed
-h, --help help for disconnect
--minio-replicas int MinIO replicas (default 1)
--mongo-replicas int MongoDB replicas (default 1)
--name string installation name (usually you don't need to change it) (default "testkube")
--namespace string namespace where to install (default "testkube")
--no-confirm don't ask for confirmation - unatended installation mode
--no-dashboard don't install dashboard
--no-minio don't install MinIO
--no-mongo don't install MongoDB
--values string path to Helm values file
```

### Options inherited from parent commands

```
-a, --api-uri string api uri, default value read from config if set (default "https://demo.testkube.io/results/v1")
-c, --client string client used for connecting to Testkube API one of proxy|direct (default "proxy")
--oauth-enabled enable oauth
--verbose show additional debug messages
```

### SEE ALSO

* [testkube cloud](testkube_cloud.md) - Testkube Cloud commands

2 changes: 1 addition & 1 deletion docs/docs/cli/testkube_completion_zsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ to enable it. You can execute the following once:

To load completions in your current shell session:

source <(testkube completion zsh); compdef _testkube testkube
source <(testkube completion zsh)

To load completions for every new session, execute once:

Expand Down
16 changes: 9 additions & 7 deletions docs/docs/cli/testkube_create_webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ testkube create webhook [flags]
### Options

```
-e, --events stringArray event types handled by executor e.g. start-test|end-test
-h, --help help for webhook
-l, --label stringToString label key value pair: --label key1=value1 (default [])
-n, --name string unique webhook name - mandatory
--payload-field string field to use for notification object payload
--selector string expression to select tests and test suites for webhook events: --selector app=backend
-u, --uri string URI which should be called when given event occurs
-e, --events stringArray event types handled by executor e.g. start-test|end-test
--header stringToString webhook header value pair: --header Content-Type=application/xml (default [])
-h, --help help for webhook
-l, --label stringToString label key value pair: --label key1=value1 (default [])
-n, --name string unique webhook name - mandatory
--payload-field string field to use for notification object payload
--payload-template string if webhook needs to send a custom notification, then a path to template file should be provided
--selector string expression to select tests and test suites for webhook events: --selector app=backend
-u, --uri string URI which should be called when given event occurs
```

### Options inherited from parent commands
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/cli/testkube_generate_tests-crds.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ testkube generate tests-crds <manifestDirectory> [flags]
### Options

```
--artifact-dir stringArray artifact dirs for container executor
--args-mode string usage mode for arguments. one of append|override (default "append")
--artifact-dir stringArray artifact dirs for scraping
--artifact-storage-class-name string artifact storage class name for container executor
--artifact-volume-mount-path string artifact volume mount path for container executor
--command stringArray command passed to image in executor
Expand Down
26 changes: 15 additions & 11 deletions docs/docs/cli/testkube_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ testkube init [flags]
### Options

```
--agent-key string Testkube Cloud agent key [required for cloud mode]
--agent-uri string Testkube Cloud agent URI [required for cloud mode]
--chart string chart name (default "kubeshop/testkube")
-h, --help help for init
--name string installation name (default "testkube")
--namespace string namespace where to install (default "testkube")
--no-confirm don't ask for confirmation - unatended installation mode
--no-dashboard don't install dashboard
--no-minio don't install MinIO
--no-mongo don't install MongoDB
--values string path to Helm values file
--agent-token string Testkube Cloud agent key [required for cloud mode]
--agent-uri string Testkube Cloud agent URI [required for cloud mode]
--chart string chart name (usually you don't need to change it) (default "kubeshop/testkube")
--cloud-root-domain string defaults to testkube.io, usually don't need to be changed [required for cloud mode] (default "testkube.io")
--dry-run dry run mode - only print commands that would be executed
--env-id string Testkube Cloud environment id [required for cloud mode]
-h, --help help for init
--name string installation name (usually you don't need to change it) (default "testkube")
--namespace string namespace where to install (default "testkube")
--no-confirm don't ask for confirmation - unatended installation mode
--no-dashboard don't install dashboard
--no-minio don't install MinIO
--no-mongo don't install MongoDB
--org-id string Testkube Cloud organization id [required for cloud mode]
--values string path to Helm values file
```

### Options inherited from parent commands
Expand Down
14 changes: 7 additions & 7 deletions docs/docs/cli/testkube_set_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ testkube set context <value> [flags]
### Options

```
-k, --api-key string API Key for Testkube Cloud
--cloud-api-uri string Testkube Cloud API URI - defaults to api.testksube.io (default "https://api.testkube.io")
-e, --env string Testkube Cloud Environment ID
-h, --help help for context
--kubeconfig reset context mode for CLI to default kubeconfig based
-n, --namespace string Testkube namespace to use for CLI commands
-o, --org string Testkube Cloud Organization ID
-k, --api-key string API Key for Testkube Cloud
--cloud-root-domain string defaults to testkube.io, usually you don't need to change it (default "testkube.io")
-e, --env string Testkube Cloud Environment ID
-h, --help help for context
--kubeconfig reset context mode for CLI to default kubeconfig based
-n, --namespace string Testkube namespace to use for CLI commands
-o, --org string Testkube Cloud Organization ID
```

### Options inherited from parent commands
Expand Down
26 changes: 15 additions & 11 deletions docs/docs/cli/testkube_upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ testkube upgrade [flags]
### Options

```
--agent-key string Testkube Cloud agent key [required for cloud mode]
--agent-uri string Testkube Cloud agent URI [required for cloud mode]
--chart string chart name (default "kubeshop/testkube")
-h, --help help for upgrade
--name string installation name (default "testkube")
--namespace string namespace where to install (default "testkube")
--no-confirm don't ask for confirmation - unatended installation mode
--no-dashboard don't install dashboard
--no-minio don't install MinIO
--no-mongo don't install MongoDB
--values string path to Helm values file
--agent-token string Testkube Cloud agent key [required for cloud mode]
--agent-uri string Testkube Cloud agent URI [required for cloud mode]
--chart string chart name (usually you don't need to change it) (default "kubeshop/testkube")
--cloud-root-domain string defaults to testkube.io, usually don't need to be changed [required for cloud mode] (default "testkube.io")
--dry-run dry run mode - only print commands that would be executed
--env-id string Testkube Cloud environment id [required for cloud mode]
-h, --help help for upgrade
--name string installation name (usually you don't need to change it) (default "testkube")
--namespace string namespace where to install (default "testkube")
--no-confirm don't ask for confirmation - unatended installation mode
--no-dashboard don't install dashboard
--no-minio don't install MinIO
--no-mongo don't install MongoDB
--org-id string Testkube Cloud organization id [required for cloud mode]
--values string path to Helm values file
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/joshdk/go-junit v1.0.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/kubeshop/testkube-operator v1.10.8-0.20230602102831-eed35fe7656d
github.com/kubeshop/testkube-operator v1.10.8-0.20230607142025-afea4265b6ea
github.com/minio/minio-go/v7 v7.0.47
github.com/montanaflynn/stats v0.6.6
github.com/moogar0880/problems v0.1.1
Expand Down
Loading

0 comments on commit 5e1eb25

Please sign in to comment.