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

feat: add vcctl jobtemplate #3507

Closed
wants to merge 1 commit into from
Closed
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
68 changes: 68 additions & 0 deletions cmd/cli/jobtemplate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"github.com/spf13/cobra"
"volcano.sh/volcano/cmd/cli/util"
"volcano.sh/volcano/pkg/cli/jobtemplate"
)

func buildJobTemplateCmd() *cobra.Command {
jobTemplateCmd := &cobra.Command{
Use: "jobtemplate",
Short: "vcctl command line operation jobtemplate",
}

jobTemplateCommandMap := map[string]struct {
Short string
RunFunction func(cmd *cobra.Command, args []string)
InitFlags func(cmd *cobra.Command)
}{
"create": {
Short: "create a jobtemplate",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, jobtemplate.CreateJobTemplate(cmd.Context()))
},
InitFlags: jobtemplate.InitCreateFlags,
},
"list": {
Short: "list a jobtemplate",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, jobtemplate.ListJobTemplate(cmd.Context()))
},
InitFlags: jobtemplate.InitListFlags,
},
"get": {
Short: "get a jobtemplate",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, jobtemplate.GetJobTemplate(cmd.Context()))
},
InitFlags: jobtemplate.InitGetFlags,
},
"delete": {
Short: "delete a jobtemplate",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, jobtemplate.DeleteJobTemplate(cmd.Context()))
},
InitFlags: jobtemplate.InitDeleteFlags,
},
"describe": {
Short: "describe a jobtemplate",
RunFunction: func(cmd *cobra.Command, args []string) {
util.CheckError(cmd, jobtemplate.DescribeJobTemplate(cmd.Context()))
},
InitFlags: jobtemplate.InitDescribeFlags,
},
}

for command, config := range jobTemplateCommandMap {
cmd := &cobra.Command{
Use: command,
Short: config.Short,
Run: config.RunFunction,
}
config.InitFlags(cmd)
jobTemplateCmd.AddCommand(cmd)
}

return jobTemplateCmd
}
1 change: 1 addition & 0 deletions cmd/cli/vcctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func main() {

rootCmd.AddCommand(buildJobCmd())
rootCmd.AddCommand(buildQueueCmd())
rootCmd.AddCommand(buildJobTemplateCmd())
rootCmd.AddCommand(versionCommand())

code := cli.Run(&rootCmd)
Expand Down
77 changes: 77 additions & 0 deletions pkg/cli/jobtemplate/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package jobtemplate

import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

flowv1alpha1 "volcano.sh/apis/pkg/apis/flow/v1alpha1"
"volcano.sh/apis/pkg/client/clientset/versioned"
"volcano.sh/volcano/pkg/cli/util"
)

type createFlags struct {
util.CommonFlags
// FilePath is the file path of job template.
FilePath string
}

var createJobTemplateFlags = &createFlags{}

// InitCreateFlags is used to init all flags during queue creating.
func InitCreateFlags(cmd *cobra.Command) {
util.InitFlags(cmd, &createJobTemplateFlags.CommonFlags)
cmd.Flags().StringVarP(&createJobTemplateFlags.FilePath, "file", "f", "", "the path to the YAML file containing the job template")
}

// CreateJobTemplate create a job template.
func CreateJobTemplate(ctx context.Context) error {
config, err := util.BuildConfig(createJobTemplateFlags.Master, createJobTemplateFlags.Kubeconfig)
if err != nil {
return err
}

// Read YAML data from a file.
yamlData, err := os.ReadFile(createJobTemplateFlags.FilePath)
if err != nil {
return err
}
// Split YAML data into individual documents.
yamlDocs := strings.Split(string(yamlData), "---")

jobTemplateClient := versioned.NewForConfigOrDie(config)
createdCount := 0
for _, doc := range yamlDocs {
// Skip empty documents or documents with only whitespace.
doc = strings.TrimSpace(doc)
if doc == "" {
continue
}

// Parse each YAML document into a JobTemplate object.
obj := &flowv1alpha1.JobTemplate{}
if err = yaml.Unmarshal([]byte(doc), obj); err != nil {
return err
}
// Set the namespace if it's not specified.
if obj.Namespace == "" {
obj.Namespace = "default"
}

_, err = jobTemplateClient.FlowV1alpha1().JobTemplates(obj.Namespace).Create(ctx, obj, metav1.CreateOptions{})
if err == nil {
fmt.Printf("Created JobTemplate: %s/%s\n", obj.Namespace, obj.Name)
createdCount++
} else {
fmt.Printf("Failed to create JobTemplate: %v\n", err)
}
}

return nil
}
107 changes: 107 additions & 0 deletions pkg/cli/jobtemplate/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package jobtemplate

import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

flowv1alpha1 "volcano.sh/apis/pkg/apis/flow/v1alpha1"
"volcano.sh/apis/pkg/client/clientset/versioned"
"volcano.sh/volcano/pkg/cli/util"
)

type deleteFlags struct {
util.CommonFlags

// Name is name of job template
Name string
// Namespace is namespace of job template
Namespace string
// FilePath is the file path of job template.
FilePath string
}

var deleteJobTemplateFlags = &deleteFlags{}

// InitDeleteFlags is used to init all flags during job template deleting.
func InitDeleteFlags(cmd *cobra.Command) {
util.InitFlags(cmd, &deleteJobTemplateFlags.CommonFlags)
cmd.Flags().StringVarP(&deleteJobTemplateFlags.Name, "name", "N", "", "the name of job template")
cmd.Flags().StringVarP(&deleteJobTemplateFlags.Namespace, "namespace", "n", "default", "the namespace of job template")
cmd.Flags().StringVarP(&deleteJobTemplateFlags.FilePath, "file", "f", "", "the path to the YAML file containing the job template")
}

// DeleteJobTemplate is used to delete a job template.
func DeleteJobTemplate(ctx context.Context) error {
config, err := util.BuildConfig(deleteJobTemplateFlags.Master, deleteJobTemplateFlags.Kubeconfig)
if err != nil {
return err
}

jobTemplateClient := versioned.NewForConfigOrDie(config)
if err != nil {
return err
}

if deleteJobTemplateFlags.FilePath != "" {
yamlData, err := os.ReadFile(deleteJobTemplateFlags.FilePath)
if err != nil {
return err
}

yamlDocs := strings.Split(string(yamlData), "---")

deletedCount := 0
for _, doc := range yamlDocs {
doc = strings.TrimSpace(doc)
if doc == "" {
continue
}

jobTemplate := &flowv1alpha1.JobTemplate{}
if err := yaml.Unmarshal([]byte(doc), jobTemplate); err != nil {
return err
}

if jobTemplate.Namespace == "" {
jobTemplate.Namespace = "default"
}

err := jobTemplateClient.FlowV1alpha1().JobTemplates(jobTemplate.Namespace).Delete(ctx, jobTemplate.Name, metav1.DeleteOptions{})
if err == nil {
fmt.Printf("Deleted JobTemplate: %s/%s\n", jobTemplate.Namespace, jobTemplate.Name)
deletedCount++
} else {
fmt.Printf("Failed to delete JobTemplate: %v\n", err)
}
}

fmt.Printf("Deleted %d JobTemplates successfully\n", deletedCount)

return nil
}

if deleteJobTemplateFlags.Name == "" {
return fmt.Errorf("job template name must be specified")
}

jobTemplate, err := jobTemplateClient.FlowV1alpha1().JobTemplates(deleteJobTemplateFlags.Namespace).Get(ctx, deleteJobTemplateFlags.Name, metav1.GetOptions{})
if err != nil {
return err
}

err = jobTemplateClient.FlowV1alpha1().JobTemplates(jobTemplate.Namespace).Delete(ctx, jobTemplate.Name, metav1.DeleteOptions{})
if err != nil {
return err
}

fmt.Printf("Deleted JobTemplate: %s/%s\n", jobTemplate.Namespace, jobTemplate.Name)

return nil
}
97 changes: 97 additions & 0 deletions pkg/cli/jobtemplate/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package jobtemplate

import (
"context"
"encoding/json"
"fmt"
"log"

"github.com/spf13/cobra"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"

"volcano.sh/apis/pkg/apis/flow/v1alpha1"
"volcano.sh/apis/pkg/client/clientset/versioned"
"volcano.sh/volcano/pkg/cli/util"
)

type describeFlags struct {
util.CommonFlags

// Name is name of job template
Name string
// Namespace is namespace of job template
Namespace string
// Format print format: yaml or json format
Format string
}

var describeJobTemplateFlags = &describeFlags{}

// InitDescribeFlags is used to init all flags.
func InitDescribeFlags(cmd *cobra.Command) {
util.InitFlags(cmd, &describeJobTemplateFlags.CommonFlags)
cmd.Flags().StringVarP(&describeJobTemplateFlags.Name, "name", "N", "", "the name of job template")
cmd.Flags().StringVarP(&describeJobTemplateFlags.Namespace, "namespace", "n", "default", "the namespace of job template")
cmd.Flags().StringVarP(&describeJobTemplateFlags.Format, "format", "o", "yaml", "the format of output")
}

// DescribeJobTemplate is used to get the particular job template details.
func DescribeJobTemplate(ctx context.Context) error {
config, err := util.BuildConfig(describeJobTemplateFlags.Master, describeJobTemplateFlags.Kubeconfig)
if err != nil {
return err
}
jobTemplateClient := versioned.NewForConfigOrDie(config)

// Get job template list detail
if describeJobTemplateFlags.Name == "" {
jobTemplates, err := jobTemplateClient.FlowV1alpha1().JobTemplates(describeJobTemplateFlags.Namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
for _, jobTemplate := range jobTemplates.Items {
PrintJobTemplateDetail(&jobTemplate, describeJobTemplateFlags.Format)
}
// Get job template detail
} else {
jobTemplate, err := jobTemplateClient.FlowV1alpha1().JobTemplates(describeJobTemplateFlags.Namespace).Get(ctx, describeJobTemplateFlags.Name, metav1.GetOptions{})
if err != nil {
return err
}
PrintJobTemplateDetail(jobTemplate, describeJobTemplateFlags.Format)
}

return nil
}

// PrintJobTemplateDetail print job template details
func PrintJobTemplateDetail(jobTemplate *v1alpha1.JobTemplate, format string) {
switch format {
case "json":
printJSON(jobTemplate)
case "yaml":
printYAML(jobTemplate)
default:
log.Fatalf("Unsupported format: %s", format)
}
}

func printJSON(jobTemplate *v1alpha1.JobTemplate) {
b, err := json.MarshalIndent(jobTemplate, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
fmt.Println("---------------------------------")
}

func printYAML(jobTemplate *v1alpha1.JobTemplate) {
b, err := yaml.Marshal(jobTemplate)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
fmt.Println("---------------------------------")
}
Loading
Loading