github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/executors/get.go (about)

     1  package executors
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
    11  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/render"
    12  	apiClient "github.com/kubeshop/testkube/pkg/api/v1/client"
    13  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    14  	"github.com/kubeshop/testkube/pkg/crd"
    15  	"github.com/kubeshop/testkube/pkg/ui"
    16  )
    17  
    18  func NewGetExecutorCmd() *cobra.Command {
    19  	var selectors []string
    20  	var crdOnly bool
    21  
    22  	cmd := &cobra.Command{
    23  		Use:     "executor [executorName]",
    24  		Aliases: []string{"executors", "er"},
    25  		Short:   "Gets executor details",
    26  		Long:    `Gets executor, you can change output format`,
    27  		Run: func(cmd *cobra.Command, args []string) {
    28  			client, namespace, err := common.GetClient(cmd)
    29  			ui.ExitOnError("getting client", err)
    30  
    31  			firstEntry := true
    32  			if len(args) > 0 {
    33  				name := args[0]
    34  
    35  				executor, err := client.GetExecutor(name)
    36  				ui.ExitOnError("getting executor: "+name, err)
    37  
    38  				if crdOnly {
    39  					options := mapExecutorDetailsToCreateExecutorOptions(namespace, &executor)
    40  					(*testkube.ExecutorUpsertRequest)(options).QuoteExecutorTextFields()
    41  					common.UIPrintCRD(crd.TemplateExecutor, options, &firstEntry)
    42  					return
    43  				}
    44  
    45  				err = render.Obj(cmd, executor, os.Stdout)
    46  				ui.ExitOnError("rendering executor", err)
    47  
    48  			} else {
    49  				executors, err := client.ListExecutors(strings.Join(selectors, ","))
    50  				ui.ExitOnError("listing executors: ", err)
    51  
    52  				if crdOnly {
    53  					for _, executor := range executors {
    54  						options := mapExecutorDetailsToCreateExecutorOptions(namespace, &executor)
    55  						(*testkube.ExecutorUpsertRequest)(options).QuoteExecutorTextFields()
    56  						common.UIPrintCRD(crd.TemplateExecutor, options, &firstEntry)
    57  					}
    58  
    59  					return
    60  				}
    61  
    62  				err = render.List(cmd, executors, os.Stdout)
    63  				ui.ExitOnError("rendering executors", err)
    64  			}
    65  		},
    66  	}
    67  
    68  	cmd.Flags().StringSliceVarP(&selectors, "label", "l", nil, "label key value pair: --label key1=value1")
    69  	cmd.Flags().BoolVar(&crdOnly, "crd-only", false, "show only test crd ")
    70  
    71  	return cmd
    72  }
    73  
    74  func mapExecutorDetailsToCreateExecutorOptions(namespace string, executor *testkube.ExecutorDetails) *apiClient.UpsertExecutorOptions {
    75  	options := &apiClient.UpsertExecutorOptions{
    76  		Name:      executor.Name,
    77  		Namespace: namespace,
    78  	}
    79  
    80  	if executor.Executor != nil {
    81  		options.Types = executor.Executor.Types
    82  		options.ExecutorType = executor.Executor.ExecutorType
    83  		options.Image = executor.Executor.Image
    84  		options.ImagePullSecrets = executor.Executor.ImagePullSecrets
    85  		options.Command = executor.Executor.Command
    86  		options.Args = executor.Executor.Args
    87  		options.Uri = executor.Executor.Uri
    88  		options.Labels = executor.Executor.Labels
    89  		options.JobTemplateReference = executor.Executor.JobTemplateReference
    90  		if executor.Executor.JobTemplate != "" {
    91  			options.JobTemplate = fmt.Sprintf("%q", executor.Executor.JobTemplate)
    92  		}
    93  
    94  		options.Features = executor.Executor.Features
    95  		options.ContentTypes = executor.Executor.ContentTypes
    96  		options.Meta = executor.Executor.Meta
    97  		options.UseDataDirAsWorkingDir = executor.Executor.UseDataDirAsWorkingDir
    98  	}
    99  
   100  	return options
   101  }