From ee236783f3d8ab8f1baacea1c766f34f66f69342 Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Wed, 11 Sep 2024 21:43:18 +0300 Subject: [PATCH 1/2] feat: labels vars Signed-off-by: Vladislav Sukhin --- cmd/tcl/testworkflow-toolkit/spawn/utils.go | 12 ++++++++++-- cmd/testworkflow-toolkit/env/config.go | 1 + .../testworkflowexecutor/executor.go | 19 +++++++++++++++++-- .../testworkflowprocessor/stage/container.go | 1 + 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cmd/tcl/testworkflow-toolkit/spawn/utils.go b/cmd/tcl/testworkflow-toolkit/spawn/utils.go index 1e785019e16..8231d16fbb4 100644 --- a/cmd/tcl/testworkflow-toolkit/spawn/utils.go +++ b/cmd/tcl/testworkflow-toolkit/spawn/utils.go @@ -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(), @@ -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{ @@ -341,7 +348,8 @@ func CreateBaseMachine() expressions.Machine { }). Register("environment", map[string]string{ "id": env.Config().Cloud.EnvId, - }), + }). + RegisterStringMap("labels", labelMap), ) } diff --git a/cmd/testworkflow-toolkit/env/config.go b/cmd/testworkflow-toolkit/env/config.go index 519a8d80d18..3b952c2ca03 100644 --- a/cmd/testworkflow-toolkit/env/config.go +++ b/cmd/testworkflow-toolkit/env/config.go @@ -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 { diff --git a/pkg/testworkflows/testworkflowexecutor/executor.go b/pkg/testworkflows/testworkflowexecutor/executor.go index 91b48ade3f3..9845f97b686 100644 --- a/pkg/testworkflows/testworkflowexecutor/executor.go +++ b/pkg/testworkflows/testworkflowexecutor/executor.go @@ -3,10 +3,12 @@ package testworkflowexecutor import ( "bufio" "context" + "encoding/json" "fmt" "io" "os" "strconv" + "strings" "sync" "time" @@ -408,6 +410,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[strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(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"), @@ -448,7 +460,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, @@ -463,7 +476,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": "", diff --git a/pkg/testworkflows/testworkflowprocessor/stage/container.go b/pkg/testworkflows/testworkflowprocessor/stage/container.go index ff90c542584..9d97ff977d2 100644 --- a/pkg/testworkflows/testworkflowprocessor/stage/container.go +++ b/pkg/testworkflows/testworkflowprocessor/stage/container.go @@ -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}}", }) } From 5aa7f2e665cadc00194bfcb15f02f4af1508200e Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Thu, 12 Sep 2024 11:32:38 +0300 Subject: [PATCH 2/2] fix: add util method Signed-off-by: Vladislav Sukhin --- pkg/expressions/utils.go | 5 +++++ pkg/testworkflows/testworkflowexecutor/executor.go | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/expressions/utils.go b/pkg/expressions/utils.go index 6bfb416c8af..f7c99af4707 100644 --- a/pkg/expressions/utils.go +++ b/pkg/expressions/utils.go @@ -2,6 +2,7 @@ package expressions import ( "fmt" + "strings" "github.com/pkg/errors" ) @@ -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, ".", "_"), "-", "_"), "/", "_") +} diff --git a/pkg/testworkflows/testworkflowexecutor/executor.go b/pkg/testworkflows/testworkflowexecutor/executor.go index 9845f97b686..dcc73c67e88 100644 --- a/pkg/testworkflows/testworkflowexecutor/executor.go +++ b/pkg/testworkflows/testworkflowexecutor/executor.go @@ -8,7 +8,6 @@ import ( "io" "os" "strconv" - "strings" "sync" "time" @@ -412,7 +411,7 @@ func (e *executor) Execute(ctx context.Context, workflow testworkflowsv1.TestWor now := time.Now() labels := make(map[string]string) for key, value := range workflow.Labels { - labels[strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(key, ".", "_"), "-", "_"), "/", "_")] = value + labels[expressions.EscapeLabelKeyForVarName(key)] = value } labelMap, err := json.Marshal(labels)