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

     1  package templates
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
    10  	apiv1 "github.com/kubeshop/testkube/pkg/api/v1/client"
    11  	"github.com/kubeshop/testkube/pkg/crd"
    12  	"github.com/kubeshop/testkube/pkg/ui"
    13  )
    14  
    15  func NewCreateTemplateCmd() *cobra.Command {
    16  	var (
    17  		name         string
    18  		templateType string
    19  		labels       map[string]string
    20  		body         string
    21  		update       bool
    22  	)
    23  
    24  	cmd := &cobra.Command{
    25  		Use:     "template",
    26  		Aliases: []string{"tp"},
    27  		Short:   "Create a new Template.",
    28  		Long:    `Create a new Template Custom Resource.`,
    29  		Run: func(cmd *cobra.Command, args []string) {
    30  			crdOnly, err := strconv.ParseBool(cmd.Flag("crd-only").Value.String())
    31  			ui.ExitOnError("parsing flag value", err)
    32  
    33  			if name == "" {
    34  				ui.Failf("pass valid name (in '--name' flag)")
    35  			}
    36  
    37  			namespace := cmd.Flag("namespace").Value.String()
    38  			var client apiv1.Client
    39  			if !crdOnly {
    40  				client, namespace, err = common.GetClient(cmd)
    41  				ui.ExitOnError("getting client", err)
    42  
    43  				template, _ := client.GetTemplate(name)
    44  				if name == template.Name {
    45  					if cmd.Flag("update").Changed {
    46  						if !update {
    47  							ui.Failf("Template with name '%s' already exists in namespace %s, ", template.Name, namespace)
    48  						}
    49  					} else {
    50  						ok := ui.Confirm(fmt.Sprintf("Template with name '%s' already exists in namespace %s, ", template.Name, namespace) +
    51  							"do you want to overwrite it?")
    52  						if !ok {
    53  							ui.Failf("Template creation was aborted")
    54  						}
    55  					}
    56  
    57  					options, err := NewUpdateTemplateOptionsFromFlags(cmd)
    58  					ui.ExitOnError("getting template options", err)
    59  
    60  					_, err = client.UpdateTemplate(options)
    61  					ui.ExitOnError("updating template "+name+" in namespace "+namespace, err)
    62  
    63  					ui.SuccessAndExit("Template updated", name)
    64  				}
    65  			}
    66  
    67  			options, err := NewCreateTemplateOptionsFromFlags(cmd)
    68  			ui.ExitOnError("getting template options", err)
    69  
    70  			if !crdOnly {
    71  				_, err := client.CreateTemplate(options)
    72  				ui.ExitOnError("creating template "+name+" in namespace "+namespace, err)
    73  
    74  				ui.Success("Template created", name)
    75  			} else {
    76  				if options.Body != "" {
    77  					options.Body = fmt.Sprintf("%q", options.Body)
    78  				}
    79  
    80  				data, err := crd.ExecuteTemplate(crd.TemplateTemplate, options)
    81  				ui.ExitOnError("executing crd template", err)
    82  
    83  				ui.Info(data)
    84  			}
    85  		},
    86  	}
    87  
    88  	cmd.Flags().StringVarP(&name, "name", "n", "", "unique template name - mandatory")
    89  	cmd.Flags().StringVarP(&templateType, "template-type", "", "", "template type one of job|container|cronjob|scraper|pvc|webhook")
    90  	cmd.Flags().StringToStringVarP(&labels, "label", "l", nil, "label key value pair: --label key1=value1")
    91  	cmd.Flags().StringVarP(&body, "body", "", "", "a path to template file to use as template body")
    92  	cmd.Flags().BoolVar(&update, "update", false, "update, if template already exists")
    93  
    94  	return cmd
    95  }