Skip to content

Commit

Permalink
feat: [TKC-2556] test workflow running context (#5926)
Browse files Browse the repository at this point in the history
* feat: running context model for test workflows

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* feat: add running context to test workflow execution models

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: unit test

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: dep update

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* feat: map test workflow running context

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* feat: pass running context to execution

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: running context for test workflows

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add test workflow running context model

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: additional enum for test workflow running context

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add one more enum

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: mapping for test workflow running context

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: mapping for cd events for test workflow running context

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: show test workflow running context

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* feat: get full psth

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add parent ids for test workflow running context

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: format running context ui

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fux: add to mock

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: remove unused var

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: dep update

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: merge

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: change running context model

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: use reference instead of array

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: dep update

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: additional running contexts

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add tcl for testworkflow mapper

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: trigger running context

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* feat: move runnng context to tcl

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fixL move mapper to tcl

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: remove test

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* ifx: move running context to tcl package

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: parse uername and email

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: dep update

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: merge

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: use api running context from cloud

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: remove username

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: remove username

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: check for cloud mode in api call

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: check for trigger cloud mode

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: check cloud for executing test workflow

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: fmt

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: tcl for printing running context

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: unit test

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: support ci/cd

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add actor filters

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: mongo query

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: test

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: int test

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: unit tests

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: assign running context

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: tune printing running context

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: forrmat printing

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: field name

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: rename parameters

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: dep update

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

---------

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>
  • Loading branch information
vsukhin authored Oct 28, 2024
1 parent 3b83571 commit 1460d08
Show file tree
Hide file tree
Showing 37 changed files with 888 additions and 34 deletions.
98 changes: 98 additions & 0 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3806,6 +3806,8 @@ paths:
parameters:
- $ref: "#/components/parameters/ID"
- $ref: "#/components/parameters/TagSelector"
- $ref: "#/components/parameters/ActorName"
- $ref: "#/components/parameters/ActorType"
summary: List test workflow executions
description: List test workflow executions
operationId: listTestWorkflowExecutionsByTestWorkflow
Expand Down Expand Up @@ -4071,6 +4073,8 @@ paths:
parameters:
- $ref: "#/components/parameters/ID"
- $ref: "#/components/parameters/TagSelector"
- $ref: "#/components/parameters/ActorName"
- $ref: "#/components/parameters/ActorType"
summary: List test workflow executions
description: List test workflow executions
operationId: listTestWorkflowExecutions
Expand Down Expand Up @@ -7158,10 +7162,77 @@ components:
- testsuite
- testtrigger
- scheduler
- testworkflow
context:
type: string
description: Context value depending from its type

TestWorkflowRunningContext:
description: running context for test workflow execution
type: object
required:
- interface
- actor
properties:
interface:
$ref: "#/components/schemas/TestWorkflowRunningContextInterface"
actor:
$ref: "#/components/schemas/TestWorkflowRunningContextActor"

TestWorkflowRunningContextInterface:
description: running context interface for test workflow execution
type: object
required:
- type
properties:
name:
type: string
description: interface name
type:
$ref: "#/components/schemas/TestWorkflowRunningContextInterfaceType"

TestWorkflowRunningContextActor:
description: running context actor for test workflow execution
type: object
required:
- type
properties:
name:
type: string
description: actor name
email:
type: string
description: actor email
executionId:
type: string
description: test workflow execution id
executionPath:
type: string
description: all test workflow execution ids starting from the root
type:
$ref: "#/components/schemas/TestWorkflowRunningContextActorType"

TestWorkflowRunningContextInterfaceType:
description: supported interfaces for test workflow running context
type: string
enum:
- cli
- ui
- api
- ci/cd
- internal

TestWorkflowRunningContextActorType:
description: supported actors for test workflow running context
type: string
enum:
- cron
- testtrigger
- user
- testworkflow
- testworkflowexecution
- program

Webhook:
description: CRD based webhook data
type: object
Expand Down Expand Up @@ -8012,6 +8083,14 @@ components:
default: false
tags:
$ref: "#/components/schemas/TestWorkflowTagValue"
runningContext:
description: running context for the test workflow execution (Pro edition only)
$ref: "#/components/schemas/TestWorkflowRunningContext"
parentExecutionIds:
type: array
description: parent execution ids
items:
type: string

TestWorkflowWithExecution:
type: object
Expand Down Expand Up @@ -8110,6 +8189,9 @@ components:
- false
tags:
$ref: "#/components/schemas/TestWorkflowTagValue"
runningContext:
description: running context for the test workflow execution (Pro edition only)
$ref: "#/components/schemas/TestWorkflowRunningContext"
required:
- id
- name
Expand Down Expand Up @@ -8144,6 +8226,9 @@ components:
$ref: "#/components/schemas/TestWorkflowSummary"
tags:
$ref: "#/components/schemas/TestWorkflowTagValue"
runningContext:
description: running context for the test workflow execution (Pro edition only)
$ref: "#/components/schemas/TestWorkflowRunningContext"
required:
- id
- name
Expand Down Expand Up @@ -10672,6 +10757,19 @@ components:
schema:
type: string
description: Test workflow execution tags
ActorName:
in: query
name: actorName
schema:
type: string
description: Test workflow running conntext actor name
ActorType:
in: query
name: actorType
schema:
type: string
description: Test workflow running conntext actor type

requestBodies:
UploadsBody:
description: "Upload files request body data"
Expand Down
1 change: 1 addition & 0 deletions cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ func main() {
triggers.WithTestkubeNamespace(cfg.TestkubeNamespace),
triggers.WithWatcherNamespaces(cfg.TestkubeWatcherNamespaces),
triggers.WithDisableSecretCreation(!secretConfig.AutoCreate),
triggers.WithProContext(&proContext),
)
log.DefaultLogger.Info("starting trigger service")
g.Go(func() error {
Expand Down
47 changes: 40 additions & 7 deletions cmd/kubectl-testkube/commands/testworkflows/executions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testworkflows

import (
"fmt"
"os"
"strings"

Expand All @@ -9,17 +10,18 @@ import (
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/render"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/testworkflows/renderer"
tc "github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
)

func NewGetTestWorkflowExecutionsCmd() *cobra.Command {
var (
limit int
selectors []string
testWorkflowName string
logsOnly bool
tags []string
limit int
selectors []string
testWorkflowName, actorName, actorType string
logsOnly bool
tags []string
)

cmd := &cobra.Command{
Expand All @@ -41,12 +43,20 @@ func NewGetTestWorkflowExecutionsCmd() *cobra.Command {
client, _, err := common.GetClient(cmd)
ui.ExitOnError("getting client", err)

err = validateActorType(testkube.TestWorkflowRunningContextActorType(actorType))
ui.ExitOnError("validatig actor type", err)

if len(args) == 0 {
client, _, err := common.GetClient(cmd)
ui.ExitOnError("getting client", err)

executions, err := client.ListTestWorkflowExecutions(testWorkflowName, limit,
strings.Join(selectors, ","), strings.Join(tags, ","))
options := tc.FilterTestWorkflowExecutionOptions{
Selector: strings.Join(selectors, ","),
TagSelector: strings.Join(tags, ","),
ActorName: actorName,
ActorType: testkube.TestWorkflowRunningContextActorType(actorType),
}
executions, err := client.ListTestWorkflowExecutions(testWorkflowName, limit, options)
ui.ExitOnError("getting test workflow executions list", err)
err = render.List(cmd, testkube.TestWorkflowExecutionSummaries(executions.Results), os.Stdout)
ui.ExitOnError("rendering list", err)
Expand Down Expand Up @@ -87,6 +97,29 @@ func NewGetTestWorkflowExecutionsCmd() *cobra.Command {
cmd.Flags().StringSliceVarP(&selectors, "label", "l", nil, "label key value pair: --label key1=value1")
cmd.Flags().BoolVar(&logsOnly, "logs-only", false, "show only execution logs")
cmd.Flags().StringSliceVarP(&tags, "tag", "", nil, "tag key value pair: --tag key1=value1")
cmd.Flags().StringVarP(&actorName, "actor-name", "", "", "test workflow running context actor name")
cmd.Flags().StringVarP(&actorType, "actor-type", "", "", "test workflow running context actor type one of cron|testtrigger|user|testworkfow|testworkflowexecution|program")

return cmd
}

func validateActorType(actorType testkube.TestWorkflowRunningContextActorType) error {
if actorType == "" {
return nil
}

actorTypes := map[testkube.TestWorkflowRunningContextActorType]struct{}{
testkube.CRON_TestWorkflowRunningContextActorType: {},
testkube.TESTTRIGGER_TestWorkflowRunningContextActorType: {},
testkube.USER_TestWorkflowRunningContextActorType: {},
testkube.TESTWORKFLOW_TestWorkflowRunningContextActorType: {},
testkube.TESTWORKFLOWEXECUTION_TestWorkflowRunningContextActorType: {},
testkube.PROGRAM_TestWorkflowRunningContextActorType: {},
}

if _, ok := actorTypes[actorType]; !ok {
return fmt.Errorf("please pass one of cron|testtrigger|user|testworkfow|testworkflowexecution|program for actor type")
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/render"
"github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
tclcmd "github.com/kubeshop/testkube/pkg/tcl/testworkflowstcl/cmd"
"github.com/kubeshop/testkube/pkg/ui"
)

Expand Down Expand Up @@ -63,6 +64,8 @@ func printPrettyOutput(ui *ui.UI, execution testkube.TestWorkflowExecution) {
ui.NL()
ui.Warn("Tags: ", testkube.MapToString(execution.Tags))
}
// Pro edition only (tcl protected code)
tclcmd.PrintRunningContext(ui, execution)
if execution.Result != nil && execution.Result.Status != nil {
ui.Warn("Status: ", string(*execution.Result.Status))
if !execution.Result.QueuedAt.IsZero() {
Expand Down
21 changes: 21 additions & 0 deletions cmd/kubectl-testkube/commands/testworkflows/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ import (
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/render"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/tests"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/testworkflows/renderer"
testkubecfg "github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
"github.com/kubeshop/testkube/cmd/testworkflow-init/instructions"
apiclientv1 "github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
tclcmd "github.com/kubeshop/testkube/pkg/tcl/testworkflowstcl/cmd"
"github.com/kubeshop/testkube/pkg/telemetry"
"github.com/kubeshop/testkube/pkg/testworkflows/testworkflowprocessor/constants"
"github.com/kubeshop/testkube/pkg/ui"
)
Expand Down Expand Up @@ -63,11 +66,29 @@ func NewRunTestWorkflowCmd() *cobra.Command {
ui.ExitOnError("getting client", err)

name := args[0]
runContext := telemetry.GetCliRunContext()
interfaceType := testkube.CICD_TestWorkflowRunningContextInterfaceType
if runContext == "others|local" {
runContext = ""
interfaceType = testkube.CLI_TestWorkflowRunningContextInterfaceType
}

cfg, err := testkubecfg.Load()
ui.ExitOnError("loading config file", err)
ui.NL()

var runningContext *testkube.TestWorkflowRunningContext
// Pro edition only (tcl protected code)
if cfg.ContextType == testkubecfg.ContextTypeCloud {
runningContext = tclcmd.GetRunningContext(runContext, cfg.CloudContext.ApiKey, interfaceType)
}

execution, err := client.ExecuteTestWorkflow(name, testkube.TestWorkflowExecutionRequest{
Name: executionName,
Config: config,
DisableWebhooks: disableWebhooks,
Tags: tags,
RunningContext: runningContext,
})
if err != nil {
// User friendly Open Source operation error
Expand Down
20 changes: 19 additions & 1 deletion cmd/tcl/testworkflow-toolkit/commands/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -72,7 +73,7 @@ func buildTestExecution(test testworkflowsv1.StepExecuteTest, async bool) (func(

exec, err := c.ExecuteTest(test.Name, test.ExecutionRequest.Name, client.ExecuteTestOptions{
RunningContext: &testkube.RunningContext{
Type_: "testworkflow",
Type_: string(testkube.RunningContextTypeTestWorkflow),
Context: fmt.Sprintf("%s/executions/%s", config.WorkflowName(), config.ExecutionId()),
},
IsVariablesFileUploaded: test.ExecutionRequest.IsVariablesFileUploaded,
Expand Down Expand Up @@ -162,13 +163,30 @@ func buildWorkflowExecution(workflow testworkflowsv1.StepExecuteWorkflow, async

tags := config.ExecutionTags()

parentIds := []string{config.ExecutionId()}
if config.Config().Execution.ParentIds != "" {
parentIds = append(strings.Split(config.Config().Execution.ParentIds, "/"), parentIds...)
}

var exec testkube.TestWorkflowExecution
for i := 0; i < CreateExecutionRetryOnFailureMaxAttempts; i++ {
exec, err = c.ExecuteTestWorkflow(workflow.Name, testkube.TestWorkflowExecutionRequest{
Name: workflow.ExecutionName,
Config: testworkflows.MapConfigValueKubeToAPI(workflow.Config),
DisableWebhooks: config.ExecutionDisableWebhooks(),
Tags: tags,
RunningContext: &testkube.TestWorkflowRunningContext{
Interface_: &testkube.TestWorkflowRunningContextInterface{
Type_: common.Ptr(testkube.API_TestWorkflowRunningContextInterfaceType),
},
Actor: &testkube.TestWorkflowRunningContextActor{
Name: workflow.Name,
ExecutionId: config.ExecutionId(),
ExecutionPath: strings.Join(parentIds, "/"),
Type_: common.Ptr(testkube.TESTWORKFLOW_TestWorkflowRunningContextActorType),
},
},
ParentExecutionIds: parentIds,
})
if err == nil {
break
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/99designs/gqlgen v0.17.27
github.com/Masterminds/semver v1.5.0
github.com/adhocore/gronx v1.6.3
github.com/avast/retry-go/v4 v4.6.0
github.com/bmatcuk/doublestar/v4 v4.6.1
github.com/cdevents/sdk-go v0.3.0
github.com/cli/cli/v2 v2.20.2
Expand Down Expand Up @@ -38,7 +39,7 @@ require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/kelseyhightower/envconfig v1.4.0
github.com/kubepug/kubepug v1.7.1
github.com/kubeshop/testkube-operator v1.17.55-0.20241023094459-ac7c03dedade
github.com/kubeshop/testkube-operator v1.17.55-0.20241028132112-e2a9c1706edd
github.com/minio/minio-go/v7 v7.0.47
github.com/montanaflynn/stats v0.6.6
github.com/moogar0880/problems v0.1.1
Expand All @@ -48,7 +49,6 @@ require (
github.com/olekukonko/tablewriter v0.0.6-0.20230925090304-df64c4bbad77
github.com/onsi/ginkgo/v2 v2.15.0
github.com/onsi/gomega v1.31.0
github.com/opencontainers/image-spec v1.1.0-rc3
github.com/otiai10/copy v1.11.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.18.0
Expand Down Expand Up @@ -91,7 +91,6 @@ require (
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/avast/retry-go/v4 v4.6.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
Expand Down Expand Up @@ -173,6 +172,7 @@ require (
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
github.com/package-url/packageurl-go v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
Expand Down
Loading

0 comments on commit 1460d08

Please sign in to comment.