From 140ceab475a429a208c39d5030290b2022acb6a5 Mon Sep 17 00:00:00 2001 From: Luiz Filho Date: Wed, 25 Nov 2020 20:31:40 -0300 Subject: [PATCH] Adding delete command --- lokustctl/cmd/common.go | 13 +++++++++++++ lokustctl/cmd/create.go | 10 ---------- lokustctl/cmd/delete.go | 38 +++++++++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 lokustctl/cmd/common.go diff --git a/lokustctl/cmd/common.go b/lokustctl/cmd/common.go new file mode 100644 index 0000000..70478a8 --- /dev/null +++ b/lokustctl/cmd/common.go @@ -0,0 +1,13 @@ +package cmd + +import "fmt" + +func buildResourceName(testName string, resourceType ...string) string { + name := fmt.Sprintf("lokust-%s", testName) + + if len(resourceType) > 1 { + name += "-" + resourceType[0] + } + + return name +} diff --git a/lokustctl/cmd/create.go b/lokustctl/cmd/create.go index c391c08..fc064a9 100644 --- a/lokustctl/cmd/create.go +++ b/lokustctl/cmd/create.go @@ -143,16 +143,6 @@ func buildConfigmapFromFile(filename string) (map[string]string, error) { return configMapData, nil } -func buildResourceName(testName string, resourceType ...string) string { - name := fmt.Sprintf("lokust-%s", testName) - - if len(resourceType) > 1 { - name += "-" + resourceType[0] - } - - return name -} - func ConvertToCoreV1ResourceList(resourceList map[string]string) corev1.ResourceList { capacity := make(corev1.ResourceList) diff --git a/lokustctl/cmd/delete.go b/lokustctl/cmd/delete.go index 61a8350..f21f6b8 100644 --- a/lokustctl/cmd/delete.go +++ b/lokustctl/cmd/delete.go @@ -16,7 +16,9 @@ limitations under the License. package cmd import ( + "errors" "fmt" + "os" "github.com/spf13/cobra" ) @@ -24,16 +26,34 @@ import ( // deleteCmd represents the delete command var deleteCmd = &cobra.Command{ Use: "delete", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("delete called") + Short: "Delete a lokust test", + Long: `Delete a lokust test by name + + lokust delete [test-name] + `, + Args: func(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return errors.New("requires the locust test name") + } + + return nil }, + Run: deleteRun, +} + +func deleteRun(cmd *cobra.Command, args []string) { + testName := args[0] + err := kclientset.CoreV1().ConfigMaps(config.Namespace).Delete(buildResourceName(testName, "configmap"), nil) + if err != nil { + fmt.Printf("Failed deleting %s locustfile configmap. err: %s\n", testName, err) + os.Exit(1) + } + + err = ltclientset.LoadtestsV1beta1().LocustTests(config.Namespace).Delete(testName, nil) + if err != nil { + fmt.Printf("Failed deleting %s test. err: %s\n", testName, err) + os.Exit(1) + } } func init() {