Skip to content

Commit

Permalink
fix: detect the Toolkit properly
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 committed Jul 25, 2024
1 parent bc0eb69 commit 421317f
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 15 deletions.
6 changes: 6 additions & 0 deletions cmd/testworkflow-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ func main() {
continue
}

// Configure the environment
orchestration.Setup.UseCurrentEnv()
if !action.Execute.Toolkit {
_ = os.Unsetenv("TK_REF")
}

// List all the parents
leaf := []*data.StepData{step}
for i := range step.Parents {
Expand Down
5 changes: 0 additions & 5 deletions cmd/testworkflow-init/orchestration/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ func (c *setup) UseEnv(group string) {
_ = os.Setenv("PATH", fmt.Sprintf("%s:%s", os.Getenv("PATH"), data.InternalBinPath))
}

// TODO: Delete unnecessary variables for non-toolkit operations
//if !toolkit {
// _ = os.Unsetenv("TK_REF")
//}

// Compute dynamic environment variables
addonMachine := expressions.CombinedMachines(data.RefSuccessMachine, data.AliasMachine, data.StateMachine, libs.NewFsMachine(os.DirFS("/"), cwd))
localEnvMachine := expressions.NewMachine().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ActionDeclare struct {
type ActionExecute struct {
Ref string `json:"r"`
Negative bool `json:"n,omitempty"`
Toolkit bool `json:"t,omitempty"`
}

type ActionPause struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
stage2 "github.com/kubeshop/testkube/pkg/testworkflows/testworkflowprocessor/stage"
)

// TODO: Disallow bypassing
func CreateContainer(groupId int, defaultContainer stage2.Container, actions []actiontypes.Action) (cr corev1.Container, actionsCleanup []actiontypes.Action, err error) {
actions = slices.Clone(actions)
actionsCleanup = actions
Expand Down
1 change: 1 addition & 0 deletions pkg/testworkflows/testworkflowprocessor/action/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func process(currentStatus string, parents []string, stage stage2.Stage, machine
Execute: &lite.ActionExecute{
Ref: exec.Ref(),
Negative: exec.Negative(),
Toolkit: exec.IsToolkit(),
},
})
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/testworkflows/testworkflowprocessor/stage/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
)

type container struct {
parent *container
Cr testworkflowsv1.ContainerConfig `expr:"include"`
parent *container
toolkit bool
Cr testworkflowsv1.ContainerConfig `expr:"include"`
}

type ContainerComposition interface {
Expand Down Expand Up @@ -47,6 +48,7 @@ type ContainerAccessors interface {

HasVolumeAt(path string) bool
ToContainerConfig() testworkflowsv1.ContainerConfig
IsToolkit() bool
}

type ContainerMutations[T any] interface {
Expand Down Expand Up @@ -410,6 +412,10 @@ func (c *container) ApplyImageData(image *imageinspector.Info, resolvedImageName
return nil
}

func (c *container) IsToolkit() bool {
return c.toolkit || slices.Contains(c.Env(), BypassToolkitCheck)
}

func (c *container) EnableToolkit(ref string) Container {
return c.
AppendEnv(corev1.EnvVar{
Expand Down
12 changes: 12 additions & 0 deletions pkg/testworkflows/testworkflowprocessor/stage/containerstage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ package stage

import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/rand"

"github.com/kubeshop/testkube/pkg/expressions"
"github.com/kubeshop/testkube/pkg/imageinspector"
)

var BypassToolkitCheck = corev1.EnvVar{
Name: "TK_TC_SECURITY",
Value: rand.String(20),
}

type containerStage struct {
stageMetadata
stageLifecycle
Expand All @@ -16,6 +23,7 @@ type containerStage struct {
type ContainerStage interface {
Stage
Container() Container
IsToolkit() bool
}

func NewContainerStage(ref string, container Container) ContainerStage {
Expand Down Expand Up @@ -72,3 +80,7 @@ func (s *containerStage) Container() Container {
func (s *containerStage) HasPause() bool {
return s.paused
}

func (s *containerStage) IsToolkit() bool {
return s.container.IsToolkit()
}
7 changes: 0 additions & 7 deletions pkg/testworkflows/testworkflowprocessor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ package testworkflowprocessor

import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"

"github.com/kubeshop/testkube/pkg/testworkflows/testworkflowprocessor/constants"
)

var BypassToolkitCheck = corev1.EnvVar{
Name: "TK_TC_SECURITY",
Value: rand.String(20),
}

func AnnotateControlledBy(obj metav1.Object, rootId, id string) {
labels := obj.GetLabels()
if labels == nil {
Expand Down

0 comments on commit 421317f

Please sign in to comment.