Skip to content

Commit

Permalink
feat: add test triggers client for external use (#5771)
Browse files Browse the repository at this point in the history
* add test triggers client for external use

* add test triggers client for external use
  • Loading branch information
dejanzele authored Aug 26, 2024
1 parent 03126e0 commit e90df19
Show file tree
Hide file tree
Showing 17 changed files with 243 additions and 11 deletions.
24 changes: 20 additions & 4 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3782,7 +3782,7 @@ paths:
- pro
parameters:
- $ref: "#/components/parameters/ID"
- $ref: "#/components/parameters/TestWorkflowExecutionName"
- $ref: "#/components/parameters/TestWorkflowExecutionName"
summary: Execute test workflow
description: Execute test workflow in the kubernetes cluster
operationId: executeTestWorkflow
Expand Down Expand Up @@ -4433,7 +4433,7 @@ paths:
parameters:
- $ref: "#/components/parameters/ID"
- $ref: "#/components/parameters/SkipDeleteExecutions"
- $ref: "#/components/parameters/SkipDeleteCRD"
- $ref: "#/components/parameters/SkipDeleteCRD"
summary: Delete test workflow
description: Delete test workflow from the kubernetes cluster
operationId: deleteTestWorkflow
Expand Down Expand Up @@ -7099,6 +7099,14 @@ components:
example:
env: "prod"
app: "backend"
annotations:
type: object
description: "webhook annotations"
additionalProperties:
type: string
example:
env: "prod"
app: "backend"
disabled:
type: boolean
description: whether webhook is disabled
Expand Down Expand Up @@ -7191,7 +7199,7 @@ components:
- become-testworkflow-up
- become-testworkflow-down
- become-testworkflow-failed
- become-testworkflow-aborted
- become-testworkflow-aborted
- created
- updated
- deleted
Expand Down Expand Up @@ -7338,6 +7346,14 @@ components:
example:
env: "prod"
app: "backend"
annotations:
type: object
description: "test trigger annotations"
additionalProperties:
type: string
example:
group: "teamA"
app: "backend"
resource:
$ref: "#/components/schemas/TestTriggerResources"
resourceSelector:
Expand Down Expand Up @@ -7941,7 +7957,7 @@ components:
namespace:
type: string
description: execution namespace
example: "my-testkube"
example: "my-testkube"
number:
type: integer
description: sequence number for the execution
Expand Down
1 change: 1 addition & 0 deletions internal/app/api/v1/testtriggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (s *TestkubeAPI) UpdateTestTriggerHandler() fiber.Handler {
crdTestTrigger := testtriggersmapper.MapTestTriggerUpsertRequestToTestTriggerCRD(request)
testTrigger.Spec = crdTestTrigger.Spec
testTrigger.Labels = request.Labels
testTrigger.Annotations = request.Annotations
testTrigger, err = s.TestKubeClientset.TestsV1().TestTriggers(namespace).Update(c.UserContext(), testTrigger, v1.UpdateOptions{})

s.Metrics.IncUpdateTestTrigger(err)
Expand Down
10 changes: 7 additions & 3 deletions pkg/api/v1/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
)

// check in compile time if interface is implemented
var _ Client = (*APIClient)(nil)

// NewProxyAPIClient returns proxy api client
func NewProxyAPIClient(client kubernetes.Interface, config APIConfig) APIClient {
return APIClient{
Expand Down Expand Up @@ -46,6 +43,7 @@ func NewProxyAPIClient(client kubernetes.Interface, config APIConfig) APIClient
NewProxyClient[testkube.Artifact](client, config),
),
TestWorkflowTemplateClient: NewTestWorkflowTemplateClient(NewProxyClient[testkube.TestWorkflowTemplate](client, config)),
TestTriggerClient: NewTestTriggerClient(NewProxyClient[testkube.TestTrigger](client, config)),
}
}

Expand Down Expand Up @@ -84,6 +82,7 @@ func NewDirectAPIClient(httpClient *http.Client, sseClient *http.Client, apiURI,
NewDirectClient[testkube.Artifact](httpClient, apiURI, apiPathPrefix),
),
TestWorkflowTemplateClient: NewTestWorkflowTemplateClient(NewDirectClient[testkube.TestWorkflowTemplate](httpClient, apiURI, apiPathPrefix)),
TestTriggerClient: NewTestTriggerClient(NewDirectClient[testkube.TestTrigger](httpClient, apiURI, apiPathPrefix)),
}
}

Expand Down Expand Up @@ -122,6 +121,7 @@ func NewCloudAPIClient(httpClient *http.Client, sseClient *http.Client, apiURI,
NewCloudClient[testkube.Artifact](httpClient, apiURI, apiPathPrefix),
),
TestWorkflowTemplateClient: NewTestWorkflowTemplateClient(NewCloudClient[testkube.TestWorkflowTemplate](httpClient, apiURI, apiPathPrefix)),
TestTriggerClient: NewTestTriggerClient(NewCloudClient[testkube.TestTrigger](httpClient, apiURI, apiPathPrefix)),
}
}

Expand All @@ -137,4 +137,8 @@ type APIClient struct {
TemplateClient
TestWorkflowClient
TestWorkflowTemplateClient
TestTriggerClient
}

// check in compile time if interface is implemented
var _ Client = (*APIClient)(nil)
20 changes: 19 additions & 1 deletion pkg/api/v1/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Client interface {
TestWorkflowAPI
TestWorkflowExecutionAPI
TestWorkflowTemplateAPI
TestTriggerAPI
}

// TestAPI describes test api methods
Expand Down Expand Up @@ -97,6 +98,16 @@ type WebhookAPI interface {
DeleteWebhooks(selector string) (err error)
}

// TestTriggerAPI describes test triggers api methods
type TestTriggerAPI interface {
CreateTestTrigger(options CreateTestTriggerOptions) (testTrigger testkube.TestTrigger, err error)
UpdateTestTrigger(options UpdateTestTriggerOptions) (testTrigger testkube.TestTrigger, err error)
GetTestTrigger(name string) (testTrigger testkube.TestTrigger, err error)
ListTestTriggers(selector string) (testTriggers []testkube.TestTrigger, err error)
DeleteTestTrigger(name string) (err error)
DeleteTestTriggers(selector string) (err error)
}

// TemplateAPI describes template api methods
type TemplateAPI interface {
CreateTemplate(options CreateTemplateOptions) (template testkube.Template, err error)
Expand Down Expand Up @@ -212,6 +223,12 @@ type CreateTemplateOptions testkube.TemplateCreateRequest
// UpdateTemplateOptions - is mapping for now to OpenAPI schema for changing template request
type UpdateTemplateOptions testkube.TemplateUpdateRequest

// CreateTestTriggerOptions - is mapping for now to OpenAPI schema for creating trigger
type CreateTestTriggerOptions testkube.TestTriggerUpsertRequest

// UpdateTestTriggerOptions - is mapping for now to OpenAPI schema for changing trigger request
type UpdateTestTriggerOptions testkube.TestTriggerUpsertRequest

// TODO consider replacing it with testkube.ExecutionRequest - looks almost the samea and redundant
// ExecuteTestOptions contains test run options
type ExecuteTestOptions struct {
Expand Down Expand Up @@ -275,7 +292,8 @@ type Gettable interface {
testkube.Webhook | testkube.TestWithExecution | testkube.TestSuiteWithExecution | testkube.TestWithExecutionSummary |
testkube.TestSuiteWithExecutionSummary | testkube.Artifact | testkube.ServerInfo | testkube.Config | testkube.DebugInfo |
testkube.TestSource | testkube.Template |
testkube.TestWorkflow | testkube.TestWorkflowWithExecution | testkube.TestWorkflowTemplate | testkube.TestWorkflowExecution
testkube.TestWorkflow | testkube.TestWorkflowWithExecution | testkube.TestWorkflowTemplate | testkube.TestWorkflowExecution |
testkube.TestTrigger
}

// Executable is an interface of executable objects
Expand Down
79 changes: 79 additions & 0 deletions pkg/api/v1/client/testtriggers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package client

import (
"encoding/json"
"net/http"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
)

// NewTestTriggerClient creates new TestTrigger client
func NewTestTriggerClient(triggerTransport Transport[testkube.TestTrigger]) TestTriggerClient {
return TestTriggerClient{
triggerTransport: triggerTransport,
}
}

// TestTriggerClient is a client for triggers
type TestTriggerClient struct {
triggerTransport Transport[testkube.TestTrigger]
}

// GetTestTrigger gets trigger by name
func (c TestTriggerClient) GetTestTrigger(name string) (trigger testkube.TestTrigger, err error) {
uri := c.triggerTransport.GetURI("/triggers/%s", name)
return c.triggerTransport.Execute(http.MethodGet, uri, nil, nil)
}

// ListTestTriggers list all triggers
func (c TestTriggerClient) ListTestTriggers(selector string) (triggers []testkube.TestTrigger, err error) {
uri := c.triggerTransport.GetURI("/triggers")
params := map[string]string{
"selector": selector,
}

return c.triggerTransport.ExecuteMultiple(http.MethodGet, uri, nil, params)
}

// CreateTestTrigger creates new TestTrigger Custom Resource
func (c TestTriggerClient) CreateTestTrigger(options CreateTestTriggerOptions) (trigger testkube.TestTrigger, err error) {
uri := c.triggerTransport.GetURI("/triggers")
request := testkube.TestTriggerUpsertRequest(options)

body, err := json.Marshal(request)
if err != nil {
return trigger, err
}

return c.triggerTransport.Execute(http.MethodPost, uri, body, nil)
}

// UpdateTestTrigger updates TestTrigger Custom Resource
func (c TestTriggerClient) UpdateTestTrigger(options UpdateTestTriggerOptions) (trigger testkube.TestTrigger, err error) {
name := ""
if options.Name != "" {
name = options.Name
}

uri := c.triggerTransport.GetURI("/triggers/%s", name)
request := testkube.TestTriggerUpsertRequest(options)

body, err := json.Marshal(request)
if err != nil {
return trigger, err
}

return c.triggerTransport.Execute(http.MethodPatch, uri, body, nil)
}

// DeleteTestTriggers deletes all triggers
func (c TestTriggerClient) DeleteTestTriggers(selector string) (err error) {
uri := c.triggerTransport.GetURI("/triggers")
return c.triggerTransport.Delete(uri, selector, true)
}

// DeleteTestTrigger deletes single trigger by name
func (c TestTriggerClient) DeleteTestTrigger(name string) (err error) {
uri := c.triggerTransport.GetURI("/triggers/%s", name)
return c.triggerTransport.Delete(uri, "", true)
}
4 changes: 3 additions & 1 deletion pkg/api/v1/testkube/model_test_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type TestTrigger struct {
// test trigger namespace
Namespace string `json:"namespace,omitempty"`
// test trigger labels
Labels map[string]string `json:"labels,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
// test trigger annotations
Annotations map[string]string `json:"annotations,omitempty"`
Resource *TestTriggerResources `json:"resource"`
ResourceSelector *TestTriggerSelector `json:"resourceSelector"`
// listen for event for selected resource
Expand Down
26 changes: 26 additions & 0 deletions pkg/api/v1/testkube/model_test_trigger_extended.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Testkube API
*
* Testkube provides a Kubernetes-native framework for test definition, execution and results
*
* API version: 1.0.0
* Contact: [email protected]
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/
package testkube

func (t TestTrigger) GetName() string {
return t.Name
}

func (t TestTrigger) GetNamespace() string {
return t.Namespace
}

func (t TestTrigger) GetLabels() map[string]string {
return t.Labels
}

func (t TestTrigger) GetAnnotations() map[string]string {
return t.Annotations
}
4 changes: 3 additions & 1 deletion pkg/api/v1/testkube/model_test_trigger_upsert_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ type TestTriggerUpsertRequest struct {
// object name
Name string `json:"name"`
// test trigger labels
Labels map[string]string `json:"labels,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
// test trigger annotations
Annotations map[string]string `json:"annotations,omitempty"`
Resource *TestTriggerResources `json:"resource"`
ResourceSelector *TestTriggerSelector `json:"resourceSelector"`
// listen for event for selected resource
Expand Down
16 changes: 16 additions & 0 deletions pkg/api/v1/testkube/model_test_workflow_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,19 @@ func (w *TestWorkflow) GetObjectRef() *ObjectRef {

func (w *TestWorkflow) QuoteWorkflowTextFields() {
}

func (w TestWorkflow) GetName() string {
return w.Name
}

func (w TestWorkflow) GetNamespace() string {
return w.Namespace
}

func (w TestWorkflow) GetLabels() map[string]string {
return w.Labels
}

func (w TestWorkflow) GetAnnotations() map[string]string {
return w.Annotations
}
16 changes: 16 additions & 0 deletions pkg/api/v1/testkube/model_test_workflow_with_execution_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,19 @@ func (t TestWorkflowWithExecutions) Table() (header []string, output [][]string)

return
}

func (t TestWorkflowWithExecution) GetName() string {
return t.Workflow.Name
}

func (t TestWorkflowWithExecution) GetNamespace() string {
return t.Workflow.Namespace
}

func (t TestWorkflowWithExecution) GetLabels() map[string]string {
return t.Workflow.Labels
}

func (t TestWorkflowWithExecution) GetAnnotations() map[string]string {
return t.Workflow.Annotations
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Testkube API
*
* Testkube provides a Kubernetes-native framework for test definition, execution and results
*
* API version: 1.0.0
* Contact: [email protected]
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/
package testkube

func (w TestWorkflowWithExecutionSummary) GetName() string {
return w.Workflow.Name
}

func (w TestWorkflowWithExecutionSummary) GetNamespace() string {
return w.Workflow.Namespace
}

func (w TestWorkflowWithExecutionSummary) GetLabels() map[string]string {
return w.Workflow.Labels
}

func (w TestWorkflowWithExecutionSummary) GetAnnotations() map[string]string {
return w.Workflow.Annotations
}
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Webhook struct {
Headers map[string]string `json:"headers,omitempty"`
// webhook labels
Labels map[string]string `json:"labels,omitempty"`
// webhook annotations
Annotations map[string]string `json:"annotations,omitempty"`
// whether webhook is disabled
Disabled bool `json:"disabled,omitempty"`
}
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_webhook_create_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type WebhookCreateRequest struct {
Headers map[string]string `json:"headers,omitempty"`
// webhook labels
Labels map[string]string `json:"labels,omitempty"`
// webhook annotations
Annotations map[string]string `json:"annotations,omitempty"`
// whether webhook is disabled
Disabled bool `json:"disabled,omitempty"`
}
16 changes: 16 additions & 0 deletions pkg/api/v1/testkube/model_webhook_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ func (list Webhooks) Table() (header []string, output [][]string) {

return
}

func (w Webhook) GetName() string {
return w.Name
}

func (w Webhook) GetNamespace() string {
return w.Namespace
}

func (w Webhook) GetLabels() map[string]string {
return w.Labels
}

func (w Webhook) GetAnnotations() map[string]string {
return w.Annotations
}
Loading

0 comments on commit e90df19

Please sign in to comment.