Skip to content

Commit

Permalink
feat: pass execution tags
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>
  • Loading branch information
vsukhin committed Aug 8, 2024
1 parent 460d46a commit a605dfb
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/tcl/testworkflow-toolkit/commands/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package commands

import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -151,10 +152,21 @@ func buildWorkflowExecution(workflow testworkflowsv1.StepExecuteWorkflow, async
return func() (err error) {
c := env.Testkube()

var tags map[string]string
if tagsData := env.ExecutionTags(); tagsData != "" {
data, err := base64.StdEncoding.DecodeString(tagsData)
if err != nil {
ui.Errf("failed to decode tags: %s: %s", workflow.Name, err.Error())
} else if err = json.Unmarshal(data, &tags); err != nil {
ui.Errf("failed to unmarshal tags: %s: %s", workflow.Name, err.Error())
}
}

exec, err := c.ExecuteTestWorkflow(workflow.Name, testkube.TestWorkflowExecutionRequest{
Name: workflow.ExecutionName,
Config: testworkflows.MapConfigValueKubeToAPI(workflow.Config),
DisableWebhooks: env.ExecutionDisableWebhooks(),
Tags: tags,
})
execName := exec.Name
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/tcl/testworkflow-toolkit/spawn/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func CreateExecutionMachine(prefix string, index int64) (string, expressions.Mac
"number": env.ExecutionNumber(),
"scheduledAt": env.ExecutionScheduledAt().UTC().Format(constants.RFC3339Millis),
"disableWebhooks": env.ExecutionDisableWebhooks(),
"tags": env.ExecutionTags(),
})
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/testworkflow-toolkit/env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type envExecutionConfig struct {
RootResourceId string `envconfig:"TK_EXR"`
FSPrefix string `envconfig:"TK_FS"`
DisableWebhooks bool `envconfig:"TK_DWH"`
Tags string `envconfig:"TK_TAG"`
}

type envSystemConfig struct {
Expand Down Expand Up @@ -157,3 +158,7 @@ func ExecutionDisableWebhooks() bool {
func JUnitParserEnabled() bool {
return Config().Features.EnableJUnitParser
}

func ExecutionTags() string {
return Config().Execution.Tags
}
27 changes: 27 additions & 0 deletions pkg/testworkflows/testworkflowexecutor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package testworkflowexecutor
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"maps"
"os"
"strconv"
"sync"
Expand Down Expand Up @@ -466,6 +469,7 @@ func (e *executor) Execute(ctx context.Context, workflow testworkflowsv1.TestWor
"number": "1",
"scheduledAt": now.UTC().Format(constants.RFC3339Millis),
"disableWebhooks": request.DisableWebhooks,
"tags": "",
})

// Preserve resolved TestWorkflow
Expand Down Expand Up @@ -504,13 +508,35 @@ func (e *executor) Execute(ctx context.Context, workflow testworkflowsv1.TestWor
return execution, errors.Wrap(err, "execution name already exists")
}

var tags map[string]string
if workflow.Spec.Execution != nil {
tags = workflow.Spec.Execution.Tags
if request.Tags != nil {
if tags == nil {
tags = make(map[string]string)
}

maps.Copy(tags, request.Tags)
}
}

var tagsData string
if tags != nil {
if data, err := json.Marshal(tags); err != nil {
log.DefaultLogger.Errorw("failed to marshal tags", "id", id, "error", err)
} else {
tagsData = base64.StdEncoding.EncodeToString(data)
}
}

// Build machine with actual execution data
executionMachine := expressions.NewMachine().Register("execution", map[string]interface{}{
"id": id,
"name": executionName,
"number": number,
"scheduledAt": now.UTC().Format(constants.RFC3339Millis),
"disableWebhooks": request.DisableWebhooks,
"tags": tagsData,
})

// Process the TestWorkflow
Expand Down Expand Up @@ -542,6 +568,7 @@ func (e *executor) Execute(ctx context.Context, workflow testworkflowsv1.TestWor
ResolvedWorkflow: testworkflowmappers.MapKubeToAPI(resolvedWorkflow),
TestWorkflowExecutionName: testWorkflowExecutionName,
DisableWebhooks: request.DisableWebhooks,
Tags: tags,
}
err = e.repository.Insert(ctx, execution)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/testworkflows/testworkflowprocessor/stage/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ func (c *container) EnableToolkit(ref string) Container {
"TK_EXC": "{{execution.number}}",
"TK_EXS": "{{execution.scheduledAt}}",
"TK_DWH": "{{execution.disableWebhooks}}",
"TK_TAG": "{{execution.tags}}",
"TK_EXI": "{{resource.id}}",
"TK_EXR": "{{resource.root}}",
"TK_FS": "{{resource.fsPrefix}}",
Expand Down
1 change: 1 addition & 0 deletions pkg/testworkflows/testworkflowresolver/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func injectTemplateToSpec(spec *testworkflowsv1.TestWorkflowSpec, template testw
spec.Pod = MergePodConfig(template.Spec.Pod, spec.Pod)
spec.Job = MergeJobConfig(template.Spec.Job, spec.Job)
spec.Events = append(template.Spec.Events, spec.Events...)
spec.Execution = MergeExecution(template.Spec.Execution, spec.Execution)

// Apply basic configuration
spec.Content = MergeContent(template.Spec.Content, spec.Content)
Expand Down
18 changes: 18 additions & 0 deletions pkg/testworkflows/testworkflowresolver/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,21 @@ func ConvertIndependentStepToStep(step testworkflowsv1.IndependentStep) (res tes
res.Steps = common.MapSlice(step.Steps, ConvertIndependentStepToStep)
return res
}

func MergeExecution(dst, include *testworkflowsv1.TestWorkflowTagSchema) *testworkflowsv1.TestWorkflowTagSchema {
if dst == nil {
return include
} else if include == nil {
return dst
}

if include.Tags != nil {
if dst.Tags == nil {
dst.Tags = make(map[string]string)
}

maps.Copy(dst.Tags, include.Tags)
}

return dst
}

0 comments on commit a605dfb

Please sign in to comment.