From 9504f7a7dbb11a0a887d88c203b8b78f95e875b1 Mon Sep 17 00:00:00 2001 From: Jacek Wysocki Date: Mon, 13 Dec 2021 10:40:15 +0100 Subject: [PATCH] added createcrd script --- cmd/kubectl-testkube/commands/scripts.go | 1 + .../commands/scripts/crd_script.go | 124 ++++++++++++++++++ pkg/ui/printers.go | 11 ++ 3 files changed, 136 insertions(+) create mode 100644 cmd/kubectl-testkube/commands/scripts/crd_script.go diff --git a/cmd/kubectl-testkube/commands/scripts.go b/cmd/kubectl-testkube/commands/scripts.go index 50665c8803b..9c59ebbed52 100644 --- a/cmd/kubectl-testkube/commands/scripts.go +++ b/cmd/kubectl-testkube/commands/scripts.go @@ -39,6 +39,7 @@ func NewScriptsCmd() *cobra.Command { cmd.AddCommand(scripts.NewWatchExecutionCmd()) cmd.AddCommand(scripts.NewListExecutionsCmd()) cmd.AddCommand(scripts.NewCreateScriptsCmd()) + cmd.AddCommand(scripts.NewCRDScriptsCmd()) cmd.AddCommand(scripts.NewUpdateScriptsCmd()) cmd.AddCommand(scripts.NewDeleteScriptsCmd()) return cmd diff --git a/cmd/kubectl-testkube/commands/scripts/crd_script.go b/cmd/kubectl-testkube/commands/scripts/crd_script.go new file mode 100644 index 00000000000..864a38195ff --- /dev/null +++ b/cmd/kubectl-testkube/commands/scripts/crd_script.go @@ -0,0 +1,124 @@ +package scripts + +import ( + "bytes" + "fmt" + "io/fs" + "io/ioutil" + "path/filepath" + "strings" + "text/template" + + "github.com/kubeshop/testkube/pkg/api/v1/client" + "github.com/kubeshop/testkube/pkg/test/script/detector" + "github.com/kubeshop/testkube/pkg/ui" + "github.com/spf13/cobra" +) + +func NewCRDScriptsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "crd", + Short: "Generate scripts CRD file based on directory", + Long: `Generate scripts manifest based on directory (e.g. for ArgoCD sync based on scripts files)`, + Run: func(cmd *cobra.Command, args []string) { + ui.Logo() + + var err error + + if len(args) == 0 { + ui.Failf("Please pass directory") + } + + dir := args[0] + firstEntry := true + + err = filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error { + if !firstEntry { + fmt.Printf("\n\n---\n\n") + } + firstEntry = false + + if err != nil { + return nil + } + if !strings.HasSuffix(path, ".json") { + return nil + } + + ns, _ := cmd.Flags().GetString("namespace") + yaml, err := GenerateCRD(ns, path) + if err != nil { + return err + } + + fmt.Print(yaml) + + return nil + }) + + ui.ExitOnError("getting directory content", err) + + }, + } + + return cmd +} + +var ErrTypeNotDetected = fmt.Errorf("type not detected") + +type Script struct { + Name string + Namespace string + Content string + Type string +} + +// TODO find a way to use internal objects as YAML +// GenerateCRD generates CRDs based on directory of test files +func GenerateCRD(namespace, path string) (string, error) { + var scriptType string + + tpl := `apiVersion: tests.testkube.io/v1 +kind: Script +metadata: + name: {{ .Name }} + namespace: {{ .Namespace }} +spec: + content: "{{ .Content }}" + type: {{ .Type }} + ` + + content, err := ioutil.ReadFile(path) + if err != nil { + return "", err + } + + // try to detect type if none passed + d := detector.NewDefaultDetector() + if detectedType, ok := d.Detect(client.UpsertScriptOptions{Content: string(content)}); ok { + ui.Debug("Detected test script type", detectedType) + scriptType = detectedType + } else { + return "", ErrTypeNotDetected + } + + t := template.Must(template.New("sccript").Parse(tpl)) + b := bytes.NewBuffer([]byte{}) + err = t.Execute(b, Script{ + Name: SanitizePath(path), + Namespace: namespace, + Content: fmt.Sprintf("%q", string(content)), + Type: scriptType, + }) + + return b.String(), err +} + +func SanitizePath(path string) string { + name := strings.Replace(path, "/", "-", -1) + name = strings.Replace(name, "_", "-", -1) + name = strings.Replace(name, ".", "-", -1) + name = strings.TrimSuffix(name, ".json") + + return name +} diff --git a/pkg/ui/printers.go b/pkg/ui/printers.go index a4d8462fc15..c54b1f22880 100644 --- a/pkg/ui/printers.go +++ b/pkg/ui/printers.go @@ -40,6 +40,17 @@ func LogLine(message string) { fmt.Printf("%s\n", DarkGray(message)) } +func Debug(message string, subMessages ...string) { + if !Verbose { + return + } + fmt.Printf("%s", DarkGray(message)) + for _, sub := range subMessages { + fmt.Printf(" %s", LightGray(sub)) + } + fmt.Println() +} + func Info(message string, subMessages ...string) { fmt.Printf("%s", DarkGray(message)) for _, sub := range subMessages {