Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vkctl delete feature #177

Merged
merged 4 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.")

})
})