Skip to content

Commit

Permalink
Merge pull request volcano-sh#177 from SrinivasChilveri/vkctldel
Browse files Browse the repository at this point in the history
vkctl delete feature
  • Loading branch information
volcano-sh-bot authored May 16, 2019
2 parents 69dcb46 + ff5ef8d commit 10c254c
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/cli/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
58 changes: 58 additions & 0 deletions pkg/cli/job/delete.go
Original file line number Diff line number Diff line change
@@ -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

}
10 changes: 10 additions & 0 deletions test/e2e/cli_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
39 changes: 39 additions & 0 deletions test/e2e/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

})
})

0 comments on commit 10c254c

Please sign in to comment.