-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (63 loc) · 1.84 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"flag"
"fmt"
pytorchversioned "github.com/kubeflow/training-operator/pkg/client/clientset/versioned"
"github.com/shuangkun/argo-workflows-pytorch-plugin/controller"
"k8s.io/klog/v2"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
type option struct {
port int
}
func main() {
opt := &option{}
cmd := &cobra.Command{
Use: "argo-pytorch-plugin",
RunE: opt.runE,
}
flags := cmd.Flags()
flags.IntVarP(&opt.port, "port", "", 3008, "The port of the HTTP server")
if err := cmd.Execute(); err != nil {
panic(err)
}
}
func (o *option) runE(c *cobra.Command, args []string) (err error) {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
if config, err = rest.InClusterConfig(); err != nil {
panic(err.Error())
}
}
ct := &controller.PytorchJobController{}
pytorchClient := getPytorchClient(config)
ct.PytorchClient = pytorchClient
router := gin.Default()
router.POST("/api/v1/template.execute", ct.ExecutePytorchJob)
if err := router.Run(fmt.Sprintf(":%d", o.port)); err != nil {
klog.Fatal("Failed to start server:", err)
}
return
}
// GetPytorchClient get a clientset for Pytorch Job.
func getPytorchClient(restConfig *rest.Config) *pytorchversioned.Clientset {
clientset, err := pytorchversioned.NewForConfig(restConfig)
klog.Info(clientset.ServerVersion())
if err != nil {
klog.Fatal(err)
}
return clientset
}