diff --git a/pkg/tcl/testworkflowstcl/testworkflowcontroller/notifier.go b/pkg/tcl/testworkflowstcl/testworkflowcontroller/notifier.go index 2b370f3fa07..8353aeb01c0 100644 --- a/pkg/tcl/testworkflowstcl/testworkflowcontroller/notifier.go +++ b/pkg/tcl/testworkflowstcl/testworkflowcontroller/notifier.go @@ -178,7 +178,10 @@ func (n *notifier) Start(ref string, ts time.Time) { } func (n *notifier) Output(ref string, ts time.Time, output *data.Instruction) { - if _, ok := n.result.Steps[ref]; !ok { + if ref == InitContainerName { + ref = "" + } + if _, ok := n.result.Steps[ref]; !ok && ref != "" { return } n.RegisterTimestamp(ref, ts) diff --git a/pkg/tcl/testworkflowstcl/testworkflowprocessor/constants/constants.go b/pkg/tcl/testworkflowstcl/testworkflowprocessor/constants/constants.go index 3d7eab4306e..7fe86110648 100644 --- a/pkg/tcl/testworkflowstcl/testworkflowprocessor/constants/constants.go +++ b/pkg/tcl/testworkflowstcl/testworkflowprocessor/constants/constants.go @@ -17,6 +17,7 @@ import ( corev1 "k8s.io/api/core/v1" testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1" + "github.com/kubeshop/testkube/cmd/tcl/testworkflow-init/data" "github.com/kubeshop/testkube/pkg/version" ) @@ -45,6 +46,15 @@ var ( "", DefaultInitPath, "", DefaultStatePath, "", DefaultTerminationLogPath, + "", strings.ReplaceAll(data.SprintOutput("tktw-init", "pod", map[string]string{ + "name": "$TK_DEBUG_POD", + "nodeName": "$TK_DEBUG_NODE", + "namespace": "$TK_DEBUG_NS", + "serviceAccountName": "$TK_DEBUG_SVC", + "agent": version.Version, + "toolkit": stripCommonImagePrefix(getToolkitImage(), "testkube-tw-toolkit"), + "init": stripCommonImagePrefix(getInitImage(), "testkube-tw-init"), + }), "\"", "\\\""), ).Replace(` set -e trap '[ $? -eq 0 ] && exit 0 || echo -n "failed,1" > && exit 1' EXIT @@ -54,6 +64,7 @@ echo "Configuring init process..." cp /init echo "Configuring shell..." cp -rf /bin /.tktw/bin +echo -n "" echo -n ',0' > && echo 'Done.' && exit 0 `)) DefaultShellHeader = "set -e\n" @@ -67,6 +78,21 @@ echo -n ',0' > && echo 'Done.' && exit 0 DefaultToolkitImage = getToolkitImage() ) +func stripCommonImagePrefix(image, common string) string { + if !strings.HasPrefix(image, "docker.io/") { + return image + } + image = image[10:] + if !strings.HasPrefix(image, "kubeshop/") { + return image + } + image = image[9:] + if !strings.HasPrefix(image, common+":") { + return image + } + return image[len(common)+1:] +} + func getInitImage() string { img := os.Getenv("TESTKUBE_TW_INIT_IMAGE") if img == "" { diff --git a/pkg/tcl/testworkflowstcl/testworkflowprocessor/processor.go b/pkg/tcl/testworkflowstcl/testworkflowprocessor/processor.go index 34c130349e8..6bbd48d81a7 100644 --- a/pkg/tcl/testworkflowstcl/testworkflowprocessor/processor.go +++ b/pkg/tcl/testworkflowstcl/testworkflowprocessor/processor.go @@ -336,6 +336,20 @@ func (p *processor) Bundle(ctx context.Context, workflow *testworkflowsv1.TestWo Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, VolumeMounts: layer.ContainerDefaults().VolumeMounts(), + Env: []corev1.EnvVar{ + {Name: "TK_DEBUG_NODE", ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{FieldPath: "spec.nodeName"}, + }}, + {Name: "TK_DEBUG_POD", ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.name"}, + }}, + {Name: "TK_DEBUG_NS", ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.namespace"}, + }}, + {Name: "TK_DEBUG_SVC", ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{FieldPath: "spec.serviceAccountName"}, + }}, + }, SecurityContext: &corev1.SecurityContext{ RunAsGroup: fsGroup, }, diff --git a/pkg/tcl/testworkflowstcl/testworkflowprocessor/processor_test.go b/pkg/tcl/testworkflowstcl/testworkflowprocessor/processor_test.go index 273866797aa..7dad7846726 100644 --- a/pkg/tcl/testworkflowstcl/testworkflowprocessor/processor_test.go +++ b/pkg/tcl/testworkflowstcl/testworkflowprocessor/processor_test.go @@ -38,6 +38,20 @@ var ( execMachine = expressionstcl.NewMachine(). Register("resource.root", "dummy-id"). Register("resource.id", "dummy-id-abc") + initEnvs = []corev1.EnvVar{ + {Name: "TK_DEBUG_NODE", ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{FieldPath: "spec.nodeName"}, + }}, + {Name: "TK_DEBUG_POD", ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.name"}, + }}, + {Name: "TK_DEBUG_NS", ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.namespace"}, + }}, + {Name: "TK_DEBUG_SVC", ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{FieldPath: "spec.serviceAccountName"}, + }}, + } ) func TestProcessEmpty(t *testing.T) { @@ -100,6 +114,7 @@ func TestProcessBasic(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup), @@ -185,6 +200,7 @@ func TestProcessBasicEnvReference(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup), @@ -258,6 +274,7 @@ func TestProcessMultipleSteps(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup), @@ -351,6 +368,7 @@ func TestProcessNestedSteps(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup), @@ -491,6 +509,7 @@ func TestProcessOptionalSteps(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup), @@ -629,6 +648,7 @@ func TestProcessNegativeSteps(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup), @@ -763,6 +783,7 @@ func TestProcessNegativeContainerStep(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup), @@ -851,6 +872,7 @@ func TestProcessOptionalContainerStep(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup), @@ -948,6 +970,7 @@ func TestProcessLocalContent(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup), @@ -1051,6 +1074,7 @@ func TestProcessGlobalContent(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup), @@ -1166,6 +1190,7 @@ func TestProcessRunShell(t *testing.T) { ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{"/bin/sh", "-c"}, Args: []string{constants.InitScript}, + Env: initEnvs, VolumeMounts: volumeMounts, SecurityContext: &corev1.SecurityContext{ RunAsGroup: common.Ptr(constants.DefaultFsGroup),