Skip to content

Commit

Permalink
Add cmd files for joint inference
Browse files Browse the repository at this point in the history
Signed-off-by: JimmyYang <yangjin39@huawei.com>
  • Loading branch information
JimmyYang authored and llhuii committed Jan 28, 2021
1 parent ab15aca commit f4fca6a
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 0 deletions.
72 changes: 72 additions & 0 deletions cmd/neptune-gm/app/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package app

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/cli/globalflag"
"k8s.io/component-base/term"
"k8s.io/klog/v2"

"github.com/edgeai-neptune/neptune/cmd/neptune-gm/app/options"
controller "github.com/edgeai-neptune/neptune/pkg/globalmanager"
"github.com/edgeai-neptune/neptune/pkg/util"
"github.com/edgeai-neptune/neptune/pkg/version/verflag"
)

// PrintFlags logs the flags in the flagset
func PrintFlags(flags *pflag.FlagSet) {
flags.VisitAll(func(flag *pflag.Flag) {
klog.V(1).Infof("FLAG: --%s=%q", flag.Name, flag.Value)
})
}

func NewControllerCommand() *cobra.Command {
opts := options.NewControllerOptions()
cmd := &cobra.Command{
Use: "neptune-gm",
Long: `neptune-gm is the core cloud part of Neptune.`,
Run: func(cmd *cobra.Command, args []string) {
verflag.PrintAndExitIfRequested()
PrintFlags(cmd.Flags())

if errs := opts.Validate(); len(errs) > 0 {
klog.Fatal(util.SpliceErrors(errs))
}

config, err := opts.Config()
if err != nil {
klog.Fatal(err)
}

if errs := config.Validate(); len(errs) > 0 {
klog.Fatal(util.SpliceErrors(errs.ToAggregate().Errors()))
}
c := controller.NewController(config)
c.Start()
},
}
fs := cmd.Flags()
namedFs := opts.Flags()
verflag.AddFlags(namedFs.FlagSet("global"))
globalflag.AddGlobalFlags(namedFs.FlagSet("global"), cmd.Name())
for _, f := range namedFs.FlagSets {
fs.AddFlagSet(f)
}

usageFmt := "Usage:\n %s\n"
cols, _, _ := term.TerminalSize(cmd.OutOrStdout())
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
fmt.Fprintf(cmd.OutOrStderr(), usageFmt, cmd.UseLine())
cliflag.PrintSections(cmd.OutOrStderr(), namedFs, cols)
return nil
})
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine())
cliflag.PrintSections(cmd.OutOrStdout(), namedFs, cols)
})

return cmd
}
47 changes: 47 additions & 0 deletions cmd/neptune-gm/app/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package options

import (
"fmt"
"path"

"k8s.io/apimachinery/pkg/util/validation/field"
cliflag "k8s.io/component-base/cli/flag"

"github.com/edgeai-neptune/neptune/pkg/globalmanager/config"
"github.com/edgeai-neptune/neptune/pkg/util"
)

const DefaultConfigDir = "."

type ControllerOptions struct {
ConfigFile string
}

func NewControllerOptions() *ControllerOptions {
return &ControllerOptions{
ConfigFile: path.Join(DefaultConfigDir, "neptune-gm.yaml"),
}
}

func (o *ControllerOptions) Flags() (fss cliflag.NamedFlagSets) {
fs := fss.FlagSet("global")
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file. Flags override values in this file.")
return
}

func (o *ControllerOptions) Validate() []error {
var errs []error
if !util.FileIsExist(o.ConfigFile) {
errs = append(errs, field.Required(field.NewPath("config"),
fmt.Sprintf("config file %v not exist.", o.ConfigFile)))
}
return errs
}

func (o *ControllerOptions) Config() (*config.ControllerConfig, error) {
cfg := config.NewDefaultControllerConfig()
if err := cfg.Parse(o.ConfigFile); err != nil {
return nil, err
}
return cfg, nil
}
19 changes: 19 additions & 0 deletions cmd/neptune-gm/neptune-gm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"os"

"k8s.io/component-base/logs"

"github.com/edgeai-neptune/neptune/cmd/neptune-gm/app"
)

func main() {
command := app.NewControllerCommand()
logs.InitLogs()
defer logs.FlushLogs()

if err := command.Execute(); err != nil {
os.Exit(1)
}
}
14 changes: 14 additions & 0 deletions cmd/neptune-lc/app/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package options

// LocalControllerOptions defines options
type LocalControllerOptions struct {
GMAddr string
NodeName string
BindPort string
VolumeMountPrefix string
}

// NewLocalControllerOptions create options object
func NewLocalControllerOptions() *LocalControllerOptions {
return &LocalControllerOptions{}
}
71 changes: 71 additions & 0 deletions cmd/neptune-lc/app/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package app

import (
"os"

"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/edgeai-neptune/neptune/cmd/neptune-lc/app/options"
"github.com/edgeai-neptune/neptune/pkg/localcontroller/common/constants"
"github.com/edgeai-neptune/neptune/pkg/localcontroller/manager"
"github.com/edgeai-neptune/neptune/pkg/localcontroller/server"
"github.com/edgeai-neptune/neptune/pkg/localcontroller/wsclient"
)

var (
Options *options.LocalControllerOptions
)

// NewLocalControllerCommand creates a command object
func NewLocalControllerCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "localcontroller",
Long: `LocalController is a local controller. It manages dataset and models.
And it controls ai features in local nodes, like joint inference service.`,
Run: func(cmd *cobra.Command, args []string) {
runServer()
},
}

Options = options.NewLocalControllerOptions()

Options.GMAddr = os.Getenv(constants.GMAddressENV)
if Options.NodeName = os.Getenv(constants.NodeNameENV); Options.NodeName == "" {
Options.NodeName = os.Getenv(constants.HostNameENV)
}

if Options.VolumeMountPrefix = os.Getenv(constants.RootFSMountDirENV); Options.VolumeMountPrefix == "" {
Options.VolumeMountPrefix = "/rootfs"
}

if Options.BindPort = os.Getenv(constants.BindPortENV); Options.BindPort == "" {
Options.BindPort = "9100"
}

return cmd
}

// runServer runs server
func runServer() {
c := wsclient.NewClient(Options)
if err := c.Start(); err != nil {
return
}

_, err := manager.NewDatasetManager(c, Options)
if err != nil {
klog.Errorf("create dataset manager failed, error: %v", err)
}

_, err = manager.NewModelManager(c)
if err != nil {
klog.Errorf("create model manager failed, error: %v", err)
}

jm := manager.NewJointInferenceManager(c)

s := server.NewServer(Options, jm)

s.Start()
}
14 changes: 14 additions & 0 deletions cmd/neptune-lc/neptune-lc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"os"

"github.com/edgeai-neptune/neptune/cmd/neptune-lc/app"
)

func main() {
command := app.NewLocalControllerCommand()
if err := command.Execute(); err != nil {
os.Exit(1)
}
}

0 comments on commit f4fca6a

Please sign in to comment.