-
Notifications
You must be signed in to change notification settings - Fork 149
/
eni.go
356 lines (319 loc) · 9.44 KB
/
eni.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package deviceplugin
import (
"fmt"
"net"
"os"
"path"
"regexp"
"sync"
"syscall"
"time"
"github.com/samber/lo"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
"github.com/AliyunContainerService/terway/pkg/utils"
)
// define resource name
const (
ENITypeENI = "eni"
ENITypeMember = "member"
ENITypeERDMA = "erdma"
// ENIResName aliyun eni resource name in kubernetes container resource
ENIResName = "aliyun/eni"
MemberENIResName = "aliyun/member-eni"
ERDMAResName = "aliyun/erdma"
)
type eniRes struct {
resName string
re *regexp.Regexp
sock string
}
var eniMap = map[string]eniRes{
ENITypeENI: {
resName: ENIResName,
re: regexp.MustCompile("^.*-eni.sock"),
sock: pluginapi.DevicePluginPath + "%d-eni.sock",
},
ENITypeMember: {
resName: MemberENIResName,
re: regexp.MustCompile("^.*-member-eni.sock"),
sock: pluginapi.DevicePluginPath + "%d-member-eni.sock",
},
ENITypeERDMA: {
resName: ERDMAResName,
re: regexp.MustCompile("^.*-erdma-eni.sock"),
sock: pluginapi.DevicePluginPath + "%d-erdma-eni.sock",
},
}
// ENIDevicePlugin implements the Kubernetes device plugin API
type ENIDevicePlugin struct {
socket string
server *grpc.Server
count int
stop chan struct{}
eniRes eniRes
eniType string
sync.Locker
}
// NewENIDevicePlugin returns an initialized ENIDevicePlugin
func NewENIDevicePlugin(count int, eniType string) *ENIDevicePlugin {
res, ok := eniMap[eniType]
if !ok {
panic("unsupported eni type " + eniType)
}
pluginEndpoint := fmt.Sprintf(res.sock, time.Now().Unix())
return &ENIDevicePlugin{
socket: pluginEndpoint,
count: count,
eniRes: res,
eniType: eniType,
}
}
// dial establishes the gRPC communication with the registered device plugin.
func dial(unixSocketPath string, timeout time.Duration) (*grpc.ClientConn, func(), error) {
timeoutCtx, cancel := context.WithTimeout(context.Background(), timeout)
c, err := grpc.DialContext(timeoutCtx, unixSocketPath, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock(),
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}),
)
if err != nil {
cancel()
return nil, nil, err
}
return c, func() {
err = c.Close()
cancel()
}, nil
}
// Start starts the gRPC server of the device plugin
func (m *ENIDevicePlugin) Start() error {
if m.server != nil {
close(m.stop)
m.server.Stop()
}
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{}...)
pluginapi.RegisterDevicePluginServer(m.server, m)
m.stop = make(chan struct{}, 1)
go func() {
err := m.server.Serve(sock)
if err != nil {
klog.Errorf("error start device plugin server, %+v", err)
}
}()
// Wait for server to start by launching a blocking connection
_, closeConn, err := dial(m.socket, 5*time.Second)
if err != nil {
return err
}
closeConn()
return nil
}
// GetDevicePluginOptions return device plugin options
func (m *ENIDevicePlugin) GetDevicePluginOptions(context.Context, *pluginapi.Empty) (*pluginapi.DevicePluginOptions, error) {
return &pluginapi.DevicePluginOptions{}, nil
}
// PreStartContainer return container prestart hook
func (m *ENIDevicePlugin) PreStartContainer(context.Context, *pluginapi.PreStartContainerRequest) (*pluginapi.PreStartContainerResponse, error) {
return &pluginapi.PreStartContainerResponse{}, nil
}
// Stop stops the gRPC server
func (m *ENIDevicePlugin) Stop() error {
if m.server == nil {
return nil
}
m.server.Stop()
m.server = nil
close(m.stop)
return m.cleanup()
}
// Register registers the device plugin for the given resourceName with Kubelet.
func (m *ENIDevicePlugin) Register(request pluginapi.RegisterRequest) error {
conn, closeConn, err := dial(pluginapi.KubeletSocket, 5*time.Second)
if err != nil {
return err
}
defer closeConn()
client := pluginapi.NewRegistrationClient(conn)
_, err = client.Register(context.Background(), &request)
if err != nil {
return err
}
return nil
}
// ListAndWatch lists devices and update that list according to the health status
func (m *ENIDevicePlugin) ListAndWatch(e *pluginapi.Empty, s pluginapi.DevicePlugin_ListAndWatchServer) error {
var devs []*pluginapi.Device
for i := 0; i < m.count; i++ {
devs = append(devs, &pluginapi.Device{ID: fmt.Sprintf("eni-%d", i), Health: pluginapi.Healthy})
}
err := s.Send(&pluginapi.ListAndWatchResponse{Devices: devs})
if err != nil {
return err
}
ticker := time.NewTicker(time.Second * 5)
for {
select {
case <-ticker.C:
err := s.Send(&pluginapi.ListAndWatchResponse{Devices: devs})
if err != nil {
klog.Errorf("error send device informance: error: %v", err)
}
case <-m.stop:
return nil
}
}
}
func (m *ENIDevicePlugin) GetPreferredAllocation(context.Context, *pluginapi.PreferredAllocationRequest) (*pluginapi.PreferredAllocationResponse, error) {
return nil, fmt.Errorf("unsupported")
}
// Allocate which return list of devices.
func (m *ENIDevicePlugin) Allocate(ctx context.Context, r *pluginapi.AllocateRequest) (*pluginapi.AllocateResponse, error) {
response := pluginapi.AllocateResponse{
ContainerResponses: []*pluginapi.ContainerAllocateResponse{},
}
klog.Infof("Request Containers: %v, eniType: %v", r.GetContainerRequests(), m.eniType)
for range r.GetContainerRequests() {
var devices []*pluginapi.DeviceSpec
if m.eniType == ENITypeERDMA {
infinibandDevs, err := os.ReadDir("/dev/infiniband/")
if err != nil || len(infinibandDevs) == 0 {
if os.IsNotExist(err) {
// maybe first erdma to attach, there is no infiniband dev on device plugin allocate
devices = []*pluginapi.DeviceSpec{
{
ContainerPath: "/dev/infiniband/uverbs0",
HostPath: "/dev/infiniband/uverbs0",
Permissions: "rw",
},
{
ContainerPath: "/dev/infiniband/rdma_cm",
HostPath: "/dev/infiniband/rdma_cm",
Permissions: "rw",
},
}
} else {
return nil, fmt.Errorf("error read infiniband dir: %+v, please check the erdma driver", err)
}
} else {
devices = lo.FilterMap(infinibandDevs, func(v os.DirEntry, _ int) (*pluginapi.DeviceSpec, bool) {
if v.Type()&os.ModeDevice != 0 {
return &pluginapi.DeviceSpec{
ContainerPath: "/dev/infiniband/" + v.Name(),
HostPath: "/dev/infiniband/" + v.Name(),
Permissions: "rw",
}, true
}
return nil, false
})
}
response.ContainerResponses = append(response.ContainerResponses,
&pluginapi.ContainerAllocateResponse{
Devices: devices,
},
)
} else {
response.ContainerResponses = append(response.ContainerResponses,
&pluginapi.ContainerAllocateResponse{})
}
}
return &response, nil
}
func (m *ENIDevicePlugin) cleanup() error {
preSocks, err := os.ReadDir(pluginapi.DevicePluginPath)
if err != nil {
return err
}
for _, preSock := range preSocks {
klog.Infof("device plugin file info: %+v", preSock)
if m.eniRes.re.Match([]byte(preSock.Name())) {
if utils.IsWindowsOS() {
// NB(thxCode): treat the socket file as normal file
// and remove them directly.
err = os.Remove(path.Join(pluginapi.DevicePluginPath, preSock.Name()))
} else {
err = syscall.Unlink(path.Join(pluginapi.DevicePluginPath, preSock.Name()))
}
if err != nil {
klog.Errorf("error on clean up previous device plugin listens, %+v", err)
}
}
}
return nil
}
func (m *ENIDevicePlugin) watchKubeletRestart() {
wait.Until(func() {
var err error
if utils.IsWindowsOS() {
// NB(thxCode): since os.Stat has not worked as expected,
// we use os.Lstat instead of os.Stat here,
// ref to https://github.com/microsoft/Windows-Containers/issues/97#issuecomment-887713195.
_, err = os.Lstat(m.socket)
} else {
_, err = os.Stat(m.socket)
}
if err == nil {
return
}
if os.IsNotExist(err) {
klog.Infof("device plugin socket %s removed, restarting.", m.socket)
err := m.Stop()
if err != nil {
klog.Errorf("stop current device plugin server with error: %v", err)
}
err = m.Start()
if err != nil {
klog.Fatalf("error restart device plugin after kubelet restart %+v", err)
}
err = m.Register(
pluginapi.RegisterRequest{
Version: pluginapi.Version,
Endpoint: path.Base(m.socket),
ResourceName: m.eniRes.resName,
},
)
if err != nil {
klog.Fatalf("error register device plugin after kubelet restart %+v", err)
}
return
}
klog.Fatalf("error stat socket: %+v", err)
}, time.Second*30, make(chan struct{}, 1))
}
// Serve starts the gRPC server and register the device plugin to Kubelet
func (m *ENIDevicePlugin) Serve() {
err := m.Start()
if err != nil {
klog.Fatalf("Could not start device plugin: %v", err)
}
time.Sleep(5 * time.Second)
klog.Infof("Starting to serve on %s", m.socket)
err = m.Register(
pluginapi.RegisterRequest{
Version: pluginapi.Version,
Endpoint: path.Base(m.socket),
ResourceName: m.eniRes.resName,
},
)
if err != nil {
klog.Errorf("Could not register device plugin: %v", err)
stopErr := m.Stop()
if stopErr != nil {
klog.Fatalf("stop current device plugin server with error: %v", stopErr)
}
}
klog.Infof("Registered device plugin with Kubelet")
go m.watchKubeletRestart()
}