Skip to content

Commit

Permalink
feat: support inlined global template in the runner (#6137)
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 authored Jan 28, 2025
1 parent 84658dc commit 3a76f00
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 31 deletions.
12 changes: 6 additions & 6 deletions cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,12 @@ func main() {
proContext,
executionWorker,
runner2.Options{
ClusterID: clusterId,
DashboardURI: cfg.TestkubeDashboardURI,
DefaultNamespace: cfg.TestkubeNamespace,
ServiceAccountNames: serviceAccountNames,
StorageSkipVerify: cfg.StorageSkipVerify,
ClusterID: clusterId,
DashboardURI: cfg.TestkubeDashboardURI,
DefaultNamespace: cfg.TestkubeNamespace,
ServiceAccountNames: serviceAccountNames,
StorageSkipVerify: cfg.StorageSkipVerify,
GlobalTemplateInline: cfg.GlobalWorkflowTemplateInline,
},
)
if !cfg.DisableRunner {
Expand All @@ -268,7 +269,6 @@ func main() {
metrics,
secretManager,
cfg.GlobalWorkflowTemplateName,
cfg.GlobalWorkflowTemplateInline,
cfg.TestkubeDashboardURI,
proContext.OrgID,
proContext.EnvID,
Expand Down
1 change: 0 additions & 1 deletion cmd/api-server/services/controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ func CreateControlPlane(ctx context.Context, cfg *config.Config, features featur
metrics,
secretManager,
cfg.GlobalWorkflowTemplateName,
cfg.GlobalWorkflowTemplateInline,
cfg.TestkubeDashboardURI,
"",
"",
Expand Down
2 changes: 1 addition & 1 deletion cmd/tcl/kubectl-testkube/devbox/devutils/runneragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *RunnerAgent) Create(ctx context.Context, env *client.Environment, runne
{Name: "TESTKUBE_PRO_TLS_INSECURE", Value: fmt.Sprintf("%v", r.cloud.AgentInsecure())},
{Name: "TESTKUBE_PRO_TLS_SKIP_VERIFY", Value: "true"},

// RunnerAgent configuration
// Runner configuration
{Name: "APISERVER_FULLNAME", Value: "devbox-agent"},
{Name: "TESTKUBE_NAMESPACE", Value: r.pod.Namespace()},
{Name: "JOB_SERVICE_ACCOUNT_NAME", Value: "devbox-account"},
Expand Down
66 changes: 50 additions & 16 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ package runner

import (
"context"
"strings"
"sync"
"time"

"github.com/pkg/errors"

testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
"github.com/kubeshop/testkube/internal/app/api/metrics"
"github.com/kubeshop/testkube/internal/config"
"github.com/kubeshop/testkube/internal/crdcommon"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/controlplaneclient"
"github.com/kubeshop/testkube/pkg/event"
"github.com/kubeshop/testkube/pkg/expressions"
"github.com/kubeshop/testkube/pkg/log"
configRepo "github.com/kubeshop/testkube/pkg/repository/config"
"github.com/kubeshop/testkube/pkg/testworkflows/executionworker/executionworkertypes"
"github.com/kubeshop/testkube/pkg/testworkflows/executionworker/registry"
"github.com/kubeshop/testkube/pkg/testworkflows/testworkflowresolver"
)

const (
Expand All @@ -24,6 +29,8 @@ const (

SaveEndResultRetryCount = 100
SaveEndResultRetryBaseDelay = 500 * time.Millisecond

inlinedGlobalTemplateName = "<inline-global-template>"
)

type RunnerExecute interface {
Expand All @@ -41,14 +48,15 @@ type Runner interface {
}

type runner struct {
worker executionworkertypes.Worker
client controlplaneclient.Client
configRepository configRepo.Repository
emitter event.Interface
metrics metrics.Metrics
proContext config.ProContext // TODO: Include Agent ID in pro context
dashboardURI string
storageSkipVerify bool
worker executionworkertypes.Worker
client controlplaneclient.Client
configRepository configRepo.Repository
emitter event.Interface
metrics metrics.Metrics
proContext config.ProContext // TODO: Include Agent ID in pro context
dashboardURI string
storageSkipVerify bool
globalTemplateInline *testworkflowsv1.TestWorkflowTemplate

watching sync.Map
}
Expand All @@ -62,16 +70,29 @@ func New(
proContext config.ProContext,
dashboardURI string,
storageSkipVerify bool,
globalTemplateInlineYaml string,
) Runner {

var globalTemplateInline *testworkflowsv1.TestWorkflowTemplate
if globalTemplateInlineYaml != "" {
globalTemplateInline = new(testworkflowsv1.TestWorkflowTemplate)
err := crdcommon.DeserializeCRD(globalTemplateInline, []byte("spec:\n "+strings.ReplaceAll(globalTemplateInlineYaml, "\n", "\n ")))
globalTemplateInline.Name = inlinedGlobalTemplateName
if err != nil {
log.DefaultLogger.Errorw("failed to unmarshal inlined global template", "error", err)
globalTemplateInline = nil
}
}
return &runner{
worker: worker,
configRepository: configRepository,
client: client,
emitter: emitter,
metrics: metrics,
proContext: proContext,
dashboardURI: dashboardURI,
storageSkipVerify: storageSkipVerify,
worker: worker,
configRepository: configRepository,
client: client,
emitter: emitter,
metrics: metrics,
proContext: proContext,
dashboardURI: dashboardURI,
storageSkipVerify: storageSkipVerify,
globalTemplateInline: globalTemplateInline,
}
}

Expand Down Expand Up @@ -233,6 +254,19 @@ func (r *runner) Notifications(ctx context.Context, id string) executionworkerty
}

func (r *runner) Execute(request executionworkertypes.ExecuteRequest) (*executionworkertypes.ExecuteResult, error) {
if r.globalTemplateInline != nil {
testworkflowresolver.AddGlobalTemplateRef(&request.Workflow, testworkflowsv1.TemplateRef{
Name: testworkflowresolver.GetDisplayTemplateName(inlinedGlobalTemplateName),
})
err := testworkflowresolver.ApplyTemplates(&request.Workflow, map[string]*testworkflowsv1.TestWorkflowTemplate{
inlinedGlobalTemplateName: r.globalTemplateInline,
}, func(key, value string) (expressions.Expression, error) {
return nil, errors.New("not supported")
})
if err != nil {
return nil, err
}
}
res, err := r.worker.Execute(context.Background(), request)
if err == nil {
// TODO: consider retry?
Expand Down
3 changes: 3 additions & 0 deletions pkg/runner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Options struct {
ServiceAccountNames map[string]string

StorageSkipVerify bool

GlobalTemplateInline string
}

type service struct {
Expand Down Expand Up @@ -69,6 +71,7 @@ func NewService(
proContext,
opts.DashboardURI,
opts.StorageSkipVerify,
opts.GlobalTemplateInline,
),
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/testworkflows/testworkflowexecutor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func New(
metrics v1.Metrics,
secretManager secretmanager.SecretManager,
globalTemplateName string,
globalTemplateInlineYaml string,
dashboardURI string,
organizationId string,
defaultEnvironmentId string,
Expand Down Expand Up @@ -100,7 +99,7 @@ func New(
return nil, nil
},
globalTemplateName,
globalTemplateInlineYaml,
"",
organizationId,
defaultEnvironmentId,
agentId,
Expand Down
11 changes: 6 additions & 5 deletions pkg/testworkflows/testworkflowexecutor/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"maps"
"math"
"slices"
"strings"
"time"

"github.com/pkg/errors"
Expand All @@ -16,11 +17,10 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/metadata"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"

testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
"github.com/kubeshop/testkube/internal/common"
"github.com/kubeshop/testkube/internal/crdcommon"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/cloud"
"github.com/kubeshop/testkube/pkg/log"
Expand Down Expand Up @@ -81,12 +81,13 @@ func NewScheduler(
) Scheduler {
var globalTemplateInline *testkube.TestWorkflowTemplate
if globalTemplateInlineYaml != "" {
inline := &testworkflowsv1.TestWorkflowTemplate{ObjectMeta: metav1.ObjectMeta{Name: inlinedGlobalTemplateName}}
err := yaml.Unmarshal([]byte(globalTemplateInlineYaml), inline.Spec)
inline := new(testworkflowsv1.TestWorkflowTemplate)
err := crdcommon.DeserializeCRD(inline, []byte("spec:\n "+strings.ReplaceAll(globalTemplateInlineYaml, "\n", "\n ")))
inline.Name = inlinedGlobalTemplateName
if err == nil {
globalTemplateInline = testworkflows2.MapTemplateKubeToAPI(inline)
} else {
log.DefaultLogger.Errorw("failed to unmarshal inline global template", "error", err)
log.DefaultLogger.Errorw("failed to unmarshal inlined global template", "error", err)
}
}
return &scheduler{
Expand Down

0 comments on commit 3a76f00

Please sign in to comment.