Skip to content

Commit

Permalink
Add code to initialize kubelet plugin and node server
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Sim <[email protected]>
  • Loading branch information
ihcsim committed May 26, 2024
1 parent 2ba570b commit 01b745f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 6 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
k8s-dra
controller
plugin
bin
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ BOILERPLATE_FILE := hack/boilerplate.go.txt
all: tidy lint test controller plugin

controller:
GOOS=$(GOOS) GOARCH=$(GOARCH) go build ./cmd/controller
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/controller ./cmd/controller

plugin:
GOOS=$(GOOS) GOARCH=$(GOARCH) go build ./cmd/plugin
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/plugin ./cmd/plugin

test:
GOOS=$(GOOS) GOARCH=$(GOARCH) go test ./...
Expand Down
6 changes: 6 additions & 0 deletions cmd/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ func NewControllerFlags() *pflag.FlagSet {
flags.Int("pprof-port", 9002, "HTTP port to expose pprof endpoints")
return flags
}

func NewPluginFlags() *pflag.FlagSet {
flags := pflag.NewFlagSet("plugin", pflag.ExitOnError)
flags.String("cdi-root", "/etc/cdi", "Absolute path to the directory where CDI files will be generated")
return flags
}
46 changes: 45 additions & 1 deletion cmd/plugin/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package main

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/ihcsim/k8s-dra/cmd/flags"
"github.com/ihcsim/k8s-dra/pkg/drivers/gpu"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"

plugin "k8s.io/dynamic-resource-allocation/kubeletplugin"
)

const driverName = "driver.resources.ihcsim"

var (
rootCmd = &cobra.Command{
Use: "dra-plugin",
Expand All @@ -17,10 +25,20 @@ var (
return run(cmd.Context())
},
}

pluginRegistrationPath = "/var/lib/kubelet/plugins_registry/" + driverName + ".sock"
pluginPath = "/var/lib/kubelet/plugins/" + driverName
pluginSocketPath = pluginPath + "/plugin.sock"
)

func init() {
rootCmd.PersistentFlags().AddFlagSet(flags.NewK8sFlags())
rootCmd.PersistentFlags().AddFlagSet(flags.NewPluginFlags())

if err := rootCmd.MarkPersistentFlagRequired("cdi-root"); err != nil {
log.Fatal().Err(err).Msg("failed to mark flags as required")
}

if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
log.Fatal().Err(err).Msg("failed to bind flags")
}
Expand All @@ -31,5 +49,31 @@ func executeContext(ctx context.Context) error {
}

func run(ctx context.Context) error {
return nil
if err := os.MkdirAll(pluginPath, 0750); err != nil {
return err
}

cdiRoot := viper.GetString("cdi-root")
if err := os.MkdirAll(cdiRoot, 0750); err != nil {
return err
}

nodeServer := gpu.NewNodeServer(ctx)
p, err := plugin.Start(
nodeServer,
plugin.DriverName(driverName),
plugin.RegistrarSocketPath(pluginRegistrationPath),
plugin.PluginSocketPath(pluginSocketPath),
plugin.KubeletPluginSocketPath(pluginSocketPath),
)
if err != nil {
return err
}

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-sigChan

p.Stop()
return nodeServer.Shutdown(ctx)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ require (
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.18.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kubelet v0.30.0 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f h1:ultW7fxlIvee4HYrtnaRPon9HpEgFk5zYpmfMgtKB5I=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f/go.mod h1:L9KNLi232K1/xB6f7AlSX692koaRnKaWSR0stBki0Yc=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down Expand Up @@ -223,6 +227,8 @@ k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 h1:BZqlfIlq5YbRMFko6/PM7FjZpUb45WallggurYhKGag=
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340/go.mod h1:yD4MZYeKMBwQKVht279WycxKyM84kkAx2DPrTXaeb98=
k8s.io/kubelet v0.30.0 h1:/pqHVR2Rn8ExCpn211wL3pMtqRFpcBcJPl4+1INbIMk=
k8s.io/kubelet v0.30.0/go.mod h1:WukdKqbQxnj+csn3K8XOKeX7Sh60J/da25IILjvvB5s=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
Expand Down
13 changes: 13 additions & 0 deletions pkg/drivers/gpu/node-server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gpu

import "context"

type NodeServer struct{}

func NewNodeServer(ctx context.Context) *NodeServer {
return &NodeServer{}
}

func (n *NodeServer) Shutdown(ctx context.Context) error {
return nil
}

0 comments on commit 01b745f

Please sign in to comment.