Skip to content

Commit 01a1c66

Browse files
authored
Merge pull request FDio#13 from hagbard5235/deviceplugin
2 parents 64445b6 + 445e1ca commit 01a1c66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+123012
-2
lines changed

Gopkg.lock

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ GOBIN?=${GOPATH}/bin
44
PROTOS = $(wildcard pkg/*/apis/**/*.proto)
55
PBS = $(PROTOS:%.proto=%.pb)
66

7-
all: protos
7+
all: protos deviceplugin
88
protos: ${PBS}
99

1010
%.pb: %.proto
1111
PATH=~/bin:$${PATH}:$(GOBIN) protoc -I $(dir $*.proto) $*.proto --go_out=plugins=grpc:$(dir $*.proto)
12+
13+
deviceplugin:
14+
@echo Building deviceplugin
15+
cd deviceplugin && go build
16+
17+
.PHONY: deviceplugin all

deviceplugin/deviceplugin.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Package deviceplugin provides a basic implementation of a Kubernetes DevicePlugin without content.
2+
// It is intended to be used to build other DevicePlugins.
3+
package deviceplugin
4+
5+
import (
6+
"golang.org/x/net/context"
7+
"google.golang.org/grpc"
8+
pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
9+
"log"
10+
"net"
11+
"os"
12+
"path"
13+
"time"
14+
)
15+
16+
type DevicePlugin struct {
17+
socket string
18+
resourceName string
19+
stop chan interface{}
20+
server *grpc.Server
21+
}
22+
23+
func NewDevicePlugin(serversock string, resourcename string) *DevicePlugin {
24+
return &DevicePlugin{
25+
socket: serversock,
26+
stop: make(chan interface{}),
27+
resourceName: resourcename,
28+
}
29+
}
30+
31+
func dial(ctx context.Context, unixSocketPath string) (*grpc.ClientConn, error) {
32+
c, err := grpc.DialContext(ctx, unixSocketPath, grpc.WithInsecure(), grpc.WithBlock(),
33+
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
34+
return net.DialTimeout("unix", addr, timeout)
35+
}),
36+
)
37+
38+
if err != nil {
39+
return nil, err
40+
}
41+
return c, nil
42+
}
43+
44+
func (d *DevicePlugin) cleanup() error {
45+
if err := os.Remove(d.socket); err != nil {
46+
return err
47+
}
48+
49+
return nil
50+
}
51+
52+
func (d *DevicePlugin) Start() error {
53+
err := d.cleanup()
54+
55+
if err != nil {
56+
return err
57+
}
58+
59+
sock, err := net.Listen("unix", d.socket)
60+
if err != nil {
61+
return err
62+
}
63+
d.server = grpc.NewServer([]grpc.ServerOption{}...)
64+
pluginapi.RegisterDevicePluginServer(d.server, d)
65+
66+
go d.server.Serve(sock)
67+
68+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
69+
conn, err := dial(ctx, d.socket)
70+
if err != nil {
71+
return err
72+
}
73+
defer conn.Close()
74+
defer cancel()
75+
76+
return nil
77+
}
78+
79+
func (d *DevicePlugin) Stop() error {
80+
if d.server == nil {
81+
return nil
82+
}
83+
84+
d.server.Stop()
85+
d.server = nil
86+
close(d.stop)
87+
return d.cleanup()
88+
}
89+
90+
func (d *DevicePlugin) Serve() error {
91+
err := d.Start()
92+
if err != nil {
93+
log.Printf("Could not start device plugin %s", err)
94+
}
95+
log.Println("Starting to serve on", d.socket)
96+
97+
err = d.Register(pluginapi.KubeletSocket, d.resourceName)
98+
if err != nil {
99+
log.Printf("Could not register device plugin %s", err)
100+
return err
101+
}
102+
log.Println("Registered device plugin with Kubelet")
103+
return nil
104+
}
105+
106+
// Define functions needed to meet the Kubernetes DevicePlugin API
107+
108+
func (d *DevicePlugin) GetDevicePluginOptions(context.Context, *pluginapi.Empty) (*pluginapi.DevicePluginOptions, error) {
109+
return &pluginapi.DevicePluginOptions{}, nil
110+
}
111+
112+
func (d *DevicePlugin) Allocate(ctx context.Context, reqs *pluginapi.AllocateRequest) (*pluginapi.AllocateResponse, error) {
113+
return nil, nil
114+
}
115+
116+
func (d *DevicePlugin) ListAndWatch(e *pluginapi.Empty, s pluginapi.DevicePlugin_ListAndWatchServer) error {
117+
return nil
118+
}
119+
120+
func (d *DevicePlugin) PreStartContainer(context.Context, *pluginapi.PreStartContainerRequest) (*pluginapi.PreStartContainerResponse, error) {
121+
return &pluginapi.PreStartContainerResponse{}, nil
122+
}
123+
124+
func (d *DevicePlugin) Register(kubeletEndpoint, resourceName string) error {
125+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
126+
conn, err := dial(ctx, kubeletEndpoint)
127+
if err != nil {
128+
return err
129+
}
130+
defer conn.Close()
131+
defer cancel()
132+
client := pluginapi.NewRegistrationClient(conn)
133+
request := &pluginapi.RegisterRequest{
134+
Version: pluginapi.Version,
135+
Endpoint: path.Base(d.socket),
136+
ResourceName: resourceName,
137+
}
138+
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
139+
_, err = client.Register(ctx, request)
140+
defer cancel()
141+
if err != nil {
142+
return err
143+
}
144+
return nil
145+
}

vendor/github.com/gogo/protobuf/AUTHORS

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gogo/protobuf/CONTRIBUTORS

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gogo/protobuf/GOLANG_CONTRIBUTORS

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gogo/protobuf/LICENSE

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gogo/protobuf/gogoproto/Makefile

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)