Skip to content

Commit

Permalink
feat: [TKC-2309] labels vars (#5845)
Browse files Browse the repository at this point in the history
* feat: labels vars

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

* fix: add util method

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

---------

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>
  • Loading branch information
vsukhin committed Sep 12, 2024
1 parent ede914a commit 6066f7e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
12 changes: 10 additions & 2 deletions cmd/tcl/testworkflow-toolkit/spawn/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ func CreateExecutionMachine(prefix string, index int64) (string, expressions.Mac
}
return id, expressions.NewMachine().
Register("workflow", map[string]string{
"name": env.WorkflowName(),
"name": env.WorkflowName(),
"labels": env.Config().Execution.Labels,
}).
Register("resource", map[string]string{
"root": env.ExecutionId(),
Expand Down Expand Up @@ -293,6 +294,12 @@ func CreateBaseMachine() expressions.Machine {
dashboardUrl = fmt.Sprintf("%s/organization/%s/environment/%s/dashboard",
env.Config().Cloud.UiUrl, env.Config().Cloud.OrgId, env.Config().Cloud.EnvId)
}

var labelMap map[string]string
if labels := env.Config().Execution.Labels; labels != "" {
json.Unmarshal([]byte(labels), &labelMap)
}

return expressions.CombinedMachines(
data.GetBaseTestWorkflowMachine(),
expressions.NewMachine().RegisterStringMap("internal", map[string]string{
Expand Down Expand Up @@ -341,7 +348,8 @@ func CreateBaseMachine() expressions.Machine {
}).
Register("environment", map[string]string{
"id": env.Config().Cloud.EnvId,
}),
}).
RegisterStringMap("labels", labelMap),
)
}

Expand Down
1 change: 1 addition & 0 deletions cmd/testworkflow-toolkit/env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type envExecutionConfig struct {
FSPrefix string `envconfig:"TK_FS"`
DisableWebhooks bool `envconfig:"TK_DWH"`
Tags string `envconfig:"TK_TAG"`
Labels string `envconfig:"TK_LBL"`
}

type envSystemConfig struct {
Expand Down
5 changes: 5 additions & 0 deletions pkg/expressions/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package expressions

import (
"fmt"
"strings"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -80,3 +81,7 @@ func EvalExpression(str string, machines ...Machine) (StaticValue, error) {
func Escape(str string) string {
return NewStringValue(str).Template()
}

func EscapeLabelKeyForVarName(key string) string {
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(key, ".", "_"), "-", "_"), "/", "_")
}
18 changes: 16 additions & 2 deletions pkg/testworkflows/testworkflowexecutor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testworkflowexecutor
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -408,6 +409,16 @@ func (e *executor) Execute(ctx context.Context, workflow testworkflowsv1.TestWor
// Build the basic Execution data
id := primitive.NewObjectID().Hex()
now := time.Now()
labels := make(map[string]string)
for key, value := range workflow.Labels {
labels[expressions.EscapeLabelKeyForVarName(key)] = value
}

labelMap, err := json.Marshal(labels)
if err != nil {
return execution, errors.Wrap(err, "marsalling labels error")
}

machine := expressions.NewMachine().
RegisterStringMap("internal", map[string]string{
"storage.url": os.Getenv("STORAGE_ENDPOINT"),
Expand Down Expand Up @@ -448,7 +459,8 @@ func (e *executor) Execute(ctx context.Context, workflow testworkflowsv1.TestWor
"images.cache.ttl": common.GetOr(os.Getenv("TESTKUBE_IMAGE_CREDENTIALS_CACHE_TTL"), "30m"),
}).
Register("workflow", map[string]string{
"name": workflow.Name,
"name": workflow.Name,
"labels": string(labelMap),
}).
Register("resource", map[string]string{
"id": id,
Expand All @@ -463,7 +475,9 @@ func (e *executor) Execute(ctx context.Context, workflow testworkflowsv1.TestWor
}).
Register("environment", map[string]string{
"id": cloudEnvId,
})
}).
RegisterStringMap("labels", labels)

mockExecutionMachine := expressions.NewMachine().Register("execution", map[string]interface{}{
"id": id,
"name": "<mock_name>",
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 @@ -494,6 +494,7 @@ func (c *container) EnableToolkit(ref string) Container {
"TK_IMG_P": "{{internal.images.persistence.enabled}}",
"TK_IMG_PK": "{{internal.images.persistence.key}}",
"TK_IMG_CRED_TTL": "{{internal.images.cache.ttl}}",
"TK_LBL": "{{workflow.labels}}",
})
}

Expand Down

0 comments on commit 6066f7e

Please sign in to comment.