-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.go
202 lines (162 loc) · 4.28 KB
/
server.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) 2019, Arm Ltd
package main
import (
"os"
"net"
"log"
"time"
"path"
"google.golang.org/grpc"
"golang.org/x/net/context"
api "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
)
type vfioDevicePlugin struct {
devs []*api.Device
socket string
resourceName string
stop chan interface{}
server *grpc.Server
}
func (m *vfioDevicePlugin) cleanup() error {
err := os.Remove(m.socket);
if err == nil {
log.Print("Removing file ", m.socket)
return nil
}
if os.IsNotExist(err) {
return nil
}
return err
}
func dial(unixSocketPath string, timeout time.Duration) (*grpc.ClientConn, error) {
conn, err := grpc.Dial(
unixSocketPath,
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithTimeout(timeout),
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}),
)
if err != nil {
return nil, err
}
return conn, nil
}
func NewDevicePlugin(iommuGroups []string, resourceName string, serverSock string) *vfioDevicePlugin {
var devices []*api.Device
for _,value := range iommuGroups {
devices = append(devices, &api.Device{
ID: value,
Health: api.Healthy,
})
}
return &vfioDevicePlugin{
devs: devices,
socket: serverSock,
resourceName: resourceName,
stop: make(chan interface{}),
}
}
func (m *vfioDevicePlugin) Serve() error {
err := m.Start()
if err != nil {
log.Print("Could not start device plugin: ", err)
return err
}
log.Print("Starting to serve on ", m.socket)
err = m.Register(api.KubeletSocket, m.resourceName)
if err != nil {
log.Print("Could not register device plugin: ", err)
m.Stop()
return err
}
log.Print("Registered device plugin with Kubelet")
return nil
}
func (m *vfioDevicePlugin) Register(kubeletEndpoint, resourceName string) error {
conn, err := dial(kubeletEndpoint, 5*time.Second)
if err != nil {
return err
}
defer conn.Close()
client := api.NewRegistrationClient(conn)
reqt := &api.RegisterRequest{
Version: api.Version,
Endpoint: path.Base(m.socket),
ResourceName: resourceName,
}
_, err = client.Register(context.Background(), reqt)
if err != nil {
return err
}
return nil
}
func (m *vfioDevicePlugin) Start() error {
err := m.cleanup()
if err != nil {
return err
}
sock, err := net.Listen("unix", m.socket)
if err != nil {
return err
}
m.server = grpc.NewServer([]grpc.ServerOption{}...)
api.RegisterDevicePluginServer(m.server, m)
go m.server.Serve(sock)
// Wait for server to start by launching a blocking connexion
conn, err := dial(m.socket, 60*time.Second)
if err != nil {
return err
}
conn.Close()
return nil
}
func (m *vfioDevicePlugin) Stop() error {
log.Printf("Stopping server with socket ", m.socket)
if m.server == nil {
return nil
}
m.server.Stop()
m.server = nil
close(m.stop)
log.Print("Server stopped with socket ", m.socket)
return m.cleanup()
}
func (m *vfioDevicePlugin) ListAndWatch(e *api.Empty, s api.DevicePlugin_ListAndWatchServer) error {
s.Send(&api.ListAndWatchResponse{Devices: m.devs})
for {
select {
case <-m.stop:
return nil
}
}
}
func (m *vfioDevicePlugin) Allocate(ctx context.Context, reqs *api.AllocateRequest) (*api.AllocateResponse, error) {
responses := api.AllocateResponse{}
for _, req := range reqs.ContainerRequests {
var devices []*api.DeviceSpec
for _,id := range req.DevicesIDs {
log.Print("Allocating IOMMU Group " + id)
devices = append(devices, &api.DeviceSpec{
ContainerPath: "/dev/vfio/" + id,
HostPath: "/dev/vfio/" + id,
Permissions: "rw",
})
}
response := api.ContainerAllocateResponse{
Devices: devices,
}
responses.ContainerResponses = append(responses.ContainerResponses, &response)
}
return &responses, nil
}
func (m *vfioDevicePlugin) PreStartContainer(context.Context, *api.PreStartContainerRequest) (*api.PreStartContainerResponse, error) {
return &api.PreStartContainerResponse{}, nil
}
func (m *vfioDevicePlugin) GetPreferredAllocation(context.Context, *api.PreferredAllocationRequest) (*api.PreferredAllocationResponse, error) {
return &api.PreferredAllocationResponse{}, nil
}
func (m *vfioDevicePlugin) GetDevicePluginOptions(context.Context, *api.Empty) (*api.DevicePluginOptions, error) {
return &api.DevicePluginOptions{}, nil
}