Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into jacek/feat/executor-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
exu committed Mar 2, 2022
2 parents a419de5 + 48083cd commit 9da5b14
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 85 deletions.
3 changes: 3 additions & 0 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,9 @@ components:
uri:
description: URI for rest based executors
type: string
jobTemplate:
description: Job template to launch executor
type: string

ExecutorDetails:
description: Executor details with Executor data and additional information like list of executions
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/crds/tests_crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type Test struct {
func GenerateCRD(namespace, path string) (string, error) {
var testType string

tpl := `apiVersion: tests.testkube.io/v1
tpl := `apiVersion: tests.testkube.io/v2
kind: Test
metadata:
name: {{ .Name }}
Expand Down
17 changes: 14 additions & 3 deletions cmd/kubectl-testkube/commands/executors/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package executors

import (
"io/ioutil"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
apiClient "github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/ui"
Expand All @@ -9,8 +11,8 @@ import (

func NewCreateExecutorCmd() *cobra.Command {
var (
types []string
name, executorType, image, uri string
types []string
name, executorType, image, uri, jobTemplate string
)

cmd := &cobra.Command{
Expand All @@ -30,13 +32,21 @@ func NewCreateExecutorCmd() *cobra.Command {
ui.Failf("Executor with name '%s' already exists in namespace %s", name, namespace)
}

jobTemplateContent := ""
if jobTemplate != "" {
b, err := ioutil.ReadFile(jobTemplate)
ui.ExitOnError("reading job template", err)
jobTemplateContent = string(b)
}

options := apiClient.CreateExecutorOptions{
Name: name,
Namespace: namespace,
Types: types,
ExecutorType: executorType,
Image: image,
Uri: uri,
JobTemplate: jobTemplateContent,
}

_, err = client.CreateExecutor(options)
Expand All @@ -47,11 +57,12 @@ func NewCreateExecutorCmd() *cobra.Command {
}

cmd.Flags().StringVarP(&name, "name", "n", "", "unique test name - mandatory")
cmd.Flags().StringArrayVarP(&types, "types", "t", []string{}, "types handled by exeutor")
cmd.Flags().StringArrayVarP(&types, "types", "t", []string{}, "types handled by executor")
cmd.Flags().StringVar(&executorType, "executor-type", "job", "executor type (defaults to job)")

cmd.Flags().StringVarP(&uri, "uri", "u", "", "if resource need to be loaded from URI")
cmd.Flags().StringVarP(&image, "image", "i", "", "if uri is git repository we can set additional branch parameter")
cmd.Flags().StringVarP(&jobTemplate, "job-template", "j", "", "if executor needs to be launched using custom job specification")

return cmd
}
6 changes: 6 additions & 0 deletions internal/app/api/v1/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func (s TestkubeAPI) CreateExecutorHandler() fiber.Handler {
}

executor := mapExecutorCreateRequestToExecutorCRD(request)
if executor.Spec.JobTemplate == "" {
executor.Spec.JobTemplate = s.jobTemplate
}

created, err := s.ExecutorsClient.Create(&executor)
if err != nil {
return s.Error(c, http.StatusBadRequest, err)
Expand Down Expand Up @@ -83,6 +87,7 @@ func mapExecutorCRDToExecutorDetails(item executorv1.Executor) testkube.Executor
Image: item.Spec.Image,
Types: item.Spec.Types,
Uri: item.Spec.URI,
JobTemplate: item.Spec.JobTemplate,
},
}
}
Expand All @@ -98,6 +103,7 @@ func mapExecutorCreateRequestToExecutorCRD(request testkube.ExecutorCreateReques
Types: request.Types,
URI: request.Uri,
Image: request.Image,
JobTemplate: request.JobTemplate,
},
}
}
14 changes: 13 additions & 1 deletion internal/app/api/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,18 @@ func NewServer(
s.Log.Warnf("load default executors %w", err)
}

s.Executor, err = client.NewJobExecutor(executionsResults, initImage)
jobTemplate := os.Getenv("TESTKUBE_JOB_TEMPLATE")
if jobTemplate != "" {
dataDecoded, err := base64.StdEncoding.DecodeString(jobTemplate)
if err != nil {
s.Log.Warnf("decode job template %w", err)
}

jobTemplate = string(dataDecoded)
}

s.jobTemplate = jobTemplate
s.Executor, err = client.NewJobExecutor(executionsResults, initImage, s.jobTemplate)
if err != nil {
panic(err)
}
Expand All @@ -85,6 +96,7 @@ type TestkubeAPI struct {
Metrics Metrics
Storage storage.Client
storageParams storageParams
jobTemplate string
}

type storageParams struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ type Executor struct {
Types []string `json:"types,omitempty"`
// URI for rest based executors
Uri string `json:"uri,omitempty"`
// Job template to launch executor
JobTemplate string `json:"jobTemplate,omitempty"`
}
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_executor_create_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ type ExecutorCreateRequest struct {
Types []string `json:"types"`
// URI for rest based executors
Uri string `json:"uri,omitempty"`
// Job template to launch executor
JobTemplate string `json:"jobTemplate,omitempty"`
}
16 changes: 12 additions & 4 deletions pkg/executor/client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"go.uber.org/zap"
)

func NewJobExecutor(repo result.Repository, initImage string) (client JobExecutor, err error) {
jobClient, err := jobs.NewJobClient(initImage)
func NewJobExecutor(repo result.Repository, initImage, jobTemplate string) (client JobExecutor, err error) {
jobClient, err := jobs.NewJobClient(initImage, jobTemplate)
if err != nil {
return client, fmt.Errorf("can't get k8s jobs client: %w", err)
}
Expand Down Expand Up @@ -98,16 +98,24 @@ func (c JobExecutor) Logs(id string) (out chan output.Output, err error) {
// Execute starts new external test execution, reads data and returns ID
// Execution is started asynchronously client can check later for results
func (c JobExecutor) Execute(execution testkube.Execution, options ExecuteOptions) (result testkube.ExecutionResult, err error) {
return c.Client.LaunchK8sJob(options.ExecutorSpec.Image, c.Repository, execution, options.HasSecrets)
return c.Client.LaunchK8sJob(c.Repository, execution, getJobOptions(options))
}

// Execute starts new external test execution, reads data and returns ID
// Execution is started synchronously client will be blocked
func (c JobExecutor) ExecuteSync(execution testkube.Execution, options ExecuteOptions) (result testkube.ExecutionResult, err error) {
return c.Client.LaunchK8sJobSync(options.ExecutorSpec.Image, c.Repository, execution, options.HasSecrets)
return c.Client.LaunchK8sJobSync(c.Repository, execution, getJobOptions(options))
}

func (c JobExecutor) Abort(id string) error {
c.Client.AbortK8sJob(id)
return nil
}

func getJobOptions(options ExecuteOptions) jobs.JobOptions {
return jobs.JobOptions{
Image: options.ExecutorSpec.Image,
HasSecrets: options.HasSecrets,
JobTemplate: options.ExecutorSpec.JobTemplate,
}
}
Loading

0 comments on commit 9da5b14

Please sign in to comment.