diff --git a/cmd/cli/job.go b/cmd/cli/job.go index d858b5df4c..a8a49daf98 100644 --- a/cmd/cli/job.go +++ b/cmd/cli/job.go @@ -52,5 +52,15 @@ func buildJobCmd() *cobra.Command { job.InitResumeFlags(jobResumeCmd) jobCmd.AddCommand(jobResumeCmd) + jobDelCmd := &cobra.Command{ + Use: "delete", + Short: "delete a job ", + Run: func(cmd *cobra.Command, args []string) { + checkError(cmd, job.DeleteJob()) + }, + } + job.InitDeleteFlags(jobDelCmd) + jobCmd.AddCommand(jobDelCmd) + return jobCmd } diff --git a/pkg/cli/job/delete.go b/pkg/cli/job/delete.go new file mode 100644 index 0000000000..f60acd9760 --- /dev/null +++ b/pkg/cli/job/delete.go @@ -0,0 +1,58 @@ +/* +Copyright 2019 The Vulcan Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package job + +import ( + "fmt" + "github.com/spf13/cobra" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "volcano.sh/volcano/pkg/client/clientset/versioned" +) + +type deleteFlags struct { + commonFlags + + Namespace string + JobName string +} + +var deleteJobFlags = &deleteFlags{} + +func InitDeleteFlags(cmd *cobra.Command) { + initFlags(cmd, &deleteJobFlags.commonFlags) + + cmd.Flags().StringVarP(&deleteJobFlags.Namespace, "namespace", "N", "default", "the namespace of job") + cmd.Flags().StringVarP(&deleteJobFlags.JobName, "name", "n", "", "the name of job") +} + +func DeleteJob() error { + config, err := buildConfig(deleteJobFlags.Master, deleteJobFlags.Kubeconfig) + if err != nil { + return err + } + + jobClient := versioned.NewForConfigOrDie(config) + err = jobClient.BatchV1alpha1().Jobs(deleteJobFlags.Namespace).Delete(deleteJobFlags.JobName, &metav1.DeleteOptions{}) + if err != nil { + return err + } + fmt.Printf("delete job %v successfully\n", deleteJobFlags.JobName) + return nil + +} diff --git a/test/e2e/cli_util.go b/test/e2e/cli_util.go index 7953f3e336..341ab6de93 100644 --- a/test/e2e/cli_util.go +++ b/test/e2e/cli_util.go @@ -52,6 +52,16 @@ func ListJobs(namespace string) string { return RunCliCommand(command) } +func DeleteJob(name string, namespace string) string { + command := []string{"job", "delete"} + Expect(name).NotTo(Equal(""), "Job name should not be empty in delete job command") + command = append(command, "--name", name) + if namespace != "" { + command = append(command, "--namespace", namespace) + } + return RunCliCommand(command) +} + func RunCliCommand(command []string) string { if masterURL() != "" { command = append(command, "--master", masterURL()) diff --git a/test/e2e/command.go b/test/e2e/command.go index 4300022172..9c1654a4f0 100644 --- a/test/e2e/command.go +++ b/test/e2e/command.go @@ -152,4 +152,43 @@ var _ = Describe("Job E2E Test: Test Job Command", func() { Expect(apierrors.IsNotFound(err)).To(BeTrue(), "Job related pod should be deleted when job aborted.") }) + + It("delete a job", func() { + + jobName := "test-del-job" + namespace := "test" + context := initTestContext() + defer cleanupTestContext(context) + rep := clusterSize(context, oneCPU) + + job := createJob(context, &jobSpec{ + namespace: namespace, + name: jobName, + tasks: []taskSpec{ + { + img: defaultNginxImage, + req: oneCPU, + min: rep, + rep: rep, + }, + }, + }) + // Pod is running + err := waitJobReady(context, job) + Expect(err).NotTo(HaveOccurred()) + // Job Status is running + err = waitJobStateReady(context, job) + Expect(err).NotTo(HaveOccurred()) + + _, err = context.vkclient.BatchV1alpha1().Jobs(namespace).Get(jobName, metav1.GetOptions{}) + Expect(err).NotTo(HaveOccurred()) + + // Delete job + DeleteJob(jobName, namespace) + + _, err = context.vkclient.BatchV1alpha1().Jobs(namespace).Get(jobName, metav1.GetOptions{}) + Expect(apierrors.IsNotFound(err)).To(BeTrue(), + "Job should be deleted on vkctl job delete.") + + }) })