volcano.sh/volcano@v1.9.0/pkg/cli/job/delete.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 job 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 deleteFlags struct { 32 commonFlags 33 34 Namespace string 35 JobName string 36 } 37 38 var deleteJobFlags = &deleteFlags{} 39 40 // InitDeleteFlags init the delete command flags. 41 func InitDeleteFlags(cmd *cobra.Command) { 42 initFlags(cmd, &deleteJobFlags.commonFlags) 43 44 cmd.Flags().StringVarP(&deleteJobFlags.Namespace, "namespace", "n", "default", "the namespace of job") 45 cmd.Flags().StringVarP(&deleteJobFlags.JobName, "name", "N", "", "the name of job") 46 } 47 48 // DeleteJob delete the job. 49 func DeleteJob() error { 50 config, err := util.BuildConfig(deleteJobFlags.Master, deleteJobFlags.Kubeconfig) 51 if err != nil { 52 return err 53 } 54 55 if deleteJobFlags.JobName == "" { 56 err := fmt.Errorf("job name is mandatory to delete a particular job") 57 return err 58 } 59 60 jobClient := versioned.NewForConfigOrDie(config) 61 err = jobClient.BatchV1alpha1().Jobs(deleteJobFlags.Namespace).Delete(context.TODO(), deleteJobFlags.JobName, metav1.DeleteOptions{}) 62 if err != nil { 63 return err 64 } 65 fmt.Printf("delete job %v successfully\n", deleteJobFlags.JobName) 66 return nil 67 }