Skip to content

Commit

Permalink
*: update server.go to align with pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Fu <[email protected]>
  • Loading branch information
fuweid committed Jan 19, 2024
1 parent ed344a2 commit 562aee6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
88 changes: 56 additions & 32 deletions cmd/kperf/commands/multirunners/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/Azure/kperf/runner"
runnergroup "github.com/Azure/kperf/runner/group"

"github.com/urfave/cli"
"k8s.io/client-go/kubernetes"
Expand All @@ -29,57 +30,80 @@ var serverCommand = cli.Command{
Usage: "The runner's conainer image",
Required: true,
},
cli.IntFlag{
Name: "port",
Value: 8080,
},
cli.StringFlag{
Name: "host",
Value: "0.0.0.0",
cli.StringSliceFlag{
Name: "address",
Usage: "Address for the server",
Required: true,
},
cli.StringFlag{
Name: "data",
Usage: "The runner result should be stored in that path",
Value: "/tmp/data",
Name: "data",
Usage: "The runner result should be stored in that path",
Required: true,
},
},
Hidden: true,
Action: func(cliCtx *cli.Context) error {
if cliCtx.NArg() != 1 {
return fmt.Errorf("required only one argument as server name")
}

name := strings.TrimSpace(cliCtx.Args().Get(0))
if len(name) == 0 {
return fmt.Errorf("required non-empty name")
return fmt.Errorf("required non-empty server name")
}

addr := fmt.Sprintf("%s:%d", cliCtx.String("host"), cliCtx.Int("port"))
dataDir := cliCtx.String("data")

kubeCfgPath := cliCtx.String("kubeconfig")
config, err := clientcmd.BuildConfigFromFlags("", kubeCfgPath)
groupHandlers, err := buildRunnerGroupHandlers(cliCtx, name)
if err != nil {
return err
return fmt.Errorf("failed to create runner group handlers: %w", err)
}

clientset, err := kubernetes.NewForConfig(config)
dataDir := cliCtx.String("data")
addrs := cliCtx.StringSlice("address")

srv, err := runner.NewServer(dataDir, addrs, groupHandlers...)
if err != nil {
return err
}
return srv.Run()
},
}

// buildRunnerGroupHandlers creates a slice of runner group handlers.
func buildRunnerGroupHandlers(cliCtx *cli.Context, serverName string) ([]*runnergroup.Handler, error) {
clientset, err := buildKubernetesClientset(cliCtx)
if err != nil {
return nil, fmt.Errorf("failed to build kubernetes clientset: %w", err)
}

groups := []*runner.GroupHandler{}
imgRef := cliCtx.String("runner-image")
ns := cliCtx.String("namespace")
for idx, specUri := range cliCtx.StringSlice("runners") {
gName := fmt.Sprintf("%s-%d", name, idx)
g, err := runner.NewGroupHandler(clientset, gName, ns, specUri, imgRef)
if err != nil {
return err
}
groups = append(groups, g)
specURIs := cliCtx.StringSlice("runners")
imgRef := cliCtx.String("runner-image")
namespace := cliCtx.String("namespace")

groups := make([]*runnergroup.Handler, 0, len(specURIs))
for idx, specURI := range specURIs {
spec, err := runnergroup.NewRunnerGroupSpecFromURI(clientset, specURI)
if err != nil {
return nil, err
}

srv, err := runner.NewServer(dataDir, addr, groups...)
groupName := fmt.Sprintf("%s-%d", serverName, idx)
g, err := runnergroup.NewHandler(clientset, namespace, groupName, spec, imgRef)
if err != nil {
return err
return nil, err
}
return srv.Run()
},
groups = append(groups, g)
}

return groups, nil
}

// buildKubernetesClientset builds kubernetes clientset from global flag.
func buildKubernetesClientset(cliCtx *cli.Context) (kubernetes.Interface, error) {
kubeCfgPath := cliCtx.GlobalString("kubeconfig")
config, err := clientcmd.BuildConfigFromFlags("", kubeCfgPath)
if err != nil {
return nil, err
}

return kubernetes.NewForConfig(config)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ module github.com/Azure/kperf
go 1.20

require (
github.com/gorilla/mux v1.8.0
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.14
golang.org/x/time v0.3.0
gopkg.in/yaml.v2 v2.4.0
helm.sh/helm/v3 v3.13.3
k8s.io/api v0.28.4
k8s.io/apimachinery v0.28.4
k8s.io/cli-runtime v0.28.4
k8s.io/client-go v0.28.4
k8s.io/klog/v2 v2.100.1
k8s.io/kubectl v0.28.4
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
)

require (
Expand Down Expand Up @@ -62,7 +65,6 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down Expand Up @@ -132,12 +134,10 @@ require (
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.4 // indirect
k8s.io/apiextensions-apiserver v0.28.4 // indirect
k8s.io/apiserver v0.28.4 // indirect
k8s.io/component-base v0.28.4 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
oras.land/oras-go v1.2.4 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
Expand Down

0 comments on commit 562aee6

Please sign in to comment.