Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support inlined global template in the runner #6137

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading