Skip to content

Commit

Permalink
fix: allow to store features in cli and return executor fully
Browse files Browse the repository at this point in the history
  • Loading branch information
vsukhin committed Oct 20, 2022
1 parent a90fc1c commit 4500cff
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 51 deletions.
6 changes: 6 additions & 0 deletions cmd/kubectl-testkube/commands/executors/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func NewUpsertExecutorOptionsFromFlags(cmd *cobra.Command, testLabels map[string
return options, err
}

features, err := cmd.Flags().GetStringArray("feature")
if err != nil {
return options, err
}

options = apiClient.UpsertExecutorOptions{
Name: name,
Types: types,
Expand All @@ -63,6 +68,7 @@ func NewUpsertExecutorOptionsFromFlags(cmd *cobra.Command, testLabels map[string
Args: executorArgs,
Uri: uri,
JobTemplate: jobTemplateContent,
Features: features,
}

// if labels are passed and are different from the existing overwrite
Expand Down
11 changes: 6 additions & 5 deletions cmd/kubectl-testkube/commands/executors/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

func NewCreateExecutorCmd() *cobra.Command {
var (
types, command, executorArgs, imagePullSecretNames []string
name, executorType, image, uri, jobTemplate string
labels map[string]string
types, command, executorArgs, imagePullSecretNames, features []string
name, executorType, image, uri, jobTemplate string
labels map[string]string
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -67,12 +67,13 @@ func NewCreateExecutorCmd() *cobra.Command {
cmd.Flags().StringVar(&executorType, "executor-type", "job", "executor type, container or job (defaults to job)")

cmd.Flags().StringVarP(&uri, "uri", "u", "", "if resource need to be loaded from URI")
cmd.Flags().StringVar(&image, "image", "", "image used for container executor")
cmd.Flags().StringArrayVar(&imagePullSecretNames, "image-pull-secrets", []string{}, "secret name used to pull the image in container executor")
cmd.Flags().StringVar(&image, "image", "", "image used for executor")
cmd.Flags().StringArrayVar(&imagePullSecretNames, "image-pull-secrets", []string{}, "secret name used to pull the image in executor")
cmd.Flags().StringArrayVar(&command, "command", []string{}, "command passed to image in container executor")
cmd.Flags().StringArrayVar(&executorArgs, "args", []string{}, "args passed to image in container executor")
cmd.Flags().StringVarP(&jobTemplate, "job-template", "j", "", "if executor needs to be launched using custom job specification, then a path to template file should be provided")
cmd.Flags().StringToStringVarP(&labels, "label", "l", nil, "label key value pair: --label key1=value1")
cmd.Flags().StringArrayVar(&features, "feature", []string{}, "feature provided by executor")

return cmd
}
5 changes: 5 additions & 0 deletions cmd/kubectl-testkube/commands/executors/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@ func mapExecutorDetailsToCreateExecutorOptions(namespace string, executor *testk
options.Types = executor.Executor.Types
options.ExecutorType = executor.Executor.ExecutorType
options.Image = executor.Executor.Image
options.ImagePullSecrets = executor.Executor.ImagePullSecrets
options.Command = executor.Executor.Command
options.Args = executor.Executor.Args
options.Uri = executor.Executor.Uri
options.Labels = executor.Executor.Labels
if executor.Executor.JobTemplate != "" {
options.JobTemplate = fmt.Sprintf("%q", executor.Executor.JobTemplate)
}

options.Features = executor.Executor.Features
}

return options
Expand Down
11 changes: 6 additions & 5 deletions cmd/kubectl-testkube/commands/executors/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

func UpdateExecutorCmd() *cobra.Command {
var (
types, command, executorArgs, imagePullSecretNames []string
name, executorType, image, uri, jobTemplate string
labels map[string]string
types, command, executorArgs, imagePullSecretNames, features []string
name, executorType, image, uri, jobTemplate string
labels map[string]string
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -49,12 +49,13 @@ func UpdateExecutorCmd() *cobra.Command {
cmd.Flags().StringVar(&executorType, "executor-type", "job", "executor type, container or job (defaults to job)")

cmd.Flags().StringVarP(&uri, "uri", "u", "", "if resource need to be loaded from URI")
cmd.Flags().StringVar(&image, "image", "", "image used for container executor")
cmd.Flags().StringArrayVar(&imagePullSecretNames, "image-pull-secrets", []string{}, "secret name used to pull the image in container executor")
cmd.Flags().StringVar(&image, "image", "", "image used for executor")
cmd.Flags().StringArrayVar(&imagePullSecretNames, "image-pull-secrets", []string{}, "secret name used to pull the image in executor")
cmd.Flags().StringArrayVar(&command, "command", []string{}, "command passed to image in container executor")
cmd.Flags().StringArrayVar(&executorArgs, "args", []string{}, "args passed to image in container executor")
cmd.Flags().StringVarP(&jobTemplate, "job-template", "j", "", "if executor needs to be launched using custom job specification, then a path to template file should be provided")
cmd.Flags().StringToStringVarP(&labels, "label", "l", nil, "label key value pair: --label key1=value1")
cmd.Flags().StringArrayVar(&features, "feature", []string{}, "feature provided by executor")

return cmd
}
5 changes: 3 additions & 2 deletions docs/docs/5-cli-reference/kubectl-testkube_create_executor.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ kubectl-testkube create executor [flags]
--args stringArray args passed to image in container executor
--command stringArray command passed to image in container executor
--executor-type string executor type, container or job (defaults to job) (default "job")
--feature stringArray feature provided by executor
-h, --help help for executor
--image string image used for container executor
--image-pull-secrets stringArray secret name used to pull the image in container executor
--image string image used for executor
--image-pull-secrets stringArray secret name used to pull the image in executor
-j, --job-template string if executor needs to be launched using custom job specification, then a path to template file should be provided
-l, --label stringToString label key value pair: --label key1=value1 (default [])
-n, --name string unique executor name - mandatory
Expand Down
5 changes: 3 additions & 2 deletions docs/docs/5-cli-reference/kubectl-testkube_update_executor.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ kubectl-testkube update executor [flags]
--args stringArray args passed to image in container executor
--command stringArray command passed to image in container executor
--executor-type string executor type, container or job (defaults to job) (default "job")
--feature stringArray feature provided by executor
-h, --help help for executor
--image string image used for container executor
--image-pull-secrets stringArray secret name used to pull the image in container executor
--image string image used for executor
--image-pull-secrets stringArray secret name used to pull the image in executor
-j, --job-template string if executor needs to be launched using custom job specification, then a path to template file should be provided
-l, --label stringToString label key value pair: --label key1=value1 (default [])
-n, --name string unique executor name - mandatory
Expand Down
29 changes: 2 additions & 27 deletions internal/app/api/v1/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"

"github.com/gofiber/fiber/v2"
executorv1 "github.com/kubeshop/testkube-operator/apis/executor/v1"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/crd"
executorsmapper "github.com/kubeshop/testkube/pkg/mapper/executors"
Expand Down Expand Up @@ -90,7 +89,7 @@ func (s TestkubeAPI) ListExecutorsHandler() fiber.Handler {

results := []testkube.ExecutorDetails{}
for _, item := range list.Items {
results = append(results, mapExecutorCRDToExecutorDetails(item))
results = append(results, executorsmapper.MapExecutorCRDToExecutorDetails(item))

}
return c.JSON(results)
Expand All @@ -114,7 +113,7 @@ func (s TestkubeAPI) GetExecutorHandler() fiber.Handler {
return s.getCRDs(c, data, err)
}

result := mapExecutorCRDToExecutorDetails(*item)
result := executorsmapper.MapExecutorCRDToExecutorDetails(*item)
return c.JSON(result)
}
}
Expand Down Expand Up @@ -144,27 +143,3 @@ func (s TestkubeAPI) DeleteExecutorsHandler() fiber.Handler {
return nil
}
}

func mapExecutorCRDToExecutorDetails(item executorv1.Executor) testkube.ExecutorDetails {
return testkube.ExecutorDetails{
Name: item.Name,
Executor: &testkube.Executor{
ExecutorType: item.Spec.ExecutorType,
Image: item.Spec.Image,
Types: item.Spec.Types,
Uri: item.Spec.URI,
JobTemplate: item.Spec.JobTemplate,
Labels: item.Labels,
Features: mapFeatures(item.Spec.Features),
},
}
}

func mapFeatures(features []executorv1.Feature) (out []string) {

for _, feature := range features {
out = append(out, string(feature))
}

return
}
66 changes: 56 additions & 10 deletions pkg/mapper/executors/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import (
// MapCRDToAPI maps Executor CRD to OpenAPI spec Webhook
func MapCRDToAPI(item executorv1.Executor) testkube.ExecutorUpsertRequest {
return testkube.ExecutorUpsertRequest{
Name: item.Name,
Namespace: item.Namespace,
Labels: item.Labels,
ExecutorType: item.Spec.ExecutorType,
Types: item.Spec.Types,
Uri: item.Spec.URI,
Image: item.Spec.Image,
JobTemplate: item.Spec.JobTemplate,
Name: item.Name,
Namespace: item.Namespace,
Labels: item.Labels,
ExecutorType: item.Spec.ExecutorType,
Types: item.Spec.Types,
Uri: item.Spec.URI,
Image: item.Spec.Image,
ImagePullSecrets: mapImagePullSecretsToAPI(item.Spec.ImagePullSecrets),
Command: item.Spec.Command,
Args: item.Spec.Args,
JobTemplate: item.Spec.JobTemplate,
Features: mapFeaturesToAPI(item.Spec.Features),
}
}

Expand All @@ -34,18 +38,60 @@ func MapAPIToCRD(request testkube.ExecutorUpsertRequest) executorv1.Executor {
Types: request.Types,
URI: request.Uri,
Image: request.Image,
ImagePullSecrets: mapImagePullSecrets(request.ImagePullSecrets),
ImagePullSecrets: mapImagePullSecretsToCRD(request.ImagePullSecrets),
Command: request.Command,
Args: request.Args,
JobTemplate: request.JobTemplate,
Features: mapFeaturesToCRD(request.Features),
},
}
}

func mapImagePullSecrets(secrets []testkube.LocalObjectReference) []v1.LocalObjectReference {
// MapExecutorCRDToExecutorDetails maps CRD Executor to OpemAPI spec ExecutorDetails
func MapExecutorCRDToExecutorDetails(item executorv1.Executor) testkube.ExecutorDetails {
return testkube.ExecutorDetails{
Name: item.Name,
Executor: &testkube.Executor{
ExecutorType: item.Spec.ExecutorType,
Image: item.Spec.Image,
ImagePullSecrets: mapImagePullSecretsToAPI(item.Spec.ImagePullSecrets),
Command: item.Spec.Command,
Args: item.Spec.Args,
Types: item.Spec.Types,
Uri: item.Spec.URI,
JobTemplate: item.Spec.JobTemplate,
Labels: item.Labels,
Features: mapFeaturesToAPI(item.Spec.Features),
},
}
}

func mapImagePullSecretsToCRD(secrets []testkube.LocalObjectReference) []v1.LocalObjectReference {
var res []v1.LocalObjectReference
for _, secret := range secrets {
res = append(res, v1.LocalObjectReference{Name: secret.Name})
}
return res
}

func mapImagePullSecretsToAPI(secrets []v1.LocalObjectReference) []testkube.LocalObjectReference {
var res []testkube.LocalObjectReference
for _, secret := range secrets {
res = append(res, testkube.LocalObjectReference{Name: secret.Name})
}
return res
}

func mapFeaturesToCRD(features []string) (out []executorv1.Feature) {
for _, feature := range features {
out = append(out, executorv1.Feature(feature))
}
return out
}

func mapFeaturesToAPI(features []executorv1.Feature) (out []string) {
for _, feature := range features {
out = append(out, string(feature))
}
return out
}

0 comments on commit 4500cff

Please sign in to comment.