volcano.sh/volcano@v1.9.0/pkg/cli/vcancel/cancel.go (about) 1 /* 2 Copyright 2019 The Volcano Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package vcancel 18 19 import ( 20 "context" 21 "fmt" 22 23 "github.com/spf13/cobra" 24 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 27 "volcano.sh/apis/pkg/client/clientset/versioned" 28 "volcano.sh/volcano/pkg/cli/util" 29 ) 30 31 type cancelFlags struct { 32 util.CommonFlags 33 34 Namespace string 35 JobName string 36 } 37 38 var cancelJobFlags = &cancelFlags{} 39 40 // InitCancelFlags init the cancel command flags. 41 func InitCancelFlags(cmd *cobra.Command) { 42 util.InitFlags(cmd, &cancelJobFlags.CommonFlags) 43 44 cmd.Flags().StringVarP(&cancelJobFlags.Namespace, "namespace", "N", "default", "the namespace of job") 45 cmd.Flags().StringVarP(&cancelJobFlags.JobName, "name", "n", "", "the name of job") 46 } 47 48 // CancelJob cancel the job. 49 func CancelJob() error { 50 config, err := util.BuildConfig(cancelJobFlags.Master, cancelJobFlags.Kubeconfig) 51 if err != nil { 52 return err 53 } 54 55 if cancelJobFlags.JobName == "" { 56 err := fmt.Errorf("job name is mandatory to cancel a particular job") 57 return err 58 } 59 60 jobClient := versioned.NewForConfigOrDie(config) 61 err = jobClient.BatchV1alpha1().Jobs(cancelJobFlags.Namespace).Delete(context.TODO(), cancelJobFlags.JobName, metav1.DeleteOptions{}) 62 if err != nil { 63 return err 64 } 65 fmt.Printf("cancel job %v successfully\n", cancelJobFlags.JobName) 66 return nil 67 }